/** * author: zjs * created: 24.06.2026 22:46:15 **/ #include #include // does not include cassert since GCC 16. using namespace std; #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #endif struct Card { int c, x, y; }; bool cmp(Card a, Card b) { return a.c < b.c; } int main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; vector a, b; int cnt = 0; long long ans = 0; for (int i = 0; i < N; i++) { int c, x, y; cin >> c >> x >> y; cnt += x <= y; if (x < y) a.push_back({c, x, y}); else if (x > y) b.push_back({c, x, y}); else ans += x; } sort(a.begin(), a.end(), cmp); priority_queue, greater> gain; for (auto [c, x, y] : a) { ans += x; int delta = y - x; if ((int) gain.size() < c) { gain.push(delta); } else if (gain.size() && gain.top() < delta) { gain.pop(); gain.push(delta); } } while (gain.size()) { ans += gain.top(); gain.pop(); } sort(b.begin(), b.end(), cmp); priority_queue loss; for (auto [c, x, y] : b) { ans += x; int delta = x - y; if (cnt < c) { loss.push(delta); } else if (loss.size() && loss.top() > delta) { loss.pop(); loss.push(delta); } cnt++; } while (loss.size()) { ans -= loss.top(); loss.pop(); } cout << ans << '\n'; }