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):
import bisect
N = int(input())
L_list = list(map(int,input().split()))
L_list.sort()
ans = 0

for i in range(N-1):
  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):
import sys
input = sys.stdin.readline

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort(reverse=True)
 
ck = [0]*(N-2)


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


def solve(*args: str) -> str:
    n = int(args[0])
    ln = sorted(list(map(int, args[1].split())))
    s = 0
    for i in range(n-1, 1, -1):
        for j in range(i-1, 0, -1):
            small = bisect.bisect_right(ln, ln[i] - ln[j])
            big = bisect.bisect_left(ln, ln[i] + ln[j], small, j)
            s += big - small

    return str(s)


if __name__ == "__main__":
    print(solve(*[input() for _ in range(2)]))

Solution (Python):
import bisect
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.bisect_left(L,a+b)-j-1
print(ans)

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

Solution (Python):
import sys
import bisect

n = int(input())
*l, = map(int, input().split())
l.sort()
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)

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


L.sort()

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

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

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

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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect as bs
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 += bs.bisect_left(L,s) - j - 1
        
        
        
        
print(ans)

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

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

Solution (Python):
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect_left

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()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    n = INT()
    l = LIST()
    l.sort()
    ans = 0
    

    for i in range(n-1):
        for j in range(i+1, n):
            low = l[i]
            high = l[j]

            p = bisect_left(l, low + high)
            
            ans += max(0, p - j - 1)

    print(ans)

if __name__ == '__main__':
    main()

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

arr.sort()
count = 0
for i in range(n-1, 0, -1):
	l, r = 0, i - 1
	while l < r:
		if arr[l] + arr[r] > arr[i]:
			count += r - l
			r -= 1
		else:
			l += 1
print(count)

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

import bisect
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])
        ans += (idx-(j+1))

print(ans)

Solution (Python):
from operator import mul
from functools import reduce
import bisect

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

N=int(input())
L=input()
L=L.split()
L=[int(item) for item in L]
L.sort()
ans=cmb(N,3)
ans2=0
for i in reversed(range(2,N)):
    for j in reversed(range(1,i)):
        a=bisect.bisect_right(L,L[i]-L[j])


        if(a<j):
            ans2+=(j-a)
    

print(ans2)

Solution (Python):
import bisect

def main():
    Input = [input() for i in range(2)]
    N = int(Input[0])
    L = list(map(int,Input[1].split()))
    L.sort()
    ans = 0
    for i1 in range(0,N-2):
        a = L[i1]
        for i2 in range(i1+1,N-1):
            b = L[i2]
            ans += bisect.bisect_left(L, a+b) - i2 - 1
    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
import bisect

N=int(input())
D=[int(x) for x in input().split()]

D.sort()
total=0

for i in range(N-2):
    for j in range(i+1,N-1):
        total+=bisect.bisect_left(D,D[i]+D[j])-j-1

print(total)

Solution (Python):
from bisect import bisect_left
N=int(input())
ll=list(map(int,input().split()))
ll.sort()
ans=0
for i in range(N):
    for j in range(i+1,N):
        if j==i+1:
            delta=bisect_left(ll,ll[i]+ll[i+1],lo=i+1)-i-2
            if delta>0:
                ans+=delta
            in_b=delta+i+1+1
        else:
            if in_b!=N:
                delta=bisect_left(ll,ll[i]+ll[j],lo=in_b)-j-1
            else:
                delta=N-j-1
            if delta>0:
                ans+=delta
            in_b=delta+j+1
print(ans)

Solution (Python):
import bisect

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

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

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string



INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7


n = I()
L  = sorted(LI())
ret = 0
for i in range(n):
    for j in range(i + 1, n):
        ret += bisect_left(L, L[i] + L[j]) - j - 1

print(ret)

Solution (Python):
import bisect

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

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

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

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
L.sort()
wa = 0
import bisect
for i in range(N-1,1,-1):
    for j in range(i-1,0,-1):
        diff = L[i]-L[j]
        k = bisect.bisect_right(L, diff)
        wa += j-k if j-k > 0 else 0           
            
print(wa)

Solution (Python):
from bisect import bisect_left, bisect_right

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

Ls.sort()


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

Solution (Python):
import itertools
import bisect

def read():
    N = int(input().strip())
    L = list(map(int, input().strip().split()))
    return N, L

def solve(N, L):
    L.sort()
    x = 0
    t = [0 for _ in range(N+1)]
    for i in range(N):
        for j in range(i+1, N):
            k = bisect.bisect_left(L, L[i] + L[j])
            if j < k:
                x += (k - j - 1)
    return x

if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Solution (Python):
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 = sorted(list(map(int, input().split())))
s = 0
for i in range(0,n-2):
  a = l[i]
  for j in range(i+1,n-1):
    b = l[j]
    s += bisect.bisect_left(l,a+b)-j-1
print(s)

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

print(ans)

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


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

    ans = 0
    for i in range(N-1, -1, -1):
        a = L[i]
        for j in range(i-1, -1, -1):
            b = L[j]
            ci = bisect.bisect_right(L, a-b, lo=0, hi=j)
            ans += j - ci
    print(ans)


if __name__ == '__main__':
    main()

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

ck = [0]*(N-2)
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(map(int, input().split()))
l = sorted(l)

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

Solution (Python):
from bisect import bisect
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(L, L[a] + L[b] - 1) - b - 1
print(ans)

Solution (Python):
import bisect
import copy

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

cout = 0
m,k = 0,0

for i in range(n-2):
	o = list1.pop(0)
	for j in range(n-i-1):
		p = list1[j]

		a = abs(o-p) +1
		b = o+p -1 

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

print(cout)

Solution (Python):
from collections import Counter
from operator import add, mul
from functools import reduce
import bisect


def nCr(n, r):
    r = min(r, n - r)
    num = reduce(mul, range(n, n - r, -1), 1)
    den = reduce(mul, range(1, r + 1), 1)
    return num // den


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

Solution (Python):
from bisect import*;N,*L=map(int,open(0).read().split());L.sort();print(sum(j-bisect(L,L[i]-L[j],0,j)for i in range(N)for j in range(i)))

Solution (Python):
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()
print(sum(~j+bisect_left(l,l[i]+l[j],j+1)for i in range(n)for j in range(i+1,n)))

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

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

ls = sorted(ll)
count = 0
for i in range(n-1):
  for j in range(i+1,n-1):
    x = bisect_left(ls,ls[i]+ls[j])
    count += x-j-1
    
print(count)

Solution (Python):
def slove():
    import sys
    import collections
    import bisect
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    l = list(map(int, input().rstrip('\n').split()))
    l.sort()
    cnt = 0
    d = collections.defaultdict(int)
    for i in range(n-2):
        for j in range(i+1, n-1):
            ma = l[i] + l[j]
            p = bisect.bisect_left(l, ma)
            if j < p:
                cnt += p - j - 1
    print(cnt)


if __name__ == '__main__':
    slove()

Solution (Python):
import bisect

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.bisect_left(L, a+b) - j - 1

print(ans)

Solution (Python):


import itertools
n = int(input())
l = list(map(int, input().split()))
l.sort()
 
from bisect import bisect_left
ans = 0

for i, j in itertools.combinations(range(len(l)),2):
    t = l[i]+l[j]
    idx = bisect_left(l, t)
    ans += max(0, idx-j-1)
print(ans)

Solution (Python):
import bisect


if __name__ == '__main__':
    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):

N=int(input())
L=[int(i) for i in input().split()]
L=sorted(L)
ans=0
import bisect

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

Solution (Python):
def triangles(a): 
    n = len(a) 
    a.sort() 
    count = 0
    for i in range(0,n-2):
        k=i+2
        for j in range(i+1,n): 
            while (k < n and a[i] + a[j] > a[k]): 
                k += 1
            if(k>j): 
                count += k - j - 1
    return count
n=int(input())
a=list(map(int,input().split()))
print(triangles(a))

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

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

ans = 0


for a in range(n-2):
	for b in range(a+1,n-1):
		index = bisect.bisect_left(l,l[a]+l[b])
		ans += index - (b+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):
    idx = bisect.bisect_left(L, L[i] + L[j]) - 1
    ans += idx - j

print(ans)

Solution (Python):
def findnumberofTriangles(arr): 
  
    
    n = len(arr) 
    arr.sort() 
    count = 0
  
    
    
    for i in range(0,n-2): 
  
        
        k = i + 2
  
        
        for j in range(i+1,n): 
  
            
            
            
            
            
            
            
            while (k < n and arr[i] + arr[j] > arr[k]): 
                k += 1
  
            
            
            
            
            
            
            
            
            
            if(k>j): 
                count += k - j - 1
  
    return count
n = int(input())
l = [int(x) for x in input().split()]
print(findnumberofTriangles(l))

Solution (Python):
import bisect
n,*l=map(int,open(0).read().split())
l.sort()
a=0
for i in range(n):
    for j in range(i+1,n):
        k=l[i]+l[j]
        a+=max(0,bisect.bisect_left(l,k)-j-1)
print(a)

Solution (Python):
from bisect import*;N,*L=map(int,open(0).read().split());L.sort();print(sum(bisect_left(L,L[i]+L[j],j+1)-j-1for i in range(N)for j in range(i+1,N)))

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

print(ans)

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

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

print(ans)

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

score = 0

for i in range (N-2):
    for j in range (i+1, N-1):
        index = bisect.bisect_left(list, list[i] + list[j]) - j - 1
        score += index

print (score)

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

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

score = 0

for i in range (N-2):
    for j in range (i+1, N-1):
        index = bisect.bisect_left(list, list[i] + list[j]) - j - 1
        score += index

print (score)

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

result =0

for i in range(N):
    for j in range(i+1,N):
        a = L_list[i] + L_list[j]
        idx = bisect.bisect_left(L_list,a)
        result += max(0,idx-j-1)
print(result)

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

print(ans)

Solution (Python):
from bisect import bisect_left
N=int(input())
ll=list(map(int,input().split()))
ll.sort()
ans=0
for i in range(N):
    for j in range(i+1,N):
        delta=bisect_left(ll,ll[i]+ll[j],lo=j)-j-1
        if delta>0:
            ans+=delta
        
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):
    L1 = L[i]
    for j in range(i+1, N):
        L2 = L[j]
        sumL = L1 + L2
        k = bisect_left(L, sumL)
        ans += max(0, 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()
result = 0
for i in range(N - 2):
  a = L[i]
  for j in range(i + 1, N - 1):
    b = L[j]
    result += bisect_left(L, a + b) - j - 1
print(result)

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

score = 0

for i in range (N-2):
    for j in range (i+1, N-1):
        index = bisect.bisect_left(list, list[i] + list[j]) - j - 1
        score += index

print (score)

Solution (Python):
import bisect

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

Llist.sort()

count = 0

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

print(count)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))
 
n = inp()
a = inpl()
ans = 0
a.sort()
for i in range(n-2):
    for j in range(i+1,n-1):
        tmp = a[i] + a[j] - 1
        tt = bisect.bisect_right(a[j+1:],tmp)
        if tt > 0 :
            ans += tt
print(ans)

Solution (Python):

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

print(ans)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.sort()
 
from bisect import bisect_left
ans = 0
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_left
N=int(input())
ll=list(map(int,input().split()))
ll.sort()
ans=0
for i in range(N):
    for j in range(i+1,N):
        if j==i+1:
            delta=bisect_left(ll,ll[i]+ll[i+1],lo=i+1)-i-2
            if delta>0:
                ans+=delta
            in_b=delta+i+1+1
        else:
            if in_b!=N:
                delta=bisect_left(ll,ll[i]+ll[j],lo=in_b)-j-1
            else:
                delta=N-j-1
            if delta>0:
                ans+=delta
            in_b=delta+j+1
print(ans)

Solution (Python):
from itertools import combinations
from bisect import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i, j in combinations(range(1,N), 2):
    
    b, c = L[i], L[j]
    k = bisect(L, c-b)
    ans += max(0, i-k)

print(ans)

Solution (Python):
import bisect
import time

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

    print(ans)


if __name__ == "__main__":
    
    main()

Solution (Python):
import bisect


def main():
    N = int(input())
    edges = [int(i) for i in input().split()]
    edges.sort()
    ans = 0
    for a in range(N - 2):
        for b in range(a + 1, N - 1):
            ans += bisect.bisect_left(edges, edges[a] + edges[b])-b-1

    print(ans)


if __name__ == "__main__":
    main()

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

from bisect import bisect_left
for k in range(N-2):
    for j in range(k+1, N-1):
        ans += bisect_left(L[j+1:], L[k]+L[j])
 
 
print(ans)

Solution (Python):
import sys
from bisect import bisect, bisect_left

input = sys.stdin.readline
N = int(input())
L = sorted([int(x) for x in input().split()])
c = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        f = bisect_left(L, L[i] + L[j])
        c += f - j - 1
print(c)

Solution (Python):
import sys
from itertools import combinations
import bisect
import math
n = int(input())
l_ls = [int(i) for i in sys.stdin.readline().split()]
l_ls.sort()
cnt = 0
for a, b in combinations(l_ls, 2):
    _sum = a + b
    ind = bisect.bisect_left(l_ls, _sum)
    cnt += n - ind
print(int(math.factorial(n) // math.factorial(3) // math.factorial(n-3)) - cnt)

Solution (Python):
from bisect import bisect_left
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+1, N):
        l = L[j] - L[i]
        r = L[j] + L[i]

        il = bisect_right(L, l, j)
        ir = bisect_left(L, r, j)

        if il == N-1:
            break

        ans += ir - il - 1

print(ans)

Solution (Python):
from bisect import*
N,*L=map(int,open(0).read().split());L.sort()
a=0
for i in range(N):
    for j in range(i+1,N):
      	a+=bisect_left(L,L[i]+L[j],j+1)-j-1
print(a)

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

Solution (Python):
import bisect

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

l.sort(reverse=True)
ans = 0

l = list(map(lambda x: x * (-1), l))

for i in range(n-2):
    for j in range(i+1,n-1):
        need = l[i] - l[j] - 1
        idx = bisect.bisect_right(l,need)
        
        ans += max(0,idx-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):
        insert_index = bisect.bisect_left(l,l[i]+l[j])
        ans+=(insert_index-j-1)
        
print(ans)

Solution (Python):




input = raw_input
n = int(input())

nums = [0] * 2001


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


ls = sorted(ls)
for l in ls:
    
    nums[l] += 1

set_vals = [val for val in range(2001) if nums[val] >= 1]





len_set = len(set_vals)
out = 0
for i in range(len_set):
    val1 = set_vals[i]
    num_i = nums[val1]
    if num_i >= 3:
        out += (num_i * (num_i-1) * (num_i-2) // 6)
    
    if num_i >= 2:
        val2 = set_vals[i]
        for j in range(i+1, len_set):
            val3 = set_vals[j]
            num_h = nums[val3]
            if 2 * val1 <= val3:
                break

            out += ((num_i * (num_i-1)) // 2) * num_h

    for j in range(i+1, len_set):
        val2 = set_vals[j]
        num_j = nums[val2]

        if num_j >= 2:
            out += (num_i *  num_j * (num_j - 1) // 2 )

        for h in range(j+1, len_set):
            val3 = set_vals[h]
            
            num_h = nums[val3]
            if val1 + val2 <= val3:
                break

            
            out += (num_i * num_j * num_h)

print(out)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import bisect

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

    L = sorted(L)

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

    print(ans)

if __name__ == '__main__':
    main()

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

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


print(sum)

Solution (Python):
import bisect

n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr)
ans=0
for i in range(n):
  a=arr[i]
  for j in range(i+1,n):
    b=arr[j]
    tmp=a+b
    pos=bisect.bisect_left(arr,tmp)-1
    if pos<=j:
      continue
    else:
      ans+=(pos-j)
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

N, *L = map(int, open(0).read().split())

L.sort()

cnt = 0
for i in range(1, N - 1):
    b = L[i]
    for j in range(i):
        a = L[j]
        l, r = bisect_left(L, b - a + 1, lo=i), bisect_right(L, a + b - 1, lo=i)
        cnt += r - l - (l <= i <= r) - (l <= j <= r)

print(cnt)

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

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

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

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

score = 0

for i in range (N-2):
    for j in range (i+1, N-1):
        index = bisect.bisect_left(list, list[i] + list[j]) - j - 1
        score += index

print (score)

Solution (Python):
n = int(input())
l_list = list(map(int, input().split()))
l_list.sort()
count = [0 for i in range(2000)]
cum = [0 for i in range(2000)]
for l in l_list:
  count[l] += 1
for i in range(1, 2000):
  cum[i] = cum[i - 1] + count[i]

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

Solution (Python):
from bisect import bisect_right
 
n = int(input())
a = sorted(map(int, input().split()))
print(sum(j - bisect_right(a, a[i] - a[j], 0, j)
          for i in range(2, n) for j in range(1, i)))

Solution (Python):
import bisect



def find_num(lst, num):
    insert_index = bisect.bisect_left(lst, num)
    return insert_index


n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        cnt += find_num(lst=l[j+1:], num=l[i] + l[j])
print(cnt)

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

dp=[0]*(N+1)
ans=0
for i in range(3,N+1):
  r=i-2
  l=0
  c=0
  while r>l:
    if L[r]+L[l]>L[i-1]:
      c+=r-l
      r-=1
    else:
      l+=1
  dp[i]=dp[i-1]+c

print(dp[-1])