Alex_tz307

CPH page 114

Sep 17th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     int X, N;
  7.     cin >> X >> N;
  8.     vector < int > w(N);
  9.     for(int& x : w)
  10.         cin >> x;
  11.     pair < int , int > dp[1 << N];
  12.     dp[0] = {1, 0};
  13.     for(int s = 1; s < (1 << N); ++s) {
  14.         dp[s] = {N + 1, 0};
  15.         for(int p = 0; p < N; ++p)
  16.             if(s & (1 << p)) {
  17.                 auto opt = dp[s ^ (1 << p)];
  18.                 if(opt.second + w[p] <= X)
  19.                     opt.second += w[p];
  20.                 else {
  21.                     ++opt.first;
  22.                     opt.second = w[p];
  23.                 }
  24.                 dp[s] = min(dp[s], opt);
  25.             }
  26.     }
  27.     cout << dp[(1 << N) - 1].first;
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment