#include <cstdio> #include <algorithm> using namespace std; long long a[1000009]; long long b[1000009]; bool taken[1000009]; int leftcnt[1000009]; long long ra[1000009]; pair<long long, long long> pr[1000009]; int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%lld %lld", &pr[i].first, &pr[i].second); } sort(pr, pr+n); for(int i=0;i<n;i++){ a[i]=pr[i].first; b[i]=pr[i].second; } /* for(int i=0;i<n;i++){ printf("%lld %lld\n", a[i], b[i]); } */ long long sum=0; for(int day=1;day<=n;day++){ int bestfor=-1; long long bestadd=-1; for(int i=0;i<n;i++){ if(taken[i]){ continue; } long long add=b[i]; add+=a[i]*leftcnt[i]; // Day at which I will be taken. add+=ra[i]; // Additional shrooms taken later. if(add>bestadd){ bestadd=add; bestfor=i; } } taken[bestfor]=1; sum+=bestadd; printf("%lld\n", sum); for(int i=0;i<bestfor;i++){ ra[i]+=a[bestfor]; } for(int i=bestfor;i<n;i++){ leftcnt[i]++; } /* printf("Right now, these are taken:\n"); for(int i=0;i<n;i++){ printf("%d\t",taken[i]); } printf("\n"); for(int i=0;i<n;i++){ printf("%d\t",leftcnt[i]); } printf("\n"); for(int i=0;i<n;i++){ printf("%lld\t",ra[i]); } printf("\n"); */ } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <cstdio> #include <algorithm> using namespace std; long long a[1000009]; long long b[1000009]; bool taken[1000009]; int leftcnt[1000009]; long long ra[1000009]; pair<long long, long long> pr[1000009]; int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%lld %lld", &pr[i].first, &pr[i].second); } sort(pr, pr+n); for(int i=0;i<n;i++){ a[i]=pr[i].first; b[i]=pr[i].second; } /* for(int i=0;i<n;i++){ printf("%lld %lld\n", a[i], b[i]); } */ long long sum=0; for(int day=1;day<=n;day++){ int bestfor=-1; long long bestadd=-1; for(int i=0;i<n;i++){ if(taken[i]){ continue; } long long add=b[i]; add+=a[i]*leftcnt[i]; // Day at which I will be taken. add+=ra[i]; // Additional shrooms taken later. if(add>bestadd){ bestadd=add; bestfor=i; } } taken[bestfor]=1; sum+=bestadd; printf("%lld\n", sum); for(int i=0;i<bestfor;i++){ ra[i]+=a[bestfor]; } for(int i=bestfor;i<n;i++){ leftcnt[i]++; } /* printf("Right now, these are taken:\n"); for(int i=0;i<n;i++){ printf("%d\t",taken[i]); } printf("\n"); for(int i=0;i<n;i++){ printf("%d\t",leftcnt[i]); } printf("\n"); for(int i=0;i<n;i++){ printf("%lld\t",ra[i]); } printf("\n"); */ } } |