CSES - Shared codeLink to this code:
https://cses.fi/paste/27ef5df72fd9b9261f7b04/
#include<bits/stdc++.h>
#include<tuple>
typedef long long ll;
#define INF 9999999999
#define mod 1000000007
#define eps 1e-9
using namespace std;
typedef tuple<ll,ll,ll>tri;
ll n,dp[2][100005],x,h[1005],s[1005];
int main(){
ios::sync_with_stdio(0);
cin>>n>>x;
for(ll i=1;i<=n;i++)
cin>>h[i];
for(ll i=1;i<=n;i++)
cin>>s[i];
for(ll i=1;i<=n;i++){
for(ll j=1;j<=h[i];j++)
dp[i%2][j]=dp[1-i%2][j];
for(ll j=h[i];j<=x;j++)
dp[i%2][j]=max(dp[1-i%2][j],dp[1-i%2][j-h[i]]+s[i]);
}
cout<<dp[n%2][x]<<endl;
}