Advertisement
georgiy110802

Рюкзак 0-1

Dec 22nd, 2021
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.54 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7.     int n, m;
  8.     cin >> n >> m;
  9.  
  10.     vector<int> a(n);
  11.     for (int i = 0; i < n; ++i) {
  12.         cin >> a[i];
  13.     }
  14.  
  15.     vector<vector<bool>> dp(n + 1, vector<bool>(m + 1));
  16.     dp[0][0] = true;
  17.  
  18.     for (int i = 1; i <= n; ++i) {
  19.         for (int j = 0; j <= m; ++j) {
  20.             dp[i][j] = dp[i - 1][j];
  21.             if (a[i - 1] <= j && dp[i - 1][j - a[i - 1]]) dp[i][j] = true;
  22.         }
  23.     }
  24.  
  25.     cout << (dp[n][m] ? "YES\n" : "NO\n");
  26.     return 0;
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement