Iamtui1010

vcowflix

Nov 7th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. //#include<bits/stdc++.h>
  2. #include<iostream>
  3. #include<fstream>
  4. #include<vector>
  5. #include<algorithm>
  6.  
  7. #define long long long
  8. #define nln '\n'
  9.  
  10. const long N = 20;
  11.  
  12. using namespace std;
  13.  
  14. // GLobal variables: cow, m, n
  15.  
  16. fstream f1,f2;
  17.  
  18. inline void openf()
  19. {
  20.     f1.open("vcowflix.inp", ios:: in);
  21.     f2.open("vcowflix.out", ios:: out);
  22. }
  23.  
  24. inline void closef()
  25. {
  26.     f1.close();
  27.     f2.close();
  28. }
  29.  
  30. long m, n;
  31. vector<long> cow;
  32.  
  33. void data()
  34. {
  35.     f1.tie(0)->sync_with_stdio(0);
  36.     f2.tie(0)->sync_with_stdio(0);
  37.     cin.tie(0)->sync_with_stdio(0);
  38.     cin >> m >> n;
  39.     for (long i = 0; i != n; ++i)
  40.     {
  41.         long x;
  42.         cin >> x;
  43.         cow.push_back(x);
  44.     }
  45. }
  46.  
  47. long mav = 0, sum;
  48. vector<bool> ava;
  49.  
  50. void carry(long i)
  51. {
  52.     if (i == n)
  53.     {
  54.         if (sum <= m && sum > mav)
  55.             mav = sum;
  56.         return;
  57.     }
  58.     if (ava[i])
  59.     {
  60.         sum += cow[i];
  61.         ava[i] = 0;
  62.         carry(i+1);
  63.         ava[i] = 1;
  64.     }
  65.     sum -= cow[i];
  66.     carry(i+1);
  67. }
  68.  
  69. void process()
  70. {
  71.     ava.resize(n, 1);
  72.     carry(0);
  73. }
  74.  
  75. void view()
  76. {
  77.     cout << mav << nln;
  78. }
  79.  
  80. int main()
  81. {
  82.     openf();
  83.     data();
  84.     process();
  85.     view();
  86.     closef();
  87.     return 0;
  88. }
  89.  
Add Comment
Please, Sign In to add comment