N = int(input()) A = [0]+[list(map(int,input().split())) for _ in range(N)] Vmax = sum(A[i][0] for i in range(1,N+1)) 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 i in range(1,N+1): for j in range(mid,A[i][1]-1,-1): dp[j] = max(dp[j],dp[j-A[i][1]]+A[i][0]) if dp[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 i in range(1,N+1): for j in range(mid,A[i][1]-1,-1): dp[j] = max(dp[j],dp[j-A[i][1]]+A[i][0]) if dp[mid]>V: high = mid else: low = mid V_high = low print(V_low) print(V_high)