Guest User

Untitled

a guest
Oct 22nd, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 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.     }
  25.    
  26.     for(int i=0; i<n; ++i)
  27.     {
  28.         for(int j=sum; j>0; --j)
  29.         {
  30.             if(res[j])
  31.             {
  32.                 if(res[j+arr[i]])
  33.                 {
  34.                     res[j+arr[i]] = min(res[j+arr[i]], 1+res[j]);
  35.                 }
  36.                 else
  37.                 {
  38.                     res[j+arr[i]] = res[j] + 1;
  39.                 }
  40.             }
  41.         }
  42.         res[arr[i]]=1;
  43.     }
  44.    
  45.     cout << res[sum];
  46.     //system("pause");
  47.     return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment