use std::{convert::TryFrom, io::stdin, iter::repeat_with, usize::MAX}; fn main() { let stdin = stdin(); let mut stdin = stdin.lines().map(Result::unwrap); let n = stdin.next().unwrap().parse::().unwrap(); let ab = repeat_with(|| { <[_; 2]>::try_from( stdin .next() .unwrap() .split_whitespace() .map(|x| x.parse::().unwrap()) .collect::>(), ) .unwrap() }) .take(n) .collect::>(); let lim = ab.iter().map(|&[x, _]| x).sum::() + 1; let mut dp = vec![MAX; lim]; dp[0] = 0; for &[x, y] in &ab { let mut swp = dp.iter().map(|&x| x.saturating_add(y)).collect::>(); for i in 0..lim - x { swp[i + x] = swp[i + x].min(dp[i]); } dp = swp; } let ans = (0..lim).map(|i| i.max(dp[i])).min().unwrap(); println!("{}", ans); }