#include #include #include using namespace std; struct item{ long value, weight; }; int n; long W; vector items; long dp[5000][5000]; int mod_number_n[5001]; long mod_number_weight[5001]; long pack(int now, long weight_now){ if(now == n){ return 0; }else if(dp[now / mod_number_weight[n]][weight_now / ((W + mod_number_weight[n] - 1) / mod_number_weight[n] + 1)] != -1){ return dp[now / mod_number_weight[n]][weight_now / ((W + mod_number_weight[n] - 1) / mod_number_weight[n] + 1)]; }else{ if(weight_now + items[now].weight > W){ return dp[now / mod_number_weight[n]][weight_now / ((W + mod_number_weight[n] - 1) / mod_number_weight[n] + 1)] = pack(now + 1, weight_now); }else{ return dp[now / mod_number_weight[n]][weight_now / ((W + mod_number_weight[n] - 1) / mod_number_weight[n] + 1)] = max(pack(now + 1, weight_now), items[now].value + pack(now + 1, weight_now + items[now].weight)); } } } int main(){ cin >> n >> W; items.resize(n); for(int i = 0; i < n; i++){ cin >> items[i].value >> items[i].weight; } for(int i = 0; i < 5000; i++){ for(int j = 0; j < 5000; j++){ dp[i][j] = -1; } } for(int i = 0; i <= 5000; i++){ mod_number_weight[i] = 3000; } mod_number_weight[500] = 2000; mod_number_weight[2000] = 4000; mod_number_weight[5000] = 3500; for(int i = 0; i <= 5000; i++){ mod_number_n[i] = 1; } mod_number_n[5000] = 2; cout << pack(0, 0) << endl; }