Advertisement
Guest User

Untitled

a guest
Oct 26th, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define endl '\n'
  5. #define all(var) var.begin(),var.end()
  6. const int N = 20;
  7. const int masksz = 1<<N;
  8. pair<int,int> dp[masksz];
  9. int W[N],po[N];
  10. void solve(){
  11.     int n,x;
  12.     cin>>n>>x;
  13.     for(int i = 0;i<n;++i)
  14.         cin>>W[i];
  15.     int sz = 1<<n;
  16.  
  17.     for(int mask = 1;mask<sz;++mask){
  18.         dp[mask] = {N,0};
  19.         for(int last = 0;last<n;++last){
  20.             if (!(mask&po[last]))
  21.                 continue;
  22.             int submask = mask^po[last];
  23.             if (dp[submask].second==0 || dp[submask].second+W[last]>x){
  24.                 dp[mask] = min(dp[mask],{dp[submask].first+1,W[last]});
  25.             }
  26.             else {
  27.                 dp[mask] = min(dp[mask],{dp[submask].first,dp[submask].second+W[last]});
  28.             }
  29.  
  30.         }
  31.     }
  32.     cout<<dp[sz-1].first<<endl;
  33. }
  34. int main() {
  35.     po[0] = 1;
  36.     for(int i = 1;i<N;++i)
  37.         po[i] = po[i-1]<<1;
  38.     ios_base::sync_with_stdio(0);
  39.     cin.tie(0);
  40.     int t = 1;
  41.     //cin>>t;
  42.     while(t--)
  43.         solve();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement