_Sarvar_

Задача №3091. Минимум предметов

Jan 22nd, 2020
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 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.     for (int i = 1; i <= n; i++) {
  21.         for (int j = 0; j <= m; j++) {
  22.             if (j - a[i] >= 0) {
  23.                 d[i][j] = min(d[i][j], d[i - 1][j - a[i]] + 1);
  24.             }
  25.             else d[i][j] = min(d[i - 1][j], d[i][j]);
  26.         }
  27.     }
  28.     d[n][m] != INF ? cout << d[n][m] : cout << 0;
  29. }
Add Comment
Please, Sign In to add comment