-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdpd.cpp
More file actions
35 lines (34 loc) · 776 Bytes
/
dpd.cpp
File metadata and controls
35 lines (34 loc) · 776 Bytes
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, w;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> w;
int val[n], wt[n];
for (int i = 0; i < n; i++){
cin >> wt[i] >> val[i];
}
ll mat[n+1][w+1];
for (int i = 0; i <= n; i++){
mat[i][0] = 0;
}
for (int i = 0; i <= w; i++){
mat[0][i] = 0;
}
for (int i = 1; i <= n; i++){
for (int c = 1; c <= w; c++) {
ll x = mat[i-1][c];
ll y = 0;
ll z = wt[i-1];
if (c >= z){
y = val[i-1];
ll left = c-z;
y += mat[i-1][left];
}
mat[i][c] = max(x,y);
}
}
cout << mat[n][w] << "\n";
}