Codeforces 1119E Pavel and Triangles (greedy)

Codeforces Global Round 2

Topic links:

E. Pavel and Triangles

Pavel has several sticks with lengths equal to powers of two.

He has \(a_0\) sticks of length \(2^0=1\), \(a1\) sticks of length \(2^1=2\), ..., \(a_{n−1}\) sticks of length \(2^{n−1}\).

Pavel wants to make the maximum possible number of triangles using these sticks. The triangles should have strictly positive area, each stick can be used in at most one triangle.

It is forbidden to break sticks, and each triangle should consist of exactly three sticks.

Find the maximum possible number of triangles.

Input

The first line contains a single integer \(n (1\le n\le 300000)\) — the number of different lengths of sticks.

The second line contains \(n\) integers \(a_0, a_1, ..., a_{n−1} (1\le a_i\le 10^9)\), where ai is the number of sticks with the length equal to \(2^i\).

Output

Print a single integer — the maximum possible number of non-degenerate triangles that Pavel can make.

Examples

input

5 
1 2 2 2 2 

output

3

input

3
1 1 1

output

0

input

3
3 3 3

output

3

Note

In the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): \((2^0,2^4,2^4), (2^1,2^3,2^3), (2^1,2^2,2^2)\).

In the second example, Pavel cannot make a single triangle.

In the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): \((2^0,2^0,2^0), (2^1,2^1,2^1), (2^2,2^2,2^2)\).

Solution

The meaning of problems

Given (n-\) \ number, the \ (I \) number \ (a [i] \) represents the length of \ (2 ^ i \) Number of sticks, seeking the maximum number of triangles can spell .

answer

greedy

Originally thought to be the FFT, the result of greed gets the job done.

Form a triangle only two situations: isosceles triangle and equilateral triangle. Give priority equilateral triangle, side to scrape together the rest of the isosceles triangle, greedy about it.

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n;
    cin >> n;
    ll ans = 0, k = 0;
    for(int i = 0; i < n; ++i) {
        ll a;
        cin >> a;
        if(k) {
            ll t = min(k, a / 2);
            a -= t * 2;
            ans += t;
            k -= t;
        }
        ans += a / 3;
        k += a % 3;
    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/wulitaotao/p/11600685.html