import std.stdio, std.array, std.conv, std.typecons, std.algorithm;

long impl(long n, long w, long[] ws, long[] vs, long[][] dp) {
    if(dp[n][w] == -1) {
        auto yes = {
            auto prev_w = w-ws[n-1];
            if (prev_w < 0) {
                return -1;
            }
            return impl(n-1, prev_w, ws, vs, dp);
        }();
        auto no = impl(n-1, w, ws, vs, dp);
        dp[n][w] = max(yes, no, dp[n][w]);
    }
    return dp[n][w];
}

long solve(long n, long w, long[] ws, long[] vs) {
    auto dp = new long[][](n+1, w+1);
    foreach(ref v ; dp[]) { v[] = -1;}
    dp[0][] = 0;
    return impl(n, w, ws, vs, dp);
}

void main() {
    auto i1 = readln.split.to!(long[]);
    immutable auto n = i1[0], w = i1[1];
    auto ws = new long[n];
    auto vs = new long[n];

    for(auto i = 0; i < n; i++) {
        auto input = readln.split.to!(long[]);
        ws[i] = input[0];
        vs[i] = input[1];
    }

    writeln(solve(n, w, ws, vs));
}
