import sys

def main():
    n = int(sys.stdin.readline())
    tasks = []
    totalA = 0
    totalB = 0
    for _ in range(n):
        a, b = map(int, sys.stdin.readline().split())
        tasks.append((a, b))
        totalA += a
        totalB += b
    
    low = 0
    high = max(totalA, totalB)
    answer = high
    
    def can_achieve(T):
        # DP[a] represents the maximum sum of B_i for the chosen tasks where Yukio's total time is exactly 'a'
        dp = [-1] * (T + 1)
        dp[0] = 0  # Initial state: choosing no tasks
        
        for a_i, b_i in tasks:
            new_dp = [-1] * (T + 1)
            for a_prev in range(T + 1):
                if dp[a_prev] == -1:
                    continue
                # Option 1: Do not take the current task
                if new_dp[a_prev] < dp[a_prev]:
                    new_dp[a_prev] = dp[a_prev]
                # Option 2: Take the current task with Yukio
                a_new = a_prev + a_i
                if a_new > T:
                    continue
                b_new = dp[a_prev] + b_i
                if new_dp[a_new] < b_new:
                    new_dp[a_new] = b_new
            dp = new_dp
        
        required_b = totalB - T
        for a in range(T + 1):
            if dp[a] >= required_b and a <= T:
                return True
        return False
    
    while low <= high:
        mid = (low + high) // 2
        if can_achieve(mid):
            answer = mid
            high = mid - 1
        else:
            low = mid + 1
    
    print(answer)

if __name__ == "__main__":
    main()