#include <bits/stdc++.h> #include <stdint.h> using namespace std; struct AffineMushroom { int64_t a, b; bool operator<(AffineMushroom const &rhs) const { return a < rhs.a; } friend istream& operator>>(istream &is, AffineMushroom &am) { return (is >> am.a >> am.b); } }; int main() { ios_base::sync_with_stdio(false); #ifndef LOCAL_TESTS cin.tie(NULL); #endif int n; cin >> n; vector<AffineMushroom> M(n); for(int i = 0; i < n; i++) cin >> M[i]; sort(M.begin(), M.end()); vector<int> pos(n, -1); vector<int64_t> sumL(n + 1); vector<int64_t> sumR(n + 1); for(int i = 0; i < n; ++i) { sumL[0] = 0; sumR[0] = 0; for(int j = 0; j < n; ++j) { if(pos[j] == -1) { sumL[j+1] = sumL[j]; sumR[j+1] = sumR[j]; } else { sumL[j+1] = sumL[j] + M[j].b + M[j].a * pos[j]; sumR[j+1] = sumR[j] + M[j].b + M[j].a * (pos[j] + 1); } } int64_t bv = 0; int bj = 0; int bp = 0; for(int j = 0, last = -1; j < n; ++j) { if(pos[j] == -1) { int64_t cv = sumL[j] + (M[j].b + M[j].a * (last + 1)) + (sumR[n] - sumR[j+1]); if(cv > bv) { bv = cv; bj = j; bp = last + 1; } } else { last = pos[j]; } } pos[bj] = bp; for(int j = bj + 1; j < n; ++j) { if(pos[j] != -1) pos[j]++; } cout << bv << '\n'; } return 0; }
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 72 73 74 75 76 77 78 79 | #include <bits/stdc++.h> #include <stdint.h> using namespace std; struct AffineMushroom { int64_t a, b; bool operator<(AffineMushroom const &rhs) const { return a < rhs.a; } friend istream& operator>>(istream &is, AffineMushroom &am) { return (is >> am.a >> am.b); } }; int main() { ios_base::sync_with_stdio(false); #ifndef LOCAL_TESTS cin.tie(NULL); #endif int n; cin >> n; vector<AffineMushroom> M(n); for(int i = 0; i < n; i++) cin >> M[i]; sort(M.begin(), M.end()); vector<int> pos(n, -1); vector<int64_t> sumL(n + 1); vector<int64_t> sumR(n + 1); for(int i = 0; i < n; ++i) { sumL[0] = 0; sumR[0] = 0; for(int j = 0; j < n; ++j) { if(pos[j] == -1) { sumL[j+1] = sumL[j]; sumR[j+1] = sumR[j]; } else { sumL[j+1] = sumL[j] + M[j].b + M[j].a * pos[j]; sumR[j+1] = sumR[j] + M[j].b + M[j].a * (pos[j] + 1); } } int64_t bv = 0; int bj = 0; int bp = 0; for(int j = 0, last = -1; j < n; ++j) { if(pos[j] == -1) { int64_t cv = sumL[j] + (M[j].b + M[j].a * (last + 1)) + (sumR[n] - sumR[j+1]); if(cv > bv) { bv = cv; bj = j; bp = last + 1; } } else { last = pos[j]; } } pos[bj] = bp; for(int j = bj + 1; j < n; ++j) { if(pos[j] != -1) pos[j]++; } cout << bv << '\n'; } return 0; } |