N = int(input()) A = [0]+[list(map(int,input().split())) for _ in range(N)] Vmax = 0 for i in range(1,N+1): Vmax += A[i][0] V = int(input()) high = 10**5+1 low = 0 while high-low>1: mid = (high+low)//2 dp = [[0 for _ in range(mid+1)] for _ in range(N+1)] for j in range(A[1][1],mid+1): dp[1][j] = A[1][0] for i in range(2,N+1): for j in range(mid+1): dp[i][j] = dp[i-1][j] if j>=A[i][1]: dp[i][j] = max(dp[i][j],dp[i-1][j-A[i][1]]+A[i][0]) if dp[N][mid]>=V: high = mid else: low = mid V_low = high V_high = "inf" if V1: mid = (high+low)//2 dp = [[0 for _ in range(mid+1)] for _ in range(N+1)] for j in range(A[1][1],mid+1): dp[1][j] = A[1][0] for i in range(2,N+1): for j in range(mid+1): dp[i][j] = dp[i-1][j] if j>=A[i][1]: dp[i][j] = max(dp[i][j],dp[i-1][j-A[i][1]]+A[i][0]) if dp[N][mid]>V: high = mid else: low = mid V_high = low print(V_low) print(V_high)