#include <cstdio> #include <utility> #include <algorithm> #include <numeric> const int MAX_N = 1000009; std::pair<long long, long long> ab[MAX_N]; long long r[MAX_N]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld%lld", &ab[i].first, &ab[i].second); } std::sort(ab, ab+n); for (int i = 0; i < n; ++i) { ab[i].second += i*ab[i].first; r[n-1] += ab[i].second; } for (int i = n-2; i >= 0; --i) { int max = -1; long long base = r[i+1]; for (int j = i+1; j >= 0; --j) { long long val = base-ab[j].second; if (val > r[i]) { r[i] = val; max = j; } base -= ab[j].first; } for (int j = max+1; j < n; ++j) { ab[j].second -= ab[j].first; ab[j-1] = ab[j]; } } for (int i = 0; i < n; ++i) { printf("%lld\n", r[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 | #include <cstdio> #include <utility> #include <algorithm> #include <numeric> const int MAX_N = 1000009; std::pair<long long, long long> ab[MAX_N]; long long r[MAX_N]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%lld%lld", &ab[i].first, &ab[i].second); } std::sort(ab, ab+n); for (int i = 0; i < n; ++i) { ab[i].second += i*ab[i].first; r[n-1] += ab[i].second; } for (int i = n-2; i >= 0; --i) { int max = -1; long long base = r[i+1]; for (int j = i+1; j >= 0; --j) { long long val = base-ab[j].second; if (val > r[i]) { r[i] = val; max = j; } base -= ab[j].first; } for (int j = max+1; j < n; ++j) { ab[j].second -= ab[j].first; ab[j-1] = ab[j]; } } for (int i = 0; i < n; ++i) { printf("%lld\n", r[i]); } } |