stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

const int inf=0x3f3f3f3f;

const int N=105;

const int maxn=1e5+5;

int a[N],v[N];

ll dp[N][maxn];

int main(){

	int n,k;

	cin>>n>>k;

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

		cin>>a[i]>>v[i];

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

		dp[i][0]=0,	dp[0][i]=0;

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

		for(int j=1;j<=k;++j)

			if(j<a[i]) dp[i][j]=dp[i-1][j];

			else dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+v[i]);

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

	return 0;

} 

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int w[105], val[105];

long long dp[100005];

int main()

{

    int t,m,res=-1;    

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

    for(int i=1;i<=m;i++)

    {

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

    }

    for(int i=1;i<=m;i++) 

    {

        for(int j=t;j>=0;j--) 

        {

            if(j>=w[i])

            {

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

            }

        }

    }    

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

    return 0;

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef pair<int, int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

typedef vector<ll> vll;



ll dp(int mw, int n, int w[], int v[]){

	long memo[n+1][mw+1];

	int i , j;

//	memset(memo, 0, sizeof memo);

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

		for(j = 0; j <= mw; j++){

			if(i ==0 || j ==0)

				memo[i][j] = 0;

			else if(w[i-1] <= j)

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

			else

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

//			cout << memo[i][j] << ' ';

		}

//		cout << endl;

	}

//	cout << i << ' ' << j << endl;

	return memo[n][mw];

}



int main() {

	int n, mw;

	cin >> n >> mw;

	int w[n],v[n];

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

		cin >> w[i] >> v[i];

	}

	cout << dp(mw,n,w,v);

	return 0;

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define MAXN 110

#define MAXW 100010

#define ll long long



using namespace std;



ll n, W, ans, w[MAXN], v[MAXN], dp[MAXW];

bool mk[MAXW];



void init(){

    mk[0]=true;

}



int main(){



    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);



    cin>>n>>W;



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

        cin>>w[i]>>v[i];

    }



    init();



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

        for(int j=W-w[i]; j>=0; j--){

            if(mk[j]&&dp[j]+v[i]>dp[j+w[i]]){

                mk[j+w[i]]=true;

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

            }

        }

    }



    for(int i=0; i<=W; i++) ans=max(ans, dp[i]);



    cout<<ans;



return 0;

}


// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



//Recursive Definition

long long maximumCost(long long * weights, long long * values, long long N, long long W){

if(N == 1){

  if(weights[N-1] <= W){

   return  values[N-1];

  }else{

   return 0;

  }

}else if(N > 1){

  if(weights[N-1] <= W){

   return  max(values[N-1] + maximumCost(weights,values,N-1,W-weights[N-1]),maximumCost(weights,values,N-1,W));

  }else{

   return maximumCost(weights,values,N-1,W);

  }

}

}

//Now Using Memoization

//Bottom up algorithm Reference: https://www.geeksforgeeks.org/0-1-knapsack-problem-dp-10/

long long knapSack(long long W, long long * weights, long long * values, long long N) {

   long long i, w;

   long long WN[N+1][W+1];

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

       for (w = 0; w <= W; w++){

           if (i==0 || w==0){

           WN[i][w] = 0;

           }else if (weights[i-1] <= w){

           WN[i][w] = max(values[i-1] + WN[i-1][w-weights[i-1]],WN[i-1][w]);

           }else{

           WN[i][w] = WN[i-1][w];

           }

       }

   }

   return WN[N][W];

}



int main(){

    long long N;

    cin>>N;

    long long W;

    cin>>W;

    long long * weights = new long long[N];

    long long * values = new long long[N];

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

     cin>>weights[i];

     cin>>values[i];

    }

  //  cout<<maximumCost(weights,values,N,W)<<endl;

    cout<<knapSack(W,weights,values,N)<<endl;

    delete[] weights;

    delete[] values;

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<algorithm>



using namespace std;



const int maxn = 100000+5;

const int inf = 0x3f3f3f3f;

const int maxm=100000+5;



long long a[maxn],t[maxn],f[maxm];



int main(){

	int n,m;

	cin>>n>>m;

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

    	cin>>t[i]>>a[i];

    }

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

    	f[j]=-inf;

    }

    f[0]=0;

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

    	for(int j=m;j>=0;j--){

    		if(j>=t[i]){

    			f[j]=max(f[j],f[j-t[i]]+a[i]);

    		}	

    		}

    }

    long long ret=-1;

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

    	ret=max(ret,f[j]);

    }

cout<<ret <<endl;

return 0;

	

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

 

int main() {

  int N,W;

 

  cin >> N >> W;

 

  vector<int> w(N), v(N);

  vector<vector<long long> > dp(N+1,vector<long long>(W+1,0));

 

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

    cin >> w[i];

    cin >> v[i];

  }

 

 

 

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

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

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

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

      }

      else {

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

      }

    }

  }

 

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

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

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

  //   }

  //   cout << endl;

  // }

 

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

 

  return 0;

}

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair<ll, ll> pii;

#define fi first 

#define se second

#define endl '\n'



pii arr[105]; //fi = wi; se = vi;

ll dp[105][100005];



int main(){

	

	ios_base::sync_with_stdio(0);

	int n, w; cin >>n >>w;

	

	for(int i = 1; i<=n; i++) cin >>arr[i].fi >>arr[i].se;

	

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

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

			if(j - arr[i].fi >= 0){

				dp[i][j] = max(dp[i-1][j], dp[i-1][j - arr[i].fi] + arr[i].se);

			}

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

		}

		/*

		for(int j = 1; j <= w; j++) cout <<dp[i][j] <<endl;

		cout <<endl;*/

	}



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

	return 0;

}	

// Optimized solution to the same problem (faster):
#include <stdio.h>

#include <stdlib.h>

long long max(long long a,long long b)

{

	if(a>b)	return a;

	return b; 

}

int main()

{

	int i,n,j,W;

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

	long long *dp=(long long*)malloc(sizeof(long long)*W);

	dp[0]=0;

	for(i=1;i<=W;i++) dp[i]=-1;

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

	{

		int w1,v1;

		scanf("%d %d",&w1,&v1);

		for(j=W;j>=0;j--)

		{

			if(dp[j]==-1)	continue;

			if(j+w1<=W) dp[j+w1] = max(dp[j+w1],dp[j]+v1);

		}	

	}

	long long ans=-1;

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

	ans=max(ans,dp[i]);

	printf("%lld",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define ll long long

using namespace std;

#define inf 0x3f3f3f3f 

int main(){

	ll  n , W; cin >> n >> W;

    ll w[101] , v[101] , dp[100001] = {0};

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

        cin >> w[i] >> v[i];

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

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

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



    cout<< dp[W] <<endl;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define ll long long

#define pb push_back

#define pf push_front

#define pob pop_back

#define pof pop_front

#define scn(n) scanf("%d",&n)

#define scnll(n) scanf("%lld",&n)

#define nl printf("\n")

#define YES printf("YES\n")

#define Yes printf("Yes\n")

#define yes printf("yes\n")

#define NO printf("NO\n")

#define No printf("No\n")

#define no printf("no\n")

using namespace std;

int n;

ll mn=-10000000000000;

ll dp[100002][2]={0};

ll w[101],v[101];

ll solve(int weight){

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

        int prev=i%2;

        int now=(i+1)%2;

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

            if(w[i]>j) dp[j][now]=dp[j][prev];

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

        }

    }

    return dp[weight][(n+1)%2];

}

int main(){

    scn(n);

    int wt;scn(wt);

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

        scnll(w[i]);

        scnll(v[i]);

    }

    ll ans=solve(wt);

    printf("%lld\n",ans);

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;





ll w[105],f[100005];

ll v[105];



signed main()

{

	ll N,W;

	while(cin>>N>>W){

		for(ll i=1;i<=N;i++)

		cin>>w[i]>>v[i];

		

		//01背包 

		memset(f,0,sizeof f);

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

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

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

		}	

		

		cout<<f[W]<<endl;

	}

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define ll long long

#define pb push_back

#define pf push_front

#define pob pop_back

#define pof pop_front

#define scn(n) scanf("%d",&n)

#define scnll(n) scanf("%lld",&n)

#define nl printf("\n")

#define YES printf("YES\n")

#define Yes printf("Yes\n")

#define yes printf("yes\n")

#define NO printf("NO\n")

#define No printf("No\n")

#define no printf("no\n")

using namespace std;

int n;

ll mn=-10000000000000;

ll dp[100002][2]={0};

ll w[101],v[101];

ll solve(int weight){

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

        int prev=i%2;

        int now=(i+1)%2;

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

            if(w[i]>j) dp[j][now]=dp[j][prev];

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

        }

    }

    return dp[weight][(n+1)%2];

}

int main(){

    scn(n);

    int wt;scn(wt);

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

        scnll(w[i]);

        scnll(v[i]);

    }

    ll ans=solve(wt);

    printf("%lld\n",ans);

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define fastio ios_base::sync_with_stdio(0);cin.tie(0)

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

#define fn(i,a,b) for(int i=a ; i>=b ; i--)

#define pb push_back

#define mk make_pair

#define ff first

#define ss second

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



typedef long long ll;

typedef pair<int,int> ii;

typedef vector<ii> vii;

typedef vector<int> vi;

typedef vector<ll> vll;



const int M=1e5+5;

const ll INF=1e15;

const int MOD=1e9+5;



int n,W;

ll dp[M][105],valor[105],peso[105];



ll ks(int w,int x){

  if (w < 0) return -INF;

  if (x > n) return 0;

  if (dp[w][x] != -1) return dp[w][x];

  dp[w][x] = max(ks(w,x+1) , valor[x] + ks(w-peso[x],x+1));

  return dp[w][x];

}



void ini(){

  fp(i,0,M)

    fp(j,1,n+1) dp[i][j] = -1;

}



int main(){



	fastio;

  cin >> n >> W;

  ini();

  fp(i,1,n+1) cin >> peso[i] >> valor[i];

  cout << ks(W,1) << "\n";



	return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define ll long long

#define pb push_back

#define pf push_front

#define pob pop_back

#define pof pop_front

#define scn(n) scanf("%d",&n)

#define scnll(n) scanf("%lld",&n)

#define nl printf("\n")

#define YES printf("YES\n")

#define Yes printf("Yes\n")

#define yes printf("yes\n")

#define NO printf("NO\n")

#define No printf("No\n")

#define no printf("no\n")

using namespace std;

int n;

ll mn=-10000000000000;

ll dp[100002][2]={0};

ll w[101],v[101];

ll solve(int weight){

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

        int prev=i%2;

        int now=(i+1)%2;

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

            if(w[i]>j) dp[j][now]=dp[j][prev];

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

        }

    }

    return dp[weight][(n+1)%2];

}

int main(){

    scn(n);

    int wt;scn(wt);

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

        scnll(w[i]);

        scnll(v[i]);

    }

    ll ans=solve(wt);

    printf("%lld\n",ans);

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

//#pragma GCC optimize("Ofast")

//#pragma GCC target("avx,avx2,fma")



using namespace std;



typedef long long ll;



//#include <ext/pb_ds/assoc_container.hpp>

//#include <ext/pb_ds/tree_policy.hpp>

//using namespace __gnu_pbds;

//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>pb_ds;



#define IN freopen("in.txt","r",stdin)

#define OUT freopen("out.txt","w",stdout)

#define pi pair<int,int>

#define F first

#define S second

#define pb push_back

#define pp pop_back

#define mod 1000000007

#define pai acos(-1)

#define N 100005



ll Weight[N],Profit[N],dp[2][N];



int main ()

{

   ios_base::sync_with_stdio(false);

   cin.tie(NULL);

   int n,w;cin>>n>>w;

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

      cin>>Weight[i]>>Profit[i];

   }

   fill(dp[0],dp[0]+2*N,-INT_MAX*105);

   dp[(n+1)%2][0]=0;

   for(int i=n;i>=1;i--){

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

         ll op1=dp[(i+1)%2][s];

         ll op2=(s-Weight[i]>=0)?Profit[i]+dp[(i+1)%2][s-Weight[i]]:0ll;

         dp[i%2][s]=max(op1,op2);

      }

   }

   ll ans=0;

   for(int i=0;i<=w;i++) ans=max(ans,dp[1][i]);

   cout<<ans<<"\n";

   return 0;

}




// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define ll long long

#define pb push_back

#define pf push_front

#define pob pop_back

#define pof pop_front

#define scn(n) scanf("%d",&n)

#define scnll(n) scanf("%lld",&n)

#define nl printf("\n")

#define YES printf("YES\n")

#define Yes printf("Yes\n")

#define yes printf("yes\n")

#define NO printf("NO\n")

#define No printf("No\n")

#define no printf("no\n")

using namespace std;

int n;

ll mn=-10000000000000;

ll dp[100002][2]={0};

ll w[101],v[101];

ll solve(int weight){

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

        int prev=i%2;

        int now=(i+1)%2;

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

            if(w[i]>j) dp[j][now]=dp[j][prev];

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

        }

    }

    return dp[weight][(n+1)%2];

}

int main(){

    scn(n);

    int wt;scn(wt);

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

        scnll(w[i]);

        scnll(v[i]);

    }

    ll ans=solve(wt);

    printf("%lld\n",ans);

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main(){

  int n,w;

  cin>>n>>w;

 	vector<long long> dp(200005);

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

  {

    int a,b;

    cin>>a>>b;

    for(int j=w-a;j>=0;j--)

    {

      dp[j+a]=max(dp[j+a],dp[j]+b);

    }

  }

  cout<<dp[w]<<" ";

  return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

int main(){

  long long n,w;

  cin>>n>>w;

 long long a[n][2];

 

  vector<long long> dp(w+1);

//   dp[0]=0;

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

     long long a,b;

     cin>>a>>b;

     for(long long j=w-a;j>=0;j--){

         dp[j+a]=max(dp[j+a],dp[j]+b);

     }

 }

 cout<<dp[w]<<endl;

}  

// Fastest known solution for this problem:
// Original C++ solution (slower):


#include<iostream>

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<unordered_map>

#include<bitset>

using namespace std;

#define INF 2147483647

#define lINF 9223372036854775807

#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define ARDEB(i,X) cout<< #X <<"["<<i<<"]: "<<X[i]<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define ARDEB(i,X) {}

#define END {}

#endif

typedef long long int ll;



ll n,W,w[111],v[111],m[111][111111];

ll dp(int i,int j){//番号,重さ

	if(m[i][j]!=-1)return m[i][j];

	if(i==n)return 0;

	

	ll sum=0;

	if(j<w[i]){

		sum=dp(i+1,j);

	}

	else{

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

	}

	

	return m[i][j]=sum;

	

}



int main(){

	

	cin>>n>>W;

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

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

			m[i][j]=-1;

		}

	}

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

		cin>>w[i]>>v[i];

	}

	cout<<dp(0,W)<<endl;

	return 0;



}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<string>

#include<cstdio>

#include<algorithm>

#include<cstdlib>

#include<cmath>

#include<vector>

#include<utility>

#include<queue>

#include<stack>

#include<set>

#include<map>

#include<unordered_set>

#include<unordered_map>

#include<bitset>

using namespace std;

#define INF 1000000007

#define LINF 100000000000000007

#define MOD 1000000007

#define int long long

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

#define repb(i, n) for (int i = n - 1; i >= 0; i--)

#define MODE 1

#ifdef MODE

#define DEB(X) cout<< #X <<": "<<X<<" ";

#define ARDEB(i,X) cout<< #X <<"["<<i<<"]: "<<X[i]<<" ";

#define END cout<<endl;

#else

#define DEB(X) {}

#define ARDEB(i,X) {}

#define END {}

#endif

typedef long long ll;

typedef pair<int,int> P;

struct edge{int to,cost;};

int ceil2(int a,int b){if(a%b){return a/b+1;}else{return a/b;}}

using namespace std;



int ans;

string s;

int N,W,dp[111111][111],w[111111],v[111111];

signed main(){

	 cin>>N>>W;

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

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

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

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

	 		else dp[i][j+1]=max(dp[i][j],dp[i][j+1]);

	 	}

	 }

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

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

typedef long long LL;



LL dp[105][MAXN];



int main()

{

	int n, w, wei, val;

	cin >> n >> w;

	

	fill(dp[0]+1, dp[0]+MAXN, -LLONG_MAX);

	

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

	{

		cin >> wei >> val;

		

		copy(dp[i-1], dp[i-1]+w, dp[i]); // ¤W¦æªº¸ê®Æáàµ§¦Ü¦¹ 

		

		for (int j = wei; j <= w; j++)

		{

			dp[i][j] = max(dp[i-1][j], dp[i-1][j-wei]+val);

		}

	}



    /*for (int i = 1; i <= n; i++)

    {

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

    	{

    		cout << dp[i][j] << ' ';

		}

		cout << "\n";

	}*/

	

	cout << *max_element(dp[n]+1, dp[n]+w+1) << "\n";

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

#define MAXN 100005

typedef long long LL;



LL pre[MAXN], cur[MAXN];



int main()

{

	int n, w, wei, val;

	cin >> n >> w;

	

	fill(pre+1, pre+w+1, -LLONG_MAX);

	

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

	{

		cin >> wei >> val;

		

		copy(pre, pre+w+1, cur);

		

		for (int j = wei; j <= w; j++)

		{

			cur[j] = max(pre[j], pre[j-wei]+val);

		}

		

		copy(cur, cur+w+1, pre);

	}

	

	cout << *max_element(cur+1, cur+w+1) << "\n";

}

// Fastest known solution for this problem: