#include <iostream> #include <vector> #include <map> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, c; cin >> n >> c; vector<pair<int, int>> blocks(n); for (int i = 0; i < n; i++) { cin >> blocks[i].first >> blocks[i].second; } sort(blocks.rbegin(), blocks.rend()); map<int, long long> maxikolor; long long globalimax = -1e18; int globmaxikol = -1; long long maxi2 = -1e18; int maxikol2 = -1; long long best = 0; for (int i = 0; i < n;) { int curra = blocks[i].first; vector<pair<int, long long>> wyniki; map<int, long long> kolorwynik; while (i < n and blocks[i].first == curra) { int a = blocks[i].first; int w = blocks[i].second; long long max_same = -1e18; if (maxikolor.find(w) != maxikolor.end()) { max_same = maxikolor[w]; } long long max_diff = -1e18; if (globmaxikol != w) { if (globalimax != -1e18) { max_diff = globalimax - c; } } else { if (maxi2 != -1e18) { max_diff = maxi2 - c; } } long long score = a + max(max_same, max(max_diff, 0LL)); wyniki.push_back({w, score}); if (score > kolorwynik[w]) { kolorwynik[w] = score; } i++; } for (const auto& [color, score] : kolorwynik) { if (score > maxikolor[color]) { maxikolor[color] = score; if (score > globalimax) { maxi2 = globalimax; maxikol2 = globmaxikol; globalimax = score; globmaxikol = color; } else if (score > maxi2) { maxi2 = score; maxikol2 = color; } } } for (const auto& [w, curr] : wyniki) { if (curr > best) { best = curr; } } } if(best!=-1e18){ cout<<best; } else{ cout<<0; } 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> #include <map> #include <algorithm> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, c; cin >> n >> c; vector<pair<int, int>> blocks(n); for (int i = 0; i < n; i++) { cin >> blocks[i].first >> blocks[i].second; } sort(blocks.rbegin(), blocks.rend()); map<int, long long> maxikolor; long long globalimax = -1e18; int globmaxikol = -1; long long maxi2 = -1e18; int maxikol2 = -1; long long best = 0; for (int i = 0; i < n;) { int curra = blocks[i].first; vector<pair<int, long long>> wyniki; map<int, long long> kolorwynik; while (i < n and blocks[i].first == curra) { int a = blocks[i].first; int w = blocks[i].second; long long max_same = -1e18; if (maxikolor.find(w) != maxikolor.end()) { max_same = maxikolor[w]; } long long max_diff = -1e18; if (globmaxikol != w) { if (globalimax != -1e18) { max_diff = globalimax - c; } } else { if (maxi2 != -1e18) { max_diff = maxi2 - c; } } long long score = a + max(max_same, max(max_diff, 0LL)); wyniki.push_back({w, score}); if (score > kolorwynik[w]) { kolorwynik[w] = score; } i++; } for (const auto& [color, score] : kolorwynik) { if (score > maxikolor[color]) { maxikolor[color] = score; if (score > globalimax) { maxi2 = globalimax; maxikol2 = globmaxikol; globalimax = score; globmaxikol = color; } else if (score > maxi2) { maxi2 = score; maxikol2 = color; } } } for (const auto& [w, curr] : wyniki) { if (curr > best) { best = curr; } } } if(best!=-1e18){ cout<<best; } else{ cout<<0; } return 0; } |