Advertisement
IlidanBabyRage

bag.cpp

Jun 29th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX = 10001;
  8.  
  9. int n, m;
  10. int w[100];
  11. int mat[100001][101];
  12.  
  13. bool f(int mas, int j, int n);
  14.  
  15. int main(){
  16.        
  17.     cin >> n >> m;
  18.     for (int i = 0; i < n; i++)
  19.         cin >> w[i];
  20.     for (int i = 0; i <= m; i++)
  21.         for (int j = 0; j <= n; j++)
  22.             mat[i][j] = -1;
  23.  
  24.     // "j" is number of golden bars
  25.     for (int mas = 1; mas <= m; mas++){
  26.         for (int i = 0; i < n; i++)
  27.             if (w[i] == mas)
  28.                 mat[mas][1] = i;
  29.         for (int j = 2; j <= n; j++){
  30.             for (int i = 0; i < n; i++){
  31.                 if (mas - w[i] > 0 && mat[mas - w[i]][j - 1] != -1)
  32.                     if (f(mas, j, i)){
  33.                         mat[mas][j] = i;
  34.                         break;
  35.                     }
  36.             }
  37.         }
  38.     }
  39.  
  40.     bool check = true;
  41.     for (int i = 1; i <= n && check; i++)
  42.         check = mat[m][i] == -1;
  43.     if (check)
  44.         cout << "NO" << endl;
  45.     else
  46.         cout << "YES" << endl;
  47.  
  48.     return 0;
  49. }
  50.  
  51. bool f(int mas, int j, int n){
  52.     int tmp = w[n];
  53.     while (j > 0 && mas - w[n] > 0){
  54.         mas -= tmp;
  55.         j--;
  56.         if (mat[mas][j] == n)
  57.             return false;
  58.         tmp = w[mat[mas][j]];
  59.     }
  60.     return true;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement