from cmath import inf N = int(input()) dp = [-inf] * (1000 * N + 1) dp[0] = 0 for _ in range(N): v, w = map(int, input().split()) for i in range(1000 * N, w - 1, -1): dp[i] = max(dp[i], dp[i - w] + v) for i in range(1, 1000 * N + 1): dp[i] = max(dp[i], dp[i - 1]) V = int(input()) mi, mx = inf, -1 for i in range(1, 1000 * N + 1): if dp[i] == V: mi = min(mi, i) mx = max(mx, i) print(mi) print(mx if mx != 1000 * N else inf)