Problem:
 Score : 400 points    Problem Statement Takahashi has N sticks that are distinguishable from each other. The length of the i -th stick is L_i .  He is going to form a triangle using three of these sticks. Let a , b , and c be the lengths of the three sticks used. Here, all of the following conditions must be satisfied:   a < b + c  b < c + a  c < a + b   How many different triangles can be formed? Two triangles are considered different when there is a stick used in only one of them.      Constraints  All values in input are integers.  3 \leq N \leq 2 \times 10^3  1 \leq L_i \leq 10^3         Input Input is given from Standard Input in the following format:  N  L_1  L_2  ...  L_N       Constraints Print the number of different triangles that can be formed.        Sample Input 1 4
3 4 2 1      Sample Output 1 1  Only one triangle can be formed: the triangle formed by the first, second, and third sticks.       Sample Input 2 3
1 1000 1      Sample Output 2 0  No triangles can be formed.       Sample Input 3 7
218 786 704 233 645 728 389      Sample Output 3 23

Solution (Python):
from bisect import bisect_left

n = int(input())
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):
from bisect import bisect_left

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


def get_bisect_left():
    cache = {}

    def _f(x):
        if x in cache:
            return cache[x]
        cache[x] = bisect_left(lll, x)
        return cache[x]

    return _f


bisect_left_cache = get_bisect_left()

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

Solution (Python):
from bisect import bisect_left

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


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

Solution (Python):
from bisect import bisect_left

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

res = 0
for i in range(n):
	l1 = lengths[i]
	for j in range(i + 1, n - 1):
		l2 = lengths[j]
		res += bisect_left(lengths, l1 + l2, j + 1) - j - 1

print(res)

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], j + 1) - 1
        ans += k - j

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left

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

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

print(ans)

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

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

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


if __name__ == '__main__':
    slove()

Solution (Python):
import bisect

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

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

print(ans)

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

print(ans)

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

print(ans)

Solution (Python):
from bisect import bisect_left

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

for i,a in enumerate(L[:N-2]):
    i += 1
    for j,b in enumerate(L[i:N-1]):
        j += i + 1
        n = bisect_left(L,a+b) - j
        result += n
print(result)

Solution (Python):
N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for c in range(2,N):
    cc=L[c]
    for b in range(1,c):
        bb=L[b]
        if cc>=bb+L[b-1]: continue
        l=-1
        u=b-1
        while u-l>1:
            a=(l+u+1)//2
            aa=L[a]
            if aa+bb>cc: u=a
            else: l=a
        ans+=(b-u)
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):
        ab=L[i]+L[j]
        r=bisect.bisect_left(L,ab)
        l=j+1
        ans+=r-l
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):
        ab=L[i]+L[j]
        r=bisect.bisect_left(L,ab)
        l=j+1
        ans+=r-l
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):
        ans += bisect.bisect_left(L[j + 1:], L[i] + L[j])

print(ans)

Solution (Python):
import bisect

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

Solution (Python):
from bisect import bisect_left

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

for aIndex,a in enumerate(L[:N-2]):
    for bIndex,b in enumerate(L[aIndex+1:N-1]):
        bIndex = aIndex + bIndex + 1
        ab = a+b
        n = bisect_left(L,ab) - bIndex - 1
        result += n
print(result)

Solution (Python):

import sys
from bisect import bisect_left, bisect_right

def solve(N: int, L: "List[int]"):
    L.sort()

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

    print(ans)

    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    L = [int(next(tokens)) for _ in range(N)]  
    solve(N, L)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range (N-1,1,-1): 
  s = 0 
  l = i - 1
  while l > s:
    if L[s] + L[l] > L[i]:
      ans = ans + (l - s)
      l = l - 1
    else:
      s = s + 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): 
        
        
        
        
        x = bisect.bisect_left(L, L[i]+L[j])
        
        
        
        
        ans += x - (j + 1)

print(ans)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
C = sorted(L)
count = 0

import bisect
for i in range(N - 2):
    for  j in range(N - i - 2):
        s = C[i] + C[j + i + 1]
        t = bisect.bisect_left(C,s)
        u = t - (j + i + 1) - 1
        if u <= 0:
            pass
        else:
            count = count + u

print(count)

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
L.sort()
ans = 0
i = 0
while i < N - 2:
    j = i + 1
    k = j + 1
    while j < N - 1:
        while k < N:
            if L[i] + L[j] <= L[k]:
                ans += k - j - 1
                break
            k += 1
        if k == N:
            ans += k - 1 - j
        j += 1
    i += 1
print(ans)

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

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

Solution (Python):
from bisect import bisect_left

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

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

print (count)

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
L.sort(reverse=True)
ans=0
for i in range(N-1):
    j=N-1
    k=i+1
    while k<j:
        if L[i]<L[j]+L[k]:
            ans+=(j-k)
            k+=1
        else:
            j-=1
print(ans)

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

Solution (Python):
from bisect import bisect_right
def main():
	N = int(input())
	L = list(map(int, input().split(" ")))
	L.sort()
	k = 0
	for i in reversed(range(2, N)):
		a = L[i]
		for j in reversed(range(1, i)):
			b = L[j]
			index = bisect_right(L, a-b, hi=j)
			k += j - index
	print(k)
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(len(L)-1):
    for j in range(i+1,len(L)-1):
        a = L[i]
        b = L[j]
        c = L[j+1]
        if c-a < b and b < c+a:
            ans += bisect.bisect_left(L,b+a)-j-1
print(ans)

Solution (Python):
import bisect


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

    ans = 0
    for i, a in enumerate(l_list):
        for j, b in enumerate(l_list[i+1:]):
            x = a + b
            temp = bisect.bisect_left(l_list, x)
            ans += temp - (i + 1 + j + 1)
    print(ans)


if __name__ == '__main__':
    main()

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

print(count)

Solution (Python):

n = int(raw_input())
L = map(int, raw_input().split())


L.sort()

ans = 0



for a in xrange(n-2):
	c = a + 2
	for b in xrange(a+1,n-1):
		while c < n and L[a] + L[b] > L[c]:
			c += 1
		if c > b:
			ans += (c-b-1)
print ans

Solution (Python):



import bisect

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




L_sorted = sorted(L)


count = 0
for i in range(0, N - 2):
    
    for j in range(i + 1, N - 1):
        a = L_sorted[i]
        b = L_sorted[j]
        
        
        
        
        
            
        
        
        
        
        
        
        index = bisect.bisect_left(L_sorted, a + b, i + 1, N)
        
        
        count += ((index - 1) - j)
        
print(count)

Solution (Python):

import sys
import bisect

n = int(input())
l = list(map(int, sys.stdin.readline().strip().split()))

l. sort()
ans = 0
for i in range(len(l) - 1):
    for j in range(i + 1, len(l)):
        a = l[i]
        b = l[j]
        ab = a + b
        right = bisect.bisect_left(l, ab) - 1
        ans += (right - j)

print(ans)

Solution (Python):
import sys
import bisect

n = int(input())
l = list(map(int, sys.stdin.readline().strip().split()))

l. sort()
ans = 0
for i in range(len(l)):
    for j in range(i + 1, len(l)):
        a = l[i]
        b = l[j]
        ab = a + b
        right = bisect.bisect_left(l, ab) - 1
        ans += (right - j)

print(ans)

Solution (Python):
import bisect

n = int(input())
l = list(int(i) for i in input().split())
list.sort(l)
count = 0

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

print(count)

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

L.sort()
ans = 0
mem_k = 2
for i in range(N-2):
    for j in range(i+1, N-1):
        for k in range(mem_k, N):
            if L[k] >= L[i] + L[j]:
                ans -= 1
                break
        ans += k - j
        mem_k = max(k, j+1)
    mem_k = i + 2

print(ans)

Solution (Python):
import bisect

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

d = 0

for i in range(n - 2):
    for j in range(i + 1, n - 1):
        b = bisect.bisect_left(lis, int(lis[i] + lis[j]))
        d += b - j - 1
print(d)

Solution (Python):
import bisect

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

d = 0

for i in range(n-2):
    for j in range(i + 1, n-1):
        b = bisect.bisect_left(lis, int(lis[i] + lis[j]))
        d += max(0, b - j - 1)
print(d)

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

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

Solution (Python):
import bisect

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

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

print(ans)

Solution (Python):
import bisect

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

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

Solution (Python):
if __name__ == '__main__':
    N = int(input())
    L = sorted(map(int, input().split()))
    ret = 0
    for adx in range(N):
        cdx = adx
        for bdx in range(adx + 1, N):
            while cdx < N and L[cdx] < L[bdx] + L[adx]: cdx += 1
            ret += cdx - (bdx + 1)
    print(ret)

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

Solution (Python):

import bisect


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


print(ans)

Solution (Python):
import bisect
from sys import exit, stdin
N = int(stdin.readline().rstrip())
L = sorted([int(_) for _ in stdin.readline().rstrip().split()])

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

Solution (Python):
import bisect

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

a = sorted(lis, reverse=True)
c = sorted(lis)
d = 0

for i in range(n):
    for j in range(i + 1, n):
        e = c[:-j-1]
        b = bisect.bisect_right(e, int(a[i]-a[j]))
        d += max(0, len(e) - b)
print(d)

Solution (Python):
from bisect import *
if __name__ == '__main__':
    N = int(input())
    L = sorted(map(int, input().split()))
    print(sum(jdx - bisect(L, a - L[jdx], 0, jdx) for idx, a in enumerate(L) for jdx in range(idx)))

Solution (Python):
import bisect

def main():
    N = int(input())
    base_list=input().split()
    int_list=[]
    i = 0
    [int_list.append(int(i)) for i in base_list]
    int_list=sorted(int_list)
    anslist=[]
    ans=0
    for index,value in enumerate(int_list):
        for index2,value2 in enumerate(int_list[index+1:]):
            ans+=bisect.bisect_left(int_list,value+value2)-(index+1+index2+1)
    print(ans)

if __name__ == "__main__":
    main()

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

def binary_area_search(target_list: list, target_item: int) -> int:
    '''
    @params
    target_list: [1,3,5](ACC)
    target_item: 2

    @return
    return index
    '''
    low = 0
    high = len(target_list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = target_list[mid]
        if guess > target_item:
            if mid == 0:
                return 0
            if target_item > target_list[mid-1]:
                return mid 
            high = mid -1
        else:
            if mid == len(target_list) - 1:
                return None
            if target_list[mid+1] > target_item:
                return mid + 1 
            low = mid + 1
    return None

def main():
    '''
    N^2*logN
    '''
    Ln.sort()
    count = 0
    for a in range(N):
        a_l = Ln[N-a-1]
        for b in range(a+1, N):
            b_idx = N-b-1
            b_l = Ln[b_idx]
            c_threshold = a_l - b_l
            
            c_idx = bisect.bisect_right(Ln,c_threshold)
            
            if c_idx >= b_idx:
                continue
            count += b_idx - c_idx
    print(count)

def main2():
    '''
    N^2
    '''
    Ln.sort(reverse=True)
    count = 0
    for a in range(N):
        a_l = Ln[a]
        current_min = N-1
        b = a +1
        while b <= current_min:
            c_idx = current_min
            bc = Ln[b] + Ln[c_idx]
            if a_l < bc:
                count += (current_min - b)
                b += 1
                continue
            current_min -= 1
    print(count)

def main3():
    '''
    N^2*logN
    '''
    Ln.sort()
    count = 0
    for a in range(N):
        a_l = Ln[N-a-1]
        for b in range(a+1, N):
            b_idx = N-b-1
            b_l = Ln[b_idx]
            c_threshold = a_l - b_l
            
            c_idx = bisect.bisect_right(Ln,c_threshold)
            
            if c_idx >= b_idx:
                continue
            count += b_idx - c_idx
    print(count)

main3()

Solution (Python):
from bisect import bisect_left

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

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

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
L.sort()
answer = 0
for i in range(N-1, 1, -1): 
    s = 0 
    l = i-1
    while l > s:
        if L[s] + L[l] > L[i]:
            answer += l - s
            l -= 1
        else:
            s += 1
print(answer)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
L.sort()
answer = 0
for i in range(N-1, 1, -1): 
    s = 0 
    l = i-1
    while l > s:
        if L[s] + L[l] > L[i]:
            answer += l - s
            l -= 1
        else:
            s += 1
print(answer)

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

def binary_area_search(target_list: list, target_item: int) -> int:
    '''
    @params
    target_list: [1,3,5](ACC)
    target_item: 2

    @return
    return index
    '''
    low = 0
    high = len(target_list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = target_list[mid]
        if guess > target_item:
            if mid == 0:
                return 0
            if target_item > target_list[mid-1]:
                return mid 
            high = mid -1
        else:
            if mid == len(target_list) - 1:
                return None
            if target_list[mid+1] > target_item:
                return mid + 1 
            low = mid + 1
    return None

def main():
    '''
    N^2*logN
    '''
    Ln.sort()
    count = 0
    for a in range(N):
        a_l = Ln[N-a-1]
        for b in range(a+1, N):
            b_idx = N-b-1
            b_l = Ln[b_idx]
            c_threshold = a_l - b_l
            
            c_idx = bisect.bisect_right(Ln,c_threshold)
            
            if c_idx >= b_idx:
                break
            count += b_idx - c_idx
    print(count)

def main2():
    '''
    N^2
    '''
    Ln.sort(reverse=True)
    count = 0
    for a in range(N):
        a_l = Ln[a]
        current_min = N-1
        b = a +1
        while b <= current_min:
            c_idx = current_min
            bc = Ln[b] + Ln[c_idx]
            if a_l < bc:
                count += (current_min - b)
                b += 1
                continue
            current_min -= 1
    print(count)
main()

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

def binary_area_search(target_list: list, target_item: int) -> int:
    '''
    @params
    target_list: [1,3,5](ACC)
    target_item: 2

    @return
    return index
    '''
    low = 0
    high = len(target_list) - 1
    while low <= high:
        mid = (low + high) //2
        guess = target_list[mid]
        if guess > target_item:
            if mid == 0:
                return 0
            if target_item > target_list[mid-1]:
                return mid 
            high = mid -1
        else:
            if mid == len(target_list) - 1:
                return None
            if target_list[mid+1] > target_item:
                return mid + 1 
            low = mid + 1
    return None

def main():
    '''
    N^2*logN
    '''
    Ln.sort()
    count = 0
    for a in range(N):
        a_l = Ln[N-a-1]
        for b in range(a+1, N):
            b_idx = N-b-1
            b_l = Ln[b_idx]
            c_threshold = a_l - b_l
            
            c_idx = bisect.bisect_right(Ln,c_threshold)
            
            if c_idx >= b_idx:
                continue
            count += b_idx - c_idx
    print(count)

def main2():
    '''
    N^2
    '''
    Ln.sort(reverse=True)
    count = 0
    for a in range(N):
        a_l = Ln[a]
        current_min = N-1
        b = a +1
        while b <= current_min:
            c_idx = current_min
            bc = Ln[b] + Ln[c_idx]
            if a_l < bc:
                count += (current_min - b)
                b += 1
                continue
            current_min -= 1
    print(count)
main()

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

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

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

res = 0
for i in range(2,n):
    cnt = 0
    for j in range(i):
        p = l[i]-l[j]
        k = bisect.bisect_right(l,p)
        cnt += i-k
        if k <= j:
            cnt -= 1
    res += cnt
print(res//2)

Solution (Python):
from bisect import bisect_left

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


a.sort()
count = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        x = bisect_left(a, a[i] + a[j])
        count += x - (j+1)

print(count)

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

print(ans)

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

res = 0
for i in range(n):
    s = i + 1
    e = i + 2
    while True:
        while e < n and lst[i] + lst[s] > lst[e]:
            res += 1
            e += 1
        if e >= n:
            res += (e - s - 1) * (e - s - 2) // 2
            break
        else:
            s += 1
            if e - s -1 > 0:
                res += e - s - 1
            if e == s:
                e += 1
print(res)

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

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

    L.sort()

    print(sum(bisect_right(L, L[i] + L[j] - 1, lo=j + 1) - (j + 1) for i, j in combinations(range(N), 2)))

if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    L=list(map(int,input().split()))
    L.sort()
    C=[0]*(3001)
    for l in L: C[l]+=1
    for i in range(3000): C[i+1]+=C[i]
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            ans+=C[L[i]+L[j]-1]-j-1
    print(ans)
resolve()

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


def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = 0
    for a in range(N-1):
        for b in range(a+1, N):
            l = b + 1
            r = bisect_left(L, L[a]+L[b])
            ans += (r - l)
    print(ans)


if __name__ == '__main__': main()

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    L=list(map(int,input().split()))
    L.sort()
    ans=0
    for b in range(1,n-1):
        c=b+1
        for a in range(b):
            while(c<n and L[a]+L[b]>L[c]): c+=1
            ans+=c-b-1
    print(ans)
resolve()

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

Solution (Python):


import sys
from bisect import bisect_left

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
A = LIST()

A.sort()
ans = 0
for i in range(N):
    a = A[i]
    for j in range(i+1, N):
        
        b = A[j]
        
        k = bisect_left(A, a+b)
        ans += k - (j+1)
print(ans)

Solution (Python):
k = 31
K = 1<<k
nu = lambda L: int("".join([bin(K+a)[-k:] for a in L[::-1]]), 2)
st = lambda n: bin(n)[2:] + "0"
li = lambda s: [int(a, 2) if len(a) else 0 for a in [s[-(i+1)*k-1:-i*k-1] for i in range(1001)]]

N = int(input())
A = [int(a) for a in input().split()]
C = [0] * 1001
for a in A:
    C[a] += 1
B = li(st(nu(C) ** 2))
for a in A:
    if a <= 500: B[2*a] -= 1
for i in range(1001):
    B[i] //= 2
for i in range(1, 1001):
    C[i] += C[i-1]
ans = N*(N-1)*(N-2)//6
for i in range(1, 1001):
    ans -= (N-C[i-1]) * B[i]
print(ans)

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

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

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

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

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a = sorted(a)
def trian(a):
    n=len(a)
    res = 0
    
    for x in range(n):
        z=x+2
        
        for y in range(x+1,n):
            
            
            while(z<n and a[x]+a[y]>a[z]):
                z+=1
            
            res += z-y-1
    return res
print(trian(a))

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

Solution (Python):











def bis_insert(L, target, top=None, bottom=None):
    if top is None:
        top = len(L)
    if bottom is None:
        bottom = 0

    while bottom < top:
        half = (top + bottom) // 2
        if target > L[half]:
            bottom = half + 1
        else:
            top = half
    return bottom


def solve(inputs):
    L = string_to_int(inputs)
    L.sort()
    total_count = 0
    for i in range(len(L) - 2):
        for j in range(i + 1, len(L)-1):
            a = L[i]
            b = L[j]
            target = a + b
            r = bis_insert(L, target)
            
            total_count += r - j - 1
    return total_count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


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

Solution (Python):

import bisect
























def solve(inputs):
    L = string_to_int(inputs)
    L.sort()
    total_count = 0
    for i in range(len(L) - 2):
        for j in range(i + 1, len(L)-1):
            a = L[i]
            b = L[j]
            target = a + b
            
            r = bisect.bisect_left(L, target)
            total_count += r - j - 1
    return total_count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


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

Solution (Python):
import sys
import bisect

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def bis_insert(L, target, top=None, bottom=None):
    if top is None:
        top = len(L)
    if bottom is None:
        bottom = 0

    while bottom < top:
        half = (top + bottom) // 2
        if target > L[half]:
            bottom = half + 1
        else:
            top = half
    return bottom


def solve(inputs):
    L = string_to_int(inputs[0])
    L.sort()
    total_count = 0
    for i in range(len(L) - 2):
        for j in range(i + 1, len(L)-1):
            a = L[i]
            b = L[j]
            target = a + b
            
            r = bisect.bisect_left(L, target)
            total_count += r - j - 1
    return total_count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)