-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathBook Shop.cpp
79 lines (63 loc) · 2.03 KB
/
Book Shop.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
Solution by Rahul Surana
***********************************************************
You are in a book shop which sells n different books. You know the price and number of pages of each book.
You have decided that the total price of your purchases will be at most x.
What is the maximum number of pages you can buy? You can buy each book at most once.
Input
The first input line contains two integers n and x:
the number of books and the maximum total price.
The next line contains n integers h1,h2,…,hn:
the price of each book.
The last line contains n integers s1,s2,…,sn:
the number of pages of each book.
Output
Print one integer: the maximum number of pages.
***********************************************************
*/
#include <bits/stdc++.h>
#define ll long long
#define vl vector<ll>
#define vi vector<int>
#define pi pair<int,int>
#define pl pair<ll,ll>
#define all(a) a.begin(),a.end()
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define FOR(i,a) for(int i = 0; i < a; i++)
#define fast_io std::ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
// #define TIME
#ifdef TIME
#include <chrono>
using namespace std::chrono;
#endif
using namespace std;
int main() {
fast_io;
#ifdef TIME
auto start = high_resolution_clock::now();
#endif
int n, x;
cin >> n >> x;
vector<vector<int>> dp(n+1,vector<int>(x+1,0));
vector<int> p(n),pp(n);
FOR(i,n){
cin >> p[i];
}
FOR(i,n){
cin >> pp[i];
for(int i = 1; i <=n; i++){
FOR(j,x+1){
dp[i][j] = dp[i-1][j];
if(p[i-1] <= j)
dp[i][j] = max(dp[i][j], dp[i-1][j-p[i-1]] + pp[i-1]);
}
}
cout << dp[n][x] <<"\n";
#ifdef TIME
cout << (duration_cast<milliseconds>(high_resolution_clock::now() - start).count()) <<"ms\n";
#endif
}