#include <iostream>
#include <unordered_map>
#include <unordered_set>
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
int n = 0;
int c = 0;
int a = 0;
int b = 0;
cin >> n >> c;
int64_t maxResult = 0;
int64_t secondMax = 0;
std::unordered_map<int, int64_t> resulIfEndsWith = {};
int lastTakenSize = 0;
std::unordered_set<int> pending = {};
for (int i = 0; i < n; i++) {
cin >> a >> b;
if (lastTakenSize == a) {
if (pending.find(b) != pending.end()) {
continue;
}
pending.insert(b);
resulIfEndsWith[b] = max(secondMax - c + a, resulIfEndsWith[b] + a);
maxResult = max(maxResult, resulIfEndsWith[b]);
continue;
}
pending.clear();
pending.insert(b);
resulIfEndsWith[b] = max(maxResult - c + a, resulIfEndsWith[b] + a);
secondMax = maxResult;
maxResult = max(maxResult, resulIfEndsWith[b]);
lastTakenSize = a;
}
cout << maxResult;
}
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 | #include <iostream> #include <unordered_map> #include <unordered_set> using namespace std; int main() { std::ios_base::sync_with_stdio(false); int n = 0; int c = 0; int a = 0; int b = 0; cin >> n >> c; int64_t maxResult = 0; int64_t secondMax = 0; std::unordered_map<int, int64_t> resulIfEndsWith = {}; int lastTakenSize = 0; std::unordered_set<int> pending = {}; for (int i = 0; i < n; i++) { cin >> a >> b; if (lastTakenSize == a) { if (pending.find(b) != pending.end()) { continue; } pending.insert(b); resulIfEndsWith[b] = max(secondMax - c + a, resulIfEndsWith[b] + a); maxResult = max(maxResult, resulIfEndsWith[b]); continue; } pending.clear(); pending.insert(b); resulIfEndsWith[b] = max(maxResult - c + a, resulIfEndsWith[b] + a); secondMax = maxResult; maxResult = max(maxResult, resulIfEndsWith[b]); lastTakenSize = a; } cout << maxResult; } |
English