#include #include #include #include #include #include int main() { std::cin.tie(nullptr)->sync_with_stdio(false); int N; std::cin >> N; long long ans{}; std::vector> add(N + 1), erase(N + 1); std::priority_queue> que; for (int i{} ; i < N ; i++) { int C, X, Y; std::cin >> C >> X >> Y; ans += std::min(X, Y); if (X <= Y) { erase[C].push_back(i); que.push({ Y - X, i }); } else { add[C].push_back(X - Y); } } std::vector erased(N); for (auto e : erase[0]) erased[e] = true; for (auto a : add[0]) que.push({ a, -1 }); for (int i{1} ; i <= N ; i++) { while (que.size()) { auto [v, j]{que.top()}; que.pop(); if (j == -1 or !erased[j]) { ans += v; break; } } for (auto e : erase[i]) erased[e] = true; for (auto a : add[i]) que.push({ a, -1 }); } std::cout << ans << '\n'; }