-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook_Shop.cpp
More file actions
51 lines (38 loc) · 1.21 KB
/
Book_Shop.cpp
File metadata and controls
51 lines (38 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
using namespace std;
#define forn(i, k, e) for (int i = k; i < e; i++)
/*
find max no. of pages(N) for given money X
since one book cannot be used more than once,
sort the books in asc. order of price of book
state vars := money, index of current book
state transition :=
dp[i][j] = 0 when i < price of book
dp[i][j] = max(dp[i][j-1] , pages + dp[i-price][j-1])
// dp[i][j-1] = max pages you can purchase upto the previous book, with money = i
// dp[i-price][j-1] = max pages you can purchase upto previous book, with money = i - price of cur book
*/
typedef pair<int, int> pii;
vector<pii> books;
int X, N;
int32_t main() {
cin >> N >> X;
books.resize(N);
forn(i, 0, N) cin >> books[i].first;
forn(i, 0, N) cin >> books[i].second;
sort(books.begin(), books.end());
vector<vector<int>> dp(X + 1, vector<int>(N + 1, 0));
forn(i, 1, X + 1) {
forn(j, 1, N + 1) {
int price = books[j - 1].first;
int pages = books[j - 1].second;
if (i < price)
dp[i][j] = 0;
else {
dp[i][j] = max(pages + dp[i - price][j - 1], dp[i][j-1]);
}
}
}
cout << dp[X][N];
return 0;
}