#[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((c, x - y)); } } else { base += x; if c != 0 { vr.push((n - c, y - x)); } } } vl.sort_unstable(); vr.sort_unstable(); let mut res = 0; for mut v in [vl, vr] { let mut sum = 0; let mut pq = BinaryHeap::new(); for d in (0..v.len()).rev() { while v.len() > 0 && v.last().unwrap().0 >= d { if v.last().unwrap().0 == d { pq.push(v.pop().unwrap().1); } else { break; } } sum += pq.pop().unwrap_or_default(); } res += sum; } println!("{}", base + res); }