-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanish-iphone-batt.cpp
54 lines (49 loc) · 1.26 KB
/
anish-iphone-batt.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
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.sync_with_stdio(0); cin.tie(0);
int E,N;
cin >> E >> N;
string names[N+1];
int costs[N+1];
int weights[N+1];
int dp[N+1][E+1];
vector <string> v;
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= E; j++) dp[i][j] = 0;
}
for (int i = 1; i <= N; i++) {
cin >> names[i] >> weights[i] >> costs[i];
if (costs[i] == 0 && weights[i] == 0) v.push_back(names[i]);
for (int j = 0; j <= E; j++) {
dp[i][j] = dp[i-1][j];
if (j >= costs[i]) dp[i][j] = max(dp[i][j],dp[i-1][j-costs[i]] + weights[i]);
}
}
int se = E, sn = N;
while (true) {
if (dp[sn][se] == dp[sn][se-1]) se = se-1;
else break;
}
cout << dp[sn][se] << " " << se << "\n";
while (sn > 0) {
if (dp[sn-1][se] != dp[sn][se]) {
se -= costs[sn];
v.push_back(names[sn]);
}
sn--;
}
sort(v.begin(),v.end());
string out = "";
for (string s : v) out += s + " ";
out.pop_back();
cout << out;
/*
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= E; j++) {
cout << dp[i][j] << " ";
}
cout << "\n";
}
*/
}