-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoci_2007_c4_p4.cpp
83 lines (78 loc) · 1.8 KB
/
coci_2007_c4_p4.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
80
81
82
83
#include <algorithm>
#include <numeric>
#include <iterator>
#include <iostream>
#include <bitset>
#include <deque>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <climits>
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<long long,long long> pii;
typedef vector<int> vi;
typedef vector<vector<pair<long long,long long>>> vvpii;
typedef unordered_set<int> uset;
#define pb push_back
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
int P[1000];
int total[1000];
int table[501][5001];
int T,N;
void recur2() {
for(int i = 1; i <= N; i++) {
for(int j = 1; j <= T; j++) {
table[i][j] = table[i-1][j];
if(j >= P[i-1]) table[i][j] = max(table[i-1][j], table[i-1][j - P[i-1]] + P[i-1]);
}
}
}
vi reconstruct(int i, int w) {
if(i == 0) {
return {};
}
if(table[i][w] > table[i-1][w]) {
vi ve = reconstruct(i-1, w - P[i-1]);
ve.push_back(i);
return ve;
} else {
return reconstruct(i-1, w);
}
}
int main() {
scan(T); scan(N);
for(int i = 0; i < N; i++) {
scan(P[i]);
}
recur2();
int cnt = 0;
set<int> s;
for(int i: reconstruct(N,T)) {
total[i-1] = cnt;
cnt+=P[i-1];
s.insert(i);
// cout << i << endl;
}
// for(int i = 0; i <= N; i++) {
// for(int j = 0; j <= T; j++) {
// cout << table[i][j] << " ";
// }
// cout << endl;
// }
cnt = 0;
for(int i = 0; i < N; i++) {
if(s.find(i+1) == s.end()){
cout << cnt << " ";
cnt+=P[i];
continue;
}
cout << total[i] << " ";
}
}