-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva624.cpp
More file actions
79 lines (59 loc) · 1.33 KB
/
uva624.cpp
File metadata and controls
79 lines (59 loc) · 1.33 KB
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
#include<bits/stdc++.h>
using namespace std;
int n, k;
vector<int> a;
vector<int> solution, best_solution;
int check_sum(vector<int> solution){
int sum = 0;
for(int i=0; i<solution.size(); ++i){
if(solution[i]>0)
sum += solution[i];
}
return sum;
}
bool can_push(int index, vector<int> solution){
return check_sum(solution)+a[index] < n;
}
void print_solution(vector<int> solution){
for(int i=0; i<solution.size(); ++i)
cout<<solution[i]<<" ";
cout<<endl;
}
bool solve(int label, vector<int> solution, int current_max){
cout<<"sum = "<< check_sum(solution)<<endl;
if(check_sum(solution)>n)
return false;
if(check_sum(solution) > current_max){
//cout<<"here\n";
current_max = check_sum(solution);
best_solution = solution;
if(current_max == n)
return true;
}
cout << "sol = ";
print_solution(solution);
for(int i=label; i<a.size(); ++i){
if(can_push(i, solution)){
solution.push_back(a[i]);
if(solve(label+1, solution, current_max))
return true;
cout<<"he2\n";
cout << "s1 = ";
print_solution(solution);
solution.erase(solution.begin()+solution.size()-1);
cout << "s2 = ";
print_solution(solution);
}
}
return false;
}
int main(){
int temp;
cin>>n>>k;
for(int i=0; i<k; ++i){
cin>>temp;
a.push_back(temp);
}
solve(0, solution, 0);
print_solution(best_solution);
}