#include <bits/stdc++.h>
using namespace std;
#define int long long
struct para {
int a;
int w;
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, c; cin >> n >> c;
vector<para> blocks(n);
for (int i = 0; i < n; ++i) cin >> blocks[i].a >> blocks[i].w;
reverse(blocks.begin(), blocks.end());
vector<vector<para>> groups;
if (!blocks.empty()) {
int current_a = blocks[0].a;
vector<para> current_group = {blocks[0]};
for (int i = 1; i < n; ++i) {
if (blocks[i].a == current_a) {
current_group.push_back(blocks[i]);
} else {
groups.push_back(current_group);
current_group = {blocks[i]};
current_a = blocks[i].a;
}
}
groups.push_back(current_group);
}
map<int, int> dp;
int res = 0;
for (auto& group : groups) {
map<int, int> temp_dp;
int current_max = res;
for (auto &block : group) {
int same = dp.count(block.w) ? dp[block.w] : 0;
int diff = (current_max > 0) ? (current_max - c) : 0;
int best = max(same, diff);
int new_wynik = block.a + best;
if (!temp_dp.count(block.w) || new_wynik > temp_dp[block.w]) {
temp_dp[block.w] = new_wynik;
}
}
for (auto& [w, wynik] : temp_dp) {
if (wynik > (dp.count(w) ? dp[w] : 0)) {
dp[w] = wynik;
if (wynik > res) {
res = wynik;
}
}
}
}
cout << res << "\n";
}
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 | #include <bits/stdc++.h> using namespace std; #define int long long struct para { int a; int w; }; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, c; cin >> n >> c; vector<para> blocks(n); for (int i = 0; i < n; ++i) cin >> blocks[i].a >> blocks[i].w; reverse(blocks.begin(), blocks.end()); vector<vector<para>> groups; if (!blocks.empty()) { int current_a = blocks[0].a; vector<para> current_group = {blocks[0]}; for (int i = 1; i < n; ++i) { if (blocks[i].a == current_a) { current_group.push_back(blocks[i]); } else { groups.push_back(current_group); current_group = {blocks[i]}; current_a = blocks[i].a; } } groups.push_back(current_group); } map<int, int> dp; int res = 0; for (auto& group : groups) { map<int, int> temp_dp; int current_max = res; for (auto &block : group) { int same = dp.count(block.w) ? dp[block.w] : 0; int diff = (current_max > 0) ? (current_max - c) : 0; int best = max(same, diff); int new_wynik = block.a + best; if (!temp_dp.count(block.w) || new_wynik > temp_dp[block.w]) { temp_dp[block.w] = new_wynik; } } for (auto& [w, wynik] : temp_dp) { if (wynik > (dp.count(w) ? dp[w] : 0)) { dp[w] = wynik; if (wynik > res) { res = wynik; } } } } cout << res << "\n"; } |
English