Advertisement
_Sarvar_

Untitled

Jan 22nd, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define N 101
  3. #define INF 100005
  4.  
  5. using namespace std;
  6.  
  7. int n, m;
  8. int a[N], d[N][INF];
  9.  
  10. int main() {
  11. cin >> n >> m;
  12. for (int i = 1; i <= n; i++)
  13. cin >> a[i];
  14. for (int i = 0; i <= m; i++) {
  15. for (int j = 1; j <= n; j++) {
  16. d[j][i] = INF;
  17. }
  18. }
  19. sort(a + 1, a + 1 + n);
  20. d[0][0] = 0;
  21. d[1][a[1]] = 0;
  22. for (int i = 2; i <= n; i++) {
  23. for (int j = 0; j <= m; j++) {
  24. if (j - a[i] >= 0) {
  25. d[i][j] = min(d[i - 1][j], d[i - 1][j - a[i]] + 1);
  26. }
  27. else d[i][j] = d[i - 1][j];
  28. }
  29. }
  30. d[n][m] != INF ? cout << d[n][m] : cout << 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement