#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 111;
int n, W;
int w[maxn], v[maxn];

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);

  cin>>n>>W;
  for (int i=0; i<n; i++) {
    cin>>w[i]>>v[i];
  }

  map<int,int> mp;
  mp[0] = 0;
  
  for (int i=0; i<n; i++) {
    map<int,int> _mp = mp;
    for (auto p: mp) {
      if (p.first + w[i] > W) continue;
      _mp[p.first+w[i]] = max(_mp[p.first+w[i]], p.second+v[i]);
    }
    mp = _mp;
  }

  int val=0;
  for (auto p: mp) {
    if (p.first <= W) {
      val = max(val, p.second);
    }
  }

  cout<<val<<endl;
  return 0;
}
