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

Solution (Python):
"""
二分探索

pythonでTLE, pypyでAC
"""

import sys
sys.setrecursionlimit(10**6)

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

L.sort()
ans = 0

for i in range(n-2):
    l_prev = 0
    for j in range(i+1,n-1):
        if not l_prev:
            l = j; r = n 
        else:
            l = l_prev; r = n
        while l+1 < r:
            c = (l+r)//2
            if L[c] < L[i]+L[j]:
                l = c
            else:
                r = c
        ans += l-j
        l_prev = l
        
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-2):  
  for j in range(i + 1, N-1):  
    ans += bisect_left(L, L[i] + L[j]) - (j + 1)

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

print(ans)

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

l = sorted(l)
ans = 0
for i in range(n - 1, -1, -1):
    for j in range(i - 1, -1, -1):
        x = l[i] - l[j]
        index = bisect_right(l, x, 0, n)
        ans += max(0, j - index)

print(ans)

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

l = sorted(l)
ans = 0
for i in range(n - 1, -1, -1):
    for j in range(i - 1, -1, -1):
        x = l[i] - l[j]
        index = bisect_right(l, x, 0, n)
        
        ans += max(0, j - index)

print(ans)

Solution (Python):
"""二分探索"""

import sys
sys.setrecursionlimit(10**6)

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

L.sort()
ans = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        l = j; r = n
        while l+1 < r:
            c = (l+r)//2
            if L[c] < L[i]+L[j]:
                l = c
            else:
                r = c
        ans += l-j
        
print(ans)

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

Solution (Python):
import bisect
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): 
        c = L[i] + L[j] 
        pos = bisect.bisect_left(L, c)
        res += max(0, pos-j-1)
print(res)

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

Solution (Python):
from itertools import accumulate

maxL = 10**3

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

nums = [0] * (2*maxL+1)
for L in Ls:
    nums[L] += 1

accNums = list(accumulate(nums))

ans = 0
for A in range(1, maxL+1):
    if nums[A] == 0: continue
    for B in range(A+1, maxL+1):
        if nums[B] == 0: continue
        ans += nums[A] * nums[B] * (accNums[A+B-1]-accNums[B])

for A in range(1, maxL+1):
    if nums[A] < 2: continue
    ans += nums[A] * (nums[A]-1) // 2 * (accNums[2*A-1]-nums[A])

for A in range(1, maxL+1):
    if nums[A] < 3: continue
    ans += nums[A] * (nums[A]-1) * (nums[A]-2) // 6

print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

def solve():
    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]) - 1
            ans += k-j

    print(ans)


solve()

Solution (Python):
from bisect import bisect_left, bisect_right

def solve():
    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]) - 1
            ans += k-j

    print(ans)


solve()

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

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

List.sort()

a = []

for i in range(N):
  for j in range(i+1,N):
    a.append(List[i] + List[j])
    
a.sort()
ans = N * (N-1) * (N-2) / 6

for i in range(len(a)):
  b = a[i]
  c = bisect.bisect_left(List, b)
  ans -= (N-c)

print(int(ans))

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

Solution (Python):












from math import ceil, floor
from bisect import bisect_left, bisect_right

def solution(N, data): 
    data.sort()
    c = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            c += bisect_left(data, data[i] + data[j], j) - j - 1
    return c

if __name__ == '__main__':
    N = int(input())
    L_i = list(map(int, input().split()))
    a = solution(N, L_i)
    print(a)

Solution (Python):












from math import ceil, floor
from bisect import bisect_left, bisect_right

def solution(N, data): 
    data.sort()
    c = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            c += bisect_left(data, data[i] + data[j], j) - j - 1

    return c

if __name__ == '__main__':
    N = int(input())
    L_i = list(map(int, input().split()))
    a = solution(N, L_i)
    print(a)

Solution (Python):
def  isOk(arg):
    if arg > mn:
        return True
    else:
        return False

def bisect_m(ng,ok):
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if isOk(l[mid]):
            ok = mid
        else:
            ng = mid
    return ok

n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(n-2):
    a = l[n-1-i]
    for j in range(i+1,n-1):
        b = l[n-1-j]
        
        mn = a - b
        ng = -1
        ok = n-1-j
        ok = bisect_m(ng,ok)
        ans += n-1-j - ok
        


print(ans)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
for i in range(n-2):
    a = l[n-1-i]
    for j in range(i+1,n-1):
        b = l[n-1-j]
        
        mn = a - b
        ng = -1
        ok = n-1-j
        if mn >= b:
            continue
        while abs(ok - ng) > 1:
            hf = (ng + ok) // 2
            
            if l[hf] > mn:
                ok = hf
            else:
                ng = hf
        ans += n-1-j - ok
        


print(ans)

Solution (Python):

import sys

INF = 2 * 10 ** 3 + 1

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

    
    def is_ok_top(mid: int, a: int, b: int):
        
        if mid < 0:
            return True
        elif mid >= N:
            return False
        return L[mid] < L[a] + L[b]

    def binSearch_top(ok: int, ng: int, a: int, b: int):
        
        while abs(ok - ng) > 1:     
            mid = (ok + ng) // 2
            if is_ok_top(mid, a, b):
                ok = mid            
            else:
                ng = mid            
            
        return ok    

    count = 0
    for a in range(0, len(L) - 2):
        for b in range(a + 1, len(L) - 1):
            count += binSearch_top(-1, INF, a, b) - b

    print(count)







if __name__ == '__main__':
    main()

Solution (Python):
import sys
import bisect

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() 
ni = lambda: int(readline().rstrip()) 
nm = lambda: map(int, readline().split()) 
nl = lambda: list(map(int, readline().split())) 

n = ni()
l = sorted(nm())

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

Solution (Python):
import bisect

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

Solution (Python):


import bisect

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

l_list.sort()
ans = 0

for i in range(n-2):
    _a = l_list[i]
    for j in range(i+1,n-1):
        _b = l_list[j]
        _tmp = bisect.bisect_left(l_list, _a+_b)
        if j < _tmp:
            ans += _tmp - j - 1

print(ans)

Solution (Python):
from bisect import bisect_left

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

print(cnt)

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

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

import bisect
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(len(L)):
    for j in range(i+1, len(L)):
        b_i = bisect.bisect_left(L, L[i]+L[j]) 
        ans += b_i - (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):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L, t)
        ans += max(0, idx-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):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L, t)
        ans += max(0, idx-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):
        t = L[i] + L[j]
        idx = bisect.bisect_left(L, t)
        ans += max(0, idx-j-1)
print(ans)

Solution (Python):
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
L = rl()

L.sort()

def search(d):
	l, r = 0, len(L)
	while r-l > 1:
		c = (r+l)//2
		if d <= L[c]:
			r = c
		else:
			l = c
	return r

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

Solution (Python):
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
L = rl()

L.sort()

import bisect

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

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
l = li()

l.sort()

ans = 0
from bisect import bisect_left
for i in range(len(l)-2):
    for j in range(i+1,len(l)-1):
        ab = l[i] + l[j]
        r = bisect_left(l, ab)
        ll = j+1
        ans += max(0, r-ll)

print(ans)

Solution (Python):
from bisect import bisect_left,bisect_right

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
l = li()
l.sort()

ans = 0

lset = list(set(l))
lset.sort()
l_dup = []
for i in range(len(lset)):
    l_dup.append(l.count(lset[i]))


for i in range(len(lset)-1):
    for j in range(i+1,len(lset)):
        if lset[i]*2 < lset[j]:
            minside = lset[j] - lset[i] +1 
        else:
            minside = lset[i] + 1

        msi = bisect_left(lset,minside)
        ans += l_dup[i] * l_dup[j] * sum(l_dup[msi:j])


        
        
        
        


for i in range(len(lset)):
    if l_dup[i] >= 2:
        msi = bisect_left(lset,lset[i]*2)
        ans += l_dup[i] * (l_dup[i]-1)/2 * (sum(l_dup[:msi]) - l_dup[i])

    if l_dup[i] >= 3:
        ans += l_dup[i] * (l_dup[i]-1) * (l_dup[i]-2)/6

print(int(ans))

Solution (Python):
from bisect import bisect_left,bisect_right

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

L.sort()

cnt=0

for a in range(N):
    for b in range(a+1,N):
        right=bisect_right(L,L[a]+L[b]-1)
        left=max(b+1,bisect_left(L,L[b]-L[a]+1))

        cnt+=max(0,right-left)

print(cnt)

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

Solution (Python):
def bis_right(lis,dev):
    a=0
    b=len(lis)-1
    while a<b:
        c=(a+b)//2
        if lis[c]>dev:
            b=c
        else:
            a=c+1
    if lis[a]<=dev:
        return a
    return a-1



n=int(input())
l=[int(x) for x in input().split()]
l.sort()
s=0
for a in range(n-2):
    for b in range(a+1,n-1):
        c=bis_right(l[b+1:],l[a]+l[b]-1)
        s+=c+1
print(s)

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

Solution (Python):
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
for i in range(0,N-1,1):
    for j in range(i+1,N,1):
        left = max(L[i]-L[j],L[j]-L[i])
        right = L[i]+L[j]
        count += -bisect.bisect_right(L,left)+bisect.bisect_left(L,right)
        if L[i] > left and L[i] < right:
            count-=1
        if L[j] > left and L[j] < right:
            count-=1
print(count//3)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from itertools import combinations 
from bisect import bisect_left
def main():
    N = int(readline())
    L = [int(i) for i in readline().split()]
    L.sort()
    sig = 0
    for i in range(N):
        for j in range(i+1, N):
            l = bisect_left(L, L[i]+L[j], j+1)
            sig += l-j-1
    print(sig)
if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect, bisect_left

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

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

Solution (Python):
from bisect import bisect_left
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    L = [int(i) for i in readline().split()]
    L.sort()

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

    print(ans)


if __name__ == "__main__":
    main()

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

counter = 0
for a_pointer in range(len(L)):
    for b_pointer in range(a_pointer+1, len(L)):
        a = L[a_pointer]
        b = L[b_pointer]
        c_left_pointer = max(bisect.bisect_left(L, b - a + 1), b_pointer + 1)
        c_right_pointer = bisect.bisect_right(L, b + a - 1)
        
        counter += max(c_right_pointer - c_left_pointer, 0)
        
            
print(counter)

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

import bisect
for i in range(len(l)):
    for j in range(i+1,len(l)):
        end = bisect.bisect_left(l,l[i]+l[j])
        start = j
        count += end - start -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):
    ans += bisect_left(l[j+1:], l[j]+l[i])
    
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[j+1:], l[j]+l[i])
    
print(ans)

Solution (Python):
import sys
from bisect import bisect_left

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


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


def main():
    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]
            ans += max(bisect_left(L, a + b) - (j + 1), 0)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect


def resolve():
    n = int(input())
    li = list(map(int, input().split()))
    ans = 0

    li.sort()

    for i in range(n):
        for j in range(i + 1, n):
            cnt = bisect.bisect_left(li, li[i] + li[j])

            ans += cnt - j - 1

    print(ans)

resolve()

Solution (Python):
N = int(input())
l = [int(x) for x in input().split()]
l.sort()
def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if item > list[-1]:
        return high
    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    if list[mid] > item:
        return mid - 1
    else:
        return mid
ans = 0
for i in range(N-1):
    for j in range(i,N-1):
        b = binary_search(l,l[i] + l[j+1]-1)
        while True:
            if b == len(l)-1:
                break
            elif l[b] == l[b+1]:
                b = b+1
            else:
                break
        ans += max(0,b-(j+1))
print(ans)

Solution (Python):
from bisect import bisect_left
n = int(input())
sides = list(map(int, input().split()))
sides.sort()
ans = 0
for a in range(len(sides)):
    for b in range(a+1,len(sides)):
        bound = bisect_left(sides, sides[a]+sides[b], b+1)
        if bound >= n:
            ans += bound - b - 1
        elif sides[bound] < sides[b]:
            ans += bound - b
        elif bound > b+1:
            ans += bound - b - 1

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-2):
 mid =i+1
 l=0
 r=i+2
 while r<N:
  if L[r]-L[l]<L[mid]:
   ans+=(mid-l)
   r+=1
  else:
   if l==mid-1:
    r+=1
   else:
    l+=1
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-2):
 mid =i+1
 l=0
 r=i+2
 while r<N:
  if L[r]-L[l]<L[mid]:
   ans+=(mid-l)
   r+=1
  else:
   if l==mid-1:
    r+=1
   else:
    l+=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):
    for j in range(i + 1, n):
        count += (bisect.bisect_left(L, L[i] + L[j]) - j - 1)

print(count)

Solution (Python):
N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for i in range(0,N-1):
    for j in range(i+1,N):
        D=L[i]+L[j]
        l=j
        r=N
        while r-l>1:
            med=(l+r)//2
            if D>L[med]:
                l=med
            else:
                r=med
        ans+=l-j
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l.sort()
def nibuta(a,x):
    h=len(a)
    if a[0]>=x:
        return 0
    elif a[h-1]<x:
        return h
    else:
        lo=-1
        up=h-1
        while up-lo>1:
            if a[(lo+up)//2]>=x:
                up=(lo+up)//2
            else:
                lo=(lo+up)//2
        if a[up]>=x:
            return lo+1
        else:
            return up+1
s=0
for i in range(n-2):
    a1=l[i]
    for j in range(i+1,n-1):
        k=a1+l[j]
        s=s+nibuta(l[j+1:],k)
print(s)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
L.sort()
from numba import njit, jit
from itertools import combinations









@njit
def solve(L):
    ans = 0
    for i in range(N - 2):
        a = L[i]
        for j in range(i + 1, N - 1):
            b = L[j]
            for k in range(j + 1, N):
                ans += a + b > L[k]
    return ans


print(solve(L))

Solution (Python):
import numpy as np
from numba import njit

@njit
def f(A):
  N = len(A)
  cnt = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        cnt += (A[k] < A[i] + A[j])
  return cnt

N = int(input())
A = np.int32(input().split())
A.sort()
print(f(A))

Solution (Python):
import numpy as np
from numba import njit

@njit('(i4[::1],)', cache=True)
def f(A):
  N = len(A)
  cnt = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        cnt += (A[k] < A[i] + A[j])
  return cnt

N = int(input())
A = np.int32(input().split())
A.sort()
print(f(A))

Solution (Python):
import numpy as np
from numba import njit

@njit('(i4[::1],)', cache=True)
def f(A):
  N = len(A)
  cnt = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        cnt += (A[k] < A[i] + A[j])
  return cnt

N = int(input())
A = np.array(input().split(), np.int32)
A.sort()
print(f(A))

Solution (Python):
from bisect import bisect_left,bisect
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):
        sahen = bisect(l,l[j]-l[i])
        uhen = bisect_left(l,l[j]+l[i])
        ans += uhen - sahen
        if sahen <= i <= uhen -1:
            ans -= 1
        if sahen <= j <= uhen -1:
            ans -= 1
print(ans//3)

Solution (Python):
from bisect import bisect_left

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

Solution (Python):
import bisect

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

lisL.sort()
ans = 0
for i in range(N):
    for j in range(i+1,N):
        ab = lisL[i] + lisL[j]
        ans += bisect.bisect_left(lisL[j+1:],ab)

print(ans)

Solution (Python):

import sys
import bisect

sys.setrecursionlimit(10 ** 8)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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


def solve():
    ans = 0
    for i in range(N):
        b = L[i]
        for j in range(i):
            a = L[j]
            k = bisect.bisect_left(L, a + b, i + 1) - 1
            if k == i:
                continue
            ans += k - i
    return ans


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

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

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.sort()
def bin_search(key, list):
    ok = n
    ng = -1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if list[mid] > key:
            ok = mid
        else:
            ng = mid
    return ok

ans = 0
for i in range(1, n - 1):
    for j in range(i + 1, n):
        c_min_len = l[j] - l[i]
        x = i - bin_search(c_min_len, l)
        ans += max(0, x)
print(ans)

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

ans = 0

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

print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
def find_min_fuka_idx(lst, e1, e2):
  ok = -1
  ng = len(lst)
  while abs(ok-ng)>1:
    mid = (ok+ng)//2
    e3 = lst[mid]
    if e1+e2 > e3 and e2+e3>e1 and e3+e1>e2:
      ok = mid
    else:
      ng = mid
  return ng
c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += find_min_fuka_idx(a[j+1:], a[i], a[j])
print(c)

Solution (Python):
N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(0, N - 2):
    a = L[i]
    for j in range(i + 1, N - 1):
        b = L[j]
        ok = j + 1
        ng = N - 1
        if L[ng] < a + b:
            ans += ng - j
            continue
        if L[ok] >= a + b:
            continue
        while ok + 1 < ng:
            c = (ok + ng) // 2
            if L[c] < a + b:
                ok = c
            else:
                ng = c
        ans += ok - j
print(ans)

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

L = sorted(L)
count = 0
for i in range(1,N-1):
    for j in range(i+1,N):
              posi = bisect.bisect_right(L,L[j]-L[i])
              if i-posi > 0:
                  count += i-posi
print(count)

Solution (Python):
import bisect

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

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

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

ln.sort()

def binary_search(x, r):
    left = r + 1
    right = n - 1
    while left <= right:
        mid = (left + right) // 2
        if ln[mid] >= x:
            right = mid - 1
        else:
            left = mid + 1
    if ln[mid] >= x:
        return mid - 1
    else:
        return mid


ans = 0
for a in range(n-2):
    for b  in range(a+1, n-1):
        cmax = ln[b] + ln[a]
        ans += max(0, binary_search(cmax, b) - b)

print(ans)

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

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

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

Solution (Python):


from bisect import bisect_left

N = int(input())
L = sorted([int(l) for l in input().split()])

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

print(cnt)

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


def lcm(a, b):
    return a * b // gcd (a, b)





N = inputI()
L = inputIL()

L.sort()

aa = [0 for i in range(1001)]
for l in L:
  aa[l] += 1

s = list(set(L))
s.sort()

count = 0
for a in range(len(s)):
  for b in range(a,len(s)):
    for c in range(b,len(s)):
      if s[a]+s[b]>s[c]:
        if s[a]!=s[b] and s[b]!=s[c]:
          count += aa[s[a]]*aa[s[b]]*aa[s[c]]
          continue
        if s[a] == s[c]:
          count += aa[s[a]]*(aa[s[a]]-1)*(aa[s[a]]-2)//6
          continue
        if s[a] == s[b]:
          count += aa[s[a]]*(aa[s[a]]-1)*aa[s[c]]//2
          continue
        if s[b] == s[c]:
          count += aa[s[a]]*aa[s[b]]*(aa[s[b]]-1)//2
          continue
        
print(count)

Solution (Python):
import bisect

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

l.sort()

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