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, bisect_right
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):
        idx = bisect_left(l, l[i] + l[j])
        ans += idx - (j+1)

print(ans)

Solution (Python):
import bisect


N = int(input())
SL = [int(i) for i in input().split()]
SL.sort()
ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ab = SL[i] + SL[j]
        r = bisect.bisect_left(SL, ab)
        l = j + 1
        ans += max(0, r - l)
print(ans)

Solution (Python):
import bisect

def f():
    N = int(input())
    SL = [int(i) for i in input().split()]
    SL.sort()
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            ab = SL[i] + SL[j]
            r = bisect.bisect_left(SL, ab)
            l = j + 1
            ans += max(0, r - l)
    print(ans)

if __name__ == "__main__":
    f()

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 += bisect_left(L, L[i] + L[j])  - j - 1
print(ans)

Solution (Python):
import bisect

def f():
    N = int(input())
    SL = [int(i) for i in input().split()]
    SL.sort()
    ans = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            ab = SL[i] + SL[j]
            r = bisect.bisect_left(SL, ab)
            l = j + 1
            ans += max(0, r - l)
    print(ans)

if __name__ == "__main__":
    f()

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

def solve():
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()
    total = 0
    for s in range(N-2):
        for m in range(s+1, N-1):
            l = bisect.bisect_left(L, L[s] + L[m], 0, N)
            if l > m: total += l - m - 1
    print(total)

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):


import bisect

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

sum = 0

length.sort()

for i in range(n):
    for j in range(n):

            a = length[i]
            b = length[j]

            if i < j:
                k = bisect.bisect_left(length, a + b)
                sum += k - j - 1

print(max(0, sum))

Solution (Python):
N = int(input())

SL = [int(i) for i in input().split()]
SL.sort()



import bisect

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ab = SL[i] + SL[j]
        
        
        r = bisect.bisect_left(SL, ab)
        l = j + 1
        
        
        
        ans += max(0, r - l)

print(ans)

Solution (Python):
N = int(input())
L = map(int, input().split())
SL = sorted(list(L))

import bisect

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ab = SL[i] + SL[j]
        
        
        r = bisect.bisect_left(SL, ab)
        l = j + 1
        
        
        
        ans += max(0, r - l)

print(ans)

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):
        l=L[i]+L[j]
        c=bisect_left(L,l)
        ans+=max(0,c-j-1)
print(ans)

Solution (Python):
from bisect import bisect_right
N = int(input())
L = sorted(list(map(int, input().split())))
L = [(l, i) for i, l in enumerate(L)]

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        l1_value, l1_index = L[i]
        l2_value, l2_index = L[j]
        ans += max(0, (bisect_right(L, (l1_value + l2_value, l2_index))) - j - 1)

print(ans)

Solution (Python):
N = int(input())
L = map(int, input().split())
SL = sorted(list(L))

import bisect

ans = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        ab = SL[i] + SL[j]
        
        
        r = bisect.bisect_left(SL, ab)
        l = j + 1
        
        
        
        ans += max(0, r - l)

print(ans)

Solution (Python):
from bisect import bisect_right

if __name__ == "__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):
            k = bisect_right(L, L[i] + L[j] - 1)
            ans += max(k - j - 1, 0)

    print(ans)

Solution (Python):
from collections import Counter
import bisect 

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

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

print(ans)

Solution (Python):
import bisect
N=int(input())
L=sorted(list(map(int,input().split())))
count=0
for d1 in range(N-2):
    for d2 in range(d1+1,N-1):
        m=bisect.bisect_left(L,L[d1]+L[d2])
        if m>d2+1:count+=m-(d2+1)
print(count)

Solution (Python):
import bisect
n = int(input())
L = sorted([int(i) for i in input().split()])
ans = 0
for b in range(n):
    for a in range(b):
        ab = L[a] + L[b]
        r = bisect.bisect_left(L,ab)
        l = b+1
        ans += max(0,r-l)
print(ans)

Solution (Python):
import bisect

n = int(input())

length = list(map(int, input().split()))
length.sort()
ans = 0
for i in range(n-2):  
    a = length[i]
    for j in range(i+1,n-1):
        b = length[j]
        if i >= j:
            continue
        else:
            max_bar = bisect.bisect_left(length,a+b)
            ans += max_bar - j - 1
            
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import itertools
import bisect

def main():
    N = int(input())
    L = [int(x) for x in input().split()]
    ans = 0
    L_sorted = sorted(L)
    for i in range(N - 2):
        a = L_sorted[i]
        for j in range(i + 1, N - 1):
            b = L_sorted[j]
            r = bisect.bisect_left(L_sorted, a + b, lo=j + 1)
            ans += r - j - 1

    print(ans)



    

if __name__ == '__main__':
    main()

Solution (Python):
import bisect

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

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

Solution (Python):
from bisect import bisect_left

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

ans = 0
l.sort()

for i in range(n):
    for j in range(i + 1, n):
        a = l[i]
        b = l[j]
        idx = bisect_left(l, a + b)
        

        ans += idx - (j + 1)


print(ans)

Solution (Python):
from bisect import bisect_left

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

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

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

N = int(input())
*L, = map(int, input().split())
L = sorted(L)
ans = 0
for i, j in combinations(range(N), 2): 
    idx = bisect_left(L, L[i] + L[j])
    ans += idx - j - 1
print(ans)

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):
    a = L[i]
    for j in range(i + 1, N - 1):
        ans += bisect_left(L, a + L[j])  - j - 1
        
print(ans)

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

lptr = [-1] * 1001
lptr[0] = n - 1
for i in range(1, 1001):
  maxptr = n -1
  minptr = 0
  fixed = [-1] * n
  cptr = (maxptr + minptr + 1) // 2

  while fixed[cptr] == -1:


    if l[cptr] > i:
      fixed[cptr] = 0
      minptr = cptr
    else:
      fixed[cptr] = 1
      maxptr = cptr
    cptr = (maxptr + minptr + 1) // 2
      
  lptr[i] = cptr - fixed[cptr]



    
cnt = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    tgt = l[i] - l[j]
    if lptr[tgt] > j:
      cnt += lptr[tgt] - j

print(cnt)

Solution (Python):
import math
import collections
import itertools
import sys
import bisect
from heapq import heappop,heappush,heapify
sys.setrecursionlimit(10**6)
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7


n = INT()
l = MAP()

l.sort()
ans=0
for i in range(n):
    for j in range(i+1,n):
        idx = bisect.bisect_left(l,l[i]+l[j])
        ans+=idx-j-1

print(ans)

Solution (Python):
import bisect
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+2, N):
            a = L[i]
            c = L[j]
            k = bisect.bisect_left(L, c-a+1)
            if k <= i:
                ans += j-i-1
            else:
                ans += j-k
    return ans
 
if __name__ == '__main__':
    print(main())

Solution (Python):
from bisect import bisect_left

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

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

Solution (Python):
import sys
inputs = sys.stdin.readlines()
n = int(inputs[0])
L = list(map(int, inputs[1].split()))
A = sorted(L)
 
ans = 0
for i in range(n-1, 0, -1):
    l, r = 0, i-1
    while l < r:
        if A[i] < A[l] + A[r]:
            ans += r-l
            r -= 1
        else:
            l += 1

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 - 1, 1, -1):
    for j in range(i - 1, 0, -1):
        idx = bisect_left(ls[:j], ls[i] - ls[j] + 1)
        if idx < j:
            count += j - idx

print(count)

Solution (Python):
from bisect import bisect_left


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

    ans = N * (N - 1) * (N - 2) // 6
    L.sort()

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            b = bisect_left(L, L[i] + L[j])
            if b != N:
                ans -= N - b

    print(ans)


if __name__ == '__main__':
    p_d()

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

cnt = 0
for i in range(N):
    a = l[i]
    for j in range(i+1, N):
        b = l[j]
        cnt += bisect_left(l[j+1:], a+b)
print(cnt)

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

L.sort()


from bisect import bisect_right,bisect_left

ans = 0
for i, a in enumerate(L[:-2]):
    for j, b in enumerate(L[i+1:-1]):
        j=i+1+j
        
        
        
        
        
        
        l = bisect_left(L, abs(a-b),j+1)
        r = bisect_left(L, a+b,j+1)
        
        ans += r-l
print(ans)

Solution (Python):
import bisect
import copy
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))


N=one_int()
L=many_int()

L=sorted(L)

sankaku_dict={}
count=0
for i in range(len(L)-1):
    low=L[i]
    for j in range(i+1, len(L)):
        
        high = L[j]
        
        
        high_index = bisect.bisect_left(L[j+1:], high+low)
        count+=high_index


print(count)

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):
    m=bisect.bisect_right(l,l[j]-l[i])
    if i>m:
      ans+=i-m
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import itertools
import bisect

def main():
    N = int(input())
    L = [int(x) for x in input().split()]
    ans = 0
    L_sorted = sorted(L)
    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            a = L_sorted[i]
            b = L_sorted[j]
            l = bisect.bisect_left(L_sorted, max(a - b, b - a), lo=j + 1)
            r = bisect.bisect_left(L_sorted, a + b, lo=j + 1)
            ans += r - l

    print(ans)



    

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import bisect
N = int(input())
L = [i for i in map(int, input().split())]
ans = 0

L.sort()

if N == 3:
    if L[0] + L[1] > L[2]:
        print("1")
        sys.exit()

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

Solution (Python):
import sys
import bisect
N = int(input())
L = [i for i in map(int, input().split())]
ans = 0

L.sort()

if N == 3:
    if L[0] + L[1] > L[2]:
        print("1")
        sys.exit()

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

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

Solution (Python):
import bisect
N = int(input())
Ls = list(map(int, input().split()))
 
Ls = sorted(Ls)
cnt = 0
for i in range(N):
    l1 = Ls[i]
    for j in range(i+1, N):
        l2 = Ls[j]
        ind = bisect.bisect_left(Ls, l1 + l2)
        cnt += ind - j - 1
print(cnt)

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

def main():
    input = sys.stdin.readline
    N = int(input())
    L = list(map(int, input().split()))

    L = sorted(L)
    ans = 0
    for a, b in combinations(range(N), 2):
        a, b = min(a, b), max(a, b)

        c_max = L[a] + L[b]

        left = b + 1
        right = bisect_left(L, c_max)

        ans += right - left

    return ans

if __name__ == '__main__':
    print(main())

Solution (Python):
from bisect import*
_,l=open(0)
l=sorted(map(int,l.split()))
print(sum(j-bisect(l,a-l[j],0,j)for i,a in enumerate(l)for j in range(i)))

Solution (Python):
import itertools
import bisect

def D():
    n = input()
    l = map(int,input().split(" "))
    temp = itertools.combinations(l,3)
    count = 0
    
    
    for i in temp:
        i = sorted(list(i),reverse=True)
        if i[0] < i[1]+i[2]:
            count += 1
    print(count)

def D_sort():
    N = int(input())
    L = sorted([int(_) for _ in input().split()])
    
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            ans += bisect.bisect(L, L[a] + L[b] - 1) - b - 1
            
            
    print(ans)
if __name__ == '__main__':
    D_sort()

Solution (Python):
import bisect
import math

def main():
    n = int(input())
    l_s = list(sorted(map(int, input().split(' '))))

    ans = 0

    for c in range(2, n):
        for b in range(1, c):
            idx = bisect.bisect_right(l_s, l_s[c] - l_s[b])
            idx = min(b, idx)
            ans += b - idx
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
def p(*_a):
  _s=" ".join(map(str,_a))
  
  sys.stderr.write(_s+"\n")

from bisect import bisect_left  as bl
from bisect import bisect_right as br
from bisect import insort_left  as il    
from bisect import insort_right as ir

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

L.sort()

ans=0

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

print(ans)

Solution (Python):
from bisect import bisect_right

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

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

Solution (Python):

from sys import stdin
import bisect

N = int(stdin.readline())
L = [int(x) for x in stdin.readline().rstrip().split()]


L.sort()

s = 0

for i in range(N-2):
    for j in range(i+1, N-1):

        
        
        
        tmp = L[i] + L[j]

        
        

        index = bisect.bisect_left(L, tmp)

        
        
        s += (index - (j+1))

print(s)

Solution (Python):
import sys
import math
import copy

def main():
    n = int(input())
    l = [int(t) for t in input().split()]

    
    l.sort()
    
    count = 0
    
    for i in range(0, n):
        for j in range(i+1, n):
            lb, ub = j, n
            while ub - lb > 1:
                half = (lb + ub) // 2
                if l[half] < l[i] + l[j]:
                    lb = half
                else:
                    ub = half
            count += ub - (j + 1)

    print(count)

    return 0

if __name__ == '__main__':
    sys.exit(main())

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


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

"""
短いほうから２本固定して条件に合うものをさがす（長いほうから２本固定でもOK）
最長と最短を固定するとややこしくなるから、こういう場合は最長/最短どちらかから固定するほうがいいか
"""


ans = 0
for i in range(N-2):
    

    for j in range(i+1, N-1):
        
        
        
        
        k = bisect_left(L, L[i]+L[j])
        ans += k - j - 1



"""
ans = 0
for i in range(N):
    for j in range(i+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]
        
        il = bisect_right(L, l, lo=j)
        
        ir = bisect_left(L, r, lo=j)
        
        if il == N-1:
            break
        
        ans += ir - il - 1
"""

print(ans)

Solution (Python):
n = int(input())
ll = list(map(int, input().split()))
ll.sort(reverse=True)

ans = 0

for i in range(n-2):
    j = i + 1
    k = n - 1
    while k > j:
        if ll[i] < ll[j] + ll[k]:
            ans += k - j
            j += 1
        else:
            k -= 1

print(ans)

Solution (Python):
N = int(input())
Lines = list(map(int, input().split(" ")))
Lines.sort()
answer = 0
 
import bisect
 
for lineI in range(N):
    for lineJ in range(lineI+1,N):
    	answer += bisect.bisect_left(Lines,Lines[lineI]+Lines[lineJ])-lineJ-1
print(answer)

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

print(ans)

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):
    a = L[i]
    for j in range(i + 1, N - 1):
        result += bisect_left(L, a + L[j]) - j - 1
print(result)

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


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

"""
短いほうから２本固定して条件に合うものをさがす（長いほうから２本固定でもOK）
最長と最短を固定するとややこしくなるから、こういう場合は最長/最短どちらかから固定するほうがいいか
"""


ans = 0
for i in range(N-2):
    

    for j in range(i+1, N-1):
        
        
        
        k = bisect_left(L, L[i]+L[j])
        ans += k - j - 1



"""
ans = 0
for i in range(N):
    for j in range(i+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]
        
        il = bisect_right(L, l, lo=j)
        
        ir = bisect_left(L, r, lo=j)
        
        if il == N-1:
            break
        
        ans += ir - il - 1
"""

print(ans)

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

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):
from bisect import bisect_left
from bisect import bisect_right


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

"""
短いほうから２本固定して条件に合うものをさがす（長いほうから２本固定でもOK）
最長と最短を固定するとややこしくなるから、こういう場合は最長/最短どちらかから固定するほうがいいか
"""


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



"""
ans = 0
for i in range(N):
    for j in range(i+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]
        
        il = bisect_right(L, l, lo=j)
        
        ir = bisect_left(L, r, lo=j)
        
        if il == N-1:
            break
        
        ans += ir - il - 1
"""

print(ans)

Solution (Python):
import bisect

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

cout = 0 

for i in range(n-2): 
	o = list1[i] 
	for j in range(i+1,n-1): 
		p = list1[j] 

		b = o+p -1 

		d = bisect.bisect_right(list1[j+1:],b) 
		
		cout += d

print(cout)

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


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

"""
短いほうから２本固定して条件に合うものをさがす（長いほうから２本固定でもOK）
最長と最短を固定するとややこしくなるから、こういう場合は最長/最短どちらかから固定するほうがいいか
"""


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



"""
ans = 0
for i in range(N):
    for j in range(i+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]
        
        il = bisect_right(L, l, lo=j)
        
        ir = bisect_left(L, r, lo=j)
        
        if il == N-1:
            break
        
        ans += ir - il - 1
"""

print(ans)

Solution (Python):
def acinput():
    return list(map(int, input().split(" ")))

import bisect
import numpy as np

N=int(input())
L=acinput()
L=sorted(L)

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

        

        count+=(r-j-1)



print(count)

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

L.sort()

ans = 0

for a in range(N - 2):
    c = a + 1
    for b in range(a + 1, N - 1):
        while c < N - 1 and L[c + 1] < L[a] + L[b]:
            c += 1
        ans += c - 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,-1,-1):
        temp=L[i]-L[j]
        idx=bisect.bisect_right(L,temp)
        ans+=max(0,j-idx)
print(ans)

Solution (Python):

import sys
import bisect
import itertools


def solve(N: int, L: "List[int]"):
    L.sort()
    L = tuple(L)
    ans = 0
    ans = sum([bisect.bisect_left(L, a+L[j]) - j - 1
               for i, a in enumerate(L[:-1], 1) for j in range(i, N)])
    print(ans)
    return


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


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
import bisect

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









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

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

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

Solution (Python):
import bisect

ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
l = nl()

l = sorted(l)

ans = 0

for a in range(n):
    fl = 0
    A = l[a]
    for b in range(a+1,n):
        B = l[b]

        idx = bisect.bisect_left(l, A+B)
        ans += max(0,idx-b-1)

print(ans)

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

n = int(input())
l = list(map(int, input().split()))
ans = 0
l.sort()
for i in range(n-1, -1, -1):
    a = l[i]
    for j in range(i-1, -1, -1):
        b = l[j]
        c = bisect.bisect_right(l, a-b, lo=0, hi=j)
        ans = ans + j-c
print(ans)

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):
    s = l[i]+l[j]
    ans += bisect_left(l,s,j+1)-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.sort()

"""
短いほうから２本固定して条件に合うものをさがす（長いほうから２本固定でもOK）
最長と最短を固定するとややこしくなるから、こういう場合は最長/最短どちらかから固定するほうがいいか
"""


ans = 0
for i in range(N):
    for j in range(i+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]

        
        il = bisect_right(L, l, lo=j)
        
        ir = bisect_left(L, r, lo=j)
        
        if il == N-1:
            break
        
        ans += ir - il - 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):
    for j in range(n):
        if i<j:
            k=bisect.bisect_left(l,l[i]+l[j])
            ans+=k-j-1
print(ans)

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

ans = 0
for i in range(N-2):
  a = ls[i]
  for j in range(i+1, N-1):
    b = ls[j]
    pos = bisect.bisect_left(ls, a+b)
    ans += pos - j - 1
print(ans)

Solution (Python):
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right


def solve_d(n, l_list):
    res = 0
    l_list_s = sorted(l_list)
    for i in range(1, n - 1):
        b = l_list_s[i]
        for j in range(0, i):
            a = l_list_s[j]
            res += bisect_left(l_list_s, a + b) - (i + 1)

    return res


def solve_d_(n, l_list):
    res = 0
    l_list_s = sorted(l_list)
    h = []
    for i in range(1, n - 1):
        for j in range(0, i):
            heappush(h, l_list_s[i] + l_list_s[j])
        while len(h) > 0:
            a = heappop(h)
            if a > l_list_s[i + 1]:
                heappush(h, a)
                break
        res += len(h)

    return res


def main():
    n = int(input())
    l_list = list(map(int, input().split()))
    res = solve_d(n, l_list)
    print(res)


def test():
    assert solve_d(4, [3, 4, 2, 1]) == 1
    assert solve_d(3, [1, 1000, 1]) == 0
    assert solve_d(7, [218, 786, 704, 233, 645, 728, 389]) == 23


if __name__ == "__main__":
    test()
    main()

Solution (Python):
from bisect import bisect_left

if __name__ == "__main__":
	N = int(input())
	L = list(map(int, input().split()))
	L.sort()
	
	sum = 0
	for a in range(N - 2):
		for b in range(a + 1, N - 1):
			key = L[a] + L[b]
			i = bisect_left(L, key)
			
			sum += i - b - 1
	print(sum)

Solution (Python):
import sys
import bisect

n = int(input())
*l, = map(int, input().split())
l.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        c = bisect.bisect_left(l, l[i] + l[j], j+1)
        ans += c - j - 1

print(ans)

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] < 0:
            ans += k - j
            j += 1
        else:
            k -= 1

print(ans)

Solution (Python):
import bisect
N = int(input())
L_list = list(map(int,input().split()))
L_list.sort()
ans = 0
 
for i in range(N-2):
  for j in range(i+1,N-1):
    x = bisect.bisect_left(L_list,L_list[i]+L_list[j])
    ans += x-j-1
print(ans)

Solution (Python):
from itertools import combinations
import bisect
n = int(input())
l = list(map(int, input().split()))

l.sort()
cnt = 0
for i in range(n-1, 1, -1):
    for j in range(i-1, 0, -1):
        x = l[i] - l[j] + 1
        idx = bisect.bisect_left(l, x)
        if j > idx:
           cnt += j - idx
print(cnt)

Solution (Python):
import bisect

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

l_s = sorted(l)

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

Solution (Python):
import sys



def is_tr(a,b,c):
	if a < b+c:
		return True
	return False


def main():
	n = int(input())
	ll = list(map(int, input().split()))
	n=len(ll)
	ll.sort(reverse = True)
	pa(ll)
	
	ret = 0
	for i,l1 in enumerate(ll[:-2]):
		for j, l2 in enumerate(ll[i+1:-1]):
			pa(ret)
			if not is_tr(l1, l2, ll[j+1]):
				break
			if is_tr(l1,l2,ll[-1]):
				pa('ggg')
				ret += n-(i+1+j+1)
				continue
			ok=i+j+1
			ng=n-1
			while ng-ok > 1:
				mid = (ok+ng)//2
				if is_tr(l1,l2,ll[mid]):
					ok =mid
				else:
					ng = mid
			pa((ok,j+i+1))
			ret += (ok - (i+j+1))
	
	print(ret)
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
import bisect

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

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import*
_,l=open(0)
l=sorted(map(int,l.split()))
print(sum(j-bisect(l,a-l[j],0,j)for i,a in enumerate(l)for j in range(i)))

Solution (Python):
import bisect

n = int(input())
ls = [int(elem) for elem in input().split()]
ls = sorted(ls)

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