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 = int(input())
L = sorted([int(x) for x in input().split()])

def solve(N, L):
    answer = 0
    for i in range(2, N):
        c = L[i]
        a = 0
        b = i-1
        while a < b:
            if c >= L[a] + L[b]:
                a += 1
            else: 
                answer += b - a
                b -= 1
                
    return answer

def check(N, L):
    from itertools import combinations

    answer = 0
    for a, b, c in combinations(L, 3):
        if a < b + c and b < a + c and c < a + b:
            
            answer += 1
    return answer


print(solve(N, L))

Solution (Python):
from bisect import bisect_left
from bisect import bisect_right

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

l.sort()

ll = []

for i in range(n):
    for j in range(i+1,n):
        ll.append(l[i]+l[j])

ll.sort()

ans = 0

for i in range(2,n):
    s = bisect_right(ll,l[i])
    ans += i*(i-1)//2 - s

print(ans)

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

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

print(result)

Solution (Python):
import bisect

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

t=0
c=0
for i in range(N-2):
    for j in range(i+1,N-1):
        t=A[i]+A[j]
        num=bisect.bisect_left(A,t)-1
        c+=max((num-j),0)

print(c)

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

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

print(counter)

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
print(result)

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

Solution (Python):
import bisect

def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))
    t = 0
    l = len(L) - 1
    for ai, a in enumerate(L):
        ci = ai + 1
        for bi, b in enumerate(L[ai + 1:], ai + 1):
            ci = bisect.bisect_left(L, a + b, ci) - 1
            t += ci - bi
    print(t)


main()

Solution (Python):
import bisect

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

ans = 0

for b in range(n):
    for a in range(b):
        ab = d[a] + d[b]
        r = bisect.bisect_left(d, ab)
        l = b+1
        ans += max(0, r-l)

print(ans)

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

cnt = 0

for i in range(n - 1, 0, -1):
    for j in range(i - 1, -1, -1):
        base = l[i] - l[j]
        idx = bisect.bisect_right(l, base)
        cnt += max(j - idx, 0)
print(cnt)

Solution (Python):
import bisect

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

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

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

Solution (Python):
from bisect import bisect_left

N = int(input())

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

L.sort()

con = 0

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


print(con)

Solution (Python):
n = int(input())
l = sorted(list(map(int,input().split())))
 
count = 0 
 
for i in range(n - 2):
    for j in range(i+1,n-1):
        
        if l[i] + l[j] > l[n-1]:
            count += n-j-1
            
            continue
        
        low = j+1
        high = n-1
        
        while low < high:
            
            mid = (low + high)//2
            
            if l[i] + l[j] > l[mid]:
                
                low = mid +1
            
            else:    
                
                high = mid 
        else:
            
            count += low - j -1
print(count)

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

print(count)

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())


n = inp()
L = inpl()

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

Solution (Python):
from bisect import bisect_left
INF = 10**4
N = int(input())
nums = list(map(int, input().split()))

nums.sort()
nums.append(INF)
ans = 0
for i in range(N):
  for j in range(i+1, N):
    k = bisect_left(nums, nums[i]+nums[j])
    if k-1 > j:
      ans += k-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(len(l)-2):
    for j in range(i+1, len(l)-1):
        a1 = l[i] + l[j]
        index = bisect.bisect_left(l, a1)
        if index:
            ans += index - 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(0, N-2):
  for j in range(i+1, N-1):
    index = bisect.bisect_left(L, L[i]+L[j])
    count += max(0, index-(j+1))
print(count)

Solution (Python):
import bisect


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



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

Solution (Python):
import bisect

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

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

Solution (Python):

from bisect import bisect_left

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

        
        ans += max(0, idx-j-1)

print(ans)

Solution (Python):

from bisect import bisect_left

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

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

print(ans)

Solution (Python):
import bisect    
    
N = int(input())
b = list(int(i) for i in input().split())
b.sort()
sum_N = 0
for i,x in enumerate(b):
    s = i+1
    for y in range(i+1,N,1):
        index = bisect.bisect_left(b,x+b[y])
        sum_N += max(0,index - y -1)
print(sum_N)

Solution (Python):

from bisect import bisect_left

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

print(ans)

Solution (Python):
from bisect import bisect_right

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

print(ans)

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

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

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

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

Solution (Python):
import bisect

def main():
    n = int(input())
    L = list(map(int,input().split()))
    L = sorted(L)
    ans = 0
    for i in range(2,n):
        for j in range(1,i):
            left = bisect.bisect_left(L,L[i]-L[j]+1)
            if j-left > 0:
                ans += j-left
    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
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[j+1:], L[i]+L[j])
        count += k

print(count)

Solution (Python):
import bisect

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

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

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

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

print(ans)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
l = sorted(l)
ans = 0
for c in range(n):
    a=0
    b=c-1
    while a<b:
        if l[a]+l[b]>l[c]:
            ans+=b-a
            b-=1
        else:
            a+=1
print(ans)

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

ans=0
for c in range(n-1,1,-1):
    a=0
    b=c-1
    while(a<b):
        
        if(L[a]+L[b]>L[c]):
            ans+=b-a
            b-=1
        else:
            a+=1
            
print(ans)

Solution (Python):
import sys
import bisect


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


if __name__ == '__main__':
    solve()

Solution (Python):

from bisect import bisect_left

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

count = 0

for i in range(len(L)-2):
    for j in range(i+1,len(L)-1):
        count += bisect_left(L,L[i]+L[j]) - j - 1
        
print(count)

Solution (Python):
import sys
import bisect


input = sys.stdin.readline

def main():
    N = int(input())
    L = sorted(map(int, input().strip().split()))

    bleft = bisect.bisect_left
    bright = bisect.bisect
    ans = 0

    for i, a in enumerate(L):
        if i > N-2:
            break
        for j, b in enumerate(L):
            if j > N-1:
                break
            if j <= i:
                continue
            c_right = bleft(L[j+1:], a+b)
            ans += c_right
    print(ans)

if __name__ == '__main__':
    main()

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

cnt = 0

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

print(cnt)

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

res = 0


for i in range(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):
import sys
import math
from itertools import accumulate as acc
import bisect

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

x.sort()
ans=0
for i in range(2,N):
    for j in range(1,i):
        p=x[i]-x[j]
        if p<x[j]:
            k=bisect.bisect_right(x,p)
            ans+=j-k
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, 1, -1):
  a = 0
  b = i-1
  while a<b:
    if L[a]+L[b] > L[i]:
      ans += b-a
      b -= 1
    else:
      a += 1
          
print(ans)

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

Solution (Python):
import bisect

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

L.sort()

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

Solution (Python):
from bisect import bisect_right, bisect_left

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

for i in range(N-2):
    for j in range(i+1,N-1):
        a = L[i]
        b = L[j]
        left = abs(a-b)
        right = a+b
        q = max(j+1,bisect_left(L,right))
        w = max(j+1,bisect_right(L,left))
        ans += q-w
print(ans)

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

Solution (Python):
from bisect import bisect_left

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

print(ans)

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

Solution (Python):
import bisect
N = int(input())
count = 0
List = sorted(list(map(int, input().split())))
for i in range(N-2):
  for j in range(N-i-2):
    a = List[i]
    b = List[i+j+1]
    index = bisect.bisect_left(List, a+b)
    count += index-i-j-2
print(count)

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


bar_list.sort()

cnt = 0


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

Solution (Python):
import bisect

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

for i in range(n):
    for j in range(i+1,n):
        T=bisect.bisect_left(L,L[i]+L[j])
        count += T-j-1

print(count)

Solution (Python):
import bisect

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

for i in range(N):
    for j in range(i+1,N):
        T=bisect.bisect_left(L,L[i]+L[j])
        count+=T-j-1

print(count)

Solution (Python):
import bisect

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

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

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

print(ans)

Solution (Python):
import bisect

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

l.sort()
ans_set = set()
ans = 0

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

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  l.sort()
  ln=len(l)

  ans=0
  for i,x in enumerate(l):
    for j,y in enumerate(l[i+1:]):
      ans+=bisect.bisect_left(l,x+y)-(i+j+2)

  return ans


print(main())

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

Solution (Python):
import bisect

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

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	b = ListL[i-1]
	c = a - b + 1
	indexb = i - 1
	indexc = bisect.bisect_left(ListL, c)
	while(indexb > indexc):
		if (ListL[indexb] + ListL[indexc] > a):
			ans += indexb - indexc
			indexb -= 1
		else:
			b = ListL[indexb]
			c = a - b + 1
			indexc = bisect.bisect_left(ListL, c)

print(ans)

Solution (Python):
n = int(input())
ListL = [int(i) for i in input().split()]

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	b = ListL[i-1]
	c = a - b + 1
	indexb = i - 1
	indexc = 0
	while(indexb > indexc):
		if (ListL[indexb] + ListL[indexc] > a):
			ans += indexb - indexc
			indexb -= 1
		else:
			indexc += 1

print(ans)

Solution (Python):
import bisect

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

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	b = ListL[i-1]
	c = a - b + 1
	indexb = i - 1
	indexc = bisect.bisect_left(ListL, c)
	while(indexb > indexc):
		if (ListL[indexb] + ListL[indexc] > a):
			ans += indexb - indexc
			indexb -= 1
		else:
			indexc += 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):
    a = l[i]
    for j in range(i+1, n):
        b = l[j]
        ans += bisect.bisect_left(l, a+b)-j-1

print(ans)

Solution (Python):
import bisect

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

ListL.sort()
ans = 0

for i in reversed(range(2, n)):
	a = ListL[i]
	for j in reversed(range(1, i)):
		b = ListL[j]
		index = bisect.bisect_right(ListL, a-b)
		
		if (index >= j):
			break
		else:
			ans += j - index

print(ans)

Solution (Python):
import bisect

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

total = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        c = bisect.bisect_left(l, l[a] + l[b]) - b - 1
        total += c

print(total)

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,L[i]+L[j])-j-1

print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    numbers = list(map(int, input().split()))
    numbers.sort()
    sol = 0
    counter = 0
    for i in range(n):
        for j in range(i+ 1, n):
            x= numbers[i] + numbers[j]
            left = j + 1
            right = n - 1
            ok = -1
            while left <= right:
                mid = (left + right) // 2
                if numbers[mid] < x:
                    ok = mid
                    left = mid + 1
                else:
                    right = mid - 1
            sol = ok
            if sol != -1:
                counter = counter - j + sol
    print(counter)

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

def meguru_nibutan(ar,m,key):
    a = m
    b = len(ar)
    while(abs(a-b) > 1):
        c = (a+b)//2
        if (ar[c] > key):
            a = c
        else:
            b = c
    return a

def main():
    cnt = 0
    l.sort()
    for a in range(0,n-2):
        for b in range(a+1,n-1):
            
            cnt += bisect.bisect_left(l,l[a]+l[b]) - 1 - b
    print(cnt)

if (__name__ == "__main__"):
    main()

Solution (Python):
import bisect

def triangle_check(a, b, c):
    if c < a + b:
        return True
    else:
        return False
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
cnt = 0
for i in range(N - 2):
    for k in range(i + 1, N - 1):
        tmp = bisect.bisect_left(L, L[i] + L[k])
        ans += max(tmp - 1 - k, 0)
print(ans)

Solution (Python):

def binary_search(cur: int, upper_bound: int, lower_bound: int) -> int:
    ok, ng, ret = upper_bound, lower_bound, 0
    while abs(ok - ng) >= 1:
        if L[ok] + L[ng] > cur:
            ret += ok - ng
            ok -= 1
        else:
            ng += 1
    return ret


def main():
    
    global L
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = sum(binary_search(L[i], i - 1, 0) for i in range(N - 1, 1, -1))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect as b

n = int(input().strip())
l = list(map(lambda x: int(x.strip()), input().split()))

l.sort()
ans = 0

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

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

Solution (Python):
from  bisect import bisect_left

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

l.sort()
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):
import bisect
n = int(input())
l = sorted(list(map(int, input().split())))
res = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        res += bisect.bisect_left(l[j+1:], l[i] + l[j])
print(res)

Solution (Python):
import bisect

n = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range(1,n):
  for j in range(i):
    if j > bisect.bisect_right(L,L[i]-L[j]):
      ans += j-bisect.bisect_right(L,L[i]-L[j])
print(ans)

Solution (Python):




import bisect

n = int(input())

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


ret = 0

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

print(ret)

Solution (Python):
def main():
    n = int(input())
    l = list(map(int,input().split()))
    l.sort()
    l.append(10**7)
    ans=0

    for i in range(n-2):
        for j in range(i+1,n-1):
            ok=j
            ng=n
            while ng-ok>1:
                mid = (ok + ng)//2
                if l[mid] < l[i] + l[j]:
                    ok=mid
                else:
                    ng=mid  
            ans+=ok-j
            
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect_left


def p_d():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = N * (N - 1) * (N - 2) // 6

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

    print(ans)
    
p_d()

Solution (Python):



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

def isOK(key, index):
    if L[index] >= key:
        return True
    else:
        return False
        
def binary_search(key):
    left = -1
    right = len(L)
    
    while right - left > 1:
        mid = left + int((right - left) / 2)
        if isOK(key, mid):
            right = mid
        else:
            left = mid
    return right

count = 0
for a in range(N):
    for b in range(a + 1, N):
        key = L[a] + L[b]
        count += (binary_search(key) - (b + 1))

print(count)

Solution (Python):
from itertools import *

def binary_search(t, v, first, last):
	if last - first == 1:
		if v[first] > t:
			return first
		else:
			return last
	
	mid = (first + last) / 2
	if v[mid] <= t:
		return binary_search(t, v, mid, last)
	else:
		return binary_search(t, v, first, mid)

def ABC143D():
	N = int(raw_input())
	v = sorted(map(int, raw_input().split()))
	
	counter = 0
	for k, l in combinations(range(1, N), 2):
		
		
		b = v[k]
		c = v[l]
		if b*2 < c:
			continue
		counter += k - binary_search(c-b, v, 0, k)
	print counter

ABC143D()

Solution (Python):
import bisect

n = int(input())
li = list(map(int,input().split()))
li2 = sorted(li)
cnt = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        cnt += bisect.bisect_left(li2,li2[i]+li2[j]) - (j+1) 
print(cnt)

Solution (Python):
n = int(input().strip())
a = list(map(int, input().strip().split()))
na = [0]*1024
for x in a:
	na[x] += 1
sa = [0] * 1024
for i in range(1023):
	sa[i+1] = sa[i] + na[i]

ans = 0
for i in range(1, 1024):
	for j in range(i, 1024):
		mink = max(j, j-i+1)
		maxk = min(1022, i+j-1)
		if maxk < mink:
			continue

		if sa[maxk+1] == sa[mink]:
			continue

		if i != j:
			if mink != j:
				ans += na[i]*na[j]*(sa[maxk+1] - sa[mink])
			else:
				ans += na[i]*na[j]*(sa[maxk+1] - sa[mink+1]) + na[i]*na[j]*(na[j]-1)//2
		else:
			if mink != j:
				ans += na[i]*(na[j]-1)//2*(sa[maxk+1] - sa[mink])
			else:
				ans += na[i]*(na[j]-1)//2*(sa[maxk+1] - sa[mink+1]) + na[i]*(na[j]-1)*(na[j]-2)//6
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 - 1):
    for j in range(i + 1, n):
        t = bisect.bisect_left(l, l[i] + l[j])
        if (t > j):
            ans += t - j - 1
print(ans)

Solution (Python):
import bisect
N = int(input())
L = [int(x) for x in input().split()]
L = sorted(L)
ans = 0
for one in range(N - 2):
    for two in range(one+1, N-1):
        ans += bisect.bisect_left(L, (L[one] + L[two]))-two-1
        
print(ans)

Solution (Python):
def bs(L, left, right, bc):
    while left != right:
        mid = (left + right)//2
        if L[mid] > bc:
            right = mid
        else:
            left = mid+1
    if L[left] <= bc:
        return -1
    return left


def solve():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    
    count = 0
    for i in range(1, N-1):
        for j in range(i + 1, N):
            left = 0
            right = i-1
            div = bs(L, left, right, L[j] - L[i])
            if (div != -1):
                count += i - div
                
                

    print(count)


if __name__ == "__main__":
    solve()

Solution (Python):
import bisect

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

count = 0
for n in range(N-2):
    for m in range(n+1,N-1):
        count += bisect.bisect_left(L,L[m]+L[n]) - (m + 1)

print(count)

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

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

Solution (Python):



import bisect

MOD = 10 ** 9 + 7

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

ans = 0
for i in range(n):
    for j in range(i + 2, n):
        k = bisect.bisect_right(ln, ln[j] - ln[i])
        if k < i + 1:
            c = j - (i + 1)
        elif k > j - 1:
            c = 0
        else:
            c = j - k
        ans += c
print(ans)

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

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

Solution (Python):



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

ll.sort()
ans = 0
for i in range(n - 2):
    for j in range(i+1, n - 1):
        lb = j + 1
        ub = n - 1
        b = ll[i] + ll[j]
        if ll[lb] >= b:
            continue
        if ll[ub] < b:
            ans += ub - lb + 1
            continue
        while True:
            if ub == lb:
                ans += ub - j
                break
            t = (lb + ub) // 2 + 1
            if ll[t] < b:
                lb = t
            else:
                ub = t - 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,1,-1):
  for j in range(i-1,0,-1):
    ans+=j-bisect.bisect_left(l,l[i]-l[j]+1,hi=j)
print(ans)

Solution (Python):
def solve(string):
    n, *l = map(int, string.split())
    l.sort(reverse=True)
    nums = 0
    for i, a in enumerate(l[:-2]):
        j, k = i + 1, n - 1
        while (j < k):
            if (a < l[j] + l[k]):
                nums += k - j
                j += 1
            else:
                k -= 1
    return str(nums)
 
 
if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))