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