#K2071. Gem Collector

    ID: 24655 Type: Default 1000ms 256MiB

Gem Collector

Gem Collector

You are given a series of levels in a gem collecting quest. At each level i (where i ranges from 1 to m), there are Gi gems. To move from level i to level i+1, the hunter must expend an energy of Ei. The hunter starts with an initial energy of S. The journey goes level by level, and at each level the hunter collects all available gems. However, the hunter can only move to the next level if his current energy is at least the required energy cost. The moment the energy is insufficient to proceed, the journey stops.

Your task is to compute the maximum number of gems the hunter can collect. Mathematically, if the hunter can progress through k levels (where 0 ≤ k ≤ m), the total gems collected is:

i=1kGi\sum_{i=1}^{k} G_i

Determine the maximum value of this sum under the energy constraint.

inputFormat

The input is given via standard input (stdin) in the following format:

  • The first line contains an integer m representing the number of levels.
  • If m > 0, the second line contains m space-separated integers representing the number of gems at each level (G1, G2, ..., Gm).
  • If m > 1, the third line contains m-1 space-separated integers representing the energy required to move from level i to level i+1 (E1, E2, ..., Em-1).
  • The last line contains an integer S representing the initial energy.

Note: When m = 0, there will be no line for gems or energies, only the first line and the last line, where the last line is the initial energy.

outputFormat

Output a single integer to standard output (stdout): the maximum number of gems that can be collected.

## sample
5
10 20 30 40 50
5 10 5 1
15
60