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):
n,*l = map(int, open(0).read().split())
 
for i in range(n):
    min = 1001
    min_j = -1
    for j in range(i, n):
        if l[j] < min:
            min = l[j]
            min_j = j
    l[i], l[min_j] = l[min_j], l[i]
    
ans = 0
for i in range(n):
    for j in range(i+1, n):
        big = l[i]+l[j]
        
        if l[i] > l[j]:
            small = l[i] - l[j]
        else:
            small = l[j] - l[i]
        
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < big:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        big_i = ch_max
        
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < small:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        small_i = ch_min
        
        ans += big_i - small_i
 
        if i < big_i and i >= small_i:
            ans -= 1
        if j < big_i and j >= small_i:
            ans -= 1
                
print(ans)

Solution (Python):
n = int(input())
l = sorted(map(int, input().split()))
 
ans = 0
for i in range(n):
    for j in range(i+1, n):
        big = l[i]+l[j]
    
        ch_max = n
        ch_min = j+1
        
        while ch_min < ch_max:
            ch_i = (ch_min + ch_max) // 2
            if l[ch_i] < big:
                ch_min = ch_i + 1
            else:
                ch_max = ch_i
                
        big_i = ch_max
        
        ans += big_i - (j + 1)
                
print(ans)

Solution (Python):
number_of_sticks = int(input())

sticks = [int(x) for x in input().split(' ')]

sticks.sort()

triangles = 0

import bisect

for i in range (number_of_sticks - 2):
    for j in range (i+2, number_of_sticks):
        max_range = j
        min_range = max(bisect.bisect(sticks, sticks[j] - sticks[i]), i + 1)
        triangles += max_range - min_range

print(triangles)

Solution (Python):
import bisect

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

cnt = 0
for first in range(N):
    for second in range(first+1,N):
        bnd = bisect.bisect_left(L,L[first]+L[second]) - 1
        dif = bnd - second
        if dif > 0:
            cnt += dif

print(cnt)

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

Solution (Python):
import bisect
n = int(input())
an = [int(i) for i in input().split()]
an.sort()
ans = 0

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

Solution (Python):
import bisect
def solve(n, l):
    l = sorted(l)
    res = 0
    for i in range(n):
        for j in range(i+1, n-1):
            a, b = l[i], l[j]
            k = bisect.bisect_left(l, a+b) - 1
            res += max(0, k-j)
    return res

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

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

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

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

def binary_search(key,l,n):
    key_number = int((n-1)//2)
    L = 0
    R = n-1
    while True:
        if l[key_number] < key:
            L = key_number
            key_number = (L+R)//2
        else:
            R = key_number
            key_number = (L+R)//2

        if key == l[key_number]:
            return key_number
        if (R-L) == 1:
            return L+1

def binary_search(key,l,n):
    L = 0
    R = n-1
    while L < R:
        key_number = int((n-1)//2)
        if l[key_number] < key:
            L = key_number + 1
        else:
            R = key_number
    return key_number

ans = 0


for i in range(n-2):
    for j in range(i+1,n-1):
        a = l[i]
        b = l[j]

        ans += bisect.bisect_left(l,a+b)- j -1

        

        
        
print(ans)

Solution (Python):
N = int(input())
L = list(map(lambda x:int(x)*(-1),input().split()))
L.sort()
count = 0
import bisect
for i in range(N-1):
  for j in range(i+1,N-1):
    a = bisect.bisect_left(L,L[i] - L[j])
    if a - j - 1 <= 0:
      count += 0
    else:
      count += a - j - 1
print(count)

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

L.sort(reverse=True)


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

print(count)

Solution (Python):
N = int(input())
L = sorted([int(i) for i in input().split()])
from bisect import bisect_left
ans = 0
for i1,a in enumerate(L[:-2]):
    L_temp = L[i1+1:-1]
    for i2,b in enumerate(L_temp):
        L_temp2 = L[i1 + i2 + 2:]
        ans += bisect_left(L_temp2,a+b)
print(ans)

Solution (Python):
N = int(input())
L = sorted([int(i) for i in input().split()])
from bisect import bisect_left
ans = 0
for i1,a in enumerate(L):
    L_temp = L[i1+1:]
    for i2,b in enumerate(L_temp):
        L_temp2 = L_temp[i2+1:]
        
        ans += bisect_left(L_temp2,a+b)
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):
        wa = l[i] + l[j]
        indent = bisect.bisect_left(l,wa)
        
        ans += indent - j - 1
print(ans)

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

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

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

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

Solution (Python):
import bisect

n = int(input())
l = list(map(int, input().split()))
ans = 0
length = len(l)
l.sort()
 
for i in range(n):
    for j in range(i+1,n):
        k = bisect.bisect_left(l,l[i]+l[j])
        ans += max(k - (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()
ans = 0
for a in range(N-2):
    for b in range(a+1, N-1):
        ans += bisect_left(L[b+1:], L[a]+L[b])
print(ans)

Solution (Python):
from bisect import bisect_right

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

for i in reversed(range(2, n)):
    for j in reversed(range(1, i)):
        fix = bisect_right(order, order[i]-order[j])
        if j > fix:
            ans += j - fix

print(ans)

Solution (Python):
from bisect import bisect_right

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

for i in reversed(range(2, n)):
    for j in reversed(range(1, i)):
        ans += j - bisect_right(order[0:j], order[i]-order[j])

print(ans)

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


sticks = sorted(sticks1, reverse=True)

count = 0
jmax = N-2
kmax = N-1
i=0
while i<N-2:
    j= i+1
    while j<jmax+1:
        k=kmax
        while j<k:
            
            if sticks[i]<sticks[j]+sticks[k]:
                count += k-j
                kmax = k
                break
            else:
                k -= 1
                continue
        jmax = kmax-1
        j += 1
    kmax = N-1
    jmax = N-2
    i += 1
      
print(count)

Solution (Python):

import sys
import bisect as bi

def solve(N: int, L: "List[int]"):
    L.append(2001)
    L.sort()
    sum=0
    if len(L)<3:
        print(sum)
        return

    for i in range(N-2):
        for j in range(i+1,N-1):
            tansum=L[i]+L[j]
            if tansum<=L[j+1]:
                continue
            sum=sum+bi.bisect_left(L,tansum,j+1,N+1) - (j+1)
    print(sum)
    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):

import bisect

N = int(input())
L =[int(i)for i in input().split()]
L.sort()
ans = 0
for i in range(1,N):
    for j in range(i):
        len = L[i] - L[j] +1
        ind = bisect.bisect_left(L,len) -1
        ans += max(j-ind-1,0)
        
print(ans)

Solution (Python):
from bisect import bisect_left,bisect_right
import sys
def input(): return sys.stdin.readline().strip()

def main(bisect_left=bisect_left, bisect_right=bisect_right):
    n=int(input())
    ll=sorted(list(map(int,input().split())))
    c=0
    lll=len(ll)
    for i in range(lll-2):
        for j in range(i+1, lll-1):
            uk = bisect_left(ll, ll[i] + ll[j], j+1)
            lk = bisect_right(ll, abs(ll[j] - ll[i]), j+1, uk)
            c+=uk-lk
    print(c)

main()

Solution (Python):
from bisect import bisect_left

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

ans = 0
for i in range(n):
    for j in range(i+1, n):
        a, b = l[i-1], l[j-1]
        ab = a + b
        c = bisect_left(l, ab)
        ans += c - 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-1):
    for j in range(i+1,n):
        min_c=j+1
        max_c=bisect.bisect_left(l, l[j]+l[i])-1
        num=max_c-min_c+1

        ans += num if num > 0 else 0
print(ans)

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

i=1
j=0
k=0
ta=0
rb=0
tc=0
cnt=0
m=0
ls=sorted(l)



for i in range(0,len(ls)-2):

    for j in range(i+1,len(ls)-1):
        l1 = ls[i]
        l2 = ls[j]
        ll= l1+l2
        
        k = bisect.bisect_left(ls,ll) - 1

        cnt+=k-j
print(cnt)

Solution (Python):

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


    n = II()
    l = LMIIS()
    a = [l[i] + l[j] for i in range(n) for j in range(i+1, n)]

    a.sort()
    ans = 0
    for ll in l:
        x =  bisect.bisect(a, ll)
        ans += x
    print(n*(n-1)*(n-2)//6 - ans)

if __name__ == '__main__':
    main()

Solution (Python):
import bisect

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

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

print(cnt)

Solution (Python):

n = int(input())
A= [int(c) for c in input().strip().split(' ')]

A = sorted(A)
cnt =0
for i in range(n-1,1,-1):
    l,r=0,i-1

    while(l<r):
        
        if(A[l]+A[r]>A[i]):
            cnt+=r-l
            r-=1
        else:
            l+=1
print(cnt)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for i in range(N):
    a = L[i]
    k = i+2
    for j in range(i+1,N):
        b = L[j]
        while k < N and a+b > L[k]:
            k += 1
        count += k - 1 - j
print(count)

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

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

a = [l[i] + l[j] for i in range(n) for j in range(i+1, n)]

len_a = n*(n-1)//2

a.sort()

ans = 0
for ll in l:
    x =  bisect.bisect(a, ll)
    ans += x

print(n*(n-1)*(n-2)//6 - ans)

Solution (Python):
import sys
import bisect
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
MOD = 1000000007
def POW(x, y): return pow(x, y, MOD)
def INV(x, m=MOD): return pow(x, m - 2, m)
def DIV(x, y, m=MOD): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LS(): return input().split()
def II(): return int(input())

N=II()
LS = LI()
LS.sort()

res = 0
for i in range(N):
    for j in range(i+1,N):
        upper = LS[i]+LS[j]
        lower = abs(LS[i]-LS[j])

        ui = bisect.bisect_left(LS, upper, j+1)
        li = bisect.bisect_left(LS, lower+1, j+1)
        num = ui-li
        res += num
print(res)

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

Solution (Python):

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

def main():
    tri = 0
    L.sort()
    for i in range(N-1,-1,-1): 
        mn = 0
        sl = i-1
        while(mn<sl):
            if (L[mn] + L[sl]) > L[i]:
                tri += sl - mn
                sl -= 1
            else:
                mn += 1
    print(tri)
    
main()

Solution (Python):



import bisect

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

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

        answer += max(0, r - l)

print(answer)

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

'''

1 2 3 4

1 < 2+3
2 < 1+3
3 < 1+2

6 - 1 = 5
6 - 2 = 4
6 - 3 = 3

'''

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


def main():
    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):
            a = L[i]
            b = L[j]
            r = bisect.bisect_left(L, b+a, lo=j+1)
            l = bisect.bisect_right(L, b-a, lo=j+1)
            cnt += r - l
    
    print(cnt)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def bi_search(array, target):
    l, r = -1, len(array)-1
    while r-l > 1:
        mid = int((r+l)/2)
        if array[mid] > target:
            r = mid
        else:
            l = mid
    return r

N = int(sys.stdin.readline().strip())
l_list = list(sorted(list(map(int, sys.stdin.readline().strip().split(" ")))))
count = 0


for i in reversed(range(N)):
    a = l_list[i]
    for j in reversed(range(i)):
        b = l_list[j]
        index = bi_search(l_list[:j], a-b)
        """
        print("====")
        print(b-a)
        print(i, j, index)
        """
        if index > -1 and index < j and l_list[index] + b > a:
            count += (j-index)

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

print(ans)

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

import bisect

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

ans = 0
for i in range(n-1, -1, -1):
    for j in range(i-1, 0, -1):
        sub = l[i]-l[j] 
        idx = bisect.bisect_right(l, sub)
        if idx >= j: continue
        ans += j-idx
print(ans)

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

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

print(result)

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

Solution (Python):
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort(reverse=True)
Lb = L[::-1]
ans = 0
for i, l1 in enumerate(L):
    for j, l2 in enumerate(L[i + 1:]):
        c = l1 - l2
        p = max(0, len(L) - (i + j + 2) - bisect.bisect_right(Lb, c))
        ans += p
print(ans)

Solution (Python):
from bisect import bisect_left
N = int(input())
L = list(map(int,input().split()))
ans = 0
L.sort()
for na,a in enumerate(L[:N-2]):
    for nb_,b in enumerate(L[na+1:N-1]):
        nb = nb_+na+1
        nc = bisect_left(L,a+b)
        ans += nc-nb-1
print(ans)

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

input = sys.stdin.readline
 
N = int(input())
l= list(map(int, input().split()))
 
l.sort()
cnt = 0
 
for n,i in enumerate(l[:N-2]):
    for m,j in enumerate(l[n+1:N-1],n+1):
        cnt += max(0,bisect_left(l,i+j)-(m+1)) 
        
print(cnt)

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

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

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

print(ans)

Solution (Python):
import bisect

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

ans = 0
L.sort()

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

Solution (Python):
import bisect

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


for i in range(n):
    for j in range(i+1,n):
        k=l[i]+l[j]-1
        index = bisect.bisect_right(l,k,lo=j+1)
        count+=index-j-1
        

print(count)

Solution (Python):
from bisect import bisect_left, bisect_right
 
def main():
    N = int(input())
    L = [int(c) for c in input().split()]
    L = sorted(L)
    num = 0
    for ai in range(N):
        for bi in range(ai+1, N):
            if bi - ai == 1:
                continue
            a = L[ai]
            b = L[bi]
            ci_left = bisect_right(L, abs(L[ai] - L[bi]))
            ci_right = bisect_left(L, L[ai] + L[bi])
            if ci_left == ci_right:
                continue
            else:
                ci_left = max(ci_left, ai + 1)
                ci_right = min(ci_right, bi)
            
            
                num += ci_right - ci_left
            
 
    print(num)
 
 
if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left

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

l.sort()
count = 0

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

print(count)

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

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

Solution (Python):
import sys
import bisect


def slove():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    a.sort()
    cnt = 0
    for i in range(n-2):
        for j in range(i + 1, n-1):
            p = bisect.bisect_left(a, a[i] + a[j])
            cnt += (p - j - 1)
    print(cnt)


if __name__ == '__main__':
    slove()

Solution (Python):
import bisect

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

m = 0

for a in range(n-2):
	for b in range(a+1,n-1):
		m+=bisect.bisect_left(d,d[a]+d[b])-1-b

print(m)

Solution (Python):
def binary_search(list, item):
    low = 0
    high = len(list)
    
    while low < high:
        mid = (low + high)//2
        if list[mid] < item:
            low = mid+1
        else:
            high = mid
    return low
  
N = int(input())
L = list( map(int, input().split()) )

L.sort()

count = 0
for a in range(0, len(L)-2):
    for b in range(a+1, len(L)-1):
        count += binary_search(L[b+1:], L[a]+L[b])
print(count)

Solution (Python):

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

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input()[:-1])
li = list(map(int,input().split()))
li.sort()
ans = 0
for i in range(n-1,1,-1):
  for j in range(i-1,0,-1):
    l = 0
    r = j
    while r-l > 0:
      p = (r+l)//2
      if li[p] + li[j] > li[i]:
        r = p
      else:
        l = p + 1
    ans += j - r
print(ans)

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

Solution (Python):


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

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

Solution (Python):
import bisect


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

if __name__ == "__main__":
    resolve()

Solution (Python):
import bisect

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

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

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

Solution (Python):
import bisect

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

Solution (Python):
import bisect

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

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

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

count = 0
for i in range(n):
    for j in range(i+1, n):
        index = bisect.bisect_left(ls[j+1:], ls[i] + ls[j])
        count += index
print(count)

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

Solution (Python):
import bisect

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

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

print(cnt)

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

from bisect import bisect_right

L = sorted(L, reverse=True)
L = [-L[i] for i in range(N)]

cnt=0
for i in range(N-2):
    for j in range(i+1, N-1):
        diff = L[i]-L[j]
        idx = bisect_right(L, diff-1, j+1, N)
        cnt+=(idx-j)-1
    
print(cnt)

Solution (Python):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
def search(arr,target,start,last):
    while last > start:
        mid = (last + start)//2
        if arr[mid] < target:
            start = mid + 1 
        else:
            last = mid 
    return start

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 = search(l,target,j+1,n)
        cnt+= f1 - j -1
print(cnt)

Solution (Python):
from bisect import *

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

a.sort()

cn = 0

for i in range(n):
    for j in range(i+1,n):
        
        t = a[i]+a[j]
        ind = bisect_left(a, t)
        cn += len(a)-ind
        
        
d = n*(n-1)*(n-2)//6

print(d-cn)

Solution (Python):

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

    N = II()
    L = LMIIS()
    L.sort()
    count = 0
    for i in range(N):
        a = L[i]
        k = i+2
        for j in range(i+1,N):
            b = L[j]
            while k < N and a+b > L[k]:
                k += 1
            count += k - 1 - j
    print(count)



if __name__ == '__main__':
    main()

Solution (Python):

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

    N = II()
    L = LMIIS()
    L.sort()
    count = 0
    for i in range(N):
        a = L[i]
        k = i+2
        for j in range(i+1,N):
            b = L[j]
            while k < N and a+b > L[k]:
                k += 1
            count += k - 1 - j
    print(count)



if __name__ == '__main__':
    main()

Solution (Python):

import sys

import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

readl= lambda: list(map(int, sys.stdin.readline().split()))
readt= lambda: tuple(map(int, sys.stdin.readline().split()))
read = lambda: sys.stdin.readline().rstrip()
readi = lambda: int(read())
readmi = lambda: map(int, sys.stdin.readline().split())
readms = lambda: map(str, sys.stdin.readline().split())

n = readi()
t = readl()
t = [t[i] * -1 for i in range(n)]
t.sort()
s = 0
while(n >= 3):
    for j in range(1, n):
        p = t[0] - t[j] - 1
        i = bisect.bisect_right(t, p)
        i -= 1
        if i >= j + 1 and t[i] <= p:
            s += i - j
    n -= 1
    t.pop(0)
print(s)

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

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

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

def binary(L):
  left = -1
  right = len(L)
  t = (left+right)//2
  while right-left != 1:
    if L[t] > max_len - next_len:
      left = t
    else:
      right = t
    t = (left + right) // 2
  return left + 1
  
  
for i in range(N-2):
  max_len = L[i]
  for j in range(i+1,N-1):
    next_len = L[j]
    ans += binary(L[j+1:])
print(ans)

Solution (Python):

import bisect

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

L.sort()

cnt = 0
for i, b in enumerate(L):
    for a in L[:i]:
        idx = bisect.bisect_left(L, a+b)
        cnt += idx - (i + 1)

print(cnt)

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

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

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

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

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)