import java.util.*; public class Main { static int[][][] dp; 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()); } Arrays.sort(pizzas); dp = new int[n][k + 1][2]; 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 (dp[idx][cost][isFree] == 0) { if (isFree == 0) { dp[idx][cost][isFree] = Math.max(dfw(idx - 1, cost, isFree), dfw(idx - 1, cost - pizzas[idx].price, 1) + pizzas[idx].delicious); } else { dp[idx][cost][isFree] = Math.max(dfw(idx - 1, cost, isFree), dfw(idx - 1, cost, 0) + pizzas[idx].delicious); } } return dp[idx][cost][isFree]; } 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; } } }