Under construction

Still being built. Things may change

Balancing Bacteria

Time limit2sMemory limit1024 MB

Summary
Given N patches with signed bacteria offsets, find the fewest sprays from a linearly decaying sprayer anchored at patch N that bring every patch to zero.
Level

Medium6 of 10

Topics
Greedy, Prefix sum, Implementation
Solved
No attempts yet

Problem

Farmer John has NN (1≤N≤2⋅1051\le N\le 2\cdot 10^5) patches of grass in a line. The bacteria level of patch ii differs from the healthy level by aia_i (−1015≤ai≤1015-10^{15}\le a_i \le 10^{15}). For example, if ai=−3a_i = -3, then patch ii is 3 units below normal, so it needs exactly 3 more units of bacteria to be healthy.

Farmer John wants every patch to reach the healthy level. He has two pesticides, one that adds bacteria and one that removes bacteria. To spray either one, he stands at patch NN (the rightmost patch) and chooses a sprayer power LL (1≤L≤N1 \leq L \leq N).

The sprayer affects patches near Farmer John most, and its effect weakens with distance. If he uses the pesticide that adds bacteria, then LL units are added to patch NN, L−1L-1 units to patch N−1N-1, L−2L-2 units to patch N−2N-2, and so on. Patches 1…N−L1 \ldots N-L receive nothing because the sprayer does not reach them. The pesticide that removes bacteria works the same way, except it subtracts the amounts instead.

Find the minimum number of times Farmer John must spray so that every patch reaches the healthy bacteria level. The answer is guaranteed to be at most 10910^9.

The values can be very large, so use a 64-bit integer type such as long long in C/C++.

Input

The first line contains NN.

The second line contains NN integers a1…aNa_1 \dots a_N, the initial bacteria levels of the patches of grass.

Output

Print the minimum number of sprays needed to make every patch of grass have the healthy bacteria level.

Examples2

  1. Example 1

    Input
    2
    -1 3
    
    Expected output
    6
    
  2. Example 2

    Input
    5
    1 3 -2 -7 5
    
    Expected output
    26