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

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

print(ans)

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

N = int(input()) 
L_list = [int(e) for e in input().split()]
index_list = [i for i in range(N)]

L_list.sort(reverse=False)


ans = 0

for i in range(0,N-1):
    for j in range(i+1,N):
        a = L_list[i]
        b = L_list[j]
        
        
        
        if a-b > 0:
            low_limit = a-b
        else:
            low_limit = b-a
        index_left = bisect_right(L_list, low_limit) 

        
        index_right = bisect_left(L_list, a+b) 
        
        
        
        
        OK_c = index_right - index_left
        
        if index_left <= i <= index_right:
            OK_c -= 1
        if index_left <= j <= index_right:
            OK_c -= 1
            
        
        ans += OK_c
    
print(ans//3)

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

N = int(input()) 
L_list = [int(e) for e in input().split()]
index_list = [i for i in range(N)]

L_list.sort(reverse=False)


ans = 0

for comb in combinations(index_list, 2): 
    a = L_list[comb[0]]
    b = L_list[comb[1]]
    
    
    
    if a-b > 0:
        low_limit = a-b
    else:
        low_limit = b-a
    index_left = bisect_right(L_list, low_limit) 

    
    index_right = bisect_left(L_list, a+b) 
    
    
    
    
    OK_c = index_right - index_left
    
    if index_left <= comb[0] <= index_right:
        OK_c -= 1
    if index_left <= comb[1] <= index_right:
        OK_c -= 1
        
    
    ans += OK_c
    
print(ans//3)

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

print(ans)

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

Solution (Python):
import bisect

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

L.sort()
ans = 0
for i in range(N-1):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        c = bisect.bisect_left(L, a+b)
        ans += c-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-1):
  a=L[i]
  for j in range(i+1,N):
    b=L[j]
    right=bisect.bisect_left(L,a+b)
    ans+=right-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-2):
    for j in range(i+1,n-1):
        x = bisect.bisect_left(l, l[i] + l[j], j+1)
        ans += x -j -1
print(ans)

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

sls = sorted(Ls)
count = 0
for i in range(N - 1):
    for j in range(i+1, N):
        a, b = sls[i], sls[j]
        right = bisect.bisect_left(sls, b + a)
        count += right - j - 1
print(count)

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

Solution (Python):
from bisect import bisect_left

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

L.sort()

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

print(ans)

Solution (Python):
N = int(input())
A = sorted(list(map(int, input().split())))
ans = 0
for i in range(N):
    k = i
    for j in range(i + 1, N):
        while k < N and A[k] < A[j] + A[i]:
            k += 1
        ans += (k - 1) - j
print(ans)

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

Solution (Python):
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from collections import deque, Counter, defaultdict
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import math
from functools import reduce
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7
def lcm_base(a, b):
    return (a * b) // gcd(a, b)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def main():
  N = int(readline())
  L = list(map(int, readline().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)
  
  
  
if __name__ == '__main__':
  main()

Solution (Python):
import sys
n = int(sys.stdin.readline())
l = list(map(int, sys.stdin.readline().split()))


l.sort()

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

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

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

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

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

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

def check(x, a):
    if a < x:
        return True
    else:
        return False

def bin_search(x, ok, ng, l):
    while ng - ok > 1:
        mid = (ng + ok) // 2
        if check(x, l[mid]):
            ok = mid
        else:
            ng = mid
    return ok + 1

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

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

L.sort()
ans = 0

for i in range(1,N-1):
    upper = N-1
    for j in reversed(range(0,i)):
        sum = L[i] + L[j]

        if upper == N :
            upper = N-1

        while upper > i and sum <= L[upper]:
            upper -= 1
        ans += upper-i

print(ans)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

L.sort()
ans = 0

for i in range(1,N-1):
    upper = N-1
    for j in reversed(range(0,i)):
        sum = L[i] + L[j]

        if upper == N :
            upper = N-1

        while upper > i and sum <= L[upper]:
            upper -= 1
        ans += upper-i

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
from bisect import bisect_left
N = int(input())



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



d = 0
A.sort()
for i in range(N-2):
    for j in range(i+1,N-1):
        s = A[i]+A[j]
        dex = bisect_left(A,s)
        if dex > j:
          d += dex -j -1
print(d)

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

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

    print(ans)

    
if __name__ == "__main__":
    main()

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

    print(ans)

    
if __name__ == "__main__":
    main()

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

    print(ans)

    
if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect_left

def main():
    N = int(input())
    L = list(map(int, input().split()))
    
    L = sorted(L)
    ans = 0
    for i in range(N-2):
        a = L[i]
        for j in range(i+1, N-1):
            k = bisect_left(L[j+1:], a + L[j])
            ans += k

    print(ans)

    
if __name__ == "__main__":
    main()

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

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

print(ans)

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

Solution (Python):
from bisect import bisect_left

n = int(input())
l = list(map(int, input().split()))
l.sort()
triangle = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    index = bisect_left(l, l[i] + l[j], j + 1, n) - 1
    triangle += index - j
print(triangle)

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

Solution (Python):
from bisect import bisect_left
INF = float("inf")
N = int(input())
L = list(map(lambda x:int(x),input().split())) + [INF]
L.sort()
cnt = 0
for i in range(N-1):
    a = L[i]
    for j in range(i+1,N):
        b = L[j]
        c = bisect_left(L,a+b)
        cnt += max(0,c-j-1)
print(cnt)

Solution (Python):
import bisect
N=int(input())
L=sorted(map(int,input().split()))
cnt=0
for i in range(N):
    for j in range(i):
        a=L[i]
        b=L[j]
        right=a+b
        left=abs(a-b)
        rind=bisect.bisect_left(L,right)
        lind=bisect.bisect_right(L,left)
        res=rind-lind
        if(left<a<right):
            res-=1
        if(left<b<right):
            res-=1
        cnt+=res
print(cnt//3)

Solution (Python):
import sys
import bisect

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

count = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        c = bisect.bisect_left(L, L[i]+L[j])
        if c > j:
            count += c - j - 1
        else:
            pass
        
print(count)

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

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

print(total)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
M = L[-1]
d = [0] * (2*M+1)
cnt = 0
num = 0
for i in range(N-1):
    cnt += 1
    for j in range(L[i], L[i+1]):
        d[j] = cnt
cnt += 1
for i in range(L[N-1], 2*M+1):
    d[i] = cnt
ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        num = L[i] + L[j]
        if num <= M:
            ans += d[num-1] - j - 1
        else:
            ans += N-1-j
print(ans)

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

Solution (Python):
import bisect

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

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

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
A.sort()
ans=int(0)
for i in range(n-2):
    for j in range(i+1,n-1):
        now=A[i]+A[j]
        l=int(j+1)
        r=int(n)
        while r-l>=1:
            x=int((r+l)/2)
            if A[x]<now:
                l=x+1
            else:
                r=x
        ans+=r-j-1
print(ans)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
n=I()
ls=sorted(LI(),reverse=True)
trans=0
for i in range(n-2):
    answer=0
    for j in range(i+1,n-1):
        l1=ls[i]
        l2=ls[j]
        l,r = j,n

        while l+1 < r:

            mid = (l+r)//2
            if l1<l2+ls[mid]:
                l = mid
            else:
                r = mid
        ansij=l-j
        
        answer+=ansij
    trans+=answer
print(trans)

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

def I(): return int(input())
def MI(): return map(int, input().split())

n=I()
bls=list(MI())
ls=sorted(bls, reverse=True)
trans=0
for i in range(n-2):
    answer=0
    for j in range(i+1,n-1):
        l1=ls[i]
        l2=ls[j]
        l,r = j,n

        while l+1 < r:

            mid = (l+r)//2
            if l1<l2+ls[mid]:
                l = mid
            else:
                r = mid
        ansij=l-j
        
        answer+=ansij
    trans+=answer
print(trans)

Solution (Python):
import bisect

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

SL = sorted(L)
ans = 0
for i in range(N):
  if i+1 == N-1:
    break
  a = SL[i]
  for j in range(i+1, N):
    b = SL[j]
    c_max = bisect.bisect_left(SL, a+b)
    ans += c_max - (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):
    
    large = bisect.bisect_left(L, L[j]+L[i])    
    
    ans += max(large-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):
    
    large = bisect.bisect_left(L, L[j]+L[i])    
    
    ans += max(large-j-1,0)    

print(ans)

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

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

Solution (Python):

from bisect import bisect_left, bisect_right
import sys
input = sys.stdin.readline
n = int(input()) 

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

Solution (Python):

from bisect import bisect_left, bisect_right
import sys
input = sys.stdin.readline
n = int(input()) 

L = [int(i) for i in input().split()]
L.sort()
pairs = []
for i in range(n):
    for j in range(i + 1, n):
        pairs.append((i, j))

ans = 0
for i, j in pairs:
    a, b = L[i], L[j]
    ans += bisect_left(L, a + b) - 1 - j
print(ans)

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

L.sort()


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


        left, right = j, N

        while right-left > 1:
            mid=(left+right) // 2
            if L[i]+L[j] <= L[mid]:
                right=mid
            else:
                left=mid
        ans += left - j
        
print(ans)

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
L.sort(reverse=1)
cnt=0
for i in range(N-2):
    for k in range(i+1,N-1):
        num=L[i]-L[k]
        left=k+1
        right=N-1
        while left<=right:
            mid=(left+right)//2
            if num<L[mid]:
                left=mid+1
            else:
                right=mid-1
        cnt+=left-(k+1)
print(cnt)

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

for i in range(n):
    for j in range(i+1,n):
        k = bisect.bisect_left(l,l[i]+l[j])
        if k > j:
            cnt += k - j - 1
        else:
            pass
            
print(cnt)

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

def binary_search_left(list,target):
    '''
    配列に対してある数nを配列のソート順を崩さないように挿入するためのindexを返す関数。
    同一の数が配列に複数含まれていた場合は最も小さいindexを返す。
    '''
    result = -1
    left = 0
    right = len(list) - 1

    while left <= right:
        center = (left + right)//2
        if list[center] == target:          
            if list[center - 1] == target:  
                right = center
            else:                           
                return center
        elif list[center] < target:
            left = center + 1
        elif list[center] > target:
            right = center - 1
    return left                             

L_list = sorted(L_list)

ans = 0
for a_index in range(0, N-2):
    for b_index in range(a_index+1, N-1):
        a = L_list[a_index]
        b = L_list[b_index]
        c_right_index = binary_search_left(L_list, a+b)
        if c_right_index > b_index:
            ans += c_right_index - b_index - 1
        else:
            pass

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

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

L.sort()
import bisect
ans = 0
for i in range(n-2):
    b = L[i]
    for j in range(i+1, n-1):
        c = L[j]
        p = bisect.bisect_left(L, b+c)
        
        ans += max(p-j-1, 0)
print(ans)

Solution (Python):
import bisect

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

cnt = 0
for a in range(n):
    for b in range(a+1, n):
        c = bisect.bisect_left(l, l[a] + l[b])
        if c > b:
            cnt += c - b - 1
print(cnt)

Solution (Python):
from bisect import bisect_left

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

ans=0
for i in range(N):
	for j in range(i+1,N):
		k=bisect_left(M,M[i]+M[j])
		
		ans+=max(0,k-(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):
    a = l[i]
    for j in range(i+1,n):
        b = l[j]
        ans += bisect_left(l,a+b)-j-1
print(ans)

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

from bisect import bisect_left

ans = 0

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

print(ans)

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

Solution (Python):
import bisect
N = int(input())
l = [int(i) for i in input().split()]
count = 0
l_new = sorted(l)
for i,a in enumerate(l_new):
    for j,b in enumerate(l_new[i+1:],start = i+1):
        left = bisect.bisect_right(l_new,abs(a-b))
        right = bisect.bisect_left(l_new,a + b)
        count += right - left
        
        if left <= i < right:
            count -= 1
        if left <= j < right:
            count -= 1
        
print(count//3)

Solution (Python):
import bisect








import sys
sys.setrecursionlimit(10 ** 5 + 10)


def resolve():
    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):

            val=bisect.bisect_left(L,L[i]+L[j])
            cnt+=val-j-1

    print(cnt)

resolve()

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

ans=0
from bisect import bisect_right
for i in range(N-1,-1,-1):
    li = L[i]
    for j in range(i-1,-1,-1):
        lj = L[j]
        k = bisect_right(L, li-lj)
        ans += max(0, j-k)
print(ans)

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

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

Solution (Python):
n= int(input())
l = list(map(int,input().split()))
l.sort()
def ok(a,b,c):
    return a < b+c and b < a+c and c < a+b
ans = 0
for i in range(n):
    for j in range(i+1,n):
        e1 = l[i]
        e2 = l[j]
        top = n
        bot = j
        while top - bot > 1:
            mid = (top+bot)//2
            if ok(l[mid],e1,e2):bot = mid
            else:top  = mid
        
        ans += top-j-1
print(ans)

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

def valid(i, j ,x):
    if x < i + j:
        return True
    else:
        return False

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

Solution (Python):
import bisect

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

for a in range(n):
    for b in range(a):
        over_c = l[a] + l[b]
        over_c_index = bisect.bisect_left(l,over_c)
        quantity = over_c_index - a - 1
        ans += max(quantity,0)
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.buffer.readline
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()

    def is_ok(i, j, k):
        if L[j]+L[k] > L[i]:
            return True
        else:
            return False

    def binary_search_meguru(i, j):
        ng = -1
        ok = j
        while abs(ok - ng) > 1:
            mid = ng + (ok - ng) // 2
            if is_ok(i, j, mid):
                ok = mid
            else:
                ng = mid
        return ok
    ans = 0
    for i in range(2, N):
        
        for j in range(i):
            k = binary_search_meguru(i, j)
            
            ans += j-k
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_right, bisect_left

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

ans = 0
for i, a in enumerate(L):
    for j, b in enumerate(L[i + 1:], start=i + 1):
        left = bisect_right(L, abs(a - b))
        right = bisect_left(L, a + b)
        ans += right - left

        if left <= i < right:
            ans -= 1
        if left <= j < right:
            ans -= 1

print(ans // 3)

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

Solution (Python):
import bisect

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

    l.sort()

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

    print(ans)

if __name__ == '__main__':
    main()

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

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

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
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)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    N = 10**6+1
    bit = [0]*(N+1)
    def add(i):
        while i <= N:
            bit[i] += 1
            i += i&-i
    def sum(i):
        res = 0
        while i:
            res += bit[i]
            i -= i&-i
        return res

    n = I()
    l = LI()
    l.sort()
    ans = 0
    s = []
    k = 0
    for i in range(n):
        li = l[i]
        ans += k-sum(li)
        for j in range(i):
            add(li+l[j])
            k += 1
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
l.sort(reverse=True)
l.append(0)
c = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    z = l[i]-l[j]
    if l[j+1] <= z:
      break
    else:
      a = j+1
      b = n+1
      while a+1 < b:
        m = (a+b)//2
        if l[m] > z:
          a = m
        else:
          b = m
      c += a-j
print(c)

Solution (Python):
mod = 10**9 + 7

def main():
    N = iip(listed=False)
    L = iip(True)
    L.sort()

    ret = 0
    for i in range(N):
        for j in range(i+1, N):
            minl = L[j]-L[i]
            maxl = L[j]+L[i]

            left = searchsorted(L, minl, "right")
            left = max(j+1, left)

            right = searchsorted(L, maxl, "left")

            

            
            ret += (right-left)
    print(ret)






def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    while r-l > 1:
        x = (l+r)//2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        return r
    if side == "right":
        return l+1



def iip(listed):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod


if __name__ == "__main__":
    main()

Solution (Python):
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):
        a = L[i]
        b = L[j]
        ok = j
        ng = N-1
        if b-a < L[ng] and L[ng] < a+b:
            ans += N-1-j
        else:
            while ng - ok > 1:
                mid = (ok+ng) // 2
                if b-a < L[mid] and L[mid] < a+b:
                    ok = mid
                else:
                    ng = mid
       
            ans += ok - j

print(ans)

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

print(cnt)

Solution (Python):
import collections
N=int(input())
L=list(map(int,input().split()))
l=[]
L.sort()
lis=[0]*(max(L)+1)
cnt=0
ans=0
c=collections.Counter(L)
values=list(c.values())	
key=list(c.keys())	
l1=0
l2=0
mi=0
ma=0
le=0
for i in range(len(key)):
    l.append([key[i],values[i]])
l.sort(key=lambda x:x[0])

for i in range(max(L)+1):
    
    cnt=cnt+c[i]
    lis[i]=cnt
for i in range(len(L)-1):
    for j in range(i+1,len(L)):
        l1=L[i]
        l2=L[j]
        ma=min(l1+l2-1,L[len(L)-1])
        mi=max(l2-l1+1,L[0])
        le=lis[ma]-lis[mi-1]
        if mi<=l1<=ma:
            le=le-1
        if mi<=l2<=ma:
            le=le-1
        
        ans=ans+max(le,0)
print(ans//3)