- #include <bits/stdc++.h>
- using namespace std;
- vector <int> length, ans, anss;
- int n, track, visit[25], sum, f, minn, sum2;
- void start(int index){
- if(sum == n){
- f = 1;
- int sz2 = ans.size();
- for(int i = 0; i < sz2; i++)cout << ans[i] << " ";
- cout << "sum:" << n << endl;
- return;
- }
- if(sum > n)return;
- for(index; index < track; index++){
- if(!visit[index]){
- visit[index] = 1;
- ans.push_back(length[index]);
- sum += length[index];
- if(sum < n){
- if(minn > (n-sum)){
- minn = n-sum;
- anss.clear();
- int sz = ans.size();
- for(int i = 0; i < sz; i++)anss.push_back(ans[i]);
- }
- }
- start(index+1);
- if(f)break;
- visit[index] = 0, ans.pop_back();
- sum -= length[index];
- }
- }
- }
- int main(){
- int mn;
- while(cin >> n >> track){
- sum = 0, f = 0, minn = 123456789, sum2 = 0;
- memset(visit, 0, sizeof(visit));
- for(int i = 1; i <= track; i++)cin >> mn, length.push_back(mn);
- start(0);
- if(!f){
- for(int i = 0; i < anss.size(); i++)cout << anss[i] << " ", sum2 += anss[i];
- cout << "sum:" << sum2 << endl;
- }
- length.clear(), ans.clear(), anss.clear();
- }
- return 0;
- }
Friday, April 24, 2015
UVa - 624 - CD
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment