import java.util.*; public class Main { static ArrayList> dpNone = new ArrayList<>(); static ArrayList> dpBonus = new ArrayList<>(); static Pizza[] pizzas; public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); pizzas = new Pizza[n]; for (int i = 0; i < n; i++) { pizzas[i] = new Pizza(sc.nextInt(), sc.nextInt()); dpNone.add(new HashMap<>()); dpBonus.add(new HashMap<>()); } Arrays.sort(pizzas); System.out.println(dfw(n - 1, k, false)); } static int dfw(int idx, int money, boolean hasBonus) { if (money < 0) { return Integer.MIN_VALUE / 10; } if (idx < 0) { return 0; } ArrayList> dp; if (hasBonus) { dp = dpBonus; } else { dp = dpNone; } if (dp.get(idx).containsKey(money)) { return dp.get(idx).get(money); } int max = 0; max = Math.max(max, dfw(idx - 1, money, hasBonus)); if (hasBonus) { max = Math.max(max, dfw(idx - 1, money, false) + pizzas[idx].value); } else { max = Math.max(max, dfw(idx - 1, money - pizzas[idx].cost, true) + pizzas[idx].value); } dp.get(idx).put(money, max); return max; } static class Pizza implements Comparable { int cost; int value; public Pizza(int cost, int value) { this.cost = cost; this.value = value; } public int compareTo(Pizza another) { if (cost == another.cost) { return value - another.value; } else { return cost - another.cost; } } } }