codeforces252-B - Valera and Fruits

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<memory.h>
#include<map>
#include<queue>
#include <deque>
#include <list>
#include <ctime>
#include <stack>
#include <vector>
#include<set>
#define Maxn 3005
typedef long long ll;
#define rep(i,j,n) for(int i=j;i<=n;i++)
#define per(i,j,k) for(int i=j;i>=k;--i)
#define lowbit(a) a&-a
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
using namespace std;
int a,b,t[Maxn];
int main()
{   int n,v,sum=0,tmp=0;
    int end_t=-1,start_t=9999;
    scanf("%d%d",&n,&v);
    rep(i,1,n)
    {scanf("%d%d",&a,&b);
     t[a]+=b;
     end_t=Max(end_t,a);
     start_t=Min(start_t,a);
    }
    rep(i,start_t,end_t+1)
    {
        if(tmp>=v)tmp=t[i],sum+=v;
        else
        {   if(tmp+t[i]<=v)sum+=tmp+t[i],tmp=0;
            else tmp=t[i]-(v-tmp),sum+=v;
        }
    }
    printf("%d\n",sum);
    return 0;
}





你可能感兴趣的:(codeforces252-B - Valera and Fruits)