-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD.cpp
63 lines (56 loc) · 1.18 KB
/
D.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
bool comp(pair<int,int> a, pair<int,int> b) {
return (a.first < b.first);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
/*#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r+", stdin);
// for writing output to output.txt
freopen("output.txt", "w+", stdout);
#endif*/
int n;
int w;
cin >> n >> w;
vector <pair<int,int>> ele(n);
for(int i=0,a,b;i<n;i++) {
cin >> a >> b;
ele[i] = mp(a,b);
}
sort(ele.begin(),ele.end(),comp);
// for(int i=0;i<n;i++) {
// cout << ele[i].first << " " << ele[i].second << '\n';
// }
ll dp[n+1][w+1];
for(int i=0;i<=n;i++) {
dp[i][0] = 0;
}
for(int i = 0;i<=w;i++) {
dp[0][i] = 0;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=w;j++) {
pair<int,int> obj = ele[i-1];
int wt = obj.first;
int val = obj.second;
if(j >= wt) {
dp[i][j] = max(dp[i-1][j],dp[i-1][j-wt] + val);
}
else {
dp[i][j] = dp[i-1][j];
}
}
}
// for(int i=0;i<=n;i++) {
// for(int j=0;j<=w;j++) {
// cout << dp[i][j] << " ";
// }
// cout << '\n';
// }
cout << dp[n][w] << '\n';
}