#include using namespace std; typedef long long ll; #define REP(i, x, y) for (auto i = (x); i < (y); i++) #define RREP(i, x, y) for (auto i = (y) - 1; (x) <= i; i--) #define ALL(x) (x).begin(), (x).end() #pragma GCC optimize("O3") #define size(A) ((A).size()) int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T=1; while(T--){ int N; cin >> N; vector K(N), L(N), R(N); REP(i, 0, N){ cin >> K[i] >> R[i] >> L[i]; K[i]=max(K[i],0); } vector> A, B; ll ans = 0; REP(i, 0, N){ if(L[i] > R[i]){ A.push_back({K[i], L[i] - R[i]}); ans += R[i]; }else{ B.push_back({N - K[i], R[i] - L[i]}); ans += L[i]; } } sort(ALL(A)); multiset ms; REP(i, 0, size(A)){ ms.insert(A[i][1]); if(size(ms) > A[i][0]){ ms.erase(ms.begin()); } } ans += accumulate(ALL(ms), 0LL); ms.clear(); sort(ALL(B)); REP(i, 0, size(B)){ ms.insert(B[i][1]); if(size(ms) > B[i][0]){ ms.erase(ms.begin()); } } ans += accumulate(ALL(ms), 0LL); cout << ans << "\n"; } return 0; }