Tractor

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

Task

On a field, there are two stacks of hay bales.
The first stack contains nn bales, where the first bale is at the bottom, and the nthn^{th} bale is at the top. The ithi^{th} bale has weight aia_i.
The second stack contains mm bales, where the first bale is at the bottom, and the mthm^{th} bale is at the top. The jthj^{th} bale has weight bjb_j.
You want to transport the n+mn + m bales to the processing plant using a tractor with a total load limit ww. In one trip, you may load bales from both stacks, but a bale cannot be loaded before the bales on top of it have been loaded. The total weight of the bales loaded into the tractor on each trip must not exceed ww.

Determine the minimum number of trips required to clear the two stacks.

Input data

The first line contains three integers representing the number of bales from the first stack nn, the number of bales from the second stack mm, and the load limit of the tractor ww.
The second line contains nn integers a1,,ana_1, \ldots, a_n.
The third line contains mm integers b1,,bmb_1, \ldots, b_m.

Output data

The output consists of a single integer representing the minimum number of trips needed to transport all n+mn + m bales.

Constraints and clarifications

  • 1n,m2 0001 \leq n, m \leq 2\ 000
  • 1ai,bjw1091 \leq a_i, b_j \leq w \leq 10^9
# Points Constraints
1 2 a1=a2==an=b1=b2==bma_1 = a_2 = \dots = a_n = b_1 = b_2 = \dots = b_m
2 3 a1=a2==an=1a_1 = a_2 = \dots = a_n = 1
3 7 n,m7n, m \leq 7
4 21 n,m50n, m \leq 50
5 30 n,m500n, m \leq 500
6 37 No further constraints

Example

stdin

4 5 10
4 3 7 5
3 4 3 6 2

stdout

4

Explanation

The minimum number of trips required to clear the two stacks is 44; this can be achieved in the following way:

  • On the first trip, we take the following from the two stacks: the hay bales with weights a4a_4 and b5b_5 with a total weight of 77;
  • On the second trip, the hay bales with weights a3a_3 and a2a_2 with a total weight of 1010;
  • On the third trip, the hay bales with weights a1a_1 and b4b_4 with a total weight of 1010;
  • On the fourth trip, the hay bales with weights b3,b2b_3, b_2 and b1b_1 with a total weight of 1010.

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