n = int(input()) a = list(map(int, input().split())) +[10**18] ans = 0 L = 1 for l in range(n-1): R = n while R-L>1: m = (R+L)//2 tmp = sorted(a[l:m+1]) if tmp[-1] <= tmp[0]+tmp[1]: L = m else: R = m ans += L - l print(ans)