#include <iostream>
#include <vector>
using namespace std;
#define MAXW 500001
#define LL long long
#define NEG (-1000000000000000000LL)
struct Block {
int a, w;
};
class Solver {
public:
static void solve() {
int n, c;
vector<Block> blocks;
cin >> n >> c;
blocks.resize(n);
for (int i = 0; i < n; i++) {
cin >> blocks[i].a >> blocks[i].w;
}
vector<LL> bestForPattern(MAXW, NEG);
LL best_global = NEG;
LL second_best_global = NEG;
int best_global_pattern = -1;
LL ans = 0;
int i = 0;
while (i < n) {
int currA = blocks[i].a;
vector<pair<int, LL> > group;
while (i < n && blocks[i].a == currA) {
int w = blocks[i].w;
LL cand1 = bestForPattern[w];
LL cand2;
if (best_global_pattern == w)
cand2 = second_best_global;
else
cand2 = best_global;
LL cand2_adj = (cand2 == NEG ? NEG : cand2 - c);
LL bestPrev = max(cand1, cand2_adj);
bestPrev = max(bestPrev, 0LL);
LL dp = currA + bestPrev;
group.emplace_back(w, dp);
ans = max(ans, dp);
i++;
}
for (auto &[fst, snd]: group) {
const int w = fst;
LL dp = snd;
bestForPattern[w] = max(bestForPattern[w], dp);
if (dp > best_global) {
if (best_global_pattern == w) {
best_global = dp;
} else {
second_best_global = best_global;
best_global = dp;
best_global_pattern = w;
}
} else if (w != best_global_pattern && dp > second_best_global) {
second_best_global = dp;
}
}
}
cout << ans << endl;
}
};
int main() {
Solver::solve();
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 <iostream> #include <vector> using namespace std; #define MAXW 500001 #define LL long long #define NEG (-1000000000000000000LL) struct Block { int a, w; }; class Solver { public: static void solve() { int n, c; vector<Block> blocks; cin >> n >> c; blocks.resize(n); for (int i = 0; i < n; i++) { cin >> blocks[i].a >> blocks[i].w; } vector<LL> bestForPattern(MAXW, NEG); LL best_global = NEG; LL second_best_global = NEG; int best_global_pattern = -1; LL ans = 0; int i = 0; while (i < n) { int currA = blocks[i].a; vector<pair<int, LL> > group; while (i < n && blocks[i].a == currA) { int w = blocks[i].w; LL cand1 = bestForPattern[w]; LL cand2; if (best_global_pattern == w) cand2 = second_best_global; else cand2 = best_global; LL cand2_adj = (cand2 == NEG ? NEG : cand2 - c); LL bestPrev = max(cand1, cand2_adj); bestPrev = max(bestPrev, 0LL); LL dp = currA + bestPrev; group.emplace_back(w, dp); ans = max(ans, dp); i++; } for (auto &[fst, snd]: group) { const int w = fst; LL dp = snd; bestForPattern[w] = max(bestForPattern[w], dp); if (dp > best_global) { if (best_global_pattern == w) { best_global = dp; } else { second_best_global = best_global; best_global = dp; best_global_pattern = w; } } else if (w != best_global_pattern && dp > second_best_global) { second_best_global = dp; } } } cout << ans << endl; } }; int main() { Solver::solve(); return 0; } |
English