using System; using System.Collections.Generic; using System.Collections; using System.Collections.Specialized; using System.Linq; using System.Text; using System.IO; using System.Reflection; using static System.Math; using System.Numerics; using System.Threading; using System.Runtime.CompilerServices; using System.Diagnostics; static class Program{ const int mod=(int)1e9+7; static void Main(){ Sc sc=new Sc(); var n=sc.I; var a=sc.Ia; long ans=0; St st=new St(a); for(int i = 0;i>1; public struct Dt{ public int d1,d2,d3; public Dt(bool b){d1=d2=inf;d3=int.MinValue>>1;} public Dt(int d1){this.d1=d3=d1;d2=int.MaxValue>>1;} public static Dt operator+(Dt a,Dt b){ a.d2=Max(Min(Min(a.d2,b.d2),a.d1),Min(Min(a.d2,b.d2),b.d1)); a.d1=Min(a.d1,b.d1); a.d3=Max(a.d3,b.d3); return a; } public override string ToString()=>"("+d1.ToString()+" "+d2.ToString()+" "+d3.ToString()+")"; } private Dt[] dat; private int n,cnt; public St(int[] a){ n=a.Length; cnt=n<<1; dat=new Dt[cnt]; for(int i = n;i0;i--) {dat[i]=dat[i<<1]+dat[(i<<1)+1];} } public void Ud(int q,int b){ q+=n; dat[q].d1=b; while(q>1){dat[q>>=1]=dat[q<<1]+dat[(q<<1)+1];} } public Dt Get(int l,int r){ Dt x1=new Dt(true),x2=new Dt(true); l+=n;r+=n; while(l<=r){ if((l&1)==1){x1+=dat[l++];} if((r&1)==0){x2=dat[r--]+x2;} l>>=1;r>>=1; } return x1+x2; } public int Bsl(int l){ var x=new Dt(true); int r=cnt-1; l+=n; var sk=new Stack(); while(l<=r){ if((l&1)==1){ var y=x+dat[l]; if(y.d1+y.d2>=1;r>>=1; } while(sk.Count>0){ l=sk.Pop(); var y=x+dat[l]; if(y.d1+y.d2=y.d3){x=y;l++;} } return l-n; } } public class Sc{ public int I{get{return int.Parse(Console.ReadLine());}} public long L{get{return long.Parse(Console.ReadLine());}} public double D{get{return double.Parse(Console.ReadLine());}} public string S{get{return Console.ReadLine();}} public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}} public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}} public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}} public string[] Sa{get{return Console.ReadLine().Split();}} public object[] Oa{get{return Console.ReadLine().Split();}} public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}} public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);} public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);} public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}} public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);} public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);} public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}} public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);} public T[] Arr(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(int n,Func f){var a=new T[n];for(int i=0;i(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]));} public (T,T,T) Tp3(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} public (T1,T1,T2) Tp3(){var s=Console.ReadLine().Split();return (Ct(s[0]),Ct(s[1]),Ct(s[2]));} private T Ct(string s){return (T)Convert.ChangeType(s,typeof(T));} }