/** * 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; long long ans = 0; for (int i = 0; i < N; i++) { int c, x, y; cin >> c >> 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); for (auto [c, x, y] : views::reverse(b)) { ans += y; int delta = x - y; if (N - (int) gain.size() - 1 >= c) { gain.push(delta); } else if (gain.size() && gain.top() < delta) { gain.pop(); gain.push(delta); } } while (gain.size()) { ans += gain.top(); gain.pop(); } cout << ans << '\n'; }