use std::cmp::Reverse; const INF: usize = 1usize << 60; fn main() { let mut n = String::new(); std::io::stdin().read_line(&mut n).ok(); let n: usize = n.trim().parse().unwrap(); let mut items = (0..n).map(|_| { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); (temp[0], temp[1]) }) .collect::>(); items.sort_by_key(|&(_, b)| Reverse(b)); let limit = n / 3; let mut dp = vec![vec![INF; limit+1]; n+1]; dp[0][0] = 0; for i in 0..n { let (a, b) = items[i]; for j in 0..=limit { if dp[i][j] == INF { continue; } dp[i+1][j] = dp[i+1][j].min(dp[i][j] + a + (i - j) * b); if j+1 <= limit { dp[i+1][j+1] = dp[i+1][j+1].min(dp[i][j]); } } } println!("{}", dp[n][limit]); }