#include <cstdio> #include <set> #include <functional> #include <algorithm> using namespace std; const int MAX = 1000010; int n; long long A[MAX], B[MAX], C[MAX], ANS[MAX]; bool V[MAX]; int sortA[MAX]; struct str_cmp { bool operator()(int a, int b) { if(A[a] == A[b]) return a < b; return A[a] < A[b]; } }; set<int, str_cmp> S; long long SUF[MAX]; // pref int main() { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%lld%lld", A + i, B + i); C[i] = B[i] - A[i]; sortA[i] = i; } sort(sortA, sortA + n, [] (int a, int b) -> bool { return A[a] < A[b]; }); for(int i = 0; i < n; i++) { long long maxi = 0, t; int max_idx; auto ptr = S.begin(); long long l = 0; for(int j = 0; j < n; j++) { if(!V[j]) { while(ptr != S.end() && A[*ptr] < A[j]) {ptr++; l++;} t = B[j] + SUF[l] + l * A[j]; if(maxi < t) { maxi = t; max_idx = j; } } } //printf("%d %lld\n", max_idx, maxi); V[max_idx] = true; S.insert(max_idx); int j = S.size(); for(auto it = S.rbegin(); it != S.rend(); it++) { SUF[j - 1] = SUF[j] + A[*it]; j --; } //for(int i = 0; i < S.size(); i++) printf("%lld ", SUF[i]); printf("\n"); //for(auto x: S) printf("%d ", x); /*long long k = 0; for(auto ptr = S.begin(); ptr != S.end(); ptr++) { ANS[i] += B[*ptr] + k * A[*ptr]; k++; }*/ if(i == 0) ANS[i] = maxi; else ANS[i] = ANS[i - 1] + maxi; } for(int i = 0; i < n; i++) printf("%lld\n", ANS[i]); }
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 69 70 71 | #include <cstdio> #include <set> #include <functional> #include <algorithm> using namespace std; const int MAX = 1000010; int n; long long A[MAX], B[MAX], C[MAX], ANS[MAX]; bool V[MAX]; int sortA[MAX]; struct str_cmp { bool operator()(int a, int b) { if(A[a] == A[b]) return a < b; return A[a] < A[b]; } }; set<int, str_cmp> S; long long SUF[MAX]; // pref int main() { scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%lld%lld", A + i, B + i); C[i] = B[i] - A[i]; sortA[i] = i; } sort(sortA, sortA + n, [] (int a, int b) -> bool { return A[a] < A[b]; }); for(int i = 0; i < n; i++) { long long maxi = 0, t; int max_idx; auto ptr = S.begin(); long long l = 0; for(int j = 0; j < n; j++) { if(!V[j]) { while(ptr != S.end() && A[*ptr] < A[j]) {ptr++; l++;} t = B[j] + SUF[l] + l * A[j]; if(maxi < t) { maxi = t; max_idx = j; } } } //printf("%d %lld\n", max_idx, maxi); V[max_idx] = true; S.insert(max_idx); int j = S.size(); for(auto it = S.rbegin(); it != S.rend(); it++) { SUF[j - 1] = SUF[j] + A[*it]; j --; } //for(int i = 0; i < S.size(); i++) printf("%lld ", SUF[i]); printf("\n"); //for(auto x: S) printf("%d ", x); /*long long k = 0; for(auto ptr = S.begin(); ptr != S.end(); ptr++) { ANS[i] += B[*ptr] + k * A[*ptr]; k++; }*/ if(i == 0) ANS[i] = maxi; else ANS[i] = ANS[i - 1] + maxi; } for(int i = 0; i < n; i++) printf("%lld\n", ANS[i]); } |