1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| #include<bits/stdc++.h> #define int long long #define ull unsigned long long #define maxn 1000005 #define put() putchar('\n') #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> using namespace std; void read(int &x){ int f=1;x=0;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();} x*=f; } namespace Debug{ Tp void _debug(char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts void _debug(char* f,Ty x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,vector<Ty>& V){os<<"[";for(auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; #define fi first #define se second #define mk make_pair const int mod=1e9+7; int power(int x,int y=mod-2) { int sum=1; while (y) { if (y&1) sum=sum*x%mod; x=x*x%mod;y>>=1; } return sum; } int n,a[maxn],ls[maxn],rs[maxn],s[maxn]; int id[maxn],tot,suf[maxn],cnt,t[maxn]; int ans; void solve(void) { int i,j;cnt=tot=0;ans=0; read(n); for (i=0;i<=n+1;i++) suf[i]=a[i]=s[i]=t[i]=0; for (i=1;i<=n;i++) read(a[i]),ls[i]=rs[i]=s[i]=0,suf[i]=suf[i-1]+a[i]; int id=1; for (i=1;i<=n;i++) if (a[id]<a[i]) id=i; for (i=1;i<=n;i++) { while (tot&&a[s[tot]]<a[i]) ls[i]=s[tot],tot--; if (tot) rs[s[tot]]=i; s[++tot]=i; } int root=s[1],now=root; for (;now;now=ls[now]) t[++cnt]=now; for (i=2;i<=cnt;i++) ans+=(t[i-1]-t[i])*a[t[i]]-(suf[t[i-1]-1]-suf[t[i]-1]); for (i=2;i<=tot;i++) ans+=(s[i]-s[i-1])*a[s[i]]-(suf[s[i]]-suf[s[i-1]]); int sum=ans; for (i=2;i<=cnt;i++) { for (j=t[i]+1;j<t[i-1];j++) ans=min(ans,sum-(a[t[i]]-a[j])); } for (i=2;i<=tot;i++) { for (j=s[i-1]+1;j<s[i];j++) ans=min(ans,sum-(a[s[i]]-a[j])); } t[cnt+1]=0; for (i=2;i<=cnt;i++) { int res=0; res-=(t[i-1]-t[i])*a[t[i]]-(suf[t[i-1]-1]-suf[t[i]-1])+(t[i]-t[i+1])*a[t[i+1]]-(suf[t[i]-1]-suf[t[i+1]-1]); int tmp=t[i],now=rs[t[i]],las=t[i-1]; for (now;now&&a[now]>a[t[i+1]];now=ls[now]) res+=(las-now)*a[now]-(suf[las-1]-suf[now-1]),las=now; now=t[i+1];res+=(las-now)*a[now]-(suf[las-1]-suf[now-1]); res+=a[t[i]]-a[t[i+1]]; ans=min(ans,sum+res); t[i]=tmp; } s[tot+1]=n+1; for (i=2;i<=tot;i++) { int res=0; res-=(s[i]-s[i-1])*a[s[i]]-(suf[s[i]]-suf[s[i-1]])+(s[i+1]-s[i])*a[s[i+1]]-(suf[s[i+1]]-suf[s[i]]); int tmp=s[i],now=ls[s[i]],las=s[i-1]; for (now;a[now]>a[s[i+1]];now=rs[now]) res+=(now-las)*a[now]-(suf[now]-suf[las]),las=now; now=s[i+1];res+=(now-las)*a[now]-(suf[now]-suf[las]),las=now; res+=(a[s[i]]-a[s[i+1]]); ans=min(ans,sum+res); s[i]=tmp; } int res=0; a[0]=a[n+1]=1e9; if (id==1||id==n) a[id]=1; else a[id]=min(a[ls[id]],a[rs[id]]); tot=cnt=0; for (i=1;i<=n;i++) ls[i]=rs[i]=s[i]=0,suf[i]=suf[i-1]+a[i]; id=1; for (i=1;i<=n;i++) if (a[id]<a[i]) id=i; for (i=1;i<=n;i++) { while (tot&&a[s[tot]]<a[i]) ls[i]=s[tot],tot--; if (tot) rs[s[tot]]=i; s[++tot]=i; } root=s[1],now=root; for (;now;now=ls[now]) t[++cnt]=now; for (i=2;i<=cnt;i++) res+=(t[i-1]-t[i])*a[t[i]]-(suf[t[i-1]-1]-suf[t[i]-1]); for (i=2;i<=tot;i++) res+=(s[i]-s[i-1])*a[s[i]]-(suf[s[i]]-suf[s[i-1]]); ans=min(ans,res); printf("%lld\n",ans); } signed main(void){ int T; read(T); while (T--) solve(); return 0; }
|