#include #include #include using namespace atcoder; using mint = modint998244353; using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000000 struct Data{ long long M=-Inf,m0 = Inf,m1 = Inf; }; Data op(Data a,Data b){ Data c; c.M = max(a.M,b.M); array A = {a.m0,a.m1,b.m0,b.m1}; sort(A.begin(),A.end()); c.m0 = A[0],c.m1 = A[1]; return c; } Data e(){ return Data(); } int main(){ int N; vector a; long long ans = 0LL; cin>>N; a.resize(N); vector D(N); rep(i,N){ scanf("%lld",&a[i]); D[i] = Data(); D[i].M = a[i]; D[i].m0 = a[i]; } segtree seg(D); rep(i,N-1){ int ok = i+1,ng = N+1; while(ng-ok>1){ int mid = (ok+ng)/2; auto ret = seg.prod(i,mid); if(ret.M <= ret.m0+ret.m1)ok = mid; else ng = mid; } ans += ok - (i+1); } cout<