Codeforces Global Round 2

题目链接:

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

题意

给定 \(n\) 个数,第 \(i\) 个数 \(a[i]\) 表示长度为 \(2^i\) 的木棒的数量,求最多可以拼成多少个三角形。

题解

贪心

本来以为要 FFT 的,结果贪心就完事了。

构成三角形只有两种情况:等腰三角形和等边三角形。优先采用等边三角形,剩下的边去凑等腰三角形,贪心一下就可以了。

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;
}

Codeforces 1119E Pavel and Triangles (贪心)的更多相关文章

  1. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  2. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  3. E. Pavel and Triangles dp+问题转化

    E. Pavel and Triangles dp+问题转化 题意 给出n种线段,每种线段给出一定数量,其中每个线段都是 \(2^k\) 问最多能组成多少个三角形 思路 因为每个是\(2^k\)所以能 ...

  4. Codeforces 1119E(贪心)

    题目传送 贪心方法 按边从小到大扫,先凑3个,没凑足的记录一下数量,后面大的优先跟这些凑,俩带走一个,多余的再凑3个,再--就这样走到最后即可. const int maxn = 3e5 + 5; i ...

  5. Codeforces Global Round 2 E. Pavel and Triangles(思维+DP)

    题目链接:https://codeforces.com/contest/1119/problem/E 题意:有n种长度的棍子,有a_i根2^i长度的棍子,问最多可以组成多少个三角形 题解:dp[i]表 ...

  6. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  7. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

  8. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  9. codeforces 803D Magazine Ad(二分+贪心)

    Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...

随机推荐

  1. freemark 语法

    我们通过后端model. addAttribute() 传递到前端的值来进行界面渲染 它的循环语句 和其他的有点不同: if 循环 <#if 条件语句> </#if> if  ...

  2. kafka的简介

    1. kafka是一个分布式消息队列.具有高性能.持久化.多副本备份.横向扩展能力.生产者往队列里写消息,消费者从队列里取消息进行业务逻辑.一般在架构设计中起到解耦.削峰.异步处理的作用. 1.1 b ...

  3. docker内的服务无法获取用户真实IP

    原文:blog.baohaipeng.top 背景:MySQL数据库和Redis运行在宿主机上(Linux),server运行在docker内,web运行在Nginx内(Nginx运行在docker内 ...

  4. fedora 26

    图标文件路径: /home/xiezhiyan/.local/share/applications

  5. 新浪sina邮箱客户端配置

    接收协议:IMAP 接收邮箱服务器地址:imap.sina.com 端口:993 加密方法:TLS 发送协议:SMTP 发送服务器:smtp.sina.com 端口:465 加密方法:TLS

  6. mybatis自学历程(二)

    传递多个参数 1.在mybatis.xml下<mappers>下使用<package> <mappers> <package name="com.m ...

  7. MySQL数据库企业级应用实践(主从复制)

    MySQL数据库企业级应用实践(主从复制) 链接:https://pan.baidu.com/s/1ANGg3Kd_28BzQrA5ya17fQ 提取码:ekpy 复制这段内容后打开百度网盘手机App ...

  8. HTML事件处理程序---内联onclick事件

    HTML事件处理程序绑定方法: <input type="button" value="click me" onclick="show(this ...

  9. Kali Linux更新和配置

    1.用vim打开 /etc/apt/source.list root@kali:~# vim /etc/apt/sources.list #中科大 deb http://mirrors.ustc.ed ...

  10. Python中 将数据插入到Word模板并生成一份Word

    搬运出处: https://blog.csdn.net/DaShu0612/article/details/82912064