Problem description

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 than v 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?

Input

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.

Output

Print a single integer — the maximum number of fruit that Valera can collect.

Examples

Input

2 3
1 5
2 3

Output

8

Input

5 10
3 20
2 20
1 20
4 20
5 20

Output

60

Note

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.

解题思路:题目的意思就是有n棵树,a,b分别表示一棵树第a天成熟和该树树上有b个果实,并且只能在第a天或第a+1天这两天摘其树上的果实,超过这两天就不能再摘该树树上的果实(已发霉)。求农夫可以摘的最大果实数目。按照提示给的规则,刚开始做法是用结构体数组,但发现如果成熟的天数是断续的,这时候就会覆盖当前果树本该要摘的果实数目,从而少摘了一些果实。以下代码中的结构体数组按果树的成熟天数从小到大排完序后其连续下标存放的是成熟天数(成熟天数可能有断续即不连续)。举个栗子:n=4,v=10;(这里不举例有相同成熟天数的一些果树)

3 20

1 20

4 20

5 20

此样例正确输出应为60,但以下代码的输出为50(错误),因此不能这样子做。过程比较简单,可以自己模拟一下。

贴一下错误代码:

 #include<bits/stdc++.h>
using namespace std;
const int maxn = ;
struct NODE{
int dayth,num;
}node[maxn];
bool cmp(NODE x,NODE y){return x.num<y.num;}
int main(){
int n,v,x,y,tmp,sum=,k=;bool flag;
cin>>n>>v;
for(int i=;i<maxn;++i)node[i].num=;
for(int i=;i<=n;++i){
cin>>x>>y;flag=false;
for(int j=;j<k;++j)
if(node[i].dayth==x){node[i].num+=y;flag=true;break;}
if(!flag){node[k].dayth=x;node[k++].num=y;}
}
sort(node,node+k,cmp);
if(node[].num>=v){sum+=v;node[].num-=v;}
else {sum+=node[].num;node[].num=;}
for(int i=;i<=k;++i){
if(node[i-].num>=v)sum+=v;
else{
sum+=node[i-].num;tmp=v-node[i-].num;
if(node[i].num>=tmp){sum+=tmp;node[i].num-=tmp;}
else {sum+=node[i].num;node[i].num=;}
}
}
cout<<sum<<endl;
return ;
}

我们定义了一个数组r[i]=value,其下标i表示第i天成熟,value为果实数目。注意某些树成熟的天数可能是相同的,因而要将它们的果实累加。枚举每一天是否有成熟的果实可以摘,并且按规则摘取,这样就不会有覆盖,即发生少摘的情况。暴力贪心模拟即可。

AC代码:

 #include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int main(){
int n,v,x,y,tmp,sum=,r[maxn];//x,y分别表示某棵树成熟的天数和该树树上的果实数量
cin>>n>>v;
memset(r,,sizeof(r));//注意清0
for(int i=;i<=n;++i){cin>>x>>y;r[x]+=y;}//可能有一些树成熟的时间相同,所以要将其果实相加
for(int i=;i<maxn;++i){//从1开始枚举
if(r[i-]>=v)sum+=v;//如果上一棵树可以摘的果实数目不小于v,则直接摘掉v个,并且摘完后上一棵树已经过期了,此后不能再摘了
else{
sum+=r[i-];tmp=v-r[i-];//否则就直接摘掉上一棵树剩下的果实
if(r[i]>=tmp){sum+=tmp;r[i]-=tmp;}//如果当前这棵树可以摘的果实还大于一天剩下可摘的果实tmp,就摘掉tmp个
else{sum+=r[i];r[i]=;}//否则就直接摘掉当前这棵树上的果实
}
}
cout<<sum<<endl;
return ;
}

C - Valera and Fruits的更多相关文章

  1. Codeforces Round #252 (Div. 2) B. Valera and Fruits(模拟)

    B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Valera and Fruits

    B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. Codeforces 441 B. Valera and Fruits

    B. Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. codeforces 441B. Valera and Fruits 解题报告

    题目链接:http://codeforces.com/problemset/problem/441/B 题目意思:有 n 棵fruit trees,每课水果树有两个参数描述:水果成熟的时间和这棵树上水 ...

  5. Codeforces Round #252 (Div. 2) B. Valera and Fruits

    #include <iostream> #include <vector> #include <algorithm> #include <map> us ...

  6. Codeforces #252 (Div. 2) B. Valera and Fruits

    题目倒是不难,可是读起来非常恶心 依据题目的描写叙述不easy找到适合存储的方法 后来我就想不跟着出题人的思路走 我自己开一个数组c 令c[a[i]] = b[i] 则c[i] == [j] 代表第i ...

  7. Codeforces Round #252 (Div. 2) 441B. Valera and Fruits

    英语不好就是坑啊.这道题把我坑残了啊.5次WA一次被HACK.第二题得分就比第一题高10分啊. 以后一定要加强英语的学习,要不然就跪了. 题意:有一个果园里有非常多树,上面有非常多果实,为了不然成熟的 ...

  8. Codeforces441B_Valera and Fruits(暴力)

    Valera and Fruits time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round 252 (Div. 2)

    layout: post title: Codeforces Round 252 (Div. 2) author: "luowentaoaa" catalog: true tags ...

随机推荐

  1. Redis 通用key操作命令

    1.在redis里面允许模糊查询key,有3个通配符:*,?,[]. *:通配任意字符 ?:通配单个字符 []:通配中括号内的某个字符 例如: 2.randomKey 随机返回所有key中的某个 3. ...

  2. Xshell连接linux服务器不成功的乌龙问题

    一般xshell连接linux服务器不成功有以下几个问题: linux防火墙拦截,导致Xshell不能访问linux 操作方法: firewalld(CentOS7):启动 :systemctl st ...

  3. Mysql [Err] 1118 - Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535.

    对于越来越多的数据,数据库的容量越来越大,压缩也就越来越常见了.在我的实际工作中进行过多次压缩工作,也遇到多次问题,在此和大家分享一下. 首先,我们先说说怎么使用innodb的压缩. 第一,mysql ...

  4. esp32(M5STACK)在线体验(Ubuntu)

    我们往m5stack烧录的固件是可以在线编程的 具体使用方法可以参考   https://github.com/m5stack/M5Cloud/blob/master/README_CN.md     ...

  5. USACO 4.1 Fence Rails

    Fence RailsBurch, Kolstad, and Schrijvers Farmer John is trying to erect a fence around part of his ...

  6. 三列布局中 float引发的一个问题-当“非float的元素”和“float的元素”在一起的时候,如果非float元素在先,那么float的元素将受到排斥。

    样式: 效果是这样的: 解答:我们发现:靠右的元素自动换行了,原因是:当“非float的元素”和“float的元素”在一起的时候,如果非float元素在先,那么float的元素将受到排斥. 解决方法: ...

  7. CentOS7.4上搭建rocketMQ集群

    一.rocketMQ集群部署方案优缺点对比: 多Master模式(2m-noslave) : 一个集群无Slave,全是Master,例如2个Master或者3个Master 优点:配置简单,单个Ma ...

  8. PHP判读MySQL是否执行成功

    针对update 语句等会对数据表进行修改的语句 在mysql_query($sql);后面加上 $result = mysql_affected_rows(); 如果$result 值为-1表明语句 ...

  9. xdebug PHPStrom调试 安装

    1.publi目录下新建info文件: 2.搜索是否已经安装过xdebug 3.下载安装 >[info] 自动查找相对应的版本 == 打开php.ini 原始: [XDebug]xdebug.p ...

  10. jquery怎样获取多个复选框的值?

    jquery的遍历方法可以获取复选框所欲的选中值 1 2 $("input:checkbox:checked").each(function(index,element));    ...