import java.util.*; public class Main { static House[] houses; static int[][] dp; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); houses = new House[n]; for (int i = 0; i < n; i++) { houses[i] = new House(sc.nextInt(), sc.nextInt()); } Arrays.sort(houses); dp = new int[n][10001]; for (int[] arr : dp) { Arrays.fill(arr, -1); } System.out.println(dfw(n - 1, 0)); } static int dfw(int idx, int value) { if (idx < 0) { return 0; } if (dp[idx][value] < 0) { dp[idx][value] = dfw(idx - 1, value); if (houses[idx].limit > value) { dp[idx][value] = Math.max(dp[idx][value], dfw(idx - 1, value + houses[idx].gain) + houses[idx].gain); } } return dp[idx][value]; } static class House implements Comparable { int gain; int limit; public House(int gain, int limit) { this.gain = gain; this.limit = limit; } public int compareTo(House another) { return another.limit + another.gain - limit - gain; } } }