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 bisect as bs
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range(len(L)-1):
    for j in range(i+1, len(L)):
        ans += bs.bisect_left(L, L[i]+L[j])-j-1
print(ans)

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

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




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

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

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





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

    ans += ok - 1 - j
        
print(ans)

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

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

print(cnt)

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
c=0
for i in range(n-2):
  for j in range(i+1,n-1):
    u=l[i]-l[j]
    if l[j+1]<=u:
      break
    ok=j+1
    ng=n
    while ng-ok>1:
      center=(ok+ng)//2
      if l[center]>u:
        ok=center
      else:
        ng=center
    c+=ng-j-1
print(c)

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

Solution (Python):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))



def is_ok(l, i, key):
    
    if l[i] >= key:
        return True


def binary_seach(l, key) -> int:
    left = -1
    right = len(l)

    while right - left > 1:
        mid = left + (right - left) // 2
        if is_ok(l, mid, key):
            right = mid
        else:
            left = mid

    return right


n = I()
l = LI()
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]
        
        
        
        ind = binary_seach(l, a + b)
        
        ans += ind - j - 1
print(ans)

Solution (Python):
def main():
    N = int(input())
    L = sorted(list(map(int, input().split())))
    ans = 0
    for ia in range(N-2):
        a = L[ia]
        ic = ia + 2
        for ib in range(ia+1, N-1):
            b = L[ib]
            while ic < N and L[ic] < a+b:
                ic += 1
            ans += ic - (ib + 1)
    print(ans)
main()

Solution (Python):
import sys
from itertools import accumulate

read = sys.stdin.buffer.read


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

    L.sort()
    max_L = L[-1]

    C = [0] * (max_L + 1)
    for l in L:
        C[l] += 1
    C = list(accumulate(C))

    ans = 0
    for i in range(N):
        for j in range(i + 1, N):
            if L[i] + L[j] > max_L:
                ans += N - j - 1
            else:
                ans += C[L[i] + L[j] - 1] - j - 1

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import accumulate

read = sys.stdin.buffer.read


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

    L.sort()
    max_L = L[-1]

    C = [0] * (max_L + 1)
    for l in L:
        C[l] += 1
    C = list(accumulate(C))

    ans = 0
    for i, a in enumerate(L):
        for j, b in enumerate(L[i + 1 :], i + 1):
            if a + b > max_L:
                ans += N - j - 1
            else:
                ans += C[a + b - 1] - j - 1

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    L.sort()

    max_L = max(L)

    C = [0] * (max_L + 1)
    for l in L:
        C[l] += 1
    C = list(accumulate(C))

    ans = 0
    for i, a in enumerate(L):
        for j, b in enumerate(L[i + 1 :], i + 1):
            if a + b > max_L:
                ans += N - j - 1
            else:
                ans += C[a + b - 1] - j - 1

    print(ans)
    return


if __name__ == '__main__':
    main()

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    L.sort()

    ans = 0
    for i, a in enumerate(L):
        for j, b in enumerate(L[i + 1 :], i + 1):
            k = bisect_left(L, a + b, lo=j + 1)
            ans += k - j - 1

    print(ans)
    return


if __name__ == '__main__':
    main()

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

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    L.sort()

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

    print(ans)
    return


if __name__ == '__main__':
    main()

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    L.sort()

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

    print(ans)
    return


if __name__ == '__main__':
    main()

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


'''
a,bの値を指定し、その合計値を下回る値の最大値のインデックス番号を取得
インデックス番号から指定していたb（大きい方の値）を引く
'''
ans = 0
for a in range(n):
  for b in range(a+1, n):
    ans += bisect.bisect_left(l, l[a]+l[b]) - (b+1)
print(ans)

Solution (Python):
import numpy as np
from numba import njit
 

@njit
def f(a):
    n = len(a)
    cnt = 0
    
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                if a[k] < a[i] + a[j]:
                    cnt += 1
    return cnt


n = int(input())
a = np.array(input().split(), dtype=np.int32)
a.sort()
print(f(a))

Solution (Python):
import numpy as np
from numba import njit
 

@njit
def f(A):
    n = len(a)
    cnt = 0
    
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(j + 1, n):
                cnt += (A[k] < A[i] + A[j])
    return cnt


n = int(input())
a = np.array(input().split(), dtype=np.int32)
a.sort()
print(f(a))

Solution (Python):
import numpy as np
from numba import njit
 

@njit
def f(A):
    N = len(A)
    cnt = 0
    
    for i in range(N):
        for j in range(i + 1, N):
            for k in range(j + 1, N):
                cnt += (A[k] < A[i] + A[j])
    return cnt


N = int(input())
A = np.int32(input().split())
A.sort()
print(f(A))

Solution (Python):
import numpy as np
from numba import njit
 
@njit
def f(A):
  N = len(A)
  cnt = 0
  for i in range(N):
    for j in range(i + 1, N):
      for k in range(j + 1, N):
        cnt += (A[k] < A[i] + A[j])
  return cnt
 
N = int(input())
A = np.int32(input().split())
A.sort()
print(f(A))

Solution (Python):
import itertools
 
n = int(input())
A = list(map(int,input().split()))
A.sort()
 
def binary_search(l,r,v):
    while r >= l:
        h = (l+r) // 2
        if A[h] < v:
            l = h+1
        else:
            r = h-1
    return r
 
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        a = binary_search(j,n-1,A[i]+A[j])
        ans += a-j
 
print(ans)

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

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

print(ans)

Solution (Python):
from bisect import bisect_left

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

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

    print(ans)
main()

Solution (Python):
from bisect import bisect_left, bisect_right

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

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

            c_min = max(a-b, b-a)
            c_max = a + b
            ic_begin = bisect_right(L, c_min, lo=ib+1)
            ic_end   = bisect_left( L, c_max, lo=ib+1)

            ans += ic_end - ic_begin

    print(ans)
main()

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

L.sort()


import bisect
ans = 0
for i in range(N-2):
    l1 = L[i]
    for j in range(i+1, N-1):
        l2 = L[j]
        l3_max = (l1 + l2)

        k = bisect.bisect_left(L, l3_max, lo=j+1)
        ans += max(0, k - j - 1)
print(ans)

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

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

N = int(input())
L = list(map(int, sys.stdin.readline().rsplit()))
L.sort()

res = 0
for i in reversed(range(1, N)):
    for j in reversed(range(i)):
        r = bisect.bisect_right(L, abs(L[i] - L[j]))
        
        if r < j:
            res += j - r
        
print(res)

Solution (Python):
import sys, bisect

N = int(input())
L = list(map(int, sys.stdin.readline().rsplit()))
L.sort()

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

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

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

Solution (Python):
from bisect import bisect
from bisect import bisect_right
from bisect import bisect_left
 
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):
    s = L[i] + L[j]
    x = bisect_left(L, s)
    
    cnt += max(x - j - 1, 0)
print(cnt)

Solution (Python):
import bisect

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

ans = 0
for i in range(n-2):
    e1 = ll[i]
    for j in range(i+1, n-1):
        e2 = ll[j]
        cnt = bisect.bisect_left(ll, e1+e2)
        cnt -= (j+1)
        ans += max(cnt,0)

print(ans)

Solution (Python):
import bisect

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

ans = 0
for i in range(n-2):
    e1 = ll[i]
    for j in range(i+1, n-1):
        e2 = ll[j]
        comb = bisect.bisect_left(ll[j+1:], e1+e2)
        
        ans += max(comb,0)

print(ans)

Solution (Python):
import bisect

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

ans = 0
for i in range(n-2):
    e1 = ll[i]
    for j in range(i+1, n-1):
        e2 = ll[j]
        comb = bisect.bisect_left(ll, e1+e2)
        comb -= (j+1)
        ans += max(comb,0)

print(ans)

Solution (Python):
from bisect import bisect_left

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

ans = 0

"""
これだとTLEしてしまう
for i in range(N-2):
    for j in range(i+1, N-1):
        left = j
        right = N
        target = L[i]-L[j]
        while right-left > 1:
            mid = (right + left)//2
            

            if L[mid] > target:
                left = mid
            else:
                right = mid

        ans += left-j
        """

L.sort()
for i in range(N):
    for j in range(i+1, N):
        target = L[i]+L[j]
        left = bisect_left(L, target) 
        ans += max(0, left-j-1)


print(ans)

Solution (Python):
import bisect

N = int(input())
numbers = [int(i) for i in input().split()]
numbers = sorted(numbers)
counter = 0

for a in range(N):
    for b in range(a+1, N):
        c = bisect.bisect_left(numbers, numbers[a] + numbers[b])
        if c > b:
            counter += c - b -1
        
print(counter)

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

for a in range(n):
    for b in range(a+1, n):
        c = bisect.bisect_left(sort_l, sort_l[a] + sort_l[b])
        if c > b:
            count += c - b -1
        else:
            pass     
        

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):
    for j in range(i + 1, N):
        p = l[:i]
        a, b = l[i], l[j]
        low = b - a + 1
        high = a + b
        ans += bisect.bisect_left(p, high) - bisect.bisect_left(p, low)
print(ans)

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

Solution (Python):
import bisect

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

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

        cmin = abs(a-b)
        cmax = a+b

        l = bisect.bisect_left(arr,cmin+1,lo=j+1)
        r = bisect.bisect_right(arr,cmax-1,lo=j+1)
        
        if r-l > 0:
            ans += r-l

print(ans)

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

def main():
    n = int(input())
    l = input_list()
    l.sort()
    ans = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            ind = bisect.bisect_left(l, l[i]+l[j])
            num = ind - 1 - j
            ans += num
    print(ans)


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

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

Solution (Python):
import math
import sys
from collections import deque
import heapq
import copy
import itertools
from itertools import permutations
from itertools import combinations
import bisect
def mi() : return map(int,sys.stdin.readline().split())
def ii() : return int(sys.stdin.readline().rstrip())
def i() : return sys.stdin.readline().rstrip()
a=ii()
l=list(mi())
l=sorted(l)
ans=0
for i in range(a-2):
  for j in range(i+1,a-1):
    s=bisect.bisect_left(l,l[i]+l[j])
    ans+=s-j-1
print(ans)

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

for a in range(n):
    for b in range(a+1, n):
        c = bisect.bisect_left(sort_l, sort_l[a] + sort_l[b])
        if c > b:
            count += c - b -1
        else:
            pass     
        

print(count)

Solution (Python):
from bisect import bisect_left
N=int(input())
triangle=[]
count=0
L=list(map(int,input().split()))
L.sort()

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

Solution (Python):
from bisect import bisect_left
N=int(input())
triangle=[]
count=0
L=list(map(int,input().split()))
L.sort()

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

Solution (Python):
import bisect

n = int(input())

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

A = sorted(L)

ans = 0

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

print(ans)

Solution (Python):
import bisect

n = int(input())

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

A = sorted(L)

ans = 0

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

print(ans)

Solution (Python):
import itertools
import bisect

n = int(input())

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

A = sorted(L)

ans = 0

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

print(ans)

Solution (Python):
import itertools
import bisect

n = int(input())

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

A = sorted(L)

ans = 0

for i in range(n):
    for j in range(i+1,n):
        total = L[i] + L[j]
        ans += bisect.bisect_left(A,total) - (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-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())
a = list(map(int,input().split()))
a.sort()

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

print(ans)

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

cnt = 0
for a in range(n-2):
    for b in range(a+1, n-1):
        cnt += bisect_left(L,L[a]+L[b])-(b+1)
print(cnt)

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

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

ans=0

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

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

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

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

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

Solution (Python):
from bisect import bisect_right
import sys
def main():
    input = sys.stdin.readline
    N = int(input())
    *L, = map(int, input().split())
    L.sort()
    ans = 0
    for i in range(2, N):
        for j in range(i):
            k = bisect_right(L, L[i] - L[j], j + 1, i)
            ans += i - k
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left

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):
        c_i = bisect_left(l, l[i] + l[j])
        cnt += c_i - (j+1)

print(cnt)

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

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

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

total = 0

for i in range(N - 2):
    a = L[i]
    for j in range(i + 1, N - 1):
        b = L[j]
        right_endpoint = bisect.bisect_left(L, a+b, j)
        total += right_endpoint - j - 1

print(total)

Solution (Python):
import sys
from io import StringIO
import unittest
import os
import bisect





def resolve():
    n = int(input())
    ls = list(map(int, input().split()))

    ls.sort()

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

    print(ans)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """4
3 4 2 1"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """3
1 1000 1"""
        output = """0"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """7
218 786 704 233 645 728 389"""
        output = """23"""
        self.assertIO(test_input, output)

    
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

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

def main():
    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):
            L = j
            R = n
            ij = l[i] + l[j]
            while R - L != 1:
                if ij <= l[j + 1]:
                    break
                now = (L + R)//2
                if ij > l[now]: 
                    L = now
                else:
                    R = now
            ans += L - j

    print(ans)

main()

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):
        maxc = l[i]+l[j] 
        index = bisect.bisect_left(l, maxc)
        if index > j:
            count += index-j-1
print(count)

Solution (Python):
import itertools
import bisect

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

L = sorted(L)

count = 0

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

print(count)

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

print(ans)

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

Solution (Python):

N = int(input())
L = sorted([int(s) for s in input().split()])


def check_search(middle, idx_short2, l_lng, l_lows):
    return l_lows[middle] + l_lows[idx_short2] > l_lng


l_lows = L[:2]
triangles = 0


for l_lng in L[2:]:
    for idx, l_low in enumerate(l_lows):
        left = 0
        right = len(l_lows) - 1 - idx
        end = right
        while (left < right):
            middle = (left + right) // 2
            if check_search(middle, len(l_lows) - 1 - idx, l_lng, l_lows):
                right = middle
            else:
                left = middle + 1
        triangles += (end - right)
    l_lows.append(l_lng)

print(triangles)

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

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

Solution (Python):
import bisect
n = int(input())
lines = list(int(i) for i in input().split())
lines.sort()
counter = 0

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
L = LIST()

L.sort()

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

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
L = LIST()

cnt = [0]*(10**3+1)
for x in L:
	cnt[x] += 1

cnt = list(accumulate(cnt))

ans = 0
for x, y in combinations(L, 2):
	ans += cnt[min(x+y-1, 1000)] - cnt[abs(x-y)] - (abs(x-y) < x < x+y) - (abs(x-y) < y < x+y)

print(ans//3)

Solution (Python):


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

count=0
le=len(S)
S.append(-100000)
for i in range(le-2):
    for j in range(i+1,le-1):
        if S[i]<S[j]+S[j+1]:
            left=j+1
            right=le
            while left<=right:
                mid=(left+right)//2
                if S[i]<S[j]+S[mid] and S[i]>=S[j]+S[mid+1]:
                    count+= mid-(j+1)+1
                    
                    break
                elif S[i]<S[j]+S[mid]:
                    left=mid+1
                elif S[i]>=S[j]+S[mid]:
                    right=mid-1
                else:
                    print("イレギュラーな例につかまりました")
                    exit()
                
print(count)