Advertisement
deadwing97

ICECREM

Sep 28th, 2019
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MX = (1<<18);
  6.  
  7. int T;
  8.  
  9. struct item{
  10.     int P , W , V;
  11.     item(){}
  12. };
  13.  
  14. int main(){
  15.  
  16.     cin>>T;
  17.  
  18.     while(T--){
  19.  
  20.         int n; cin>>n;
  21.  
  22.         vector < item > v;
  23.  
  24.         for(int j = 1 ; j <= n ; j++){
  25.             item x;
  26.             cin>>x.W>>x.P>>x.V;
  27.             v.push_back(x);
  28.         }
  29.  
  30.  
  31.         if(n == 1){
  32.             cout<<v[0].V<<endl;
  33.             continue;
  34.         }
  35.  
  36.         vector < item > v1 , v2;
  37.  
  38.         for(int j = 0 ; j < n/2 ; j++) v1.push_back(v[j]);
  39.  
  40.         for(int j = n/2 ; j < n ; j++) v2.push_back(v[j]);
  41.  
  42.         vector < pair < long long , long long > > vec;
  43.  
  44.         for(int mask = 0 ; mask < (1<<(int)(v1.size())) ; mask++){
  45.             long long curT = 0 , curV = 0;
  46.             bool ok = 1;
  47.             for(int j = 0 ; ok && j < v1.size() ; j++){
  48.                 if( (mask & (1<<j)) == 0 ) continue;
  49.                 if(curT > v1[j].W)
  50.                     ok = 0;
  51.                 curT += v1[j].P;
  52.                 curV += v1[j].V;
  53.             }
  54.             if(!ok) continue;
  55.             vec.push_back({curT , -curV});
  56.         }
  57.  
  58.         sort(vec.begin() , vec.end());
  59.  
  60.         vector < pair < long long , long long > > ph;
  61.  
  62.         for(auto &pp : vec){
  63.             pp.second *= -1;
  64.             if(ph.empty()){
  65.                 ph.push_back(pp);
  66.             }
  67.             else{
  68.                 if(ph.back().second < pp.second)
  69.                     ph.push_back(pp);
  70.             }
  71.  
  72.         }
  73.  
  74.         long long ans = 0;
  75.  
  76.         for(int mask = 0 ; mask < (1<<( (int) (v2.size()))) ; mask++){
  77.             long long curT = 0 , curV = 0 , f = (1ll << 33);
  78.             bool ok = 1;
  79.             for(int j = 0 ; ok && j < v2.size() ; j++){
  80.                 if( (mask & (1<<j)) == 0 ) continue;
  81.                 if(curT > v2[j].W)
  82.                     ok = 0;
  83.                 f = min(f , 1ll * v2[j].W - curT);
  84.  
  85.                 curT += v2[j].P;
  86.                 curV += v2[j].V;
  87.             }
  88.             if(!ok) continue;
  89.             auto ind = upper_bound(ph.begin() , ph.end() , make_pair(f , (1ll << 62))) - ph.begin();
  90.             --ind;
  91.             ans = max(ans , curV + ph[ind].second);
  92.         }
  93.  
  94.  
  95.         cout<<ans<<endl;
  96.  
  97.  
  98.     }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement