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 dp = [[0 for _ in range(10**5+1)] for _ in range(2)] while high-low>1: mid = (high+low)//2 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%2][j] = dp[(i-1)%2][j] if j>=A[i][1]: dp[i%2][j] = max(dp[i%2][j],dp[(i-1)%2][j-A[i][1]]+A[i][0]) if dp[N%2][mid]>=V: high = mid else: low = mid V_low = high V_high = "inf" if V1: mid = (high+low)//2 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%2][j] = dp[(i-1)%2][j] if j>=A[i][1]: dp[i%2][j] = max(dp[i%2][j],dp[(i-1)%2][j-A[i][1]]+A[i][0]) if dp[N%2][mid]>V: high = mid else: low = mid V_high = low print(V_low) print(V_high)