Advertisement
IISergeyII

Untitled

May 17th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <math.h>
  5. #include <vector>
  6. #include <fstream>
  7.  
  8. using namespace std;
  9. void Backtracking(int NCoin);
  10.  
  11. static int CoinsSet[16];
  12. static int N, M;
  13. static vector<int> coins;
  14. static int a[17];
  15.  
  16. static int MinAmount = 1000000001, CurAmount;
  17. static int Sum = 0;
  18.  
  19. int AmountCoins(int A[])
  20. {
  21.     MinAmount = M + M + 1;
  22.     CurAmount = 0;
  23.     Sum = 0;
  24.     Backtracking(1);
  25.     //if (MinAmount > M + M)
  26.     //    MinAmount = 0;
  27.     return MinAmount;
  28.  
  29. }
  30.  
  31. void Backtracking(int NCoin)
  32. {
  33.     if (NCoin > M) return;
  34.     if (Sum > N) return;
  35.  
  36.     for (int i = 0; i <= 2; i++)
  37.     {
  38.         if (Sum == N)
  39.         {
  40.             if (CurAmount < MinAmount)
  41.                 MinAmount = CurAmount;
  42.         }
  43.         Backtracking(NCoin + 1);
  44.         CurAmount++;
  45.         Sum += Sum + a[NCoin];
  46.     }
  47.     CurAmount -= 3;
  48.     Sum -= 3*a[NCoin];
  49.  
  50. }
  51.  
  52. int main()
  53. {
  54.  
  55.     int Summ = 0;
  56.     cin >> N >> M;
  57.  
  58.     for (int i = 0; i < M; ++i) {
  59.         cin >> a[i];
  60.         Summ += a[i];
  61.     }
  62.  
  63.     if (Summ + Summ < N) {
  64.         cout << "-1";
  65.     } else {
  66.         cout << AmountCoins(a);;
  67.     }
  68.  
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement