Skip to content

Commit 0df1916

Browse files
Create book-shop.cpp
1 parent c35857a commit 0df1916

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

book-shop.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
typedef long long ll;
4+
#define vi vector<int>
5+
#define vvi vector<vector<int>>
6+
int n,x;
7+
int rec(int cap,vi& price, vi& pages) {
8+
// dp[i][j] = max pages we can buy with i items and j rupees
9+
vvi dp(n+1,vi(cap+1));
10+
for (int i = 1; i <= n; ++i) {
11+
for (int j = 0; j <= cap; ++j) {
12+
if(price[i - 1] > j) dp[i][j] = dp[i-1][j];
13+
else dp[i][j] = max(dp[i-1][j-price[i-1]] + pages[i-1],dp[i-1][j]);
14+
}
15+
}
16+
return dp[n][x];
17+
}
18+
void solve() {
19+
cin >> n >> x;// x = capacity;
20+
vi price(n); for (int i = 0; i < n; ++i) cin >> price[i];
21+
vi pages(n); for (int i = 0; i < n; ++i) cin >> pages[i];
22+
cout << rec(x, price, pages) << endl;
23+
24+
}
25+
int32_t main() {
26+
solve();
27+
}

0 commit comments

Comments
 (0)