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 sys; readline = sys.stdin.readline
from collections import deque


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

L.sort()

Ns = [0] * (2*10**3 + 10)

for l in L:
    Ns[l] += 1

cNs = [0] * len(Ns)
for i in range(1, len(Ns)):
    cNs[i] = cNs[i - 1] + Ns[i]

count = 0
for i in range(N - 1, -1, -1):
    for j in range(i - 1, -1, -1):
        toosmall = cNs[L[i] - L[j]]
        count += max(0, j - toosmall)

        

print(count)

Solution (Python):
import sys
from bisect import bisect_left as bl
from bisect import bisect_right as br
input = sys.stdin.buffer.readline
N = int(input())
L = list(map(int, input().split()))
L.sort()
dl = []
res = 0
for i in range(N - 1):
  for j in range(i + 1, N):
    d0 = L[i] + L[j]
    d1 = i
    d2 = j
    d3 = abs(L[i] - L[j])
    ind = bl(L, d0)
    ind2 = br(L, d3)
    res += ind - ind2
    if ind2 <= d1 <= ind:
      res -= 1
    if ind2 <= d2 <= ind:
      res -= 1
  
print(res // 3)

Solution (Python):
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):
        a = L[i]+L[j]
        ind = bisect.bisect_left(L,a)
        cnt += ind-j-1
print(cnt)

Solution (Python):
import bisect
N = int(input())
L = list(map(int, input().split()))
index = [-1 for i in range(2001)]
L.sort()

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

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

Solution (Python):
def check(a, b, c):
    return True if c < a + b else False

import bisect
def resolve():
    n = int(input())
    targets = list(map(int, input().split()))
    targets.sort()
    ans = 0

    for i in range(len(targets)):
        for j in range(i + 1, len(targets)):
            rest = targets[j+1:len(targets)]
            ans += bisect.bisect_left(rest,targets[i]+targets[j])

    print(ans)
resolve()

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

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

Solution (Python):
from bisect import*
from itertools import*
n,*L=map(int,open(0).read().split())
L.sort()
ans=0
for x,y in combinations(L,2):
	low=abs(x-y)
	high=x+y
	ans+=bisect_left(L,high)-bisect(L,low)
	if low<x<high:
		ans-=1
	if low<y<high:
		ans-=1
print(ans//3)

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

import bisect

L.sort()
L.append(1001)

ans = 0
for i in range(N - 2):
    for k in range(i + 2, N):
        
        j_left = bisect.bisect_right(L, L[k] - L[i])
        if j_left <= i: j_left = i + 1
        if j_left >= k: continue
        
        if L[j_left] == L[k] - L[i]:
            j_left += 1
        
        j_right = bisect.bisect_left(L, L[k] + L[i])
        if j_right < 0: continue
        if j_right >= k: j_right = k - 1
        
        if L[j_right] == L[k] + L[i]:
            j_right -= 1
        
        
        ans += max(0, j_right - j_left + 1)

print(ans)

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

Solution (Python):
N = int(input())
Ls = sorted(map(int, input().split()))

import bisect
ans = 0
for i, a in enumerate(Ls[:-2]):
  for j, b in enumerate(Ls[i + 1:-1], start=i + 1):

    rest = a + b
    end_idx = bisect.bisect_left(Ls, rest)

    start_idx = j + 1
    ans += end_idx - start_idx

print(ans)

Solution (Python):
import bisect

n = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for a in range(n-1, 1, -1):
    for b in range(a-1, 0, -1):
        length1 = L[a]
        length2 = L[b]
        limit = length1 - length2 + 1
        index = bisect.bisect_left(L, limit)
        ans += max(0, b - index)

print(ans)

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

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

l.sort()
ans = 0

for i in range(n):
    count = i
    for j in range(i+1,n-1):
        ad = l[i] + l[j]
        if count < n:
            while l[count] < ad and count < n:
                count += 1
                if count == n:
                    break
        ans += count - j - 1

print(ans)

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):
def CountTriangles( A):
	n = len(A);
	A.sort();
	count = 0;
	for i in range(n - 1, 0, -1):
		l = 0;
		r = i - 1;
		while(l < r):
			if(A[l] + A[r] > A[i]):
				count += r - l;
				r -= 1;
			else:
				l += 1;
	print(count);

n = int(input())
a = [ int(i) for i in input().split() ]
CountTriangles(a);

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  l = list(int(i) for i in input().split())
  l = sorted(l)
  ans = 0
  
  for i in range(n):
    for j in range(i+1,n):
        a = l[i]
        b = l[j]
        c_max = bisect.bisect_left(l,a+b)
        if c_max <= j+1:
          continue
        else:
          ans += c_max-j-1
          
        

  print(ans)
  
solve()

Solution (Python):
from bisect import bisect_left, bisect_right
n = int(input())
ls = sorted(list(map(int, input().split())))
total = 0
for i in range(n):
    for j in range(i+1, n):
        b, c = ls[i], ls[j]
        lower = abs(b-c) + 0.5
        upper = b+c - 0.5
        ii = bisect_left(ls, lower)
        jj = bisect_left(ls, upper)
        cnt = jj - ii
        if ii <= i < jj:
            cnt -= 1
        if ii <= j < jj:
            cnt -= 1
        total += cnt
total //= 3

print(total)

Solution (Python):
import bisect

def solve(n, l):
    l.sort()
    r = 0
    for i in range(n - 2):
        li = l[i]
        for j in range(i + 1, n - 1):
            r += bisect.bisect_left(l, li + l[j], j + 1) - j - 1
    return r

def main():
    n = int(input())
    l = [int(x) for x in input().split()]
    print(solve(n, l))


main()

Solution (Python):
from bisect import bisect_left as bl
from bisect import bisect_right as br
n=int(input())
s=sorted(list(map(int,input().split())))
q=0
d=[[""]*(1001)for i in range(1001)]
for i in range(n):
    for j in range(i+1,n):
        x,y=max(s[i],s[j]),min(s[i],s[j])
        if d[x][y]=="":
            c=br(s,x+y-1)-bl(s,x-y+1)-1
            if x-y<y:
                c-=1
            c=max(c,0)
            d[x][y]=c
        q+=d[x][y]
print(q//3)

Solution (Python):
from bisect import bisect_right

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


for i in reversed(range(2, N)):
    for j in reversed(range(1, i)):
        idx = bisect_right(L, L[i] - L[j])
        
        if idx < j:
            ans += j - idx

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

Solution (Python):
n = int(input())
l = sorted([int(i) for i in input().split()])
ans = 0
for i in range(n-2):
    k = i + 2
    for j in range(i+1, n):
        while k < n and l[i] + l[j] > l[k]:
            k += 1
        if k > j:
            ans += k-j-1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())

def main():
    N = ii()
    L = lmis()
    L.sort()
    import bisect
    ans = 0
    for ia in range(N):
        a = L[ia]
        for ib in range(ia+1, N):
            b = L[ib]
            ans += max(0, bisect.bisect_left(L, a+b) - (ib+1))
    print(ans)

main()

Solution (Python):
def fnt(arr,n): 
  
     
     
     
    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())
arr=input()
arr=arr.split()
arr=list(map(int,arr))
arr.sort()
print(fnt(arr,n))

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
cnt = [0 for i in range(1001)]
for l in L:
	cnt[l] += 1
total = [0 for i in range(1001)]
for i in range(1, 1001):
	total[i] = total[i-1] + cnt[i]

ans = 0
for i in range(1, 1001):
	if cnt[i] <= 0:
		continue
	I = cnt[i]
	for j in range(i, 1001):
		J = cnt[j]
		if J <= 0:
			continue
		if i == j and J <= 1:
			continue
		base = (I * (I - 1)) // 2 if i == j else I * J
		M = min(1000, i + j - 1)
		if i == j:
			if cnt[j] >= 3:
				ans += (I * (I-1) * (I-2)) // 6
		elif cnt[j] >= 2:
			ans += I * (J * (J-1)) // 2
		ans += base * (total[M] - total[j])
print(ans)

Solution (Python):
import sys
import bisect
sys.setrecursionlimit(10**7)
N = int(input())
L = list(map(int, input().split()))
L.sort()

cnt = 0
lenL = len(L)
for i, l1 in enumerate(L):
    for j, l2 in enumerate(L[i+1:]):
        sumll = l1 + l2
        pos = bisect.bisect_left(L, sumll)
        add = pos - i - j - 2
        cnt += add
print(cnt)

Solution (Python):
import bisect

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

ans = 0
l.sort()
for i in range(n):
    for j in range(i+1, n):
        lt = bisect.bisect_right(l, l[j]-l[i], j+1)
        rt = bisect.bisect_left(l, l[j]+l[i], j+1)
        ans += max(0, rt-lt)
print(ans)

Solution (Python):
n=int(input())
l=[int(i) for i in input().split()]
l.sort()
ll=[0]*(l[-1]+1)

for i in l:
    ll[i]+=1
for i in range(1,len(ll)):
    ll[i]+=ll[i-1]

ans=0
import itertools
for i,j in itertools.combinations(l,2):
    if max(i-j,j-i)<i+j-1:
        ans+=ll[min(len(ll)-1,i+j-1)]-ll[min(len(ll)-1,max(i-j,j-i))]
        if max(i-j,j-i)<i<i+j:
            ans-=1
        if max(i-j,j-i)<j<i+j:
            ans-=1
print(ans//3)

Solution (Python):
import bisect as bi

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

ans=0

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

print(ans)

Solution (Python):

import sys
def input():
	return sys.stdin.readline().strip()

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

num_length = [0]*(2001)

for i in range(N):
	num_length[L[i]] += 1

for i in range(1, len(num_length)):
	num_length[i] += num_length[i - 1]

sum = 0
for i in range(N):
	for j in range(N):
		if i != j:
			x = max(L[i], L[j]) - min(L[i], L[j])
			sum += num_length[L[i] + L[j] - 1]
			sum -= num_length[x]
			if L[i] > x:
				sum -= 1
			if L[j] > x:
				sum -= 1


print(sum//6)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
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 II(): return int(sys.stdin.readline())
def SI(): return sys.stdin.readline().strip()
INF = 10 ** 18
MOD = 10 ** 9 + 7

debug = True
debug = False

def dprint(*objects):
    if debug == True:
        print(*objects)

def solve():
    N = II()
    l_list = LI()

    
    l_list = sorted(l_list)
    ind_list = list(range(len(l_list)))

    from bisect import bisect_left, bisect_right
    from itertools import combinations
    dprint(l_list)
    cnt = 0
    for a, b in combinations(ind_list, 2):
        

        
        
        
        
        

        
        la = l_list[a]
        lb = l_list[b]

        mx = la + lb
        mn = max(la, lb) - min(la, lb)

        
        right_ind_plus1 = bisect_left(l_list, mx)
        left_ind = bisect_right(l_list, mn)

        

        cnt += right_ind_plus1 - left_ind

        if left_ind <= a and a < right_ind_plus1:
            cnt -= 1

        if left_ind <= b and b < right_ind_plus1:
            cnt -= 1

    print(cnt//3)

solve()

Solution (Python):
N=int(input())
L = [int(i) for i in input().split()]
L.sort(reverse=True)
ans = 0
for a_index,a in enumerate(L):
    if a_index == N-1:break
    for b_index in range(a_index+1,N):
        b = L[b_index]
        min_c = a - b 
        U = N 
        D = -1 
        if b<=min_c:continue
        while U-D > 1:
            X = (U+D)//2
            if L[X]<=min_c:
                U = X
            else:
                D = X
        c_index_max = D
        ans += c_index_max - b_index
print(ans)

Solution (Python):

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

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

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

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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


solve2()

Solution (Python):


import bisect


def main():
  input()
  values = list(map(int, input().split()))
  values.sort()
  result = 0

  for i in range(len(values) - 2):
    for j in range(i + 1, len(values) - 1):
      targets = values[j + 1:]
      index = bisect.bisect_left(targets, values[i] + values[j])
      result += index

  print(result)


if __name__ == '__main__':
  main()

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):
    if L[i]>=2*L[j]:
      break
    
    ans+=j-bisect.bisect(L,L[i]-L[j])
print(ans)

Solution (Python):
import bisect
def main():
    N = int(input())
    L = [int(n) for n in input().split()]
    
    L.sort()
    res = 0
    for i in range(N):
        for j in range(i+1, N):
            c = L[i]
            b = L[j]
            targ = b - c
            
            insert_index = bisect.bisect_right(L, targ)
            if insert_index >= i:
                continue
            
            
            
            res += i - insert_index
    print(res)
main()

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



sys.setrecursionlimit(1000000000)

MIN = -10 ** 9
MOD = 10 ** 9 + 7


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

    r = 0
    for a in range(N):
        for b in range(a + 1, N):
            p = bisect.bisect_left(L, L[a] + L[b])
            r += (p - 1 - b)

            
            
            
            

    print(r)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect

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

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

Solution (Python):
from bisect import bisect_left as bi
n=int(input())
l=sorted(map(int,input().split()))
ans=0

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

print(ans)

Solution (Python):
import bisect

n=int(input())
llist = list(int(i) for i in input().split())
llist=sorted(llist)
kotae=0

swit=0
etswit=0

for i in range(n-2):
    swit=0
    for j in range(n-2-i):
        a=i
        b=i+j+1
        koho=llist[b+1:]
        nagasa=len(koho)
        if etswit==0:
            if swit==0:
                index = bisect.bisect_left(koho, llist[a]+llist[b])
                if index==nagasa:
                    swit=1
                    if j==0:
                        etswit=1
                
                kotae+=index
            else:
                kotae+=nagasa
        else:
            kotae+=nagasa

print(kotae)

Solution (Python):

import sys
import bisect
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = lambda *something : print(*something) if DEBUG else 0
DEBUG = False
def main(given = sys.stdin.readline):
    input = lambda : given().rstrip()
    LMIIS = lambda : list(map(int,input().split()))
    II = lambda : int(input())
    XLMIIS = lambda x : [LMIIS() for _ in range(x)]

    N = II()
    L = LMIIS()
    L.sort()
    count = 0
    for i in range(N):
        for j in range(i+1,N):
            a = L[i]
            b = L[j]
            l = bisect.bisect_right(L,a-b,lo=j+1)
            r = bisect.bisect_left(L,a+b,lo=j+1)
            count += max(r - l ,0)
    print(count)



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

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

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

L.sort()

K = [0] * (4 * (10 ** 3)+1)

K[L[0]+L[1]] = 1

count = 0

for i in range(2,N):
    count += sum(K[L[i]+1:])
    for j in range(i):
        K[L[j]+L[i]] += 1

print(count)

Solution (Python):
import bisect
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):
        Lij = L[i] + L[j]
        k = bisect.bisect_left(L, Lij)
        ans += max(k - 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):
    t=l[i]+l[j]-1
    index=bisect.bisect_right(l,t,j,n)
    ans+=max(0,index-j-1)
print(ans)

Solution (Python):
from bisect import bisect_left
N = int(input())
L = [int(c) for c in input().split()]
L.sort()
ans = 0
for i in range(N):
  for j in range(i+1, N):
    x = L[i]+L[j]
    ind = bisect_left(L, x)
    ans += ind-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):
    
    a = L[i]
    ci = n-1
    for j in range(i+1,n-1):
        b = L[j]
        ci = max(ci,j)
        
        while ci > j and b+L[ci] <= a:
            ci -= 1
        ans += ci-j
print(ans)

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

import bisect
L.sort()
ans = 0
bl = bisect.bisect_left
br = bisect.bisect_right
for i,a in enumerate(L):
    for j,b in enumerate(L[i+1:],i+1):
        m = b-a+1
        M = b+a-1
        ans += br(L,M,j+1) - bl(L,m,j+1)
print(ans)

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

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

print(res)

Solution (Python):
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))
ans = 0
for key_i,i in enumerate(l):
    for key_j,j in enumerate(l[key_i+1:]):
        key_j += key_i + 1
        ans += bisect.bisect_left(l, i+j) - key_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-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,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):
n=int(input())
l=[int(i) for i in input().split()]
l=sorted(l)
n=len(l)
ans=0 
for i in range(n-1,0,-1):
    le=0 
    r=i-1 
    while le<r:
        if l[le]+l[r]>l[i]:
            ans=ans+(r-le)
            r-=1 
        else:
            le+=1 
print(ans)

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

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

Solution (Python):
import bisect
n=int(input())
li=[int(i) for i in 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]
        c=bisect.bisect_left(li,a+b)
        ans+=c-j-1
                  
print(ans)

Solution (Python):










from bisect import bisect_left

n = int(input())
a = tuple(sorted(map(int, input().split())))

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

Solution (Python):
import bisect
n = int(raw_input())
edges = list(map(int, raw_input().split(" ")))


def solve():
	edges.sort()
	ans = 0

	for i in range(len(edges)):
		for j in range(i+1, len(edges)):
			sum_of_two = edges[i] + edges[j]
			idx = bisect.bisect_left(edges, sum_of_two)

			if idx-1 > j:
				ans += idx-1-j
	return ans 
print(solve())

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

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

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

def it():
    for i,j in combinations(range(N),2):
        if i+1 < j:
            a,c = L[i],L[j]

            yield max(0,j-bisect(L,abs(a-c), hi=j,lo=i+1))
print(sum(it()))

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):
        idx1 = bisect_right(l, abs(l[i] - l[j]))
        idx2 = bisect_left(l, l[i] + l[j])
        ansi = idx2 - idx1
        if abs(l[i] - l[j]) < l[i]:
            ansi -= 1
        if abs(l[i] - l[j]) < l[j]:
            ansi -= 1
        ans += ansi
        

ans //= 3
print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



from bisect import bisect_left, bisect_right

N = I()
L = III()

L.sort(reverse=True)
L2 = L[::-1]

ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        p = bisect_right(L2,L[i]-L[j])
        if N-1-p-j>=0:
            ans += N-1-p-j

print(ans)

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

Solution (Python):
N = int(input())
Ls = list(map(int, input().split()))
Ls.sort()
ans = 0
for i in range(N-2):
  li = Ls[i]
  idx = i
  for j in range(i+1, N-1):
    lj = Ls[j]
    while idx < N and li + lj > Ls[idx]:
      idx += 1
    ans += idx - j - 1
print(ans)

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

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

Solution (Python):
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):
    ans+=bisect.bisect_left(L,L[i]+L[j])-j-1
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=list(map(int,raw_input().split()))

print findnumberofTriangles(l)

Solution (Python):
import bisect
n=int(input())
l=list(map(int,input().split()))
l.sort()
count=0
for i in range(n):
    for j in range(i+1,n):
        x=l[i]+l[j]
        idx=bisect.bisect_left(l,x,j+1,n)
        count+=idx-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):
    for j in range(i + 1, n - 1):
        if i == j:
            continue
        ans += bisect_left(l, l[i] + l[j], lo=j + 1) - j - 1
print(ans)

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

import bisect

n = int(input())
l = nl()
l.sort()
ans = 0
for i in range(n-1):
    for j in range(i+1,n):
        a,b = l[i],l[j]
        if a > b:
            a,b = b,a
        lef = bisect.bisect_right(l,b-a)
        rig = bisect.bisect_left(l,a+b)
        t = rig - lef - 1 - (b < 2*a)
        ans += t
print(ans//3)

Solution (Python):
import bisect
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

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

L.sort()

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        a = L[i]
        b = L[j]
        l = bisect.bisect_left(L, b - a + 1)
        r = bisect.bisect_left(L, b + a)
        ans += r - l - 1 - (b < a * 2)
print(ans // 3)

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

Solution (Python):
from bisect import bisect_right, 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):
        a = l[i]
        b = l[j]
        left = abs(a-b)
        right = a+b
        q = max(j+1, bisect_left(l, right))
        w = max(j+1, bisect_right(l, left))
        
        ans += q-w
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):
        c = l[i]-l[j] + 0.01
        k = bisect.bisect_left(l,c)
        k = min(k,j)
        ans += (j-k)
print(ans)

Solution (Python):
import bisect

N = int(input())
L = list(sorted(map(int, input().split())))
s = 0
for i in range(N):
    for j in range(i+1, N):
        a, b = L[i], L[j]
        lc, rc = abs(a-b), a+b
        l = bisect.bisect_left(L, lc+1, j+1)
        r = bisect.bisect_left(L, rc, j+1)
        s += r - l
print(s)

Solution (Python):
from bisect import bisect_left

n = int(input())
lll = list(map(int, input().split()))
lll.sort()
ans = 0

for i in range(n - 2):
    li = lll[i]
    for j in range(i + 1, n - 1):
        k = bisect_left(lll, li + lll[j], lo=j)
        ans += k - j - 1
print(ans)

Solution (Python):
import bisect
import math

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

Solution (Python):
N = int(input())
A = sorted([int(a) for a in input().split()])
X = [0] * 1001
for a in A:
    X[a] += 1
for i in range(1, 1001):
    X[i] += X[i-1]

ans = 0
for i in range(N):
    for j in range(i+1, N):
        ans += X[min(A[i]+A[j]-1, 1000)] - (j+1)
print(ans)

Solution (Python):
import bisect

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

for i in range(N):
    for j in range(i+1,N):
        A=bisect.bisect_left(L,L[i]+L[j])
        B=bisect.bisect_right(L,abs(L[i]-L[j]))
        tmp=A-B
        if B<=i<A:
            tmp-=1
        if B<=j<A:
            tmp-=1
        ans+=tmp
        
print(ans//3)

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

        mid = L[i]
        mini = L[j]

        ind = bisect.bisect_left(L,mid+mini)
        

        ans += ind - i - 1

print (ans)

Solution (Python):

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

def count(first,second):
    ok = second
    ng = n
    while ng-ok>1:
        mid = (ng+ok)//2
        if l[first]+l[second] > l[mid]:
            ok = mid
        else:
            ng = mid
    return ok - second

for first in range(n):
    for second in range(first+1,n):
        
        ans += count(first,second)

print(ans)

Solution (Python):
import bisect

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

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