Codeforces Round #252 (Div. 2)B. Valera and Fruits
来源:互联网 发布:java http上传图片 编辑:程序博客网 时间:2024/04/29 07:19
Valera loves his garden, where n fruit trees grow.
This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).
Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more thanv fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?
The first line contains two space-separated integers n and v (1 ≤ n, v ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.
Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the i-th tree and the number of fruits on the i-th tree.
Print a single integer — the maximum number of fruit that Valera can collect.
2 31 52 3
8
5 103 202 201 204 205 20
60
In the first sample, in order to obtain the optimal answer, you should act as follows.
- On the first day collect 3 fruits from the 1-st tree.
- On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree.
- On the third day collect the remaining fruits from the 2-nd tree.
In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.
#include <iostream>#include <cstring>using namespace std;#define inf 0x3f3f3f3fint main(){ int n,m,a,b; int s=0; int t[3111]; memset(t,0,sizeof(t)); cin>>n>>m; int maxt=0; for(int i=0;i<n;i++) { cin>>a>>b; t[a]+=b; if(maxt<a) { maxt=a; } } for(int i=1;i<=maxt+1;i++) { if(t[i-1]!=0) { if(t[i-1]<m) { s+=t[i-1]; if(t[i]<m-t[i-1]) { s+=t[i]; t[i]=0; } else { s+=m-t[i-1]; t[i]-=(m-t[i-1]); } } else s+=m; } else { if(t[i]<m) { s+=t[i]; t[i]=0; } else { s+=m; t[i]-=m; } } } cout<<s<<endl; return 0;}
- Codeforces Round #252 (Div. 2) 441B. Valera and Fruits
- Codeforces Round #252 (Div. 2)B. Valera and Fruits
- Codeforces Round #252 (Div. 2)-B. Valera and Fruits
- Codeforces Round #252 (Div. 2) B. Valera and Fruits
- Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)
- Codeforces Round #252(Div. 2) 441B. Valera and Fruits 贪心
- Codeforces #252 (Div. 2) B. Valera and Fruits
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Codeforces Round #216 (Div. 2) B. Valera and Contest
- Codeforces 441 B. Valera and Fruits
- CodeForces 441B Valera and Fruits
- codeforces-441B-Valera and Fruits【暴力】
- CodeForces - 441B -Valera and Fruits
- codeforces-Valera and Fruits
- Codeforces Valera and Fruits
- B. Valera and Fruits
- Codeforces Round #252 (Div. 2) C. Valera and Tubes
- Codeforces Round #252 (Div. 2) 441C. Valera and Tubes
- 双链表内部迁移操作
- 字节流转换字符流实例
- [C++学习]C++常见问题
- getResource(string name)
- Lua中的字符串函数库
- Codeforces Round #252 (Div. 2)B. Valera and Fruits
- Android上播放Gif图片的方法之二
- 谷歌被墙后,可以搜索的ip地址
- iptables 防火墙 只允许某IP访问某端口、访问特定网站
- 【C++学习笔记】03_C++程序结构
- JSP的内置对象及方法。
- POJ 1936 All in All (模拟)
- [C] 数组另类写法
- eclipse 找回误删除java文件