Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     ifstream in("zebughil.in");
  10.     ofstream out("zebughil.out");
  11.     int t = 1;
  12.     while(t--)
  13.     {
  14.         int n, g;
  15.         in >> n >> g;
  16.         vector<long long> v(n);
  17.         for(int i = 0; i < n; ++i)
  18.             in >> v[i];
  19.         vector<long long> dp(1 << n);
  20.         for(int state = 0; state < (1 << n); ++state)
  21.         {
  22.             long long sum = 0;
  23.             for(int i = 0; i < n; ++i)
  24.                 if((state & (1 << i)) != 0)
  25.                     sum += v[i];
  26.             if(sum <= g)
  27.                 dp[state] = 1;
  28.             else
  29.             {
  30.                 dp[state] = (1 << 62);
  31.                 for(int sub = (state - 1) & state; sub > 0; sub = (sub - 1) & state)
  32.                 {
  33.                     dp[state] = min(dp[state], dp[sub] + dp[state - sub]);
  34.                     if(state == (1 << n) - 1)
  35.                         cout << sub << " " << state-sub << "\n";
  36.                 }
  37.             }
  38.         }
  39.         out << dp[(1 << n) - 1] << "\n";
  40.     }
  41.     in.close();
  42.     out.close();
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement