One-Arm Bandit


Submit solution

Points: 1
Time limit: 1.0s
Memory limit: 256M

Author:
Problem type
Allowed languages
C++, Java, Python

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 1 game, then let others play 3 games, then play 2 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 n (1 \leq n \leq 10^5), the number of entries in the slot machine's payout cycle.

The second line consists of n integers p_i (1 \leq p_i \leq 100), the pattern of payouts made by the machine.

The third line consists of an integer m (1 \leq m \leq 10^5), the number of entries in your sequence of plays.

The fourth line consists of m boolean integers x_i (x_i = 0 or x_i = 1), 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 (10). You will:

  • Win 10 chips.
  • Skip a win of 1 chip.
  • Win 5 chips.
  • Skip a win of 4 chips.
  • Win 3 chips.

You will win 18 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 (5). You will:

  • Win 5 chips.
  • Skip a win of 1 chip.
  • Skip a win of 1 chip.
  • Win 5 chips.

You will win 10 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

There are no comments at the moment.