This page is still under construction.

Parts of this page are still being built. What you see may change.

Lawnmower

Time limit1sMemory limit1024 MB

Summary
Simulate a lawn that grows each morning and is mowed b_j times per day, reporting the total remaining grass height after each day.
Level

Medium6 of 10

Topics
Sorting, Prefix sum, Binary search, Implementation
Solved
No attempts yet

Problem

There is a lawn in front of Martynas's house. The lawn can be viewed as a line of NN centimeters, with one tuft of grass growing at every centimeter. The tuft at position ii (1≤i≤N1 \le i \le N) has height aia_i centimeters.

The grass has never been cut, so it is hard to even take a walk across the lawn, let alone have a picnic.

Martynas bought a lawnmower and plans to cut most of the grass over MM days. Day jj (1≤j≤M1 \le j \le M) proceeds as follows, in order:

  • Morning: every tuft that is not yet fully cut (ai≠0a_i \ne 0) grows by 1 cm1\,\text{cm}.
  • Day: Martynas drives the lawnmower across the lawn bjb_j times. Each pass reduces the height of every uncut tuft by 1 cm1\,\text{cm} (a height never drops below 00).
  • Evening: he counts the total height of grass still left uncut.

For example, suppose the lawn is 4 cm4\,\text{cm} long (N=4N = 4) and the tuft heights are 1,2,1,31, 2, 1, 3. Martynas works for M=2M = 2 days, driving across the lawn b1=2b_1 = 2 times on the first day and b2=1b_2 = 1 time on the second day.

On the morning of day 1 every tuft grows by 1 cm1\,\text{cm}, giving 2,3,2,42, 3, 2, 4. During the day he mows twice, so each tuft loses 2 cm2\,\text{cm}, leaving 0,1,0,20, 1, 0, 2; in the evening 0+1+0+2=3 cm0 + 1 + 0 + 2 = 3\,\text{cm} of grass remains.

On the morning of day 2 only the tufts that still have grass grow (the first and third stay at 00), giving 0,2,0,30, 2, 0, 3. He mows once, leaving 0,1,0,20, 1, 0, 2; in the evening 0+1+0+2=3 cm0 + 1 + 0 + 2 = 3\,\text{cm} remains again.

Given the initial state of the lawn and the mowing plan for MM days, compute the total height of grass left uncut in the evening of each of the MM days.

Input

  • The first line contains an integer NN — the length of the lawn.
  • The second line contains NN space-separated integers aia_i (1≤i≤N1 \le i \le N) — the heights of the grass tufts.
  • The third line contains an integer MM — the number of days Martynas mows the grass.
  • The fourth line contains MM space-separated integers bjb_j (1≤j≤M1 \le j \le M) — the number of times the lawn is mowed on day jj.

Output

Print MM lines. Line kk (1≤k≤M1 \le k \le M) must contain a single integer — the total height of grass (in centimeters) left uncut at the end of day kk.

Constraints

  • 1≤N,M≤1000001 \le N, M \le 100000
  • 1≤ai≤10000001 \le a_i \le 1000000 (1≤i≤N1 \le i \le N)
  • 1≤bj≤10000001 \le b_j \le 1000000 (1≤j≤M1 \le j \le M)

Hint

Note that the computation may require a 6464-bit integer type (long long in C/C++).

Examples3

  1. Example 1

    Input
    4
    1 2 1 3
    2
    2 1
    
    Expected output
    3
    3
    
  2. Example 2

    Input
    4
    10 10 10 10
    4
    2 2 2 2
    
    Expected output
    36
    32
    28
    24
    
  3. Example 3

    Input
    5
    1 3 5 7 9
    3
    1 2 3
    
    Expected output
    25
    20
    12