#ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; int n, c; const int maxN = 500'005; int a[maxN], w[maxN]; vector<pair<int,int>> f[maxN]; //for color ll dp[maxN]; void umax(ll& a,const ll& b) { a = max(a, b); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); auto start = std::chrono::high_resolution_clock::now(); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif map<pair<int,int>,int> mp; cin >> n >> c; for (int i = 1; i <= n; i++) { cin >> a[i] >> w[i]; mp[{a[i], w[i]}] += 1; } for (auto& it : mp) { f[it.first.first].emplace_back(it.first.second, it.second); } const ll INF = 1e18; for (int i = 1; i < maxN; i++) { dp[i] = -INF; } ll mx = c; for (int i = 1; i < maxN; i++) { if (f[i].empty()) continue; // debug(f[i]); vector<ll> upd(f[i].size(), -INF); ll S = 0; for (int j = 0; j < (int)f[i].size(); j++) { int cnt = f[i][j].second; //+a * cnt - c ll X = 1LL * cnt * i - c; S += max(0LL, X); } //X -> dp[t] + X - c} for (int j = 0; j < (int)f[i].size(); j++) { ll curS = S; int cnt = f[i][j].second; ll X = 1LL * i - c; curS -= max(0LL, X); umax(upd[j], mx + X); umax(upd[j], dp[f[i][j].first] + X + c); } /*for (int z = 0; z < 2; z++) { reverse(f[i].begin(), f[i].end()); reverse(upd.begin(), upd.end()); ll mxadd = -INF; for (int j = 0; j < (int)f[i].size(); j++) { ll curS = S; int cnt = f[i][j].second; ll X = 1LL * cnt * i - c; curS -= max(0LL, X); umax(upd[j], mxadd + X + curS); ll hisdelta = dp[f[i][j].first] + c + X - max(0LL, X); umax(mxadd, hisdelta); } } //1LL * cnt * i - c*/ for (int j = 0; j < f[i].size(); j++) { umax(dp[f[i][j].first], upd[j]); // debug(dp[f[i][j].first]); umax(mx, upd[j]); } } cout << max(0LL, *max_element(dp + 1, dp + maxN)) << '\n'; auto end = std::chrono::high_resolution_clock::now(); std::cerr << "Execution Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; 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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | #ifdef DEBUG #define _GLIBCXX_DEBUG #endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; int n, c; const int maxN = 500'005; int a[maxN], w[maxN]; vector<pair<int,int>> f[maxN]; //for color ll dp[maxN]; void umax(ll& a,const ll& b) { a = max(a, b); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); auto start = std::chrono::high_resolution_clock::now(); #ifdef DEBUG freopen("input.txt", "r", stdin); #endif map<pair<int,int>,int> mp; cin >> n >> c; for (int i = 1; i <= n; i++) { cin >> a[i] >> w[i]; mp[{a[i], w[i]}] += 1; } for (auto& it : mp) { f[it.first.first].emplace_back(it.first.second, it.second); } const ll INF = 1e18; for (int i = 1; i < maxN; i++) { dp[i] = -INF; } ll mx = c; for (int i = 1; i < maxN; i++) { if (f[i].empty()) continue; // debug(f[i]); vector<ll> upd(f[i].size(), -INF); ll S = 0; for (int j = 0; j < (int)f[i].size(); j++) { int cnt = f[i][j].second; //+a * cnt - c ll X = 1LL * cnt * i - c; S += max(0LL, X); } //X -> dp[t] + X - c} for (int j = 0; j < (int)f[i].size(); j++) { ll curS = S; int cnt = f[i][j].second; ll X = 1LL * i - c; curS -= max(0LL, X); umax(upd[j], mx + X); umax(upd[j], dp[f[i][j].first] + X + c); } /*for (int z = 0; z < 2; z++) { reverse(f[i].begin(), f[i].end()); reverse(upd.begin(), upd.end()); ll mxadd = -INF; for (int j = 0; j < (int)f[i].size(); j++) { ll curS = S; int cnt = f[i][j].second; ll X = 1LL * cnt * i - c; curS -= max(0LL, X); umax(upd[j], mxadd + X + curS); ll hisdelta = dp[f[i][j].first] + c + X - max(0LL, X); umax(mxadd, hisdelta); } } //1LL * cnt * i - c*/ for (int j = 0; j < f[i].size(); j++) { umax(dp[f[i][j].first], upd[j]); // debug(dp[f[i][j].first]); umax(mx, upd[j]); } } cout << max(0LL, *max_element(dp + 1, dp + maxN)) << '\n'; auto end = std::chrono::high_resolution_clock::now(); std::cerr << "Execution Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; return 0; } |