提交时间:2025-03-30 12:36:42
运行 ID: 37412
#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define pi pair<int,int> #define p1 first #define p2 second #define m_p make_pair #define p_b push_back typedef long long ll; using namespace std; const int maxn=2e5+10; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; } int n; struct node { int x,b; }; bool operator<=(node a,node b){ int c=min(a.b,b.b); a.b-=c,b.b-=c; ll va=a.x,vb=b.x; va<<=(ll)min(a.b,30),vb<<=(ll)min(b.b,30); return va<=vb; } const int N=2e4+10; pair<node,int> f[N],g[N],v[N]; void calc(pair<node,int>*f,pair<node,int>*g,int fn,int &gn){ int u=1,mn=1e9; up(i,1,gn){ while(u<=fn&&f[u].p1<=g[i].p1)mn=min(mn,f[u++].p2); g[i].p2+=mn; } mn=1e9;int top=0; up(i,1,gn){ if(g[i].p2<mn)v[++top]=g[i],mn=g[i].p2; } gn=top;up(i,1,gn)g[i]=v[i]; } void slv(){ n=read(); f[1]=m_p((node){0,0},0); int fn=1,gn=0; int m=200; up(i,1,n){ int x=read(); if(i&1)gn=0;else fn=0; int tot=0; int xx=x; while(1){ tot++;if(!xx)break;xx>>=1;} up(j,-tot,m){ int xx=x,c=0; if(i&1){ int P=gn+max(tot-max(-j,0),0);gn=P; while(1){if(j+c>=0)g[P--]=m_p((node){xx,j+c},j+c*2);if(!xx)break;xx>>=1,c++;} } else { int P=fn+max(tot-max(-j,0),0);fn=P; while(1){if(j+c>=0)f[P--]=m_p((node){xx,j+c},j+c*2);if(!xx)break;xx>>=1,c++;} } } //for(auto it:ve)cout<<it.p1.x<<" "<<it.p1.b<<" "<<it.p2<<endl; //cout<<endl; if(i&1)calc(f,g,fn,gn); else calc(g,f,gn,fn); } if(n&1)swap(f,g),swap(fn,gn); int res=1e9; up(i,1,fn)res=min(res,f[i].p2); cout<<res; } int main(){ //freopen("c.in","r",stdin),freopen("c.out","w",stdout); slv(); fclose(stdin),fclose(stdout); return 0; }