use std::collections::BinaryHeap; use proconio::input; fn main() { input! { n: usize, cxy: [(usize, i64, i64); n], } let mut score = 0_i64; let mut heap: BinaryHeap = cxy.iter().map(|&(c, x, y)| Node::new(c, x, y)).collect(); let mut cnt = 0_usize; while let Some(node) = heap.pop() { score += if cnt >= node.c { node.x } else { node.y }; cnt += 1; } println!("{}", score); } #[derive(Debug, Clone, Copy)] struct Node { c: usize, x: i64, y: i64, diff: i64, } impl PartialEq for Node { fn eq(&self, other: &Self) -> bool { self.diff == other.diff } } impl Eq for Node {} impl PartialOrd for Node { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(&other)) } } impl Ord for Node { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.diff.cmp(&other.diff) } } impl Node { fn new(c: usize, x: i64, y: i64) -> Self { Self { c, x, y, diff: y - x, } } }