-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patha.cpp
45 lines (41 loc) · 1.06 KB
/
a.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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, C;
cin >> n >> C;
vector<int> W(n);
int sum = 0;
for (int& w : W) cin >> w, sum += w;
if (sum <= C) {
cout << sum << endl;
return 0;
}
sort(W.begin(), W.end());
vector<int> prefix(n);
prefix[0] = 0;
for (int i = 1; i < n; ++i) prefix[i] = prefix[i-1]+W[i-1];
vector<vector<bool>> T(n, vector<bool>(C+1, false));
for (int i = n-1; i >= 0; --i) {
if (i+1<n) for (int j = 1; j <= C; ++j) {
if (T[i+1][j]) T[i][j] = true;
if (j>=W[i] && T[i+1][j-W[i]]) T[i][j] = true;
}
T[i][0] = T[i][W[i]] = true;
}
for (int waste = W.back()-1; waste >= 0; --waste) {
auto itr = upper_bound(W.begin(), W.end(), waste);
int i = distance(W.begin(), itr);
int t = C-prefix[i]-waste;
#ifdef DEBUG
printf("waste=%d i=%d C(%d)-prefix(%d)-waste(%d)=%d\n", waste, i, C, prefix[i], waste, t);
#endif
if (t < 0) continue;
if (T[i][t]) {
cout << C - waste << endl;
return 0;
}
}
return 0;
}