Problem:
 Score : 400 points    Problem Statement Takahashi has N sticks that are distinguishable from each other. The length of the i -th stick is L_i .  He is going to form a triangle using three of these sticks. Let a , b , and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:   a < b + c  b < c + a  c < a + b   How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.      Constraints  All values in input are integers.  3 \leq N \leq 2 \times 10^3  1 \leq L_i \leq 10^3         Input Input is given from Standard Input in the following format:  N  L_1  L_2  ...  L_N       Constraints Print the number of different triangles that can be formed.        Sample Input 1 4
3 4 2 1      Sample Output 1 1  Only one triangle can be formed: the triangle formed by the first, second, and third sticks.       Sample Input 2 3
1 1000 1      Sample Output 2 0  No triangles can be formed.       Sample Input 3 7
218 786 704 233 645 728 389      Sample Output 3 23

Solution (Python):
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))
L.sort()

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ans += max(0, bisect_left(L, L[i]+L[j]) - j - 1)
print(ans)

Solution (Python):
import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort()

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ans += max(0, bisect.bisect_left(L, L[i]+L[j]) - j - 1)
print(ans)

Solution (Python):
import bisect
N = int(input())
L = sorted([int(i) for i in input().split()])


ans = 0
for i in range(N-1):
    for j in range(i+1, N):
        c = bisect.bisect_left(L, L[i]+L[j])

        ans += max(0, c-j-1)

        
print(ans)

Solution (Python):
N = int(input())
L = [int(i) for i in input().split()]

from bisect import bisect_left

L.sort()

count = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        length = L[i] + L[j]
        idx = bisect_left(L, length)
        count += idx - j - 1

print(count)

Solution (Python):
import bisect
n=int(input()) 
L=list(map(int, input().split())) 
L.sort()

count = 0        
for i in range(n-2):
  a = L[i]
  for k in range(i+1,n-1):
    b = L[k]

    count += bisect.bisect_left(L, a + b) - k -1
print(count)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l2=sorted(l)
box=[0] 
ans=0

num=1
while num<l2[0]: 
    box.append(0)
    num+=1
    
now=l2[0]
bounum=1
for i in range(1,n):
    if l2[i]!=now:
        box.append(bounum)
        next=now+1
        while next<l2[i]:
            box.append(bounum)
            next+=1 
        now=l2[i]
        bounum+=1
    else:
        bounum+=1
if len(box)!=l2[-1]+1:
    box.append(bounum) 

for i in range(n-2):
    for j in range(i+1,n-1):
        c=l2[i]+l2[j]-1
        if c>l2[-1]:
            c=l2[-1]
        add=box[c]-(j+1)
        if add<0:
            add=0
        ans+=add

print(ans)

Solution (Python):
import bisect

n = int(input())
ls = list(map(int, input().split()))
ls.sort()

count = 0

for i in range(n - 2):
  a = ls[i]
  for j in range(i + 1, n - 1):
    b = ls[j]
    index = bisect.bisect_left(ls, a + b)
    count += index - j - 1
print(count)

Solution (Python):






from sys import stdin,setrecursionlimit
from bisect import bisect_left





setrecursionlimit(10**6)
input=stdin.readline

n=int(input().rstrip())
l=sorted([int(i) for i in input().split()])
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        m=bisect_left(l,l[i]+l[j])
        ans+=m-j-1
        
print(ans)

Solution (Python):
from bisect import bisect_left
N = int(input())
l = list(map(int, input().split()))
l.sort()
res = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        t = l[i] + l[j]
        idx = bisect_left(l, t)
        res += idx-j-1
print(res)

Solution (Python):





N = int(input())
Ln = list(map(int, input().split()))
Ln = sorted(Ln)


ret = 0
for a in range(2,N):
  c = 0
  for b in range(a-1,0,-1):
    if c >= b:
      continue
    else:
      while (c<b and Ln[a]>=Ln[b]+Ln[c]):
        c += 1
      ret += max(b-c,0)
print(ret)

Solution (Python):
import bisect
import copy

ans = 0
n = int(input())
l = [int(x) for x in input().split()]
l.sort()
for i in range(n-2):
  for j in range(i+1, n-1):
    a = l[i]
    b = l[j]
    m = bisect.bisect_left(l, a + b)
    ans += (m - j - 1)
print(ans)

Solution (Python):
import sys
import bisect
readline = sys.stdin.readline
N = int(readline())
L = list(map(int, readline().split()))
L.sort()

ans = 0
for i in range(N):
    for j in range(i, N):
        if i == j: continue
        ab = L[i] + L[j]
        to = bisect.bisect_left(L, ab, j, N)
        ans += to - 1 - j
print(ans)

Solution (Python):
import bisect
import copy



N = int(input())
L = [int(i) for i in input().split()]
L.sort()
ans = 0
for i in range(N):
    for j in range(i+1,N):
        Max = bisect.bisect_left(L,L[i]+L[j])
        ans += max(Max-(j+1),0)
print(ans)

Solution (Python):
from bisect import bisect_left
n = int(input())
ls = list(map(int, input().split()))
ls.sort()
count = 0
for i in range(n - 2):
  for j in range(i + 1, n - 1):
    bi = bisect_left(ls, ls[i] + ls[j])
    count += bi - j - 1
print(count)

Solution (Python):
import bisect
n = int(input())

li = list(map(int, input().split()))
li.sort()

count = 0

for i in range(n - 2):
  for j in range(i + 1, n - 1):
    bi = bisect.bisect_left(li, li[i] + li[j])
    count += bi - j - 1
print(count)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    L=list(map(int,input().split()))
    L.sort()
    ans=0
    for b in range(n):
        c=b+1
        for a in range(b):
            while(1):
                if(c>=n or L[a]+L[b]<=L[c]): break
                c+=1
            ans+=c-(b+1)
    print(ans)
resolve()

Solution (Python):
from sys import stdin
import bisect

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

li.sort()

point = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        bi = bisect.bisect_left(li,li[i]+li[j])
        point += bi-j-1
print(point)

Solution (Python):
from collections import Counter
n = int(input())
l = list(map(int, input().split()))
memo = [[0] for i in range(n)]
for j in range(n):
    c = Counter(l[j:])
    tmp = 0
    for i in range(1000):
        if c[i+1]:
            tmp  += c[i+1]
        memo[j].append(tmp)

ans = 0
for i in range(n):
    for j in range(i+1, n - 1):
        b = l[i]
        c = l[j]
        lo = abs(b - c)
        up = min(b + c, 1001)
        ans += memo[j+1][up-1] - memo[j+1][lo]
print(ans)

Solution (Python):
from bisect import bisect_left
N = int(input())
l = list(map(int, input().split()))
l.sort()
res = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        t = l[i] + l[j]
        idx = bisect_left(l, t)
        res += idx-j-1
print(res)

Solution (Python):
N = int(input())
l = [int(x) for x in input().split()]
l.sort(reverse=True)
 
ans = 0
 
for i in range(N-2):
  j = i + 1
  k = N - 1
  while j < k:
    if l[i] < l[j] + l[k]:
      ans += k - j
      j += 1
    else:
      k -= 1
print(ans)

Solution (Python):
from bisect import bisect_right
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(N):
    for j in range(i):
        m = L[i]-L[j]
        k = bisect_right(L, m)
        n = max(0, j-k)
        ans+=n
print(ans)

Solution (Python):
import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(1, N-1):
    for j in range(i+1, N):
        ans += i - bisect.bisect_right(L[:i], L[j]-L[i])
print(ans)

Solution (Python):
def solve():
    """
    基本方針:
        a <= b <= c で三角形を作る.
        a, b を決定して取りうる c を計算で求める(O(N ** 2)).
    """
    MAX_LENGTH = 10 ** 3
    N = input()
    L = [int(i) for i in input().split()]
    L.sort()

    
    O = [0] * (MAX_LENGTH + 1)
    for l in L:
        O[l] += 1
    O2 = [0] * (MAX_LENGTH + 1)
    for i in range(1, MAX_LENGTH + 1):
        O2[i] = O2[i - 1] + O[i]

    
    sum_count = 0
    for i in range(len(L)):
        a = L[i]
        for j in range(i + 1, len(L)):
            b = L[j]
            
            c_high = a + b - 1
            if c_high > MAX_LENGTH:
                c_high = MAX_LENGTH
            
            sum_count += O2[c_high] - (j + 1)  
            

    print(sum_count)


if __name__ == '__main__':
    solve()

Solution (Python):
from bisect import bisect_left as bi
n = int(input())
x = [int(i) for i in input().split()]
x,ans = sorted(x),0
for i in range(2,n):
    for j in range(i-1,0,-1):
        b = bi(x,x[i]-x[j]+1)
        if b<j: ans+=j-b
        else: break
print(ans)

Solution (Python):
n=int(input())
L_lst=sorted(list(map(int,input().split())))
cnt=0
for j in range(n-1,1,-1):
    i=j-1
    x=0
    while x<i:
        if L_lst[i]+L_lst[x]>L_lst[j]:
            cnt+=i-x
            i-=1
        else:
            x+=1
print(cnt)

Solution (Python):
from sys import stdin
import bisect

N = int(input())
L = list(map(int,stdin.readline().strip().split()))

L.sort()

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        idx = bisect.bisect_left(L,L[i]+L[j],j)
        ans += (idx-j-1)

print(ans)

Solution (Python):
from sys import stdin
import bisect

N = int(input())
L = list(map(int,stdin.readline().strip().split()))

L.sort()

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        idx = bisect.bisect_left(L,L[i]+L[j],j)
        ans += (idx-j-1)

print(ans)

Solution (Python):
from _bisect import bisect_left
from _bisect import bisect_right

n = int(input())
L = list(map(int, input().split()))
L = sorted(L)

cnt = 0

for a in range(n):
    for b in range(n):
        if a < b:
            c = bisect_left(L, L[a] + L[b], b)
            cnt += c - b - 1
print(cnt)

Solution (Python):
import bisect

def solve():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()

    cnt = 0
    for i in range(2,N):
        for j in range(i):
            shortest_len = L[i] - L[j] + 1
            index = bisect.bisect_left(L, shortest_len)
            if index < j:
                cnt += j - index
    
    print(cnt)

if __name__ == '__main__':
    solve()

Solution (Python):



def main():
    from bisect import bisect

    n = int(input())
    l = sorted(list(map(int, input().split())))
    ans = 0

    
    
    
    
    
    for i in range(n):
        for j in range(i + 1, n):
            ab = l[i] + l[j]
            right = bisect(l, ab - 1)
            left = j + 1
            ans += right - left

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
num = int(input())
pole_list = sorted([int(item) for item in input().split()])

from bisect import bisect_left as bil

cnt = 0

for i in range(num):
    for j in range(i + 1, num):
        cnt += bil(pole_list, pole_list[i] + pole_list[j]) - j - 1
print(cnt)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
c=0
for i in range(n-1,0,-1):
  l = 0
  r = i - 1
  while(l < r):
    if(a[l] + a[r] > a[i]):
      c += r - l
      r -= 1
    else:
      l += 1
print(c)

Solution (Python):
import bisect

N=int(raw_input())
LN=map(int, raw_input().split())

LN.sort()

r = 0
for i in xrange(N-1, 1, -1):
    a = LN[i]
    for j in xrange(i-1, 0, -1):
        b = LN[j]
        if a > 2*b:
            break
        idx = bisect.bisect_left(LN, a-b+1)
        if idx < j:
            r += j - idx

print r

Solution (Python):
n = int(input())
l = list(map(int, input().split()))

l.sort(reverse=True)

ans = 0


for i in range(n - 2):
    
    j = i + 1
    
    k = n - 1

    while j < k:
        
        
        if l[i] < l[j] + l[k]:
            ans += k - j
            j += 1
        
        else:
            k -= 1

print(ans)

Solution (Python):
from bisect import bisect_left
 
N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()
 
sum = 0

for i in range(N):
    for j in range(i + 1, N):
        sum += bisect_left(Lmat, Lmat[i]+Lmat[j]) - (j + 1)
        
print(sum)

Solution (Python):
import numpy as np
n = int(input())

l = np.array(list(map(int,input().split())))

l.sort()
l = l[::-1]
ans = 0
for i in range(n-2):
    j = i+1
    k = n-1
    while j < k:
        if l[i] < l[j] + l[k]:
            ans += k - j
            j += 1
        else:
            k -= 1
print(ans)

Solution (Python):
from bisect import bisect_right, bisect_left
n = int(input())
l = list(map(int, input().split()))
count = 0
l.sort()
for i in range(n-2):
    for j in range(i+1, n-1):
        count += bisect_left(l, l[i]+l[j]) -j -1
print(count)

Solution (Python):
from bisect import bisect_left, bisect_right

N = int(input())
L = list(map(int, input().split()))

L.sort()
ans = 0

for a in range(N):
    for b in range(a + 1, N):
        c = bisect_left(L, L[a] + L[b])
        ans += c - b - 1
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l.sort()
l.reverse()
count=0
for i in range(n-2):
  j=i+1
  k=n-1
  while j<k:
    if l[i]<l[j]+l[k]:
      count+=k-j
      j+=1
    else:
      k-=1
print(count)

Solution (Python):
N=int(input())
S=list(map(int,input().split( )))
S = sorted(S,reverse=True)
tri_num=0
for i in range(N-2):
    j = i+1
    k = N-1
    while k > j:
        if S[i] < S[j] + S[k]:
            tri_num += k-j
            j+=1
        else:
            k-=1
print(tri_num)

Solution (Python):
import sys
import bisect
N=int(input())
d=sorted(list(map(int,input().split())))
ans=0
for i in range(1,len(d)-1):
  for j in range(i+1,len(d)):
    v=(N-j)-(bisect.bisect_left(d,d[-i]-d[-j]+1))
    if v<=0:
      pass
    else:
      ans+=v
print(ans)

Solution (Python):
N=int(input())
S=list(map(int,input().split( )))
S = sorted(S)
tri_num=0
for i in range(N-2):
    k = i+2
    for j in range(i+1,N-1):
        while k < N:
            if S[i] + S[j] <= S[k]:
                tri_num = tri_num + k-j-1
                if k == i+2:
                    k+=1
                break
            if k == N-1:
                tri_num = tri_num + k-j
                break
            k+=1
print(tri_num)

Solution (Python):
import time
from bisect import bisect_left
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)


res = 0
for i in range(n):
    for j in range(i+1,n):
        ab = l[i] + l[j]
        c_index = bisect_left(l, ab)
        
        res += c_index - j - 1
print(res)

Solution (Python):
import bisect
N=int(input())
L=list(map(int,input().split()))
l=sorted(L)
result=0
for i in range(N-2):
    for j in range(i+1,N-1):
        result+=bisect.bisect_left(l,l[i]+l[j])-j-1
print(result)

Solution (Python):
import bisect

N = int(input())
L = list(map(int, input().split()))

L.sort()

buff = []



for i in range(N):
    l_b = []
    for j in range(i+1, N):
        l_b.append(L[i]+L[j])
        
    if i != N-1:
        buff.append(l_b)




ans = 0
_len = len(buff)

for i in range(_len):
    for j in range(len(buff[i])):
        index = bisect.bisect_left(L[i+j+1+1:], buff[i][j])
        
        
        ans += index
print(ans)

Solution (Python):
import bisect
n = int(input())
l = list(map(int, input().split()))
count = 0
l = sorted(l)

for i in range(n-2):
  for j in range(i+1, n-1):
    k = bisect.bisect_left(l, l[i]+l[j])
    count += k -j - 1
print(count)

Solution (Python):
import bisect
n=int(input())
l = list(map(int,input().split()))
l.sort()
うんち = 0
for i in range(1,n):
    for j in range(i):
        p = l[i] + l[j]
        q = bisect.bisect_left(l,p)
        うんち += q-i-1
print(うんち)

Solution (Python):

import bisect

N = int(input())
L = list(map(int,input().split()))

count = 0
L.sort()
for i in range(N-2):
    for j in range(i+1,N-1):
        k = bisect.bisect_left(L,L[i]+L[j])
        count += k - j - 1 
        
print(count)

Solution (Python):
from bisect import bisect_left
from itertools import combinations

n = int(input())
l = sorted(map(int, input().split()))
ans = 0

for (i1, a), (i2, b) in combinations(enumerate(l[:-1]), 2):
    i3s = i2 + 1
    i3e = bisect_left(l, a + b, i3s)
    ans += i3e - i3s

print(ans)

Solution (Python):
import bisect

N=int(input())
L=list(map(int, input().split()))
ll=len(L)
L.sort()

s=0
for i in range(ll):
    for j in range(i+1,ll):
        s+=bisect.bisect_right(L,L[i]+L[j]-1)-j-1

print(s)

Solution (Python):
from bisect import bisect_left
from itertools import combinations

n = int(input())
l = sorted(map(int, input().split()))
ans = 0

for (i1, a), (i2, b) in combinations(enumerate(l[:-1]), 2):
    i3s = i2 + 1
    i3e = bisect_left(l, a + b, i3s)
    ans += i3e - i3s

print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right
from itertools import combinations

n = int(input())
l = sorted(map(int, input().split()))
ans = 0

for (i1, a), (i2, b) in combinations(enumerate(l[:-1]), 2):
    i3s = bisect_right(l, b - a, i2 + 1)
    i3e = bisect_left(l, a + b, i3s)
    ans += i3e - i3s

print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right
from itertools import combinations

n = int(input())
l = sorted(map(int, input().split()))
ans = 0

for l1, l2 in combinations(enumerate(l[:-1]), 2):
    (i1, a), (i2, b) = l1, l2
    i3s = bisect_right(l, b - a, i2 + 1)
    i3e = bisect_left(l, a + b, i3s)
    ans += i3e - i3s

print(ans)

Solution (Python):
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
sum = 0
for i in range(0,N):
  L_1st= L[i]
  for j in range(i+1,N):
    L_2nd = L[j]
    c_min = L_2nd-L_1st+1
    c_max = L_2nd+L_1st-1
    l = bisect.bisect_left(L,c_min,lo = j+1)
    if l!= N:
      r = bisect.bisect_right(L,c_max,lo = j+1)
      if r:
        r = r-1
        if r>= l:
          sum += r-l+1
print(sum)

Solution (Python):

N = int(input())
L = list(map(int, input().split()))


import heapq
import bisect

L = [-l for l in L]
heapq.heapify(L)

sum = 0
while len(L) > 2:
    largest = heapq.heappop(L)
    L.sort()
    for i in range(len(L)-1):
        p = bisect.bisect_left(L, largest-L[i])
        if p-(i+1) >0:
            sum += p-(i+1)
        
print(sum)

Solution (Python):
n=int(input())
listl = [int(i) for i in input().split()]

"""listl.sort(reverse=True)


	ans=0
for i in range(n-2):
	for j in range(i+1,n-1):
		if listl[j]<=(listl[i]/2):
			break
		for k in range(j+1,n):

			if listl[i]<listl[j]+listl[k]:
				ans=ans+1
			else:
				break	

print(ans)"""
from bisect import bisect_right				 
listl.sort()

ans=0

for i in range(n-1,1,-1):
	a=listl[i]
	for j in range(i-1,0,-1):
		b=listl[j]
		c=bisect_right(listl,a-b)
		
		num = j-c
		if num>0:
			ans+=num
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

n = int(input())
l = sorted(map(int, input().split()))
ans = 0
for i1 in range(n - 2):
    for i2 in range(i1 + 1, n - 1):
        a = l[i1]
        b = l[i2]
        i3s = max(bisect_right(l, b - a), i2 + 1)
        i3e = bisect_left(l, a + b)
        ans += i3e - i3s
print(ans)

Solution (Python):

import bisect

n=int(raw_input())
l=map(int,raw_input().split())
l.sort()
res=0
for i in range(n):
 for j in range(i+1,n):
  k=l[i]+l[j]
  res+=max(0,bisect.bisect_left(l,k)-j-1)
print res

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))

arr.sort(reverse=True)
count = 0
for i in range(n-2):
    j = i+1
    k = n-1
    while j < k:
        if arr[i] < arr[j] + arr[k]:
            count += k-j
            j += 1
        else:
            k -= 1
print(count)

Solution (Python):
n = int(input())
l = [int(_) for _ in input().split()]
ans = 0
L = [0 for _ in range(1,2000+2)]
s = [0 for _ in range(0,2000+2)]
for k in range(0, n):
    L[l[k]-1] += 1
for x in range(0,2001):
    s[x+1] = s[x] + L[x]
 
for i in range(0, n-1):
    for j in range(i+1, n):
        exc = 0
        m = abs(l[i] - l[j])
        M = l[i] + l[j]
        if l[i] > m:
            exc += 1
        if l[j] > m:
            exc += 1
        ans += s[M] - s[m] - L[M-1] - exc
print(ans//3)

Solution (Python):
from bisect import bisect_left

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

sum = 0
for i in range(N):
    for j in range(i+1,N):
        sum+= bisect_left(Lmat, Lmat[i]+Lmat[j]) - j - 1
        x = 1 + 1
print(sum)

Solution (Python):
from bisect import bisect_left

N = int(input())
Lmat = [int(n) for n in input().split()]
Lmat.sort()

sum = 0
for i in range(N):
    for j in range(i+1,N):
        sum+= bisect_left(Lmat, Lmat[i]+Lmat[j]) - j - 1
print(sum)

Solution (Python):
import time
from bisect import bisect_left
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)

start = time.time()
res = 0
for i in range(n):
    for j in range(i+1,n):
        ab = l[i] + l[j]
        c_index = bisect_left(l, ab)
        res += c_index - j - 1
print(res)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
ans = 0
for i in range(n-2):
    j = i+1
    k = n-1
    while j < k:
        if l[i]<l[j]+l[k]:
            ans += k-j
            j += 1
        else:
            k -= 1
print(ans)

Solution (Python):
import bisect
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
ans = 0
 
for i in range(n-2):
    for j in range(i+1, n-1):
        idx = bisect.bisect_right(l, l[i]+l[j]-1, j+1)
        ans+=idx-(j+1)
print(ans)

Solution (Python):
import bisect
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        idx = bisect.bisect_right(l, l[i]+l[j]-1, j+1)
        ans+=idx-(j+1)
print(ans)

Solution (Python):
n = int(input())
l = [x for x in map(int, input().split())]
l = sorted(l)

cnt = 0
for i in range(n - 2):
    a = l[i]
    k = i + 2
    for j in range(i + 1, n - 1):
        b = l[j]
        while(l[k] < a + b):
            k += 1
            if k == n:
                break
            else:
                pass

        k -= 1
        cnt += k - j
print(cnt)

Solution (Python):
import bisect


def triangles():
    n = int(input())
    l = [int(v) for v in input().split()]
    l.sort()

    count = 0
    for i in range(n):
        for j in range(i + 1, n):
            c = l[i] + l[j]
            index = bisect.bisect_left(l, c)
            count += max(index - j - 1, 0)

    print(count)


if __name__ == '__main__':
    triangles()

Solution (Python):
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import bisect
import sys
input = sys.stdin.readline

setrecursionlimit(1000000)

dp = defaultdict(lambda x:-1)

def main():
	n = int(input())
	l = list(map(int,input().split()))
	l.sort()
	ans = 0
	for mx in reversed(range(1,n)):
		for sc in reversed(range(1,mx)):
			tr_mx = l[mx] - l[sc] + 1
			tr = bisect.bisect_left(l,tr_mx)
			if(sc - tr > 0):
				ans += sc-tr
	print(ans)

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(l):
	x = l.pop()
	y = l.pop()
	while(x%y!=0):
		z = x%y
		x = y
		y = z
	l.append(min(x,y))
	return gcd(l) if len(l) > 1 else l[0]

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]
main()

Solution (Python):
import bisect
n=int(input())
L=list(map(int,input().split()))
L.sort()
ans=0
for b in range(1,n-1):
    for c in range(b+1,n):
        ans+=b-bisect.bisect_right(L[:b],L[c]-L[b])
print(ans)

Solution (Python):
import bisect

n=int(input())
l=list(map(int,input().split()))

l.sort()
ans=0
for i in range(n-1,1,-1):
    for j in range(i-1,0,-1):
        c=bisect.bisect_right(l,l[i]-l[j])
        
        if j-c > 0:
            ans+=(j-c)
            
print(ans)

Solution (Python):
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))
count = 0
for b in range(n-2):
  for c in range(b+1, n-1):
    count += max(0, bisect.bisect_left(l, l[b]+l[c])-c-1)

print(count)

Solution (Python):
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import bisect
from heapq import heappop, heappush

import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
l = sorted(LIST())
res = 0

for i in range(n-2):
    a = l[i]
    for j in range(i+1, n-1):
        b = l[j]
        index = bisect.bisect_left(l,a+b)
        res += max(index-j-1,0)

print(res)

Solution (Python):
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))
count = 0
for b in range(n-2):
  for c in range(b+1, n-1):
    count += max(0, bisect.bisect_left(l, l[b]+l[c])-c-1)

print(count)

Solution (Python):
from bisect import bisect_left
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for b in range(N):
    for a in range(b):
        ans += bisect_left(L, L[a] + L[b])-(b+1)
print(ans)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for b in range(N):
    r = b + 1
    for a in range(b):
        while r<N and L[r]<L[a]+L[b]: r += 1
        ans += r-(b+1)
print(ans)

Solution (Python):
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for b in range(N):
    for a in range(b):
        ans += bisect.bisect_left(L, L[a] + L[b])-(b+1)
print(ans)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
cmax = L[-1] + L[-2]
csum = list(0 for _ in range(cmax+1))
for i in range(N): csum[L[i]] += 1
for i in range(1,cmax+1): csum[i] += csum[i-1]
ans = 0
for ib in range(N):
    b = L[ib]
    for ia in range(ib):
        a = L[ia]
        minL, maxL = abs(a-b), a+b
        ans += csum[maxL-1] - csum[minL]
        if minL < a < maxL: ans -= 1
        if minL < b < maxL: ans -= 1
print(ans//3)

Solution (Python):
from bisect import bisect_left

def main():
    n=int(input())
    L=list(map(int,input().split()))
    L.sort()
    ans=0
    for i in range(n-2):
        for j in range(i+1,n-1):
            ans+=bisect_left(L,L[i]+L[j])-j-1
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
N = int(input())
csum = list(0 for _ in range(2000+1))
L = []
cmax1, cmax2 = 0, 0
for l in map(int,input().split()):
    L.append(l)
    csum[l] += 1
    if cmax2 < l: cmax2 = l
    if cmax1 < l: cmax1, cmax2 = l, cmax1
for i in range(1,cmax1+cmax2+1): csum[i] += csum[i-1]
ans = 0
for ib in range(N):
    b = L[ib]
    for ia in range(ib):
        a = L[ia]
        minL, maxL = abs(a-b), a+b
        ans += csum[maxL-1] - csum[minL]
        if minL < a < maxL: ans -= 1
        if minL < b < maxL: ans -= 1
print(ans//3)

Solution (Python):
from bisect import bisect_left

N = int(input())
L = list(map(int, input().split()))

L.sort()
result = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        result += bisect_left(L, L[i] + L[j]) - j - 1
print(result)

Solution (Python):
N = int(input())
L = sorted(list(map(int, input().split())))
res = 0
for i in range(N - 1, 1, - 1):
    j = i - 1
    k = 0
    while j > k:
        if L[i] < L[j] + L[k]:
            res += j - k
            j -= 1
        else:
            k += 1
print(res)

Solution (Python):
N = int(input())
L = sorted(list(map(int, input().split())))
res = 0
for i in range(N - 1, 1, - 1):
    j = i - 1
    k = 0
    while j > k:
        while j > k and L[i] >= L[j] + L[k]:
            k += 1
        res += j - k
        j -= 1
print(res)

Solution (Python):
import itertools
import heapq
from bisect import bisect_left


def heapsort(iterable):
    h = []
    for value in iterable:
        heapq.heappush(h, value)
    return [heapq.heappop(h) for i in range(len(h))]

N = int(input())
L = list(map(int, input().split()))
L.sort()


answer = 0
for i, a in enumerate(L[0:N-2]):
    for j, b in enumerate(L[i+1:N-1]):
        index = bisect_left(L, a+b)
        answer += max(index - (j+i+1) -1, 0)
        
print(answer)

Solution (Python):
import bisect
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans =0
for i in range(1,n):
    for j in range(i):
        p = l[i]+ l[j]
        q = bisect.bisect_left(l,p)
        ans += q-i-1

print(ans)

Solution (Python):

import sys, bisect
sys.setrecursionlimit(300000)


def solve(N: int, L: "List[int]"):
    ret = 0
    L.sort()
    for i in range(N):
        for j in range(i + 1, N):
            mx = L[i] + L[j] - 1
            r = bisect.bisect_right(L, mx)
            cnt = max(0, r - j - 1)
            ret += cnt
    print(ret)
    return

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    L = [int(next(tokens)) for _ in range(N)]  
    solve(N, L)

if __name__ == '__main__':
    main()

Solution (Python):

def main():
    
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = 0
    for i in range(N - 1, 1, -1):
        x, ng, ok = L[i], 0, i - 1
        while ng < ok:
            if L[ng] + L[ok] > x:
                ans += ok - ng
                ok -= 1
            else:
                ng += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
n=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    k=bisect.bisect_left(L,L[i]+L[j],j+1)-1
    ans+=k-j
print(ans)

Solution (Python):
from bisect import bisect_left,bisect_right
import sys
def input(): return sys.stdin.readline().strip()
n=int(input())
ll=sorted(list(map(int,input().split())))
c=0
lll=len(ll)
for i in range(lll-2):
    for j in range(i+1, lll-1):
        uk = bisect_left(ll, ll[i] + ll[j], j+1)
        lk = bisect_right(ll, abs(ll[j] - ll[i]), j+1, uk)
        c+=uk-lk
print(c)

Solution (Python):
def find(edges):
    import bisect
    edges=sorted(edges)
    ans=0
    for i in range(len(edges)):
        for j in range(i+1,len(edges)):
            temp=edges[i]+edges[j]
            pos=bisect.bisect_left(edges,temp)
            ans+=max(0,pos-1-j)
            
    return ans

input()
print(find(list(map(int,input().strip().split(" ")))))