Valera and Fruits

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 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 Input

Input

2 31 52 3

Output

8

Input

5 103 202 201 204 205 20

Output

60

Hint

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.

大意:摘水果,每种水果的成熟期为两天,单纯暴力,就把i与时间相比,如果在这个时间内,就有两种情况,一种是摘得水果不够,那就摘下一天的,另一种是已经满了,不过在每次计算后,都要把值减去已经摘了的。原本一直看不出哪儿错了- -结果回寝室一眼看出了,还是得歇一会儿再看清楚!

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct edge{
    int d;
    int num;
}a[5000];
bool cmp(edge i, edge j){
    return i.d < j.d;
}
int main()
{
 int n,m;
 while(~scanf("%d%d",&n,&m)){
        for(int i = 1; i <= n ; i++)
        scanf("%d%d", &a[i].d,&a[i].num);
      sort(a+1, a+n+1,cmp);
      int ans = 0;
      for(int i = 1; i <= 3100 ; i++){
            int m1 = m;
         for(int j = 1; j <= n ;j++){
                if(i-a[j].d >= 0&&i - a[j].d <= 1){
                    if(a[j].num <= m1){
                        ans += a[j].num;
                        m1 -= a[j].num;
                        a[j].num = 0;
                    }
                   else {
                        ans += m1;
                        a[j].num -= m1;
                        m1 = 0;
                        break;
                   }
                }
             }
         }
      printf("%d\n",ans);
      }
      return 0;
 }

时间: 2024-08-12 20:27:53

Valera and Fruits的相关文章

Codeforces Round #252 (Div. 2)

A. Valera and Antique Items 题目大意:有一场拍卖,有n个卖家,每个卖家有ki个物品并且底价知道,你想买东西并且只有V元钱,问可以从哪几个商家那里去买 思路:对每个卖家的每样商品作比较即可 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #define maxn 1000 5 using namespace std; 6 int ans[maxn],h,x,k; 7

CodeForces 12C Fruits

Fruits Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 12C Description The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera deci

HDU 1503 Advanced Fruits

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3622    Accepted Submission(s): 1872Special Judge Problem Description The company "21st Century Fruits" has specialized in cr

Advanced Fruits(HDU 1503 LCS变形)

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2358    Accepted Submission(s): 1201Special Judge Problem Description The company "21st Century Fruits" has specialized in cr

HDU1503:Advanced Fruits 【LCS】

Advanced Fruits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 2 Special Judge Problem Description The company "21st Century Fruits" has specialized in creating

HDU 1503 Advanced Fruits (LCS,DP)

题意:给你两字符串s1,s2,用最短的字符串表示他们(公共字串输出一次). Sample Input apple peach ananas banana pear peach Sample Output appleach bananas pearch dp[i][j] : 第一个字符串的前 i 个 ,和第二个字符串的前 j 个最短组合的长度 . pre[i][j] : 第一个字符串的第 i 个 ,和第二个字符串的第 j 个字符的状态. #include<cstdio> #include<

Codeforces A. Valera and X 题解

判断二维字符串是否满足下面条件: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the progra

Advanced Fruits HDU - 1503

The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new fruit emerges th

杭电 1503 Advanced Fruits

Description The company "21st Century Fruits" has specialized in creating new sorts of fruits by transferring genes from one fruit into the genome of another one. Most times this method doesn't work, but sometimes, in very rare cases, a new frui