Valera and Fruits
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; struct edge{ int d; int num; }a[3500]; 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 <= n ;i++){ int m1 = m; for(int j = 1; j <= n; j++){ printf("%d ",ans); if(i - a[j].d >=0 && i - a[j].d <= 1){ if(a[j].num <= m1){ ans += a[j].num; a[j].num = 0; m1 -= a[j].num; } else{ ans += m1; a[j].num -= m1; m1 = 0; break; } } } } printf("%d\n",ans); } return 0; }