SHARE
TWEET

uva 624

next129 May 16th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. vector<int>solv;
  5. int n,m,temp;
  6.  
  7. void func(int arr[22],vector<int>rakhbo,int i,int total){
  8.    // cout<<m<<endl;
  9.     if(total > n){
  10.         return;
  11.     }
  12.     if(i >= m){
  13.         if((n - total) >= 0 && (n-total) < temp){
  14.             temp = n-total;
  15.             solv.clear();
  16.             solv = rakhbo;
  17.         }
  18.         return;
  19.     }
  20.     func(arr,rakhbo,i+1,total);
  21.     total += arr[i];
  22.     rakhbo.push_back(arr[i]);
  23.     func(arr,rakhbo,i+1,total);
  24. }
  25.  
  26. int main()
  27. {
  28.     int arr[22];
  29.     vector<int>keep;
  30.     while(scanf("%d%d",&n,&m) == 2){
  31.         for(int i = 0; i < m; i++){
  32.             cin>>arr[i];
  33.         }
  34.         temp = 10000000;
  35.         for(int i = 0; i < m; i++){
  36.             keep.push_back(arr[i]);
  37.             func(arr,keep,i+1,arr[i]);
  38.             keep.clear();
  39.         }
  40.         int sum = 0;
  41.         for(int i = 0; i < solv.size(); i++){
  42.             cout<<solv[i]<<" ";
  43.             sum += solv[i];
  44.         }
  45.         cout<<"sum:"<<sum<<endl;
  46.         solv.clear();
  47.     }
  48.     return 0;
  49. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top