from atcoder.segtree import SegTree N = int(input()) A = list(map(int,input().split())) def op(x,y): a = sorted([x[0], y[0], x[1], y[1]])[1] return (min(x[0], y[0]), a, max(x[2], y[2])) e = (10**10, 10**10, 0) T = SegTree(op, e, [(A[i], 10**18, A[i]) for i in range(N)]) ans = 0 for i in range(N): ans += T.max_right(i, lambda x: x[2] <= x[0] + x[1]) - (i + 1) print(ans)