Advertisement
limimage

prediduchaya

Sep 25th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using ld = long double;
  7. using pii = pair<int, int>;
  8.  
  9. constexpr int N = 1e4+5;
  10.  
  11. int n, m, weight[N];
  12. int dp[N];
  13.  
  14. void Solve()
  15. {
  16. cin >> n >> m;
  17. fill(dp, dp+N, INT32_MAX);
  18. for (int i = 1; i <= n; i++)
  19. cin >> weight[i];
  20. dp[0] = 0;
  21. for (int i = 1; i <= n; i++)
  22. for (int j = m; j >= weight[i]; j--)
  23. if (dp[j - weight[i]] != INT32_MAX)
  24. dp[j] = min(dp[j - weight[i]] + 1, dp[j]);
  25. cout << (dp[m] == INT32_MAX ? 0 : dp[m]);
  26. }
  27.  
  28. int main()
  29. {
  30. ios::sync_with_stdio(false);
  31. cin.tie(nullptr);
  32. cout.tie(nullptr);
  33. Solve();
  34. return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement