#include #include using namespace std; using namespace atcoder; const long long INF_LL = 2000000000000000000LL; const int INF = 2000000000; const long long MOD = 1000000007; #define ll long long #define all(x) x.begin(), x.end() #define REP(i, a, b) for(int i = a; i < b; i++) #define rep(i, n) REP(i, 0, n) // typedef float double; // typedef priority_queue prique; typedef pair P; typedef vector vi; typedef vector vvi; typedef vector

vp; typedef vector vl; typedef vector matrix; int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; int sign[2] = {1, -1}; template bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; } template bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; } ll modpow(ll a, ll b, ll m) { if(b == 0) return 1; ll t = modpow(a, b / 2, m); if(b & 1) { return (t * t % m) * a % m; } else { return t * t % m; } } struct edge { int to; ll cost; edge(int t, ll c) { to = t, cost = c; } }; typedef vector> graph; // using mint = modint998244353; P op1(P x, P y){ if(x.first < y.first){ if(x.second < y.first){ return {x.first, x.second}; }else{ return {x.first, y.first}; } }else{ if(y.second < x.first){ return {y.first, y.second}; }else{ return {y.first, x.first}; } } } P e1(){return {INF, INF};} int op2(int x, int y){return max(x, y);} int e2(){return 0;} int main(){ cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; vi a(n); rep(i, n) cin >> a[i]; segtree seg1(n); segtree seg2(a); ll ans = 0; rep(i, n){ seg1.set(i, {a[i], INF}); } rep(i, n){ int l = i, r = n + 1; while(r - l > 1){ int m = (l + r) / 2; P tmp = seg1.prod(i, m); if(tmp.first + tmp.second >= seg2.prod(i, m)){ l = m; }else{ r = m; } } ans += l - i - 1; } cout << ans << endl; return 0; }