Advertisement
allia

монеты

Nov 20th, 2020
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include<iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.   int n, sum;
  9.   cin >> n >> sum;
  10.   int *arr = new int[n];
  11.  
  12.   for (int i = 0; i < n; i++)
  13.      cin >> arr[i];
  14.  
  15.   int *result = new int [sum+1];
  16.  
  17.   result[0] = 0;
  18.  
  19.   for(int s=1 ; s <= sum; ++s)
  20.   {
  21.     result[s] = INT16_MAX;
  22.     for (int i = 0; i < n; ++i)  
  23.       if (s - arr[i] >= 0)
  24.         result[s] = min(result[s], result[s - arr[i]] + 1);
  25.   }
  26.  
  27.   if (result[sum] != INT16_MAX)
  28.     cout << result[sum];
  29.   else cout << -1;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement