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(sorted(map(int, input().split())))

res = 0

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

        res += k - j - 1

print(res)

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

Solution (Python):
from bisect import bisect_left

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

li.sort()

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a = li[i]
        b = li[j]
        idx = bisect_left(li, a+b)
        ans += idx - (j+1)
print(ans)

Solution (Python):


import bisect

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






count = 0

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

print(count)

Solution (Python):
from sys import stdin, stderr
from bisect import bisect_right, bisect_left


def main():
    N = int(input())
    L = sorted([int(x) for x in input().split()])
    ret = 0

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            c = L[i] + L[j] - 1
            k = bisect_right(L, c)
            
            ret += k - j - 1
    return ret


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

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

Solution (Python):
from sys import stdin, stderr
from itertools import groupby, permutations, combinations
from bisect import bisect_right
import numpy as np


def main():
    N = int(input())
    L = sorted([int(x) for x in input().split()])
    ret = 0
    
    
    
    
    
    
    

    for i in range(N - 2):
        for j in range(i + 1, N - 1):
            c = L[i] + L[j] - 1
            
            k = bisect_right(L, c)
            
            ret += k - j - 1
    return ret


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

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

Solution (Python):
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        lp = j+1
        rp = n-1
        while lp <= rp:
            mp = (lp+rp)//2
            if l[mp] < l[i] + l[j]:
                lp = mp+1
            else:
                rp = mp-1
        ans += lp-j-1
print(ans)

Solution (Python):
n = int(input())
l = [int(i) for i in input().split()]
l.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        lp = j+1
        rp = n-1
        while lp < rp:
            mp = (lp+rp)//2
            if l[mp] < l[i] + l[j]:
                lp = mp+1
            else:
                rp = mp-1
        if l[lp] < l[i] + l[j]:
            ans += lp-j
        else:
            ans += lp-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):
        n=L[i]+L[j]
        ans+=bisect.bisect_left(L,n)-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):
    for j in range(i+1, N-1):
        ans += bisect_left(L, L[i]+L[j]) - 1 - j
print(ans)

Solution (Python):
import bisect

def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = 0
    for ai in range(N-2):
        a = L[ai]
        for bi in range(ai+1,N-1):
            b = L[bi]
            cmax = a + b
            m_index = bisect.bisect_left(L,cmax)
            ans += max(m_index - bi - 1, 0)

    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
import bisect

N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0
for bi in range(1,N-1):
  for ai in range(0,bi):
    ci = bisect.bisect(L,L[bi]+L[ai]-1) - 1
    if bi<ci:
      ans += ci-bi
print(ans)

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

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

print(ans)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
l = sorted(l)
ans = 0
for i in range(n):
	    for j in range(i+1,n):
	    	    bottom  = j
	    	    top = n
	    	    while top -bottom > 1:
	    	    	    middle = (top+bottom)//2
	    	    	    if l[middle] <l[i]+l[j]:
	    	    	    	    bottom = middle
	    	    	    else:
	    	    	    	    top = middle
	    	    ans += bottom - j 
print(ans)

Solution (Python):
import bisect
import itertools

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

ans = 0
for ij in itertools.combinations(range(N), 2):
    i, j = ij
    a, b = L[i], L[j]

    cl = abs(a - b)
    cr = a + b
    l = bisect.bisect_right(L, cl)
    r = bisect.bisect_left(L, cr)
    l = max(l, j+1)
    if r - l > 0:
        ans += r - l
print(ans)

Solution (Python):
from bisect import bisect_left


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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
import bisect
stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
l = na()
l.sort()


c = 0
for i_1 in range(n-2):
    for i_2 in range(i_1+1, n-1):
        m = l[i_1] + l[i_2]
        i_3max = bisect.bisect_left(l, m) - 1
        c += i_3max - i_2

print(c)

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

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

    def canuse(x):
        index = bisect.bisect_left(A,x)
        return index
        
    ans = 0
    for i in range(N):
        for j in range(N-i-1):
            s = A[i]+A[i+j+1]
            ans += canuse(s)-(i+j+2)
            
    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):
        idx = bisect.bisect_left(l, l[i]+l[j])
        ans += max(0, idx - 1 - j)        






print(ans)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
L.sort()
result = 0
for x in range(N):
    z = x + 2
    for y in range(x+1, N):
        while z < N and L[x] + L[y] > L[z]:
            z += 1
        result += z - y - 1
print(result)

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

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

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

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

def binary_search(arr, low, high, x):
    while low < high:
        mid = (low + high) // 2
        if arr[mid] < x: low = mid+1
        else: high = mid

    return low


def rec_binary_search(arr, l, r, x):
    if r-l <= 0:
        return l
    
    else:
        mid = (l + r) // 2
        if arr[mid] == x:
            return mid
    
        elif arr[mid] > x:
            return binary_search(arr, l, mid-1, x)

        else:
            return binary_search(arr, mid+1, r, x)

ll.sort()
ans = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        c = ll[i] + ll[j]
        limit = bisect_left(ll, c)
        ans += limit - j - 1

print(ans)

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

from bisect import bisect_left as bl
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        ans+=bl(l,l[j]+l[i])-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):
    for j in range(i+1, N-1):
        ans += bisect_left(L, L[i]+L[j]) - 1 - j
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):
        ans += bisect_left(L, L[i]+L[j]) - 1 - j
print(ans)

Solution (Python):


def bsearch(l,n,k):
    ok = -1
    ng = n

    while ng-ok>1:
        mid = (ng+ok)//2
        if l[mid]<k:
            ok = mid
        else:
            ng = mid
    return ok

def f(l):
    n = len(l)
    l.sort()

    res = 0
    for i in range(n-1):
        for j in range(i+1,n):
            k = bsearch(l,n,l[i]+l[j])
            res += k-j

    return res

if __name__ == '__main__':
    n = int(input())
    l = list(map(int, input().split()))
    print(f(l))

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]) - 1 - j
print(ans)

Solution (Python):
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):
        ng=N
        ok=j
        while abs(ok-ng)!=1:
            med =(ok+ng)//2
            if L[med]<L[i]+L[j]:
                ok=med
            else:
                ng=med
        ans+=ok-j
print(ans)

Solution (Python):
import bisect

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

count = 0

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

Solution (Python):
import bisect

N = int(input())
L = [int(l) for l in input().split()]
L.sort()
S = 0
n = 0
S = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        l = L[i] + L[j]
        s = bisect.bisect_left(L, l)
        S += bisect.bisect_left(L, L[i] + L[j]) - j - 1

print(S)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def main():
    N = I()
    L = LI()
    L = sorted(L)
    cnt = 0
    for i in range(N-1):
        a = L[i]
        for j in range(i+1, N-1):
            b = L[j]
            ok = j + 1
            ng = N - 1
            
            if L[ok] >= a + b:
                ok = ng = j
            
            elif L[ng] < a + b:
                ok = ng = N - 1
            else:
                pass

            while ng - ok > 1:
                m = (ok + ng) // 2
                c = L[m]
                if c < a + b:
                    ok = m
                else:
                    ng = m
            cnt += (ok - j)
    print(cnt)
main()

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

Solution (Python):
import bisect
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):
        index = bisect.bisect_left(l,l[i]+l[j],j)
     
        count += index-j-1
print(count)

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

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

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

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

print(ans)

Solution (Python):
import bisect


def sort_list():
    _ = input()
    K = [int(x) for x in input().split()]
    ans = 0

    len_k = len(K)
    n_lst = sorted(K)

    for idx in reversed(range(2, len_k)):
        for diff_idx in range(1, idx):
            diff = n_lst[idx] - n_lst[diff_idx]
            insert_index = bisect.bisect_right(n_lst, diff)
            if insert_index < diff_idx:
                
                ans += diff_idx - insert_index
    print(ans)


if __name__ == "__main__":
    sort_list()

Solution (Python):
import bisect

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

Sum = 0
L.sort(reverse=False)

for i in reversed(range(2,N)):
    for j in reversed(range(1,i)):
        hoge = 0
        hoge = j - bisect.bisect(L, L[i] - L[j])
        if hoge < 0:
            hoge = 0
        Sum += hoge

print(Sum)

Solution (Python):
import bisect

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

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

print(ans)

Solution (Python):
n = int(input())
X = list(map(int, input().split()))
X.sort()
k=r=0
for i in range(n):
    k = i
    for j in range(i+1, n):
        while k<n and X[i]+X[j]>X[k]:
            k +=1
        r += k-j-1
print(r)

Solution (Python):
n = int(input())
xs = map(int, input().split())
xs = sorted(list(xs))
ts = []
for i in range(n):
    for j in range(i+1, n):
        ts.append(xs[i] + xs[j])
ts.sort()
i = 0
j = 0
r = 0
xs.append(10**18)
while True:
    if j == len(xs) or i == len(ts):
        break
    if ts[i] <= xs[j]:
        i += 1
        r += n-j
    else:
        j += 1
print((n*(n-1)*(n-2)//6-r))

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

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


def binarysearch(lista,b):
    left=0
    right=len(lista)
    while left<right:
        mid=(left+right)//2
        if lista[mid]>=b:
            right=mid
        else:
            left=mid+1
            mid+=1
    return mid

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

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

import bisect

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

Solution (Python):
import bisect

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):
                max_c = L[i] + L[j]
                count += bisect.bisect_left(L[j+1:], max_c)
print(count)

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

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):
        count += bisect.bisect(L,L[i]+L[j]-1)-j-1
print(count)

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):
        count += bisect.bisect(L,L[i]+L[j]-1)-j-1
print(count)

Solution (Python):
from collections import Counter
N = int(input())
L = Counter(int(i) for i in input().split())
maxL = max(L)
key = sorted(L.keys())
val = [L[i] for i in range(1, max(L) + 1)]
acc = [0] * (len(val) + 1)
for i in range(len(val)):
    acc[i + 1] = acc[i] + val[i]
cnt = 0
for a in key:
    cnt += L[a] * (L[a] - 1) * (L[a] - 2) // 6
    cnt += max(acc[min(2 * a - 1, maxL)] - acc[a], 0) * L[a] * (L[a] - 1) // 2
for i in range(len(key) - 1):
    for j in range(i + 1, len(key)):
        a, b = key[i], key[j]
        cnt += L[a] * L[b] * (L[b] - 1) // 2
for i in range(len(key) - 2):
    for j in range(i + 1, len(key) - 1):
        a, b = key[i], key[j]
        cnt += max(acc[min(a + b - 1, maxL)] - acc[b], 0) * L[a] * L[b]
print(cnt)

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):
    
    idx = bisect.bisect_left(L,L[c]-L[b]+1)
    
    ans += max(b-idx,0) if idx >= 0 else 0 
    

print(ans)

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):
    
    idx = bisect.bisect_left(L,L[c]-L[b]+1)
    
    ans += max(b-idx,0) if idx >= 0 else 0 
    

print(ans)

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):
    
    idx = bisect.bisect_left(L[:b],L[c]-L[b]+1)
    
    ans += max(b-idx,0) if idx >= 0 else 0 
    

print(ans)

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

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

res = 0

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

print(res)

Solution (Python):
from bisect import bisect_left
n = int(input())
l = list(map(int,input().split()))
l.sort()
cnt = 0

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

print(cnt)

Solution (Python):
import bisect
from bisect import bisect_left

n = int(input()) 
l = []
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):
        b=l[j]
        k = bisect_left(l,a+b)
        result += k-j-1

print(result)

Solution (Python):
import collections
import math
import operator as op
from functools import reduce

import numpy as np
import math
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+1, N-1):
            a = L[i]
            b = L[j]


            r = bisect.bisect_right(L, a+b-1) 

            ans += r - (j + 1)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import collections
import math
import operator as op
from functools import reduce

import numpy as np
import math
from bisect 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+1, N-1):

            ans += bisect(L,L[i]+L[j]-1)-j-1

    print(ans)


if __name__ == '__main__':
    main()

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

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(i+1,N):
        a = L[i]
        b = L[j]
        k = bisect.bisect_left(L,(a+b))-1
        ans += k-j
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from bisect import bisect_left
def resolve():
    n=int(input())
    L=list(map(int,input().split()))
    L.sort()

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

    print(ans)
resolve()

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

Solution (Python):
from bisect 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):
		ans+=bisect(L, L[i]+L[j]-1)-j-1
print(ans)

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

Solution (Python):
import bisect

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

    
    ans = 0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass

    print(ans)

if __name__ == "__main__":
    solve()

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-1):
    for j in range(i+1,N):
        ans += 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()

cnt=0
for i in range(N-1):
    for j in range(i+1,N):
        ind = bisect.bisect_left(l,l[i]+l[j])
        cnt += ind - j-1

        
        
        
        
        
        
        
        
print(cnt)

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):
        b = l[j]
        ans += bisect_left(l,a+b)-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):
        b = l[j]
        k = bisect_left(l,a+b)
        ans += k-j-1
print(ans)

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

print(ans)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.sort()
lt = [n] * (l[n-1] + l[n-2] + 1)
ans = 0
for i in range(l[0]+1):
    lt[i] = 0
length = l[0]
for i in range(1, n):
  if length < l[i]:
    for j in range(length+1, l[i]+1):
      lt[j] = i
    length = l[i]
for i in range(n):
  for j in range(i+1, n):
    right = l[i]+l[j]
    ans += max(0,  lt[right] - (j+1))
print(ans)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.sort()
lt = [n] * (l[n-1] + l[n-2] + 1)
ans = 0
for i in range(l[0]+1):
    lt[i] = 0
length = l[0]
for i in range(1, n):
  if length < l[i]:
    for j in range(length+1, l[i]+1):
      lt[j] = i
    length = l[i]
for i in range(n):
  for j in range(i+1, n):
    right = l[i]+l[j]
    ans += max(0,  lt[right] - (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):
  for j in range(i+1, n):
    left = l[j]-l[i]
    right = l[i]+l[j]
    ans += max(0, bisect_left(l, right) - (j+1))
print(ans)

Solution (Python):
from bisect import bisect
from bisect import bisect_left
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):
    left = l[j]-l[i]
    right = l[i]+l[j]
    ans += max(0, bisect_left(l, right) - max(j+1, bisect(l,left)))
print(ans)

Solution (Python):
def lower_bound(a, x):
  l = -1
  r = len(a)
  while r - l > 1:
    h = (l + r) // 2
    if a[h] < x:
      r = h
    else:
      l = h
  return l
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse=True)
res = 0
for i in range(n):
  for j in range(i+1, n):
    lb = lower_bound(l[j+1:], l[i]-l[j]+1)
    res += lb + 1
print(res)

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

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

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

print(count)

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):
        A, B = L[i], L[j]
        delta = A-B+1
        idx = bisect.bisect_left(L, delta)
        if idx >= j:
            break
        ans += j-idx

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):
    a = l[i]
    for j in range(i + 1, n):
        b = l[j]
        shita = max(a - b, b - a)
        ue = a + b
        ans += bisect.bisect_left(l, ue) - bisect.bisect_right(l, shita)
        if shita < a < ue:
            ans -= 1
        if shita < b < ue:
            ans -= 1

print(ans // 3)

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

ans = 0
for i in range(1, N - 1):
    for j in range(i + 1, N + 1):
        ans += N - j - bisect(sides, sides[-i] - sides[-j], hi=N-j)
print(ans)

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

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  

n = ni()
l = na()

l.sort()

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

Solution (Python):
import bisect


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

    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)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_right, bisect_left
N = int(input())
L = [int(_) for _ in input().split()]
ans = 0
L.sort()

for n in range(N - 1, -1, -1):
    for m in range(n - 1, -1, -1):
        ans += bisect_left(L, L[n] + L[m]) - n - 1

print(ans)

Solution (Python):
from bisect import bisect_left


def main(N, L):
    L.sort()
    ans = 0
    for i in range(0, N-1):
        for j in range(i+1, N):
            r = bisect_left(L, L[i] + L[j])
            ans += r - (j + 1)
    return ans

if __name__ == "__main__":
    N = int(input())
    L = list(map(int, input().split()))
    print(main(N, L))

Solution (Python):
n = int(input())
L = sorted(list(map(int, input().split())))
def isOK(target,a,b):
  return target < a+b

def bin_search(a,b):
  left = 1
  right = n
  while abs(right-left)>1:
    mid = (left+right)//2
    if isOK(L[mid],a,b):
      left = mid
    else:
      right = mid
  return left
ans = 0
for i in range(n-2):
  for j in range(i+1,n):
    a,b = L[i],L[j]
    ans += max(0, bin_search(a,b) - j)
      
print(ans)

Solution (Python):
import bisect

n = int(input())
L = [int(i) for i in input().split()]

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
N=int(input())
num=list(map(int, input().split()))
c=0
num.sort()
for i in range(N-1,1,-1): 
    for j in range(i-1,0,-1):
        t=num[i]-num[j]
        k=bisect.bisect_right(num,t,0,j)
        c+=(j-k)
print (c)

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 sys
import bisect


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    l = list(map(int, readline().split()))
    l.sort()
    cnt = 0
    for i in range(n):
        for j in range(i + 1, n):
            p = bisect.bisect_left(l, l[j] + l[i])
            cnt += max(0, p - j - 1)
    print(cnt)


if __name__ == '__main__':
    solve()

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


res = 0
for i in range(N):
    a = L[i]
    for j in range(i + 1, N):
        b = L[j]
        k = bisect.bisect_left(L, a + b)
        res += max(k - (j + 1), 0)
        

print(res)

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
 
n = int(input())
l = [int(li) for li in input().split(' ')]
l.sort()

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