Guest User

Untitled

a guest
Oct 21st, 2015
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef long double ld;
  7. typedef pair<ll, ll> pii;
  8. typedef pair<ld, ld> pdd;
  9.  
  10. int arr[10005];
  11. int res[10000005];
  12.  
  13. int main()
  14. {
  15.     //freopen("input.txt","r",stdin);
  16.     //freopen("output.txt","w",stdout);
  17.     //ios_base::sync_with_stdio(0);
  18.     int n, sum;
  19.     cin >> n >> sum;
  20.  
  21.     for(int i=0; i<n; ++i)
  22.     {
  23.         cin >> arr[i];
  24.         res[arr[i]] = 1;
  25.     }
  26.  
  27.     for(int i=0; i<n; ++i)
  28.     {
  29.         for(int j=sum; j>0; --j)
  30.         {
  31.             if(res[j])
  32.             {
  33.                 if(res[j+arr[i]])
  34.                 {
  35.                     res[j+arr[i]] = min(res[j+arr[i]], res[arr[i]]+res[j]);
  36.                 }
  37.                 else
  38.                 {
  39.                     res[j+arr[i]] = res[arr[i]]+res[j];
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     cout << res[sum];
  46.     //system("pause");
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment