Winter Sales

Time limit: 0.5s Memory limit: 256MB Input: Output:

Winter is beginning and Giorgio's shop has some unsold items left in its warehouses, which will now be put on sale.

The items are arranged in NN boxes, numbered from 00 to N1N - 1, stacked one on top of the other, each with a weight WiW_i. Box 00 is the topmost box.

A box can only be removed if all the boxes above it have already been removed.

Giorgio moving some boxes with a cart..\text{Giorgio moving some boxes with a cart.}.

Giorgio must completely empty his warehouse and to do so he has MM carts numbered from 00 to M1M - 1.

The jj-th cart can move a maximum of KjK_j boxes and the total weight of the boxes transported cannot exceed TjT_j.

On each trip, Giorgio chooses one of his carts, goes to the warehouse and loads the boxes from the top, respecting the cart's limits. He then returns to the shop and unloads the boxes.

Next, if there are still boxes left, he repeats the operation, choosing whether to change carts or use the same one.

Task

Help Giorgio plan his day by calculating the minimum number of trips he will need to make to empty the warehouse.

Input data

The input file consists of:

  • a line containing integer NN.
  • a line containing the NN integers W0,,WN1W_{0}, \, \ldots, \, W_{N-1}.
  • a line containing integer MM.
  • a line containing the MM integers K0,,KM1K_{0}, \, \ldots, \, K_{M-1}.
  • a line containing the MM integers T0,,TM1T_{0}, \, \ldots, \, T_{M-1}.

Output data

The output file must contain a single line consisting of integer SS, the answer to the problem.

Constraints and clarifications

  • 1N,M200 0001 \leq N, M \leq 200 \ 000.
  • 0Wi10 0000 \leq W_i \leq 10 \ 000 for each i=0N1i=0\ldots N-1.
  • 0KiN0 \leq K_i \leq N for each i=0M1i=0\ldots M-1.
  • 0Ti1090 \leq T_i \leq 10^9 for each i=0M1i=0\ldots M-1.
  • It is always possible to remove all boxes from the warehouse.
# Score Constraints
1 0 Examples
2 10 N5000N \leq 5000, M=1M = 1, Wi10W_i \leq 10 and T0=50000T_0 = 50000
3 10 M=1M = 1
4 15 All KiK_i are equal
5 25 N1000N \leq 1000 and M1000M \leq 1000
6 40 No additional restrictions

Example 1

stdin

3
10 10 30
3
3 1 1
25 35 20

stdout

2

Explanation

In the first sample case Giorgio can use the first cart for the first two boxes and the second cart for the last box, so he needs two trips.

Example 2

stdin

5
1 1 1 1 1
2
2 5
5 2

stdout

3

Explanation

In the second sample case Giorgio needs three trips regardless of which cart he chooses.

Log in or sign up to be able to send submissions!