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. 监控memcached服务

    #!/bin/bash #监控memcached服务 printf "del key\r\n" | nc 127.0.0.1 11211 &>/dev/null #使 ...

  2. 紫书 例题 10-10 UVa 10491(概率计算)

    公式很好推,表示被高中生物遗传概率计算虐过的人 这个公式简直不需要动脑 #include<cstdio> using namespace std; int main() { double ...

  3. 紫书 例题 10-17 UVa 1639(数学期望+对数保存精度)

    设置最后打开的是盒子1, 另外一个盒子剩下i个 那么在这之前打开了n + n - i次盒子 那么这个时候的概率是C(2 * n - i, n) p ^ (n+1) (1-p)^ (n - i) 那么反 ...

  4. ECNUOJ 2150 完美的拯救

    完美的拯救 Time Limit:1000MS Memory Limit:65536KBTotal Submit:147 Accepted:50 Description  一只可怜的蚂蚁被万恶的魔术师 ...

  5. sql server 2000 自动收缩数据库大小

    转载.......http://mars968.blog.163.com/blog/static/7400033200941642356258/ SQLServer2000压缩日志及数据库文件     ...

  6. Android 五大存储方式具体解释

    SharedPreferences与Editor SharedPreferences保存的数据仅仅要是类似于配置信息格式的数据.因此它保存的数据主要是简单的key-value对形式.以下关系图 上图全 ...

  7. 动态限制EdiText仅仅能输入特定字符

    怎样设置EditText,使得仅仅能输入数字或者某些字母呢? 一.设置EditText,仅仅输入数字: 方法1:直接生成DigitsKeyListener对象就能够了. et_1.setKeyList ...

  8. net.sf.json Maven依赖配置

    转自:https://blog.csdn.net/qq_36698956/article/details/80772984 今天搭框架开始实现前台的json了,于是逐个找适合的框架,发现要实现json ...

  9. Ubuntu16.04进入挂起或休眠状态时按任何键都无法唤醒问题解决办法

    挂起(待机)计算机将目前的运行状态等数据存放在内存,关闭硬盘.外设等设备,进入等待状态.此时内存仍然需要电力维持其数据,但整机耗电很少.恢复时计算机从内存读 出数据,回到挂起前的状态,恢复速度较快.一 ...

  10. Vim使用心得总结

    基本快捷键 v 进入可视模式 i / a 光标前/后插入模式 I / A 行首/末插入模式 Crtl+c 进入命令模式 Crtl+v 进入块可视模式 Q 进入EX模式 gh 进入选择模式 u 撤销 U ...