-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathele.cpp
82 lines (77 loc) · 1.55 KB
/
ele.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
#include <bits/stdc++.h>
using namespace std;
int N;
int a[300];
int sum = 0;
vector<int> dp[100001];
bool vis[100001];
bool solve(int mid){
int csum = 0;
for(int i = 0; i < N; i++){
if(mid - a[i] <= sum/2){
csum += a[i];
}
}
if(csum >= mid){
return true;
}else{
return false;
}
}
bool solve2(int mid){
fill(vis, vis+mid+1, false);
vis[0] = true;
for(int i = 0; i < N; i++){
if(mid - a[i] > sum/2){
continue;
}
for(int j = mid; j >= 0; j--){
if(j >= a[i] && !vis[j] && vis[j-a[i]]){
vis[j] = true;
}
}
}
return vis[mid];
}
signed main(){
cin >> N;
for(int i = 0; i < N; i++){
cin >> a[i];
sum += a[i];
}
int lo = 0, hi = sum, mid = 0;
while(lo < hi){
mid = lo + (hi-lo)/2;
if(solve(mid)){
lo = mid+1;
}else{
hi = mid-1;
}
}
while(solve(mid)){
mid++;
}
mid--;
while(!solve2(mid)){
mid--;
}
fill(vis, vis+mid+1, false);
vis[0] = true;
for(int i = 0; i < N; i++){
if(mid - a[i] > sum/2){
continue;
}
for(int j = mid; j >= 0; j--){
if(j >= a[i] && !vis[j] && vis[j-a[i]]){
dp[j] = dp[j-a[i]];
dp[j].push_back(i+1);
vis[j] = true;
}
}
}
cout << dp[mid].size() << endl;
for(int i : dp[mid]){
cout << i << " ";
}
cout << endl;
}