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

int main() {
    int N, W; cin >> N >> W;
    vector<pair<ll,ll>> items(N+1);
    for (int i = 1; i <= N; i++) {
        ll wi, vi; cin >> wi >> vi;
        items[i] = {wi, vi};
    }

    vector<vector<ll>> dp(N+1, vector<ll>(W+1, 0));
    for (int i = 1; i <= N; i++) {
        auto [w,v] = items[i];
        for (int j = 0; j <= W; j++) {
            dp[i][j] = dp[i-1][j];
            if (j >= w) dp[i][j] = max(dp[i][j], dp[i-1][j-w] + v);
        }
    }

    cout << dp[N][W] << endl;
    return 0;
}