sandeep03edu

Good Key, Bad Key

Jul 13th, 2022 (edited)
133
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.  
  4. #pragma GCC target ("avx2")
  5. #pragma GCC optimization ("O3")
  6. #pragma GCC optimization ("unroll-loops")
  7. #define ll  long long
  8.  
  9. void solve();
  10.  
  11. int main() {
  12.     ios_base::sync_with_stdio(false);
  13.     cin.tie(NULL);
  14.     cout.tie(NULL);
  15.  
  16.     ll t;
  17.     t=1;
  18.     cin>>t;
  19.     while(t--){
  20.         solve();
  21.     }
  22.  
  23.     return 0;
  24. }
  25.  
  26. ll getAns(ll i, vector<ll> vec, ll &k, ll &n, ll bad, vector<vector<ll>> &dp){
  27.     if(i>=n){
  28.         return 0;
  29.     }
  30.    
  31.     if(dp[i][bad]!=-1){
  32.         return dp[i][bad];
  33.     }
  34.    
  35.     ll t = vec[i];
  36.     for(ll a=0; a<bad; ++a){
  37.         t/=2;
  38.     }
  39.    
  40.     ll choice1 = getAns(i+1, vec, k, n, bad, dp) + t - k;
  41.    
  42.    
  43.     ll choice2 = getAns(i+1, vec, k, n, bad+1, dp) + t/2;
  44.    
  45.     return dp[i][bad] = max(choice2, choice1);
  46. }
  47.  
  48. void solve(){
  49.     ll n, k; cin>>n>>k;
  50.     vector<ll> vec(n);
  51.     vector<vector<ll>> dp(n, vector<ll> (n, -1));
  52.    
  53.     for(ll i=0; i<n; ++i){
  54.         cin>>vec[i];
  55.     }
  56.    
  57.     ll ans = getAns(0, vec, k, n, 0, dp);
  58.     cout<<ans<<endl;
  59. }
Add Comment
Please, Sign In to add comment