Pavel and Triangles

##Pavel and Triangles

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

He has a0 sticks of length 20=1, a1 sticks of length 21=2, …, an−1 sticks of length 2n−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≤n≤300000) — the number of different lengths of sticks.

The second line contains n integers a0, a1, …, an−1 (1≤ai≤109), where ai is the number of sticks with the length equal to 2i.

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): (20,24,24), (21,23,23), (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), (21,21,21), (22,22,22).
1.第一行包含单个整数n(1≤n≤300000)-不同长度的棒子的数目。

第二行包含n个整数a0, a1,…−1(1≤ai≤109),其中ai为长度为2的i次幂的杆数。

2.先求等腰 再求等边
3.在求等腰时与这两个腰匹配的一条边需要从上一次剩余的边里面扣除

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int N=3e5+10;
int a[N];
int main()
{
    int n,t=0,k=0;    
    long long s=0;
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
        k=min(t,a[i]/2);  //最多能匹配多少个等腰
        s+=k;   //算组成三角形的个数  先算等腰
        t-=k;  //和两个腰匹配的边从剩余的边里扣
        a[i]-=2*k;
        s+=a[i]/3;   //再算等边
        t+=a[i]%3;   //剩下的边
    }
    printf("%lld",s);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值