import java.util.*; public class Main { static ArrayList> paid = new ArrayList<>(); static ArrayList> free = 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()); paid.add(new HashMap<>()); free.add(new HashMap<>()); } Arrays.sort(pizzas); System.out.println(dfw(n - 1, k, true)); } static class Pizza implements Comparable { int price; int value; public Pizza(int price, int value) { this.price = price; this.value = value; } public int compareTo(Pizza another) { return price - another.price; } } static int dfw(int idx, int cost, boolean isPaid) { if (cost < 0) { return Integer.MIN_VALUE; } if (idx < 0) { return 0; } if (isPaid) { if (paid.get(idx).containsKey(cost)) { return paid.get(idx).get(cost); } int ans = Math.max(dfw(idx - 1, cost, true), dfw(idx - 1, cost - pizzas[idx].price, false) + pizzas[idx].value); paid.get(idx).put(cost, ans); return ans; } else { if (free.get(idx).containsKey(cost)) { return free.get(idx).get(cost); } int ans = Math.max(dfw(idx - 1, cost, true) + pizzas[idx].value, dfw(idx - 1, cost, false)); free.get(idx).put(cost, ans); return ans; } } }