B. Valera and Fruits
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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?

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.

Sample test(s)
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.


import java.util.*;

public class Main
{
public static void main(String[] args)
{
class Tree
{
int a,b;
Tree(){}
Tree(int A,int B)
{
a=A; b=B;
}
}
Scanner cin=new Scanner(System.in);
int n,v;
n=cin.nextInt();
v=cin.nextInt();
Tree[] tree=new Tree[n];
for(int i=0;i<n;i++)
{
int a=cin.nextInt();
int b=cin.nextInt();
tree[i]=new Tree(a,b);
}
int[] havday=new int[3010];
for(int i=0;i<n;i++)
{
havday[tree[i].a]+=tree[i].b;
}
int fit=0,unfit=0,sum=0;
for(int i=1;i<=3001;i++)
{
fit=havday[i];
if(unfit>=v)
{
sum+=v;
unfit=fit;
}
else
{
sum+=unfit;
int carry=v-unfit;
if(carry>=fit)
{
sum+=fit;
unfit=0;
}
else
{
sum+=carry;
unfit=fit-carry;
}
}
}
System.out.println(sum);
}
}

Codeforces 441 B. Valera and Fruits的更多相关文章

  1. CodeForces 441 A. Valera and Antique Items

    纯粹练JAVA.... A. Valera and Antique Items time limit per test 1 second memory limit per test 256 megab ...

  2. 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 ...

  3. Valera and Fruits

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

  4. Codeforces#441 Div.2 四小题

    Codeforces#441 Div.2 四小题 链接 A. Trip For Meal 小熊维尼喜欢吃蜂蜜.他每天要在朋友家享用N次蜂蜜 , 朋友A到B家的距离是 a ,A到C家的距离是b ,B到C ...

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

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

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

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

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

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

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

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

  9. C - Valera and Fruits

    Problem description Valera loves his garden, where n fruit trees grow. This year he will enjoy a gre ...

随机推荐

  1. Linux下的压缩与解压缩

    1 .gz 1)压缩 root@xiaohuang-virtual-machine:/home/xiaohuang/桌面/hellow/hellow# gzip 2.txt 3.txt root@xi ...

  2. 【BZOJ 1045】 [HAOI2008] 糖果传递

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 思路来自hzwer.. 设xi表示第i个人往左传递了xi个糖果. (如果小于0表示旁边的人给他了糖果. 则ans=∑|xi| 最后所 ...

  3. File的getPath()和getAbsolutePath()和getCanonicalPath()的差别

    这几个方法是有一次无意的发现,我当时也不知道什么意思,就百度了,查到了一些列子: 原文地址http://www.blogjava.net/dreamstone/archive/2007/08/08/1 ...

  4. 思考一下activity的启动模式

    在android里,有4种activity的启动模式.分别为:"standard" (默认) "singleTop" "singleTask" ...

  5. base64格式的图片数据如何转成图片

    base64格式的图片数据如何转成图片 一.总结 一句话总结:不仅要去掉前面的格式串,还需要base64_decode()解码才行. // $base_img是获取到前端传递的值 $base_img ...

  6. java线程深入学习

    一.java中的线程是通过Thread类创建的, //下面是构造函数,一个共同的特点就是:都是调用init()进行创建的 public Thread() { init(null, null, &quo ...

  7. C++实现矩阵求逆

    最近实现一个算法要用到求逆等矩阵运算,在网上搜到一个别人写的矩阵类,试了一下效果不错,贴在这里,做个保存. matrix.h文件: #ifndef __MATRIX_H__ #define __MAT ...

  8. C# DevExpress GridControl使用方法

    一.如何解决单击记录整行选中的问题 View->OptionsBehavior->EditorShowMode 设置为:Click 二.如何新增一条记录 ().gridView.AddNe ...

  9. 休假结束,Linus重回内核开发岗位

    在休假反省一个多月之后,Linus Torvalds 又回来了.10 月 22 日爱丁堡举行的欧洲开源峰会上,Linus Torvalds 将与内核维护者们碰头,这是他重新接管Linux内核开发的第一 ...

  10. tar---打包,解压缩linux的文件和目录

    tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁带上创建档案,现在,用户可以在 ...