def knapsack(items, budget)
    table = (0..1).map{ Array.new(budget+1, 0) }
    for i in 0..items.size-1
        c, v = items[i]
        for j in 0..budget
            x = table[0][j]
            y = (j >= c ? table[0][j-c] + v : 0)
            table[1][j] = x > y ? x : y 
        end
        table << table.shift
    end 
    table[0][budget]
end

loop do
    n = gets.to_i
    break if n == 0
    read_time, write_time = (1..n).map{gets.split.map(&:to_i)}.sort.transpose
    if n == 1
        p read_time[0] * 2 + write_time[0]
    elsif read_time[-1] * 2 > read_time.inject(:+)  
        t1 = read_time.pop
        t2 = read_time.inject(:+)
        space = t1 - t2
        t3 = knapsack(write_time[0..-2].map{|x| [x, x]}, space)
        p 2 * t1 + write_time.inject(:+) - t3
    else
        p read_time.inject(:+) + write_time.inject(:+)
    end 
end