CSES - Shared codeLink to this code:
https://cses.fi/paste/ee8c6dc48ddb88479dfaa8/
/*
AUTHOR -> KIMJONGOOF
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #define int long long
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;
const int mod = 1e9 + 7;
const int inf = 1e15 + 20;
/*
__builtin_popcountll(x): Counts the number of one’s(set bits) in an integer(long/long long).
__builtin_parityll(x): Checks the Parity of a number.Returns true(1) if the number has odd parity(odd number of set bits) else it returns false(0) for even parity(even number of set bits).
__builtin_clzll(x): Counts the leading number of zeros of the integer(long/long long).
__builtin_ctzll(x): Counts the trailing number of zeros of the integer(long/long long).
*/
int n, x;
vector<int> price, nums;
vector<vector<int>> dp;
void solve()
{
cin >> n >> x;
price.assign(n, 0);
nums.assign(n, 0);
for (int i = 0; i < n; i++)
{
cin >> price[i];
}
for (int i = 0; i < n; i++)
{
cin >> nums[i];
}
dp.assign(n + 1, vector<int>(x + 1, 0));
// dp[0][0] = 0;
// dp[i][j]=max number of pages posible with j amount of money using only the first i books
for (int i = 1; i <= n; i++)
{
for (int j = 0; j <= x; j++)
{
dp[i][j] = max(dp[i][j], dp[i - 1][j]); // chose not to pick the current book
if (j - price[i - 1] >= 0) // check if current book can be picked in the first place
{
dp[i][j] = max(dp[i][j], nums[i - 1] + dp[i - 1][j - price[i - 1]]);
}
}
}
cout << dp[n][x] << "\n";
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
}