stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

template<class T, class U> inline bool chmax(T& a,U b) { if (a < b) { a = b; return 1; } return 0; }

template<class T, class U> inline bool chmin(T& a,U b) { if (a > b) { a = b; return 1; } return 0; }

const int MAX_N = 110;

const int MAX_W = 100100;



int main() {

    int N, W;

    cin >> N >> W;

    long long dp[MAX_W] = {0};

    vector<int> weight(N), value(N);

    for (int i = 0; i < N; i++) { cin >> weight[i] >> value[i]; }

    

    for (int i = 0; i < N; i++) {

        for (int w = MAX_W; w >= 0; w--) {

            if (0 <= w-weight[i]) chmax(dp[w], dp[w-weight[i]] + value[i]);

        }

    }

    

    cout << dp[W] << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.3179588138156764x):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef vector<string> vs;

ll llmax = LLONG_MAX;

string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

string alp = "abcdefghijklmnopqrstuvwxyz";

vi d1 = {1, 0, -1, 0}, d2 = {0, 1, 0, -1};

void Yes() {cout << "Yes" << endl;}

void No() {cout << "No" << endl;}

void YES() {cout << "YES" << endl;}

void NO() {cout << "NO" << endl;}

void neg() {cout << -1 << endl;}



int main() {

  ll n, ww;

  cin >> n >> ww;

  vll w(n), v(n);

  for (ll i = 0; i < n; i++) cin >> w[i] >> v[i];

  vll dp(100001, 0);

  vector<vll> check(1, vll(n, 0));

  for (ll i = 1; i <= ww; i++) {

    dp[i] = dp[i - 1];

    ll memo = -1;

    for (ll j = 0; j < n; j++) {

      if (i < w[j]) continue;

      if (check[i - w[j]][j] == 1) continue;

      if (dp[i] < dp[i - w[j]] + v[j]) {

        dp[i] = dp[i - w[j]] + v[j];

        memo = j;

      }

    }

    if (memo != -1) {

      vll x = check[i - w[memo]];

      x[memo] = 1;

      check.emplace_back(x);

    }

    else check.emplace_back(check[i - 1]);

  }

  cout << dp[ww] << endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

template<class T, class U> inline bool chmax(T& a,U b) { if (a < b) { a = b; return 1; } return 0; }

template<class T, class U> inline bool chmin(T& a,U b) { if (a > b) { a = b; return 1; } return 0; }

const int MAX_N = 110;

const int MAX_W = 100100;



int main() {

    int N, W;

    cin >> N >> W;

    long long dp[MAX_W] = {0};

    vector<int> weight(N), value(N);

    for (int i = 0; i < N; i++) { cin >> weight[i] >> value[i]; }

    

    for (int i = 0; i < N; i++) {

        for (int w = MAX_W; w >= 0; w--) {

            if (0 <= w-weight[i]) chmax(dp[w], dp[w-weight[i]] + value[i]);

        }

    }

    

    cout << dp[W] << endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.3608629071337073x):
#include<bits/stdc++.h>

using namespace std;



#define rep(i,n) for(int i = 0; i < n; i++)

#define REP(i,n) for(int i = 1; i < n; i++)

#define sz(x) int(x.size())

#define all(v) v.begin(), v.end()

#define rall(v) v.rbegin(), v.rend()

#define YesorNo(a) printf(a ? "Yes\n" : "No\n")

#define endl '\n'

using ll = long long;

using P = pair<int,int>;

using Pl = pair<ll,ll>;

using vi = vector<int>;

using vii = vector<vi>;

using vl = vector<ll>;

using vll = vector<vl>;

using vs = vector<string>;

using vb = vector<bool>;

const int dx[] = {0,1,0,-1,1,1,-1,-1};

const int dy[] = {1,0,-1,0,1,-1,-1,1};

const int inf = (1<<30)-1;

const ll infll = (1LL<<62)-1;

ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



ll dp[110][100100] = {};



int main(){

  ios::sync_with_stdio(false);

  cin.tie(nullptr);



  ll n, mx_w;

  cin >> n >> mx_w;

  vl w(n), v(n);

  rep(i,n) cin >> w[i] >> v[i];



  rep(i,n) {

    rep(j,mx_w+1) {

      if(j >= w[i]) chmax(dp[i+1][j], dp[i][j-w[i]] + v[i]);

      chmax(dp[i+1][j], dp[i][j]);

    }

  }

  cout << dp[n][mx_w] << endl;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef vector<int> vi;

typedef vector<ll> vll;

typedef vector<string> vs;

ll llmax = LLONG_MAX;

string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

string alp = "abcdefghijklmnopqrstuvwxyz";

vi d1 = {1, 0, -1, 0}, d2 = {0, 1, 0, -1};

void Yes() {cout << "Yes" << endl;}

void No() {cout << "No" << endl;}

void YES() {cout << "YES" << endl;}

void NO() {cout << "NO" << endl;}

void neg() {cout << -1 << endl;}



int main() {

  ll n, ww;

  cin >> n >> ww;

  vll w(n), v(n);

  for (ll i = 0; i < n; i++) cin >> w[i] >> v[i];

  vll dp(100001, 0);

  vector<vll> check(1, vll(n, 0));

  for (ll i = 1; i <= ww; i++) {

    dp[i] = dp[i - 1];

    ll memo = -1;

    for (ll j = 0; j < n; j++) {

      if (i < w[j]) continue;

      if (check[i - w[j]][j] == 1) continue;

      if (dp[i] < dp[i - w[j]] + v[j]) {

        dp[i] = dp[i - w[j]] + v[j];

        memo = j;

      }

    }

    if (memo != -1) {

      vll x = check[i - w[memo]];

      x[memo] = 1;

      check.emplace_back(x);

    }

    else check.emplace_back(check[i - 1]);

  }

  cout << dp[ww] << endl;

}


// Faster C++ solution for the same problem (measured speedup: 1.7913024916906748x):
#include<bits/stdc++.h>

using namespace std;



#define rep(i,n) for(int i = 0; i < n; i++)

#define REP(i,n) for(int i = 1; i < n; i++)

#define sz(x) int(x.size())

#define all(v) v.begin(), v.end()

#define rall(v) v.rbegin(), v.rend()

#define YesorNo(a) printf(a ? "Yes\n" : "No\n")

#define endl '\n'

using ll = long long;

using P = pair<int,int>;

using Pl = pair<ll,ll>;

using vi = vector<int>;

using vii = vector<vi>;

using vl = vector<ll>;

using vll = vector<vl>;

using vs = vector<string>;

using vb = vector<bool>;

const int dx[] = {0,1,0,-1,1,1,-1,-1};

const int dy[] = {1,0,-1,0,1,-1,-1,1};

const int inf = (1<<30)-1;

const ll infll = (1LL<<62)-1;

ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



ll dp[110][100100] = {};



int main(){

  ios::sync_with_stdio(false);

  cin.tie(nullptr);



  ll n, mx_w;

  cin >> n >> mx_w;

  vl w(n), v(n);

  rep(i,n) cin >> w[i] >> v[i];



  rep(i,n) {

    rep(j,mx_w+1) {

      if(j >= w[i]) chmax(dp[i+1][j], dp[i][j-w[i]] + v[i]);

      chmax(dp[i+1][j], dp[i][j]);

    }

  }

  cout << dp[n][mx_w] << endl;

}
// Slower C++ solution:




#include<bits/stdc++.h>







#define loop(i,a,b) for(int i=a;i<b;i++) 

#define rep(i,a) loop(i,0,a)



using namespace std;



template <class T>

using vec = std::vector<T>;

typedef long long ll;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



const long long INF = 1LL<<60;



int lcm(int a,int b){

    //最小公倍数

    return a/__gcd(a,b) * b;

}











int main()

{

    int n,W;cin>>n>>W;

    vec<int> weight(n+10);

    vec<int> val(n+10);

    rep(i,n){

        cin>>weight[i]>>val[i];

    }

    vec<vec<ll>> dp(n+100,vec<ll> (W+100,0)); //dp[(商品id)][(総重量w以下)]



    rep(i,n){

        rep(w,W+1){

            if(w<weight[i]) dp[i+1][w]=dp[i][w];

            else dp[i+1][w]=max(dp[i][w],dp[i][w-weight[i]]+val[i]);

            

        }

    }



    cout<<dp[n][W]<<endl;



    return 0;



}






// Faster C++ solution for the same problem (measured speedup: 1.1860287520177195x):




#include<bits/stdc++.h>







#define loop(i,a,b) for(int i=a;i<b;i++) 

#define rep(i,a) loop(i,0,a)



using namespace std;



template <class T>

using vec = std::vector<T>;

typedef long long ll;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



const long long INF = 1LL<<60;



int lcm(int a,int b){

    //最小公倍数

    return a/__gcd(a,b) * b;

}





// int型の2次元配列(3×4要素の)の宣言

// vector<vector<int>> data(3, vector<int>(4));









int main()

{

    int n,W;cin>>n>>W;

    vec<int> weight(n+10,0),val(n+10,0);

    rep(i,n) cin>>weight[i]>>val[i];

    vec<ll> dp(W+10,0);



    rep(i,n){

        for(int w=W;w>=0;w--){

            if(w>=weight[i]) dp[w]=max(dp[w],dp[w-weight[i]]+val[i]);

        }

    }



    cout<<dp[W]<<endl;

    return 0;



}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;



const int N = 1e2 + 5;

const int W = 1e5 + 5;



long long dp [W];



int main () {

  int n, w;

  cin >> n >> w;

  for (int i = 0; i < n; i++) {

    int tempo, lucro;

    cin >> tempo >> lucro;

    vector<long long> cache (W);

    for (int j = tempo; j <= w; j++) {

      cache[j] = dp[j - tempo] + lucro;

    }

    for (int j = tempo; j <= w; j++) {

      dp[j] = max (dp[j], cache[j]);

    }

  }

  cout << dp[w] << '\n';

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 2.673127684502553x):
#include <bits/stdc++.h>

using namespace std;



const int N = 1e2 + 5;

const int W = 1e5 + 5;



long long dp [W];

long long cache [W];



int main () {

  ios::sync_with_stdio(0);

  cin.tie(0);

  int n, w;

  cin >> n >> w;

  for (int i = 0; i < n; i++) {

    int tempo, lucro;

    cin >> tempo >> lucro;

    for (int j = tempo; j <= w; j++) {

      cache[j] = dp[j - tempo] + lucro;

    }

    for (int j = tempo; j <= w; j++) {

      dp[j] = max (dp[j], cache[j]);

    }

  }

  cout << dp[w] << '\n';

  return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std ;



#define int long long

const int N = 1e5 + 10 ;

int tp = N ;

int dp[N] ;

int32_t main(){

	int n, val; 

	cin >> n >> val ;

	for(int i = 0 ; i < n ; i++){

		int w , v ; 

		cin >> w >> v ;

		for(int j = tp - w - 1 ; j >= 0 ; j--){

			dp[j+w] = max(dp[j+w] , dp[j] + v) ;

		}

	}

	cout << dp[val] ;

	return 0 ;

}




// Faster C++ solution for the same problem (measured speedup: 1.7780938184771042x):
#include <bits/stdc++.h>

using namespace std ;



#define int long long



int32_t main(){

	int n , w ;

	cin >> n >> w ;

	int dp[w+1] = {} ;

	for(int i = 0 ; i < n ; i++){

		int x , y ; 

		cin >> x >> y ;

		for(int j = w - x ; j >= 0 ; j--){

			dp[j+x] = max(dp[j+x] , dp[j] + y) ;

		}

	}

	cout << dp[w] ;

	return 0 ;

}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <vector>



using namespace std;



struct item {

    int w, v;

};



void knapsack1(){

    vector<item> items;

    int n, w;

    cin >> n >> w;

    long maxVal[n + 1][w + 1];

    items.push_back({0,0});

    for (int i = 0; i < n; i++) {

        int wt, vl;

        cin >> wt >> vl;

        items.push_back({wt, vl});

    }

    for (int i = 0; i <= w; i++){

        maxVal[0][i] = 0;

    }

    for (int i = 0; i <= n; i++){

        maxVal[i][0] = 0;

    }

    for (int i = 1; i <= n; i++){

        for (int j = 1; j <= w; j++) {

            if (j < items[i].w) {

                maxVal[i][j] = maxVal[i - 1][j];

            }

            else

                maxVal[i][j] = max(maxVal[i - 1][j], maxVal[i - 1][j - items[i].w] + items[i].v);

        }

    }

    cout << maxVal[n][w];

}



int main() {

    knapsack1();

}






// Faster C++ solution for the same problem (measured speedup: 5.257027335315924x):
#include<cstdio>

#include<algorithm>

using namespace std;

long long opt[106][100005];

int main()

{

    int v[106],w[105];

    int i,j,n,m;

    scanf("%d %d",&n,&m);

    for(i = 1; i <= n; i++)

        scanf("%d %d",&w[i],&v[i]);

    for(i = 0; i <= n; i++)

        opt[0][i] = 0;

    for(i = 1; i <= n; i++)

    {

        for(j = 0; j <= m; j++)

        {

            //printf("%d*",j - w[i]);

            if(j - w[i] >= 0)

                opt[i][j] = max(opt[i - 1][j],opt[i - 1][j - w[i]] + v[i]);

            else opt[i][j] = opt[i - 1][j];

            //printf("%d ",opt[i][j]);

        }

        //printf("\n");

    }

    printf("%lld\n",opt[n][m]);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#include <limits.h>

#include <algorithm>  

#include <numeric>

 

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

using namespace std;

 

#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define endl '\n'

using ll = long long;

 

const int precision = 3;

const int modulo =  1000000007; // 10^9 + 7

const ll lmodulo =  1000000007; // 10^9 + 7

 

const double EPS = 1e-9;

const ll inf = 1e18;



// void solve () {

// 	int n, W; cin >> n >> W;

// 	vector<vector<ll>> dp(n + 1, vector<ll>(W + 1));



// 	for (auto i = 1; i <= n; ++i) {

// 		int cw, v; cin >> cw >> v;

// 		for (int w = W; w >= 0; --w) {

// 			dp[i][w] = dp[i - 1][w];

// 			if(w - cw >= 0)

// 				dp[i][w] = max(dp[i][w], v + dp[i - 1][w - cw]);

// 		} 

// 	}

// 	cout << dp[n][W] << endl;

// }



void solve () {

	int n, W; cin >> n >> W;

	vector<vector<ll>> dp(2, vector<ll>(W + 1));



	for (auto i = 1; i <= n; ++i) {

		int cw, v; cin >> cw >> v;

		auto & cur = dp[i % 2];

		const auto & prev = dp[(i - 1) % 2];

		for (int w = W; w >= 0; --w) {

			cur[w] = prev[w];

			if(w - cw >= 0) {

				cur[w] = max(cur[w], v + prev[w - cw]);

			}

		}

	}



	cout << dp[n % 2][W];

}



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);

  cout.tie(nullptr);

  cout.precision(precision);

  cout.setf(ios_base::fixed);

  solve();

  return 0;

}




// Faster C++ solution for the same problem (measured speedup: 5.32393978897625x):
#include<cstdio>

#include<algorithm>

using namespace std;

long long opt[106][100005];

int main()

{

    int v[106],w[105];

    int i,j,n,m;

    scanf("%d %d",&n,&m);

    for(i = 1; i <= n; i++)

        scanf("%d %d",&w[i],&v[i]);

    for(i = 0; i <= n; i++)

        opt[0][i] = 0;

    for(i = 1; i <= n; i++)

    {

        for(j = 0; j <= m; j++)

        {

            //printf("%d*",j - w[i]);

            if(j - w[i] >= 0)

                opt[i][j] = max(opt[i - 1][j],opt[i - 1][j - w[i]] + v[i]);

            else opt[i][j] = opt[i - 1][j];

            //printf("%d ",opt[i][j]);

        }

        //printf("\n");

    }

    printf("%lld\n",opt[n][m]);

    return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#define ll long long

using namespace std;



ll dp[100000 +10], w[100000 +10];

ll v[100000 +10];

int main()

{

	int n,m; cin>>n>>m;

	int i,j;

	for(i=1;i<=n;i++) cin>>w[i]>>v[i];

	

	for(i=1;i<=n;i++)

	{

		for(j=m;j>=w[i];j--)

		{

			dp[j] = max(dp[j] , dp[j-w[i]] + v[i]);

		}

	}

	printf("%lld\n",dp[m]);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.203722887289107x):
#include<cstdio>

#include<algorithm>

using namespace std;

long long opt[106][100005];

int main()

{

    int v[106],w[105];

    int i,j,n,m;

    scanf("%d %d",&n,&m);

    for(i = 1; i <= n; i++)

        scanf("%d %d",&w[i],&v[i]);

    for(i = 0; i <= n; i++)

        opt[0][i] = 0;

    for(i = 1; i <= n; i++)

    {

        for(j = 0; j <= m; j++)

        {

            //printf("%d*",j - w[i]);

            if(j - w[i] >= 0)

                opt[i][j] = max(opt[i - 1][j],opt[i - 1][j - w[i]] + v[i]);

            else opt[i][j] = opt[i - 1][j];

            //printf("%d ",opt[i][j]);

        }

        //printf("\n");

    }

    printf("%lld\n",opt[n][m]);

    return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define MAX 1000000

//3

typedef long long ll;

typedef pair<int,int> pii;

priority_queue< int > pq;

priority_queue< int , vector<int> , greater<int> > rpq;

set<int>s;

set <int > :: iterator itr; 

vector<int> vec[1001];

int r,n,m,inp,tc;

int arr[1000007]= {};

int w;

ll dp[107][100100];

ll solve(ll cost[], ll weight[]){

	for(int i=0 ; i<= n; i++){

		for(int j=0 ; j<= w ;j++){

			if(i ==0 || j==0) dp[i][j] = 0;

			else{

				if(weight[i] <= j){

					dp[i][j] = max(dp[i-1][j] , cost[i] + dp[i-1][j-weight[i]]);

				} 

				else dp[i][j] = dp[i-1][j];

			}

		}

	}

//	for(int i=0 ; i<= n; i++){

//		for(int j=0 ; j<= w ;j++){

//			cout<<dp[i][j]<<" ";

//		}

//		cout<<endl;

//	}

	return dp[n][w];

}



int main()

{

	cin>>n>>w;

	ll cost[n+7],weight[w+7];

	for(int i=1 ;i<=n ;i++){

		cin>>weight[i]>>cost[i];

	}

	cout<<solve(cost,weight)<<endl;

	return 0;

}



//65 900 17 13

// Faster C++ solution for the same problem (measured speedup: 5.351192173614465x):
#include<cstdio>

#include<algorithm>

using namespace std;

long long opt[106][100005];

int main()

{

    int v[106],w[105];

    int i,j,n,m;

    scanf("%d %d",&n,&m);

    for(i = 1; i <= n; i++)

        scanf("%d %d",&w[i],&v[i]);

    for(i = 0; i <= n; i++)

        opt[0][i] = 0;

    for(i = 1; i <= n; i++)

    {

        for(j = 0; j <= m; j++)

        {

            //printf("%d*",j - w[i]);

            if(j - w[i] >= 0)

                opt[i][j] = max(opt[i - 1][j],opt[i - 1][j - w[i]] + v[i]);

            else opt[i][j] = opt[i - 1][j];

            //printf("%d ",opt[i][j]);

        }

        //printf("\n");

    }

    printf("%lld\n",opt[n][m]);

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <vector>



using namespace std;



struct item {

    int w, v;

};



void knapsack1(){

    vector<item> items;

    int n, w;

    cin >> n >> w;

    long maxVal[n + 1][w + 1];

    items.push_back({0,0});

    for (int i = 0; i < n; i++) {

        int wt, vl;

        cin >> wt >> vl;

        items.push_back({wt, vl});

    }

    for (int i = 0; i <= w; i++){

        maxVal[0][i] = 0;

    }

    for (int i = 0; i <= n; i++){

        maxVal[i][0] = 0;

    }

    for (int i = 1; i <= n; i++){

        for (int j = 1; j <= w; j++) {

            if (j < items[i].w) {

                maxVal[i][j] = maxVal[i - 1][j];

            }

            else

                maxVal[i][j] = max(maxVal[i - 1][j], maxVal[i - 1][j - items[i].w] + items[i].v);

        }

    }

    cout << maxVal[n][w];

}



int main() {

    knapsack1();

}






// Faster C++ solution for the same problem (measured speedup: 5.271407750630616x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int n,m,w,v;

long long f[100005];

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;++i){

		scanf("%d%d",&w,&v);

		for(int j=m;j>=w;--j)

			f[j]=max(f[j],f[j-w]+v);

	}

	printf("%lld\n",f[m]);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#include <limits.h>

#include <algorithm>  

#include <numeric>

 

#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

using namespace std;

 

#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define endl '\n'

using ll = long long;

 

const int precision = 3;

const int modulo =  1000000007; // 10^9 + 7

const ll lmodulo =  1000000007; // 10^9 + 7

 

const double EPS = 1e-9;

const ll inf = 1e18;



// void solve () {

// 	int n, W; cin >> n >> W;

// 	vector<vector<ll>> dp(n + 1, vector<ll>(W + 1));



// 	for (auto i = 1; i <= n; ++i) {

// 		int cw, v; cin >> cw >> v;

// 		for (int w = W; w >= 0; --w) {

// 			dp[i][w] = dp[i - 1][w];

// 			if(w - cw >= 0)

// 				dp[i][w] = max(dp[i][w], v + dp[i - 1][w - cw]);

// 		} 

// 	}

// 	cout << dp[n][W] << endl;

// }



void solve () {

	int n, W; cin >> n >> W;

	vector<vector<ll>> dp(2, vector<ll>(W + 1));



	for (auto i = 1; i <= n; ++i) {

		int cw, v; cin >> cw >> v;

		auto & cur = dp[i % 2];

		const auto & prev = dp[(i - 1) % 2];

		for (int w = W; w >= 0; --w) {

			cur[w] = prev[w];

			if(w - cw >= 0) {

				cur[w] = max(cur[w], v + prev[w - cw]);

			}

		}

	}



	cout << dp[n % 2][W];

}



int main() {

  ios_base::sync_with_stdio(false);

  cin.tie(nullptr);

  cout.tie(nullptr);

  cout.precision(precision);

  cout.setf(ios_base::fixed);

  solve();

  return 0;

}




// Faster C++ solution for the same problem (measured speedup: 5.338503240979165x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int n,m,w,v;

long long f[100005];

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;++i){

		scanf("%d%d",&w,&v);

		for(int j=m;j>=w;--j)

			f[j]=max(f[j],f[j-w]+v);

	}

	printf("%lld\n",f[m]);

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<iostream>

#define ll long long

using namespace std;



ll dp[100000 +10], w[100000 +10];

ll v[100000 +10];

int main()

{

	int n,m; cin>>n>>m;

	int i,j;

	for(i=1;i<=n;i++) cin>>w[i]>>v[i];

	

	for(i=1;i<=n;i++)

	{

		for(j=m;j>=w[i];j--)

		{

			dp[j] = max(dp[j] , dp[j-w[i]] + v[i]);

		}

	}

	printf("%lld\n",dp[m]);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.217957490141384x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int n,m,w,v;

long long f[100005];

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;++i){

		scanf("%d%d",&w,&v);

		for(int j=m;j>=w;--j)

			f[j]=max(f[j],f[j-w]+v);

	}

	printf("%lld\n",f[m]);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define MAX 1000000

//3

typedef long long ll;

typedef pair<int,int> pii;

priority_queue< int > pq;

priority_queue< int , vector<int> , greater<int> > rpq;

set<int>s;

set <int > :: iterator itr; 

vector<int> vec[1001];

int r,n,m,inp,tc;

int arr[1000007]= {};

int w;

ll dp[107][100100];

ll solve(ll cost[], ll weight[]){

	for(int i=0 ; i<= n; i++){

		for(int j=0 ; j<= w ;j++){

			if(i ==0 || j==0) dp[i][j] = 0;

			else{

				if(weight[i] <= j){

					dp[i][j] = max(dp[i-1][j] , cost[i] + dp[i-1][j-weight[i]]);

				} 

				else dp[i][j] = dp[i-1][j];

			}

		}

	}

//	for(int i=0 ; i<= n; i++){

//		for(int j=0 ; j<= w ;j++){

//			cout<<dp[i][j]<<" ";

//		}

//		cout<<endl;

//	}

	return dp[n][w];

}



int main()

{

	cin>>n>>w;

	ll cost[n+7],weight[w+7];

	for(int i=1 ;i<=n ;i++){

		cin>>weight[i]>>cost[i];

	}

	cout<<solve(cost,weight)<<endl;

	return 0;

}



//65 900 17 13

// Faster C++ solution for the same problem (measured speedup: 5.36583017356709x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int n,m,w,v;

long long f[100005];

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;++i){

		scanf("%d%d",&w,&v);

		for(int j=m;j>=w;--j)

			f[j]=max(f[j],f[j-w]+v);

	}

	printf("%lld\n",f[m]);

	return 0;

}
// Slower C++ solution:
#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

int main(){

  long long n, m;

  cin>>n>>m;

  long long M[n+1][m+1];

  for(long long i=0;i<=n;i++){

    for(long long j=0;j<=m;j++){

      M[i][j]=0;

    }

  }

  long longresult=0;

  vector<long long>peso;

  vector<long long>valor;

  for(long long i=0;i<n;i++){

    long long a,b;

    cin>>a>>b;

    peso.push_back(a);

    valor.push_back(b);

  }

  for(long long j=1;j<=m;j++){

   for(long long i=1;i<=n;i++){

      if(j >= peso[i-1])

        M[i][j]=max(M[i-1][j], valor[i-1]+M[i-1][j-peso[i-1]]);



      else M[i][j]=M[i-1][j];   

      }

  }

  cout<<M[n][m]<<endl;

}

// Faster C++ solution for the same problem (measured speedup: 5.278824873329725x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

int n,m,w,v;

long long f[100005];

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;++i){

		scanf("%d%d",&w,&v);

		for(int j=m;j>=w;--j)

			f[j]=max(f[j],f[j-w]+v);

	}

	printf("%lld\n",f[m]);

	return 0;

}