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,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

a.sort()

L = [0] * 2001

co = 0

ans = 0

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

Solution (Python):
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
from bisect import bisect_right
input = lambda: sys.stdin.readline().rstrip()


def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return


def main():
    n = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    cnt = 0
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            
            k =  Ls[i] + Ls[j] -1
            
            
            cnt += max(bisect_right(Ls, k) - j-1, 0)
            
            
    print(cnt)
            

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
from bisect import bisect_right
input = lambda: sys.stdin.readline().rstrip()


def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return


def main():
    n = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    cnt = 0
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            
            k =  Ls[i] + Ls[j] -1
            
            
            cnt += max(bisect_right(Ls, k) - j-1, 0)
            
            
    print(cnt)
            

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
from bisect import bisect_right
input = lambda: sys.stdin.readline().rstrip()


def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return


def main():
    n = int(input())
    Ls = list(map(int, input().split()))
    Ls.sort()
    cnt = 0
    for i in range(0,n-2):
        for j in range(i+1,n-1):
            
            k =  Ls[i] + Ls[j] -1
            
            
            cnt += max(bisect_right(Ls, k) - j-1, 0)
            
    print(cnt)
            

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
L = [0] * 2001
ans = 0
for i in range(N-2):
    for j in range(i+1):
        L[A[i+1] + A[j]] += 1
    ans += sum(L[A[i+2]+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):
  for j in range(i+1, N):
    index = bisect_left(L, L[i]+L[j])
    ans += max(index-j-1, 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):
    for j in range(i+1, n):
        ans += bisect.bisect_left(l[j+1:], l[i] + l[j])

print(ans)

Solution (Python):
from collections import deque
import sys
input = sys.stdin.readline
import bisect as bi

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

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

    print(ans)

main()

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

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

    l.sort()
    c = 0

    import bisect

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

if __name__ == "__main__":
    main()

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

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

l.sort()
c = 0

import bisect

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

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

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

l.sort()
c = 0

import bisect

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

Solution (Python):
from numpy import cumsum
n = int(input())
a = list(map(int,input().split()))
l = [0]*1001
for i in range(n):
    l[a[i]] += 1
cs = cumsum(l)
count = 0
for i in range(1,1001):
    if l[i] != 0:
        for j in range(i+1,1001):
            if l[j] != 0:
                count += l[i]*l[j]*(cs[min(i+j-1,1000)]-cs[j])
for i in range(1,1001):
    n = l[i]
    if n > 1:
        count += n*(n-1)//2*(cs[min(2*i-1,1000)]-n)
    if n > 2:
        count += n*(n-1)*(n-2)//6
print(count)

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

print(ans)
















































"""3回目

for v in hoge:
    ll.append([v, len(hoge[v <= hoge])])


combi = np.array([[sum(v), v[1]] for v in combinations(hoge, 2)])

print(combi)
print(combi[:, 0])
print(ll)


for v, cnt in ll:
    print(v, cnt, len(combi[(v < combi[:, 0]) & (combi[] < v)]), sep=',')
    ans += cnt * len(combi[v < combi])

print(ans)

"""


"""2回目 comnbinationループがだめ
for v in combinations(hoge, 2):
    s = sum(v)
    ans += len(hoge[(v[1] < hoge) & (hoge < s)])

"""



"""1
これだとTLEが出る
combinationの全パターンを数え上げると遅すぎる
ソート済み配列 × なにかで組み合わせを数え上げる必要

from itertools import combinations

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

ans = 0

for v in combinations(l, 3): 
    if v[0] < v[1] + v[2] and v[1] < v[0] + v[2] and v[2] < v[0] + v[1]:
        ans += 1

print(ans)
"""

Solution (Python):
import bisect

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)





























"""3回目

for v in hoge:
    ll.append([v, len(hoge[v <= hoge])])


combi = np.array([[sum(v), v[1]] for v in combinations(hoge, 2)])

print(combi)
print(combi[:, 0])
print(ll)


for v, cnt in ll:
    print(v, cnt, len(combi[(v < combi[:, 0]) & (combi[] < v)]), sep=',')
    ans += cnt * len(combi[v < combi])

print(ans)

"""


"""2回目 comnbinationループがだめ
for v in combinations(hoge, 2):
    s = sum(v)
    ans += len(hoge[(v[1] < hoge) & (hoge < s)])

"""



"""1
これだとTLEが出る
combinationの全パターンを数え上げると遅すぎる
ソート済み配列 × なにかで組み合わせを数え上げる必要

from itertools import combinations

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

ans = 0

for v in combinations(l, 3): 
    if v[0] < v[1] + v[2] and v[1] < v[0] + v[2] and v[2] < v[0] + v[1]:
        ans += 1

print(ans)
"""

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

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

    L.sort()

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

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left

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

ans = 0

for i in range(n):
    for j in range(i+1, n):
        a, b = l_ls[i], l_ls[j]
        right = bisect_left(l_ls, a+b)
        ans += right-(j+1)

print(ans)

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

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

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):



'''
A=[]
B=[]
for i in range():
    a, b = map(int, input().split())
    A.append(a)
    B.append(b)'''


def sol(n,l):
    import bisect
    l.sort()
    ans = 0
    
    for i in range(n-1):
        for j in range(i+1,n):
            ji = l[j]+l[i]
            ind = bisect.bisect_left(l,ji)
            
            ans += ind-j-1
    return ans


def main():
    import sys
    input = sys.stdin.readline
    n = int(input())
    l = list(map(int, input().split()))
    print(sol(n,l))

        
if __name__ == "__main__":
    main()

Solution (Python):
import bisect

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

answer = 0

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

print(answer)

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



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









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

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

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

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

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

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

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

Solution (Python):

from bisect import bisect_left, bisect_right

n = int(input().rstrip())
bo = sorted([int(i) * -1 for i in input().rstrip().split(" ")])

ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if j < int(i/2):
            break
        idx = bisect_left(bo, bo[i]-bo[j], j+1)
        ans += idx - j - 1

print(ans)

Solution (Python):

from bisect import bisect_left, bisect_right

n = int(input().rstrip())
bo = sorted([int(i) * -1 for i in input().rstrip().split(" ")])

def get_count(start:int, num:int):
    if bo[len(bo)-1] > num:
        return len(bo)-1
    if bo[start] <= num:
        return -1
    return get_count_sub(start, len(bo)-1, num)

def get_count_sub(min:int, max:int, target:int):
    if max - min <= 1:
        if bo[max] > target:
            return max
        else:
            return min

    mid = int((max+min)/2)
    if bo[mid] > target:
        return get_count_sub(mid, max, target)
    else:
        return get_count_sub(min, mid, target)

ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        if j < int(i/2):
            break
        idx = bisect_left(bo, bo[i]-bo[j], j+1)
        ans += idx - 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):
    l1 = l[i]

    l3_ind = i+2
    for j in range(i+1,n):
        l2 = l[j]
        while(True):
            if(l3_ind == n):
                ans += l3_ind - j - 1
                break

            if(l[l3_ind] >= l1+l2):
                ans += l3_ind - j - 1
                break
            l3_ind += 1
            
print(ans)

Solution (Python):
import bisect
n = int(input())
dat = list(map(int, input().split()))
dat.sort(reverse=True)
dat = list(map(lambda x: -x, dat))

res = 0
for a in range(n):
    
    for b in range(a+1, n):
        
        cind = bisect.bisect_left(dat, dat[a] - dat[b], b)
        cannum = cind - (b+1)
        cannum = 0 if cannum < 0 else cannum
        
        
        res += cannum
print(res)

Solution (Python):
import bisect
n = int(input())
dat = list(map(int, input().split()))
dat.sort(reverse=True)
dat = list(map(lambda x: -x, dat))

res = 0
for a in range(n):
    
    for b in range(a+1, n):
        
        cind = bisect.bisect_left(dat, dat[a] - dat[b], b)
        cannum = cind - (b+1)
        cannum = 0 if cannum < 0 else cannum
        
        
        res += cannum
print(res)

Solution (Python):
import array
import bisect

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

L.sort()

A = array.array('i', L)




count = 0

for ia in range(2,N):
    for ib in range(1, ia):
        ic_max = ib-1
        c_min = A[ia]-A[ib]
        ic_min = bisect.bisect_right(A, c_min)
        
        
        
        c_count = ic_max - ic_min + 1
        if c_count > 0:
            count += c_count

print(count)

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):
import sys
import math
import bisect
input = sys.stdin.readline
MOD = 10**9 + 7
INF = float('INF')

def main():
    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]
            x = bisect.bisect_left(l, a+b)
            ans += max(x - (j+1), 0)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):



def main():
  n  = int(input())
  Ls = list(map(int, input().strip().split())) + [2*10**3+1]
  Ls.sort()
  def bs(l, r, x):
    while r - l > 1:
      mid = (r+l)//2
      if Ls[mid] >= x:
        r = mid
      if Ls[mid] <  x:
        l = mid
    return r
  
  count = 0
  for i in range(n-2):
    for j in range(i+1, n-1):
      r = bs(j, len(Ls), Ls[i]+Ls[j])
      count += (r - 1  - j)
  print(count)
  
if __name__=='__main__':
  main()

Solution (Python):
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]
        left = j
        right = N
        if L[left + 1] >= a + b:
            continue
        while right - left > 1:
            mid = (left + right) // 2
            c = L[mid]
            if c < a + b:
                left = mid
            else:
                right = mid
        ans += left - j

print(ans)

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

n, *l = map(int, sys.stdin.read().split())
l.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        
        
        cnt = (n - 1) - j - bisect_right(l, l[-(i+1)] - l[-(j+1)])
        if cnt > 0:
            ans += cnt
print(ans)

Solution (Python):
n = int(input())
v = list(sorted(map(int, input().split())))
v.append(2000)
total = n*(n-1)*(n-2) / 6



def bs(left, s):
    right = n+1
    while left < right-1:
        m = (right+left)//2

        if v[m]>=s:
            right = m
        else:
            left = m
    return right


for i, x in enumerate(v[:-2]):
    for j, y in enumerate(v[i+1:-1]):
        k = bs(j, y+x)
        total -= (n-k)

print(int(total))

Solution (Python):
n = int(input())
v = list(sorted(map(int, input().split())))
v.append(2000)
total = n*(n-1)*(n-2) / 6



def bs(left, s):
    right = n+1
    while left < right-1:
        m = (right+left)//2

        if v[m]>=s:
            right = m
        else:
            left = m
    return right






for i, x in enumerate(v[:-2]):
    for j, y in enumerate(v[i+1:-1]):
        k = bs(j+1, y+x)
        total -= (n-k)

print(int(total))

Solution (Python):
n = int(input())
v = list(sorted(map(int, input().split())))
v.append(2000)
total = n*(n-1)*(n-2) / 6



def bs(left, s):
    right = n+1
    while left < right-1:
        m = (right+left)//2

        if v[m]>=s:
            right = m
        else:
            left = m
    return left+1






for i, x in enumerate(v[:-2]):
    for j, y in enumerate(v[i+1:-1]):
        k = bs(j+1, y+x)
        total -= (n-k)

print(int(total))

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

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

from bisect import bisect_left

cnt = 0
for i in range(0, N - 2):
    for j in range(i + 1, N - 1):
        t = L[i] + L[j]
        idx = bisect_left(L, t, j + 1, len(L))
        cnt += max(0, idx - j - 1)
print(cnt)

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

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

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

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

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

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

Solution (Python):
N = int(input())
L = sorted([int(l) for l in input().split()])
INF = 10**8
L.append(INF)
L.append(INF)

num = 0
for na in range(N-2):
    for nb in range(na+1, N - 1):
        r = N+1
        l = nb
        while r - l > 1:
            c = (l + r) // 2
            if L[c] < L[na] + L[nb]:
                l = c
            else:
                r = c
        num += l - nb

print(num)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007



def main():
	N = inputInt()
	L = inputList()
	L.sort()
	

	ans = 0
	for a in range(N):
		for b in range(a+1,N):
			A = L[a]
			B = L[b]
			tmp = A + B
			C_ind = bisect_left(L, tmp)

			if C_ind >= N:
				ans += N - b - 1
			else:
				ans += C_ind - b - 1

	print(ans)







def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Solution (Python):
import sys
import bisect

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

count = 0
for a_i in range(N-1):
    for b_i in range(a_i+1, N):
        max_c = bisect.bisect_left(L, L[a_i]+L[b_i])
        if max_c > b_i:
            count += max_c - b_i - 1
        
print(count)

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

ans = 0
for i in range(1,n-1):
  for j in range(i):
    if L[n-1] - L[i]-L[j] < 0:
      ans += (n-1) - (i+1) + 1
      continue
    elif L[i+1] - L[i]-L[j] >= 0:
      continue
    l = i+1
    r = n-1
    while abs(r-l) > 1:
      mid = (l+r)//2
      if L[i]+L[j] - L[mid]> 0:
        l = mid
      else:
        r= mid
    ans += l-(i+1)+ 1
print(ans)

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

count=0
l=sorted(L)

for i in range(N-2):
    for j in range(i+1,N-1):
        count+=bi.bisect_left(l,l[i]+l[j],j+1)-j-1
         
print(count)

Solution (Python):
import bisect

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

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

print(count)

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

Solution (Python):
import bisect

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

ld={}
for i in range(n):
    ld.setdefault(i,ls[i])


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

print(ans)

Solution (Python):
from bisect import bisect_left

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

L_sorted = sorted(L)

count = 0
for i in range(N):  
    for j in range(i+1, N):  
        r = bisect_left(L_sorted, L_sorted[i] + L_sorted[j], j)
        count += r - j - 1

print(count)

Solution (Python):

from bisect import bisect_right

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

L_sorted = sorted(L)

count = 0
for i in range(N):  
    for j in reversed(range(i)):  
        r = bisect_right(L_sorted, L_sorted[i] - L_sorted[j])
        if j <= r:
            break
        else:
            count += j - r

print(count)

Solution (Python):
from bisect import bisect_right

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

L_sorted = sorted(L)

count = 0
for i in range(N):  
    for j in range(i):  
        r = bisect_right(L_sorted, L_sorted[i] - L_sorted[j])
        count += max(0, j - r)

print(count)

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

  
ans=0
for i in range(len(ll)-1):
  for j  in range(i+1,len(ll)):
    a,b=ll[i],ll[j]
    ans+=bisect.bisect_left(ll,a+b)-(j+1)
print(ans)

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

Solution (Python):
from bisect import bisect_left,bisect_right
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):
        bc=l[i]+l[j]
        idx=bisect_left(l,bc)
        ans+=idx-j-1
print(ans)

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

for a in range(N - 1, 0, -1):
    b = 0
    c = a - 1
    while (b < c):
        if L[a] < L[b] + L[c]:
            cnt += c - b
            c -= 1
        else:
            b += 1

print(cnt)

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

L_array.sort()


ans = 0
for i in range(N):
    for j in range(i+1,N):
        max_len = L_array[i] + L_array[j] - 0.1
        max_index = bisect.bisect_left(L_array,max_len) - 1
        
        if j < max_index:
            ans += (max_index - j)
print(ans)

Solution (Python):
n = int(input())
sticks = list (map (int, input().strip().split()))
sticks.sort()
ans = 0
for a_idx in range(n - 1, 0, -1):
    b_idx = a_idx - 1
    c_idx = 0
    while c_idx < b_idx:
        if sticks[a_idx] < sticks[b_idx] + sticks[c_idx]:
            ans += b_idx - c_idx
            b_idx -= 1
        else:
            c_idx += 1
print(ans)

Solution (Python):
from bisect import bisect_right

def main():
    N = int(input())
    L = list(map(int, input().split()))
    sort_L = sorted(L)
    ans = 0
    for i in range(N-2):
        for j in range(i,N-1):
            if i == j:
                continue
            ok = j
            ng = N
            tmp = sort_L[i] + sort_L[j]
            while abs(ok - ng) > 1:
                mid = (ok + ng) // 2
                if sort_L[mid] < tmp:
                    ok = mid
                else:
                    ng = mid

            ans += ok - j

    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
import bisect
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):
from bisect import bisect_left
n=int(input())
L=sorted((map(int,input().split())))
t=0
for i in range(n-2):
	for j in range(i+1,n-1):
		l=j
		r=n
		while l+1<r:
			c=(l+r)//2
			if L[i]+L[j]>L[c]:
				l=c
			else:
				r=c
		t+=l-j
print(t)

Solution (Python):
import bisect
N = int(input())
L = sorted([int(x) for x in input().split()])
cnt = 0

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

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

li.sort()
ans = 0

for i in range(n-2):
    for j in range(i+1,n-1):
        ans += bisect.bisect_left(li, li[i]+li[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):
        a = l[i]+l[j]
        index = bisect.bisect_left(l, a)
        count += index - 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-1):
   for j in range(i+1,n):
      idx=bisect.bisect_left(L,L[i]+L[j])
      ans+=max(0,idx-j-1)
print(ans)