n = int(input()) W = 10 ** 5 dp = [0] * (W + 1) for _ in range(n): v, w = map(int, input().split()) for i in range(W, w - 1, -1): dp[i] = max(dp[i], dp[i - w] + v) min_ = -1 max_ = -1 V = int(input()) for i in range(W + 1): if dp[i] == V: if min_ == -1: min_ = i max_ = i print(min_) print(max_)