n = int(input()) a = list(map(int, input().split())) t = 1 ans = 0 l = 0 while l < n-1: M = sorted(a[l:l+t+1])[-1] m1 = sorted(a[l:l+t+1])[0] m2 = sorted(a[l:l+t+1])[1] while l + t+1 <= n-1: t += 1 x = M y = m1 z = m2 if a[l+t] > x: M = a[l+t] if y <= a[l+t] <= z: m2 = a[l+t] if a[l+t] < y: m2 = y m1 = a[l+t] if M <= m1 + m2: continue if M > m1 + m2: break if l + t == n-1: ans += t l += 1 t -= 1 else: ans += t -1 l += 1 if t-1 == 1: t = 1 else: t = t -2 print(ans)