Codeforces Global Round 2 1119E. Pavel and Triangles【贪心】

E. Pavel and Triangles

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

He has a0a0 sticks of length 20=120=1, a1a1 sticks of length 21=221=2, ..., an−1an−1 sticks of length 2n−12n−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 nn (1≤n≤3000001≤n≤300000) — the number of different lengths of sticks.

The second line contains nn integers a0a0, a1a1, ..., an−1an−1 (1≤ai≤1091≤ai≤109), where aiai is the number of sticks with the length equal to 2i2i.

Output

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

Examples

input

Copy

5
1 2 2 2 2

output

Copy

3

input

Copy

3
1 1 1

output

Copy

0

input

Copy

3
3 3 3

output

Copy

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): (20,24,24)(20,24,24), (21,23,23)(21,23,23), (21,22,22)(21,22,22).

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): (20,20,20)(20,20,20), (21,21,21)(21,21,21), (22,22,22)(22,22,22).

题意:有N种木棒,其中第i种木棒的长度是2的i次方。给你每种木棒的个数a[i],求最多能组成的三角形个数。

分析:能组成三角形的形式只有2根一样的木棒和1根其他长度的木棒,3根相同的木棒。

直接贪心就完事儿了,记录每次用剩下的木棒,优先采用2+1的方式,再采用3的方式。

注意一下范围需要LL,其他没啥可说的。这题比D简单多了。

#include "bits/stdc++.h"
using namespace std;
long long a[300004];
int main()
{
    int n;
    cin>>n;
    for (int i = 0; i < n; ++i) {
        scanf("%lld",&a[i]);
    }
    long long ans=a[0]/3;
    a[0]%=3;
    long long les=a[0];
    for (int i = 1; i < n; ++i) {
        long long num=min(les,a[i]/2);
        les-=num;
        a[i]-=num*2;
        ans+=num;
        ans+=a[i]/3;
        a[i]%=3;
        les+=a[i];
    }
    printf("%lld\n",ans);
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/89067714