CSES - Shared codeLink to this code: https://cses.fi/paste/960597f78651616cc56856/
/**
* Coded by : Harsh vardhan
**/
#include <bits/stdc++.h>
using namespace std;
void harsh(){
int n, x;
cin >> n >> x;
vector<int> price(n), pages(n);
for(int i = 0; i < n; i++) cin >> price[i];
for(int i = 0; i < n; i++) cin >> pages[i];
// dp[i][j] = max pages can be bought at first i index and in coin j
vector<vector<int>> dp(n + 1, vector<int>(x + 1, 0));
for(int i = 0; i < n; i++){
for(int coin = 0; coin <= x; coin++){
// Don't Buy the current book
if(i > 0) dp[i][coin] = dp[i - 1][coin];
// Buy the current book
int temp = coin - price[i];
if(temp >= 0){
if(i > 0) dp[i][coin] = max(dp[i][coin], pages[i] + dp[i - 1][temp]);
else dp[i][coin] = max(dp[i][coin], pages[i]);
}
}
}
cout << dp[n - 1][x];
}
int main(){
harsh();
return 0;
}