#include #define rep(i, n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; using P = pair; ll f(vector

p) { int n = p.size(); rep(i, n) p[i].first = min(p[i].first, n); vector> s(n+1); rep(i, n) s[p[i].first].push_back(p[i].second); ll res = 0; priority_queue q; for (int i = n; i >= 1; --i) { for (int x : s[i]) q.push(x); if (q.size()) { res += q.top(); q.pop(); } } return res; } int main() { int n; cin >> n; ll ans = 0; vector

pl, pr; rep(i, n) { int k, l, r; cin >> k >> l >> r; int m = min(l, r); ans += m; l -= m; r -= m; if (r > 0) pr.emplace_back(k, r); else pl.emplace_back(n-k, l); } ans += f(pl); ans += f(pr); cout << ans << '\n'; return 0; }