def read_data(): N = int(input()) vs = [] ws = [] for _ in range(N): v, w = map(int, input().split()) vs.append(v) ws.append(w) V = int(input()) return N, vs, ws, V def solve(N, vs, ws, V): W = sum(ws) dp = [0] * (W + 1) for v, w in zip(vs, ws): for imw in range(W - w, 0, -1): if dp[imw]: newdp = dp[imw] + v if newdp > dp[imw + w]: dp[imw + w] = newdp dp[w] = max(dp[w], v) minw = 1 maxw = 'inf' for i in range(1, W + 1): if dp[i] == V: minw = i break for j in range(i + 1, W + 1): if dp[j] > V: maxw = j - 1 break print(minw) print(maxw) N, vs, ws, V = read_data() solve(N, vs, ws, V)