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

Solution (Python):
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd 
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
from bisect import bisect_left, bisect_right
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) 

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

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

l.sort()
l.append(10**9)
cnt=0
i=0


while i<n-2:
    j=i+1
    while j<n-1:
        LL=j+1
        RR=n-1
        
        while LL<=RR:
            k=(LL+RR)//2
            if k==n:
                break
            
            
            
            if l[i]+l[j]>l[k] and l[i]+l[j]<=l[k+1]:
                cnt+=k-j
                break
            elif LL==RR:
                break
            elif l[i]+l[j]<=l[k]:
                RR=k
            elif l[i]+l[j]>l[k]:
                LL=k+1
            elif l[i]+l[j]<l[k] and k==j+1:
                break
        j+=1
    i+=1

print(cnt)

Solution (Python):
import bisect

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.bisect_left(l, l[i] + l[j]) - j - 1
print(c)

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

import bisect
L=sorted(L)
comb = 0
for a in range(N-2):
    for b in range(1+a,N-1):
        posIndex = bisect.bisect_left(L, L[a] + L[b])
        comb += posIndex - b - 1 if posIndex != 0 else 0
print(comb)

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

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


import bisect

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

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

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


import bisect

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

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
li.sort(reverse=True)
count = 0

for i in range(n-2):
    for j in range(i+1, n-1):
        left = j
        right = n

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

print(count)

Solution (Python):
import bisect

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

L = sorted(L)

for c in reversed(range(2, N)):
	for b in reversed(range(1, c)):
		a = bisect.bisect_right(L, L[c]-L[b])
		if a<b:
			s += b-a
print(s)

Solution (Python):

import sys

import bisect

def solve(N: int, L: "List[int]"):
    L = sorted(L)
    ct = 0
    for a in range(len(L)-2):
        a_ = L[a]
        for b in range(a+1, len(L)-1):
            b_in = bisect.bisect_left(L,a_+L[b]) - b -1
            if b_in > 0:
                ct+=b_in

            
        
    print(ct)

    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):
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):
import sys
def input(): return sys.stdin.readline().rstrip()
from bisect import bisect_left
def main():
    n=int(input())
    L=[int(_) for _ 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,L[a]+L[b])-b-1
    print(ans)


if __name__=='__main__':
    main()

Solution (Python):
from bisect import *
import itertools
n = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for a,b in itertools.combinations(L,2):
    ans += max(0,bisect_left(L,a+b) - bisect_right(L,abs(a-b)))
    ans -= (2*a > b) + (2*b > a)
print(ans//3)

Solution (Python):
N = int(input())
M = list(map(int, input().split()))
M = sorted(M)
count = 0
for i in range(N):
  a = M[i]
  for j in range(i+1, N):
    b = M[j]
    upper = a+b
    l, r = j, N
    while (r - l) > 1:
      c = (l + r) // 2
      if M[c] < upper:
        l = c
      else:
        r = c  
    
    count += l - j
      
print(count)

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

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

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

Solution (Python):
import bisect

n, *l = map(int, open(0).read().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_num = bisect.bisect_left(l, a+b)  
        ans += c_num - (j+1)  
print(ans)

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

Solution (Python):

import bisect

def solve():
    N = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()

    
    ans = 0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass

    print(ans)

if __name__ == "__main__":
    solve()

Solution (Python):
import bisect


def solve():
    N = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    ans = 0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_i = bisect.bisect_left(Ls, Ls[a_i]+Ls[b_i])
            if c_i > b_i:
                ans += c_i - b_i - 1
            else:
                pass
    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):
from bisect import bisect_left

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

L.sort()

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

print(ans)

Solution (Python):
def main():
    from bisect import bisect_right

    N = int(input())
    *e, = map(int, input().split())

    e.sort()

    ans = 0
    for j in range(2, N):
        for i in range(1, j):
            d = e[j] - e[i]
            ans += max(0, i - bisect_right(e, d))

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from bisect import bisect_right

    N = int(input())
    *e, = map(int, input().split())

    e.sort()

    ans = 0
    for j in range(2, N):
        for i in range(1, j):
            d = e[j] - e[i]
            ans += i - bisect_right(e, d, lo=0, hi=i)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from bisect import bisect_right

    N = int(input())
    *e, = map(int, input().split())

    e.sort()

    ans = 0
    for j in range(N):
        for i in range(j):
            d = e[j] - e[i]
            ans += i - bisect_right(e, d, lo=0, hi=i)

    print(ans)


if __name__ == '__main__':
    main()

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):
N = int(input())
L = list(map(int,input().split()))

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

Solution (Python):
from bisect import bisect_left


def main():
    n = int(input())
    l = [int(x) for x in input().split()]
    l.sort()
    answer = 0
    for i in range(n):
        for j in range(i + 1, n):
            index = bisect_left(l, l[j] + l[i])
            answer += index - j - 1
    print(answer)


if __name__ == '__main__':
    main()

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

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




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

Solution (Python):
from heapq import heappush, heappop
from itertools import permutations, accumulate, combinations
import math
import bisect
import numpy as np
from collections import defaultdict, deque
from operator import itemgetter
from bisect import bisect_left, bisect_right
import sys
input = sys.stdin.readline





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

if __name__ == '__main__':
    main()

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

Solution (Python):
from bisect import bisect_left,bisect_right
from itertools import combinations
N = int(input())
L = sorted(list(map(int,input().split())))
cnt = 0
for x in combinations(range(N),2):
    i,j = x[0],x[1]
    if j<i:
        i,j = j,i
    indr = bisect_left(L,L[i]+L[j])
    indl = bisect_right(L,abs(L[i]-L[j]))
    if j<indl or i>indr:
        cnt += indr-indl
    elif indr>j>=indl and i<indl:
        cnt += indr-indl-1
    elif indr>j and i>=indl:
        cnt += indr-indl-2
    elif j>=indr and indr>i>=indl:
        cnt += indr-indl-1
print(cnt//3)

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





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

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

print(ans)

Solution (Python):
import bisect

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

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

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import bisect
n = int(input())
a = [int(i) for i in input().split()]
a = tuple(sorted(a))
ans = 0
for i in range(n-2):
  for j in range(i+1,n-1):
    chk = a[i]+a[j]
    ind = bisect.bisect_left(a,chk)
    ans += (ind-j-1)
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    from bisect import bisect_left
    N = int(input())
    L = sorted(list(map(int, input().split())))
    ans = 0
    for a in range(N-2):
        for b in range(a+1, N-1):
            ans += bisect_left(L, L[a]+L[b]) - b - 1
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  

N = read_a_int()
L = read_ints()
L.sort()
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        a = L[i]
        b = L[j]
        bigest_idx = bisect_left(L, a + b, lo=j + 1)
        smallest_idx = bisect_right(L, b - a, lo=j + 1)
        tmp = bigest_idx - smallest_idx
        
        
        
        
        ans += tmp
        
print(ans)

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

    def is_ok(l1, l2, l3):
        if A[l2] < A[l1] + A[l3]:
            return True
        else:
            return False

    def binary_search_meguru(l1, l2):
        ng = l1
        ok = l2
        while abs(ok - ng) > 1:
            mid = ng + (ok - ng) // 2
            if is_ok(l1, l2, mid):
                ok = mid
            else:
                ng = mid
        return ok

    ans = 0
    for l1 in range(N):
        for l2 in range(l1+1, N):
            
            ans += l2 - binary_search_meguru(l1, l2)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect


n = int(input())
l = list(map(int, input().split()))
ans = 0
l = sorted(l)
for i in range(n):
    for j in range(i + 1, n):
        sum_ = l[i] + l[j]
        diff = l[j] - l[i]
        right = bisect.bisect_left(l, sum_)
        left = bisect.bisect_right(l, diff)
        if left <= i < right:
            ans -= 1
        if left <= j < right:
            ans -= 1
        ans += right - left
print(ans // 3)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from bisect import bisect_left

n = int(readline())
l = sorted(list(map(int, readline().split())))
ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        ans += bisect_left(l, l[i] + l[j]) - (j + 1)
print(ans)

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

Solution (Python):
from bisect import bisect_left

n = int(input())

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

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

Solution (Python):
from bisect import bisect_left

n = int(input())

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


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

Solution (Python):
n = int(input())
l = [int(x) for x in input().split()]
l.sort()
ans = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    a, b = l[i], l[j]
    s = a+b
    bit_l, bit_r = j+1, n
    while bit_r-bit_l > 1:
      key = (bit_l+bit_r)//2
      if s > l[key]:
        bit_l = key
      else:
        bit_r = key
    if bit_r == n and l[bit_l] >= s:
      bit_r = n-1
    elif bit_l == j+1 and l[bit_l] >= s:
      bit_r = j+1
    ans += bit_r-j-1
print(ans)

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


n = int(input())
li1 = [0]
li2 = list(map(int, input().split()))
li = li1+li2
count = 0
li.sort()
for i in range(1, n-1):
    for j in range(1, n):
        if j>i:
            k = bisect_left(li, li[i]+li[j])
            if k - j -1> 0:
                count += k - j - 1
print(count)

Solution (Python):

import io
import math
import heapq
import bisect

MOD = 10**9+7

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

result = 0
for a in range(N-2):
    for b in range(a+1,N-1):
        c = bisect.bisect_left(array,array[a]+array[b])
        result += c-b-1
            
print(result)

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

Solution (Python):
def low(list, j, tag):
    ok = j
    ng = n
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if list[mid] >= tag:ok = mid
        else:ng = mid
    return ok

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

cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        temp0 = l[i] - l[j]
        temp1 = l[j] + l[i]
        cnt += low(l, j, temp0 + 1) - j
        
print(cnt)

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

l.sort()
from bisect import bisect_left as bl

ans = 0
for i in reversed(range(2,n)):
    for k in range(1 ,i):
        ng = l[i] - l[k] + 1
        tmp = bl(l[:k],ng)
        ans += max(0,k-tmp)
print(ans)

Solution (Python):

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

l.sort()
s=0
for i in range(n):
  if i+2<n:
    k=i+2
  else:
    break
  for j in range(i+1,n):
    while (k<n and l[k]<l[i]+l[j]):
      k+=1
    s+=k-(j+1)
print(s)

Solution (Python):
def main():
    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)
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):
import bisect


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

for i in range(N - 2):
    c = L[i]
    
    for j in range(i + 1, N - 1):
        b = L[j]
        idx = bisect.bisect_left(L, c+b)
        cnt += idx - j - 1
        
print(cnt)

Solution (Python):
N = int(input())
L = sorted(map(int, input().split()), reverse=True)
cnt = 0

for i in range(N - 2):
    c = L[i]
    
    for j in range(i + 1, N - 1):
        b = L[j]
        l = j
        r = N - 1
        
        while True:
            idx = -(-(l + r) // 2)
            
            if L[idx] > c - b:
                if l == idx:
                    break
                l = idx
            else:
                if r == idx:
                    break
                r = idx
                
        cnt += l - j
        
print(cnt)

Solution (Python):
import bisect

N = int(input())

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

def calculate(n,arr):
    arr = sorted(arr)
    sum = 0
    for i in range(n-1):
        for j in range(i+1,n):
            mmm = arr[j] + arr[i] - 1
            nnn = bisect.bisect_right(arr,mmm)
            sum = sum + nnn - (j + 1)

    print(sum)


calculate(N,ARR)

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

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

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

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

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

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

print(ans)

Solution (Python):
import bisect
N=int(input())
L=list(map(int,input().split()))
i=0
L=sorted(L)
for a in range(N):
  for b in range(a+1,N):
    c=bisect.bisect_left(L,L[a]+L[b])
    if c>b:
      i+=c-b-1
    else:
      i=i
print(i)

Solution (Python):
N = int(input())
*L, = map(int, input().split())
L.sort()
count = 0
for a in reversed(range(0, N)):
  for b in range(1, a):
    range_c = L[:b]
    key = L[a] - L[b]
    left = -1
    right = len(range_c)
    while right - left > 1:
      mid = int((left + right) / 2)
      if range_c[mid] > key:
        right = mid
      else:
        left = mid
    count += len(range_c)-right
print(count)

Solution (Python):
import bisect


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

L.sort()

ans = 0
for i in range(0, N):
    for j in range(i + 1, N):
        left = bisect.bisect_right(L, L[j] - L[i])
        right = bisect.bisect_left(L, L[j] + L[i])
        res = right - left
        if left <= i and i <= right:
            res -= 1
        if left <= j and j <= right:
            res -= 1
        ans += max(0, res)

print(ans // 3)

Solution (Python):
from bisect import bisect_left, bisect_right

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


cnt = 0
for i in range(n):
    for j in range(i + 1, n):
        p = L[i]
        q = L[j]
        hidari = max(i + 1, bisect_left(L, q - p + 1))
        migi = min(j - 1, bisect_right(L, p + q - 1))
        delta = migi - hidari
        if delta >= 0:
            cnt += delta + 1
print(cnt)

Solution (Python):
import bisect

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

l.sort()

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

print(ans)

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

L = sorted(L)




ans = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1, N-1):
        
        b = L[j]
        left = j+1
        right = N-1
        c = L[N-1]
        if a+b>c:
            
            
            ans += N-j-1
        else:
            mid = right
            while left < right:
                if mid == (left+right)//2:
                    break
                mid = (left+right)//2
                
                c = L[mid]
                if a+b>c:
                    left = mid
                else:
                    right = mid
                
            
            if a+b>c:
                
                ans += mid - j
            else:
                
                ans += mid - j - 1
        

print(ans)

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

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

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

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

Solution (Python):
import bisect

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

for ind_a, a in enumerate(L):
    for ind_b, b in enumerate(L[ind_a+1:]):
        x = bisect.bisect_left(L[ind_b+ind_a+2:], a+b)
        ans += x

print(ans)

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

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

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

Solution (Python):
from bisect import bisect_left, bisect_right
n = int(input())
nums = [int(i) for i in input().split()]

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

Solution (Python):


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



import bisect

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

print(count)

Solution (Python):
import bisect

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

count  =0
L.sort()
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)

"""
    失敗:回答は出るがTLE:ランダム配列1000本で約6.2秒
    index = bisect.bisect_left(L[j+1:],L[i] + L[j]) 
    count += index
"""
print(count)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
C = sorted(L)
def X(n,j):
    L1 = j+1
    L2 = N-1
    while L1 != L2:
        a = (L1+L2)//2
        if C[a] == n:
            while C[a] == C[a-1]:
                a = a-1
            return len(C[j+1:a])
        elif L2 - L1 <= 1:
            if C[L2] < n:
                return len(C[j+1:L2+1])
            elif C[L1] < n <= C[L2]:
                return len(C[j+1:L2])
            while C[L1] == C[L1-1]:
                if j+1 <= L1-1:
                    L1 = L1-1
                else:
                    break
            if C[L1] > n:
                return len(C[j+1:L1])
            elif C[L1] == n:
                return len(C[j+1:L1])
        elif C[a] < n:
            L1 = a
        else:
            L2 = a
    if C[L1] < n:
        return 1
    else:
        return 0
ans = 0

for i in range(N-2):
    for j in range(i+1,N-1):
        
        ans += X((C[i]+C[j]),j)
print(ans)

Solution (Python):
n=int(input())
l=sorted(list(map(int,input().split())),reverse=True)
ans=0
for c in range(n-2):
    for b in range(c+1,n-1):
        flag=0
        if l[c]-l[b]+1>l[b+1]:
            flag=1
        elif l[c]-l[b]+1<=l[n-1]:
            ans+=(n-1-b)
            flag=1
        m=n-1
        h=b+1
        while flag!=1:
            x=(h+m)//2
            bef=x
            if l[x]>l[c]-l[b]+1:
                h=x
                x=(m+h)//2
            elif l[x]<l[c]-l[b]+1:
                m=x
                x=(m+h)//2
            else:
                if x==n-1:
                    ans+=(x-b)
                    break
                if l[x+1]==l[c]-l[b]+1:
                    h=x
                    x=(m+h)//2
                else:
                    ans+=(x-b)
                    break
            if bef==x:
                ans+=(x-b)
                break
print(ans)

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

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

print(s)

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

ans = 0

import bisect

for a in range(N):
    for b in range(a):
        A=L[a]
        B=L[b]
        c=bisect.bisect_left(L, A+B)
        ans+=c-a-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):
        A=l[a]
        B=l[b]
        c=bisect.bisect_left(l,A+B)
        ans+=c-a-1
print(ans)

Solution (Python):
def main():
  import bisect
  n=int(input())
  l=sorted(list(map(int,input().split())))
  ans=0
  for a in range(n-2):
    for b in range(a+1,n-1):
      ans+=bisect.bisect_left(l,l[a]+l[b])-b-1
  print(ans)
if __name__=='__main__':
  main()