One-Arm Bandit
Along the edges of the casino, you've been watching how the slot machines work. You notice that they pay out chips in a fixed, repeating cycle. Incredible!
Now, you want to take advantage of this discovery to win as much money as possible. However, to avoid drawing suspicion, you've planned a sequence of plays. For example, you might play game, then let others play
games, then play
more, and so on. This sequence of plays is also fixed and cannot be changed.
Determine the maximum total payout you can earn by choosing the best starting point in the machine's payout cycle.
Input
The first line consists of an integer
, the number of entries in the slot machine's payout cycle.
The second line consists of integers
, the pattern of payouts made by the machine.
The third line consists of an integer
, the number of entries in your sequence of plays.
The fourth line consists of boolean integers
(
or
), indicating that you must either play or not play in the sequence.
Output
Output the maximum number of chips the machine can pay out to you, by starting your play anywhere in its payout cycle.
Example
Input 1
7
2 3 10 1 5 4 3
5
1 0 1 0 1
Output 1
18
Start playing at the third payout (). You will:
- Win
chips.
- Skip a win of
chip.
- Win
chips.
- Skip a win of
chips.
- Win
chips.
You will win chips, which is the maximum amount in this case.
Below shows the payout you will recieve when playing at each of the 7 unique times.
Input 2
7
5 1 1 1 5 1 1
4
1 0 0 1
Output 2
10
Start playing at the third payout (). You will:
- Win
chips.
- Skip a win of
chip.
- Skip a win of
chip.
- Win
chips.
You will win chips, which is the maximum amount in this case. Note that in this case, you get to the end of the payout cycle and loop back to the start.
Comments