N = int(raw_input()) tuples = [] for _ in xrange(N): v, t = map(int, raw_input().split()) tuples.append(t * 20000 + v) dp = [-1] * 10001 dp[0] = 0 tuples.sort() for tuple_value in tuples: V = tuple_value % 20000 T = tuple_value // 20000 for n in xrange(T - 1, -1, -1): if dp[n] == -1: continue v = min(n + V, 10000) dp[v] = max(dp[v], dp[n] + V); print max(dp)