|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +using ll = long long; |
| 5 | +using ld = long double; |
| 6 | +#define tcU template<class T |
| 7 | +tcU> using V = vector<T>; |
| 8 | +#define rep(i,a,b) for(int i=a;i<b;++i) |
| 9 | +#define per(i,a,b) for(int i=b;i>=a;i--) |
| 10 | +#define sz(a) (int)(a.size()) |
| 11 | +#define all(a) a.begin(),a.end() |
| 12 | +#define precision(n) cout << fixed << setprecision(n) |
| 13 | + |
| 14 | +void setIO(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } |
| 15 | +const int mod = 1e9 + 7; |
| 16 | +tcU> T max(T &a,T b) {return a > b ? a : b;} |
| 17 | +tcU> T min(T &a, T b) { return a > b ? b : a;} |
| 18 | + |
| 19 | +template<typename T1, typename T2> // cin >> pair |
| 20 | +istream &operator>>(istream &istream, pair<T1, T2> &p) { return (istream >> p.first >> p.second); } |
| 21 | +tcU> // cin >> vector |
| 22 | +istream &operator>>(istream &istream, vector<T> &v) { for (auto &it : v) { cin >> it; } return istream; } |
| 23 | +template<typename T1, typename T2> // cout << pair |
| 24 | +ostream &operator<<(ostream &ostream, const pair<T1, T2> &p) { return (ostream << p.first << " " << p.second); } |
| 25 | +tcU> // cout << vector |
| 26 | +ostream &operator<<(ostream &ostream, const vector<T> &c) { for (auto &it : c) { cout << it << " "; } return ostream; } |
| 27 | + |
| 28 | +int f(int idx, int tar, vector<ll> &cost, vector<ll> &weight,vector<vector<ll>> &dp) { |
| 29 | + if (idx == 0) { |
| 30 | + if (tar >= cost[0]) return weight[0]; |
| 31 | + return 0; |
| 32 | + } |
| 33 | + if (dp[idx][tar] != -1) return dp[idx][tar]; |
| 34 | + int notake = f(idx-1,tar,cost,weight,dp); |
| 35 | + int take = 0; |
| 36 | + if (tar >= cost[idx]) take = f(idx-1,tar - cost[idx], cost, weight,dp) + weight[idx]; |
| 37 | + return dp[idx][tar] = max(take , notake); |
| 38 | +} |
| 39 | + |
| 40 | +void solve() { |
| 41 | + int n, x; cin >> n >> x; |
| 42 | + vector<ll> cost(n); cin >> cost; |
| 43 | + vector<ll> weight(n); cin >> weight; |
| 44 | + vector<vector<ll>> dp(n+1,vector<ll> (x+1,-1)); |
| 45 | + cout << f(n-1,x,cost,weight,dp) << '\n'; |
| 46 | +} |
| 47 | + |
| 48 | +signed main() { |
| 49 | + ios_base::sync_with_stdio(false),cin.tie(nullptr); |
| 50 | + |
| 51 | + int T = 1; |
| 52 | + rep(i,1,T+1){ |
| 53 | + /* cout << "Case #" << i << ": "; */ |
| 54 | + solve(); |
| 55 | + } |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
0 commit comments