链接:http://codeforces.com/problemset/problem/441/B
Valera and Fruits
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.
Sample test(s)
2 3 1 5 2 3
8
5 10 3 20 2 20 1 20 4 20 5 20
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.
直接附上AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef unsigned int li;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const int maxn = 3005;
int n, get, a, b;
int tree[maxn];
int main()
{
ios::sync_with_stdio(false);
while (~scanf("%d%d", &n, &get))
{
memset(tree, 0, sizeof(tree));
int maxa = 0;
while (n--)
{
scanf("%d%d", &a, &b);
tree[a] += b;
maxa = max(maxa, a);
}
int sum = 0;
for (int i=1; i<=maxa+1; i++)
{
if (tree[i] > get)
{
sum += get;
tree[i] -= get;
if (tree[i] > get)
tree[i+1] += get;
else
tree[i+1] += tree[i];
}
else
sum += tree[i];
}
printf("%d\n", sum);
}
return 0;
}