#[allow(unused_imports)] use proconio::{ input, marker::{Bytes, Chars, Usize1}, }; use std::collections::BinaryHeap; fn main() { input! { n: usize, } let mut base = 0; let mut vl = vec![]; let mut vr = vec![]; for _ in 0..n { input! { c: usize, x: usize, y: usize, } if x >= y { base += y; if c < n { vr.push((n - c, x - y)); } } else { base += x; if c > 0 { vl.push((c, y - x)); } } } vl.sort_unstable(); vr.sort_unstable(); // vl.reverse(); // vr.reverse(); let mut res = 0; for mut v in [vl, vr] { let mut sum = 0; let mut pq = BinaryHeap::new(); for d in (1..=v.len()).rev() { while v.len() > 0 && v.last().unwrap().0 >= d { pq.push(v.pop().unwrap().1); } sum += pq.pop().unwrap_or_default(); } res += sum; } println!("{}", base + res); }