Hydracody

Untitled

Jul 31st, 2023
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define endl "\n"
  4. #define LagaKar ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
  5. #define pb push_back
  6. #define pf push_front
  7. #define pob pop_back
  8. #define pof pop_front
  9. #define mp make_pair
  10. #define fi first
  11. #define se second
  12. #define precise(ans)  cout<<fixed<<setprecision(15)<<ans
  13. #define fo(i,n) for(ll i=0;i<n;i++)
  14. #define Fo(i,k,n) for(ll i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  15. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  16. #define sz(x) ((ll)(x).size())
  17. #define all(x) x.begin(), x.end()
  18. #define PI 3.1415926535897932384626
  19. #define MOD 1000000007
  20. #define MOD1 998244353
  21. typedef long long ll; typedef unsigned long long ull; typedef long double lld; typedef pair<ll, ll>  pii; typedef pair<ll, ll>    pl; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<vvi>    vvvi; typedef vector<ll>  vl; typedef vector<vl>  vvl; typedef vector<pii> vpii; typedef vector<pl>  vpl; template <typename T> using prq_mx  = priority_queue<T>; template <typename T> using prq_mn = priority_queue<T, vector<T>, greater<T>>;
  22. const double eps = 1e-9; const ll INF = (ll) 1e9; const ll inf64 = 2e18; const ll INF64 = 9e18;
  23. #ifndef ONLINE_JUDGE
  24. #define debug(x) cerr << #x <<" "; _prll(x); cerr << endl;
  25. #else
  26. #define debug(x)
  27. #endif
  28. void _prll(ll t) {cerr << t;} void _print(ll t) {cerr << t;} void _prll(string t) {cerr << t;} void _prll(char t) {cerr << t;} void _prll(lld t) {cerr << t;} void _prll(double t) {cerr << t;} void _prll(ull t) {cerr << t;} template <class T, class V> void _prll(pair <T, V> p); template <class T> void _prll(vector <T> v); template <class T> void _prll(set <T> v); template <class T, class V> void _prll(map <T, V> v); template <class T> void _prll(multiset <T> v); template <class T, class V> void _prll(pair <T, V> p) {cerr << "{"; _prll(p.fi); cerr << ","; _prll(p.se); cerr << "}";} template <class T> void _prll(vector <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T> void _prll(set <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T> void _prll(multiset <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T, class V> void _prll(map <T, V> v) {cerr << "[ "; for (auto i : v) {_prll(i); cerr << " ";} cerr << "]";}
  29. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. /////////////////////////////////////////////////////////////////////////////////////////////////
  38.  
  39. const ll N=21;
  40. ll dp[1ll<<21];
  41. ll n;
  42.  
  43. ll rec(ll mask,vl& aa){
  44.   ll val=__builtin_popcount(mask);
  45.   if((n-val)<3)return 0;
  46.   if(dp[mask]!=-1)return dp[mask];
  47.   ll sum=0;
  48.   fo(i,n){
  49.     if((mask&(1ll<<i))==0){
  50.       sum+=aa[i];
  51.     }
  52.   }
  53.   queue<pl> q;
  54.   // vl cc;
  55.   ll s=0,ans=INF64,m1=mask;
  56.   fo(i,n+3){
  57.     ll j=(i%n);
  58.     if((mask&(1ll<<j))==0){
  59.       while(q.size()>=3){
  60.         pl x=q.front();
  61.         q.pop();
  62.         s-=x.fi;
  63.         m1^=(1ll<<x.se);
  64.       }
  65.       q.push({aa[j],j});
  66.       s+=aa[j];
  67.       m1|=(1ll<<j);
  68.       if(q.size()==3){
  69.         // cc.pb(s);
  70.         ans=min(ans,sum-s+rec(m1,aa));
  71.       }
  72.     }
  73.   }
  74.   // cerr<<val<<" "<<ans<<" "<<sum<<" "<<mask<<endl;
  75.   // fo(i,n){
  76.   //   if((mask&(1ll<<i))==0){
  77.   //     cerr<<i<<"-";
  78.   //   }
  79.   // }
  80.   // cerr<<endl;
  81.   // debug(cc);
  82.   return dp[mask]=ans;
  83.  
  84. }
  85.  
  86.  
  87. void  chal(){
  88.   cin>>n;
  89.   vl aa(n);
  90.   fo(i,n){
  91.     cin>>aa[i];
  92.   }
  93.   // debug(aa);
  94.   memset(dp,-1,sizeof(dp));
  95.   cout<<rec(0,aa)<<endl;
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115. int32_t main() {
  116.   LagaKar;
  117. #ifndef ONLINE_JUDGE
  118.   freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("Error.txt", "w", stderr);
  119. #endif
  120.   ll  t; t = 1;
  121.   // cin>>t;
  122.   for (ll i = 1; i <= t; i++) {
  123.     chal();
  124.   } return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment