Problem:
Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
  
using namespace std;
using namespace __gnu_pbds;
 
#define ll long long
#define uint unsigned int
#define ull  unsigned long long
#define pb push_back
#define mk make_pair
#define ins insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define X first
#define Y second
#define umap unordered_map
#define speed() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define setvalue(d,s,e,n) for(int qwe = s; qwe < e; ++qwe) d[qwe] = n
#define mset multiset
#define pqueue priority_queue
   
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
        
const int  N = 2e5 + 314;
const int INF = 1e9;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
const double eps = 1e-9;
ll dp[2050][2050];
ll c[2050];
ll w[2050];
bool cmp(pair <ll,ll> a, pair <ll,ll> b) {
    return a.Y >= b.Y;
}
ll sum1[N],sum2[N],sum3[N], sum4[N];
void solve(){
    ll n,s;
    cin >> n >> s; 
    for(int i = 1; i <= n; ++i)
        cin >> w[i] >> c[i];
    int mn = *min_element(w + 1, w + n + 1); 
    vector <pair <ll,ll > > v1, v2, v3,v4;
    for(int i = 1; i <= n; ++i) {
        if(w[i] == mn) {
            v1.pb({w[i],c[i]});
        }
        else if(w[i] == mn + 1) {
            v2.pb({w[i],c[i]});
        }
        else if(w[i] == mn+ 2){
            v3.pb({w[i],c[i]});
        }
        else {
            v4.pb({w[i], c[i]});
        }
    }
    sort(all(v1));
    sort(all(v2));
    sort(all(v3));
    sort(all(v3));
    for(int i = 0; i < v1.size(); ++i) {
        if(i)
            sum1[i] = sum1[i - 1] + v1[i].Y;
        else 
            sum1[i] = v1[i].Y;
    }
    for(int i = 0; i < v2.size(); ++i) {
        if(i)
            sum2[i] = sum2[i - 1] + v2[i].Y;
        else 
            sum2[i] = v2[i].Y;
    }
    
    for(int i = 0; i < v3.size(); ++i) {
        if(i)
            sum3[i] = sum3[i - 1] + v3[i].Y;
        else 
            sum3[i] = v3[i].Y;
    }
     
    for(int i = 0; i < v4.size(); ++i) {
        if(i)
            sum4[i] = sum4[i - 1] + v4[i].Y;
        else 
            sum4[i] = v4[i].Y;
    }
    
    
    ll mx = 0;
    for(ll i = 0; i <= v1.size(); ++i) {
        for(ll j = 0; j <= v2.size(); ++j) {
            for(ll k = 0; k <= v3.size(); ++k) {
                if(i * mn + j * (mn + 1) + k * (mn + 2) > s)
                    continue;
                ll tot = 0;
                if(i)
                    tot += sum1[i - 1];
                if(j)
                    tot += sum2[j - 1];
                if(k)
                    tot += sum3[k - 1]; 
                ll m = s - (i * mn + j * (mn + 1) + k * (mn + 2));
                if(m)
                    tot += sum4[m - 1];
                mx = max(mx, tot);
            }
        }
    }
    cout << mx;
}
int main(){
    speed();
    int t = 1;
    // cin >> t;
    while(t--)solve();
}