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. OAuth四种模式

    授权码模式(authorization code)----适用于网站服务端去oauth服务端申请授权 简化模式(implicit)----没有服务端,js+html页面去oauth服务端申请授权 密码 ...

  2. React Native - 使用Vibration API实现设备振动

    有时程序中需要实现这样的功能,当有重要的消息提醒时,我们会发出声音告知用户.而如果用户关闭了声音,那么就可以改用振动来提醒用户. 使用 React Native 提供的 Vibration API,我 ...

  3. React Native未来导航者:react-navigation 使用详解

    该库包含三类组件: (1)StackNavigator:用来跳转页面和传递参数 (2)TabNavigator:类似底部导航栏,用来在同一屏幕下切换不同界面 (3)DrawerNavigator:侧滑 ...

  4. 连接mysql时遇到的问题

    1.报错:The server time zone value '???ú±ê×??±??' is unrecognized or represents 解决方法:在jdbc连接的url后面加上ser ...

  5. eas之获取不同类型的组织视图

    OrgViewF7 orgF7=new OrgViewF7(this);orgF7.setCurrentCUID(company.getId().toString());orgF7.setMultiS ...

  6. 10.3 .NET 3.5 中的扩展方法

    10.3.4 用 Select 方法和匿名类型进行投影 class Program { static void Main(string[] args) { , ).Where(x => x % ...

  7. java环境搭建心得

     右击此电脑,点击属性, 在打开的电脑系统对话框里发电机i直接点击左侧导航里的[高级系统设置]在打开的电脑系统属性对话框里直接点击下面的[环境变量] 打开环境变量对话框后,直接点击系统变量下面的新建, ...

  8. Ubuntu安装RTX2080显卡驱动

    安装RTX2080显卡驱动 近日新购了一台DELL服务器,用于TensorFlow,由于显卡是另加的,需要安装显卡驱动. 服务器配置 服务器型号:DELL PowerEdge R730 CPU:2*I ...

  9. 【Leetcode】【简单】【169求众数】【JavaScript】

    题目 169. 求众数 给定一个大小为 n 的数组,找到其中的众数.众数是指在数组中出现次数大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的,并且给定的数组总是存在众数. 示例 1: 输入: [ ...

  10. constraint、index、view(day04)

    回顾: 1.sql99中的表连接 select 字段列表 from 左表 {[inner]|{left|right|full} [outer]} join 右表 on 关联条件; 集合操作 union ...