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

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

num = 0
import bisect
for i in range(n):
    for j in range(i+1,n):
        num += bisect.bisect_left(li,li[i]+li[j])-(j+1)
print(num)

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

cntL = [0] * (M + 1)
for l in L:
    cntL[l] += 1

accCntL = [0] * (M + 2)
for i in range(1, M + 2):
    accCntL[i] = accCntL[i - 1] + cntL[i - 1]

ans = 0
for i, A in enumerate(L):
    for j, B in enumerate(L):
        if i == j:
            continue

        right = min(A + B, M + 1)
        left = abs(A - B) + 1
        ans += accCntL[right] - accCntL[left]

        if left <= A < right:
            ans -= 1
        if left <= B < right:
            ans -= 1

print(ans // 6)

Solution (Python):
import bisect

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

    

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
 
L.sort()
 
dp = [0]*(1005)
frm = 0
cnt = 0
Lcopy = [1001] + L[::-1]
while Lcopy:
  to = Lcopy.pop()
  for i in range(frm, to):
    dp[i] = cnt
  frm = to
  cnt += 1
  
ans = 0
b = 1
c = 2
while b < N-1:
  sub = L[c] - L[b]

  x = b - dp[sub]
  if x > 0:
    ans += x
  else:
    b += 1
    c = b
    
  c += 1
  if c >= N:
    b += 1
    c = b + 1

print(ans)

Solution (Python):
from bisect import bisect_left

def main():

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


    ans = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            ind = bisect_left(l, l[i]+l[j])
            ans += ind - 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, A in enumerate(L):
    for j, B in enumerate(L[i + 1:], start=i + 1):
        right = bisect_left(L, A + B)
        ans += right - j - 1

print(ans)

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

def f(j, max1, max2):
    L_temp = L[j+1:]
    
    dif = max1 - max2
    left = -1
    right = len(L_temp)
    while right - left != 1:
        temp = (left+right)//2
        if dif < L_temp[temp]:
            left = temp
        else:
            right = temp
    if left != N:
        return left+1
    else:
        return left


ans = 0
max1 = 0
max2 = 0
for i in range(N):
    max1 = L[i]
    for j in range(i+1, N-1):
        max2 = L[j]
        ans += f(j, max1, max2)
        
print(ans)

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

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

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    l = sorted(LI())
    r = 0
    for i in range(n):
        k = i + 1
        li = l[i]
        for j in range(i+1,n):
            ij = li + l[j]
            while k < n-1 and l[k+1] < ij:
                k += 1
            r += k - j

    return r


print(main())

Solution (Python):
import sys
from bisect import bisect_left

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n = int(input())
    ll = list(map(int, input().split()))
    ll.sort()
    ans = 0
    for j, l1 in enumerate(ll[:-1]):
        for i, l0 in enumerate(ll[:j]):
            ans += bisect_left(ll, l0 + l1) - (j + 1)
    print(ans)

main()

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

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

print (cnt)

Solution (Python):
import bisect
import sys

input = sys.stdin.readline


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


n = int(input())
ls = sorted(read_i())
res = 0
for i in range(n - 2):
    x = ls[i]
    for j in range(i + 1, n - 1):
        y = ls[j]
        res += bisect.bisect_left(ls, x + y) - j - 1
print(res)

Solution (Python):
import bisect
import sys

input = sys.stdin.readline


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


n = int(input())
ls = sorted(read_i())
ls2 = ls[::-1]
res = 0
for i, x in enumerate(ls2):
    for j, y in enumerate(ls2[i + 1:], i + 1):
        res += n - j - 1 - bisect.bisect(ls[:-(j + 1)], x - y)
print(res)

Solution (Python):
import bisect

n = int(input())
l = [int(v) for v in input().split(' ')]
sum = 0
l.sort()
for i in range(n):
    for j in range(n):
        if i < j:
            ab = l[i] + l[j]
            index = bisect.bisect_left(l, ab)
            sum += index - j - 1
print(sum)

Solution (Python):
def main():
  n = int(input())
  l = list(map(int, input().split()))
  l.sort()
  l.append(99999999)
  c = 0
  for i in range(n - 2):
      t = i + 2
      for j in range(i + 1, n - 1):
          for k in range(t, n + 1):
              if l[i] + l[j] <= l[k]:
                  t = k
                  break
          if t > j: c += t - j - 1
          if j + 1 == t: t += 1
  print(c)
if __name__ == "__main__":
  main()

Solution (Python):
import sys
input = sys.stdin.readline
def main():
  n = int(input())
  l = list(map(int, input().split()))
  l.sort()
  l.append(99999999)
  c = 0
  for i in range(n - 2):
      t = i + 2
      for j in range(i + 1, n - 1):
          for k in range(t, n + 1):
              if l[i] + l[j] <= l[k]:
                  t = k
                  break
          if t > j: c += t - j - 1
          if j + 1 == t: t += 1
  print(c)
if __name__ == "__main__":
  main()

Solution (Python):
import sys
import bisect

input = sys.stdin.readline


def main():
    n = int(input())
    l = map(int, input().split())
    
    l = sorted(l)
    hoge = 0
    for i, a in enumerate(l[:-2], start=1):
        for j, b in enumerate(l[i:-1], start=i):
            index = bisect.bisect_left(l, a + b)
            hoge += index - j - 1
    print(hoge)


if __name__ == "__main__":
    main()

Solution (Python):
def find_triangle_count(d):
    n = len(d)
    d.sort()
    res = 0
    for i in range(n - 2):
        k = i + 2
        for j in range(i + 1, n - 1):
            while k < n and d[i] + d[j] > d[k]:
                k += 1
            if k > j:
                res += k - j - 1
    return res


def solve():
    input()
    input_list = list(map(lambda x: int(x), input().split()))
    print(find_triangle_count(input_list))


if __name__ == '__main__':
    solve()

Solution (Python):
n = int(input())
d = list(map(lambda x: int(x), input().split()))
d.sort()

res = 0

for i in range(n-2):
    k = i + 2
    for j in range(i+1, n-1):
        while k < n and d[i] + d[j] > d[k]:
            k += 1
        if k > j:
            res += k - j - 1

print(res)

Solution (Python):
import bisect

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

count=0
for i in range(N-2):
  a=L[i]
  for j in range(i+1, N-1):
    b=L[j]
    r=bisect.bisect_left(L, a+b)
    count+=r-1-j
    
print(count)

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

Solution (Python):
import bisect

N = int(input())
L = sorted([int(x) for x in input().split()])
ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        ab = L[i] + L[j]
        r = bisect.bisect_left(L,ab)
        l = j + 1
        ans += r - l

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
from numpy.fft import rfft,irfft

N = int(readline())
L = np.array(read().split(),np.int32)

fft_len = 2048

counter = np.bincount(L,minlength=1001)
add = (irfft(rfft(counter,fft_len)**2)+.5).astype(int)


add[:2002:2] -= counter
add //= 2



cum = counter.cumsum()
remove = ((N-cum+counter) * add[:1001]).sum()

answer = N*(N-1)*(N-2)//6 - remove
print(answer)

Solution (Python):
from sys import stdin
import itertools
import bisect
N = int(stdin.readline().rstrip())
L = [int(x) for x in stdin.readline().rstrip().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]
        k = bisect.bisect_left(L, a+b)
        ans += (k-j-1)
print(ans)

Solution (Python):
import sys


def main():
    n = int(sys.stdin.readline().strip())
    l = [int(s) for s in sys.stdin.readline().strip().split()]
    
    

    l.sort()

    
    
    def b_search(lo, hi, limit):
        while lo + 1 < hi:
            mid = (lo + hi) // 2
            if l[mid] < limit:
                lo = mid
            else:
                hi = mid
        if lo >= hi:
            return -1
        if lo + 1 == hi:
            return lo

    ans = 0
    for i in range(n-2):
        for j in range(i+1, n-1):
            a_plus_b = l[i] + l[j]
            valid_longest_idx = b_search(j, n, a_plus_b)
            ans += valid_longest_idx - j

    print(ans)


main()

Solution (Python):
import bisect
n = int(input())
l_li = [int(x) for x in input().split()]
l_li.sort()

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

Solution (Python):
from itertools import accumulate, combinations

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

    A = [0] * (2 * 10 ** 3 + 7)
    for l in L:
        A[l] += 1

    S = list(accumulate(A))

    ans = 0
    for b, c in combinations(L, 2):
        l, r = c - b if c > b else b - c, b + c
        ans += S[r - 1] - S[l] - (l < b < r) - (l < c < r)

    print(ans // 3)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import accumulate, combinations

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

A = [0] * (2 * 10 ** 3 + 7)
for l in L:
    A[l] += 1

S = list(accumulate(A))

ans = 0
for b, c in combinations(L, 2):
    l, r = abs(b - c), b + c
    ans += S[r - 1] - S[l] - (l < b < r) - (l < c < r)

print(ans // 3)

Solution (Python):
from itertools import accumulate

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

A = [0] * (2 * 10 ** 3 + 7)
for l in L:
    A[l] += 1

S = list(accumulate(A))

ans = 0
for i in range(N):
    b = L[i]
    for j in range(i + 1, N):
        c = L[j]
        l, r = abs(b - c), b + c
        ans += S[r - 1] - S[l] - (l < b < r) - (l < c < r)

print(ans // 3)

Solution (Python):
import bisect
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
 
n = int(input())
l = [int(item) for item in input().split()]
l.sort()
 
ans = 0
for i, a in enumerate(l):
    for j in range(i+1, n):
        b = l[j]
        lower = max(a-b, b-a)
        upper = a + b
        d = bisect.bisect_right(l, lower)
        u = bisect.bisect_left(l, upper)
        double = 0
        if d <= i < u:
            double += 1
        if d <= j < u:
            double += 1
        ans += max(0, u - d - double)
 
print(ans // 3)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



import numpy as np

N = int(readline())
L = np.array(read().split(),np.int32)

L.sort()

U=1000
counter = np.bincount(L,minlength=U+U+10)
cum = counter.cumsum()

add = L[:,None] + L[None,:]
max_c = cum[add-1]
cnt_c = max_c - np.arange(len(L)) - 1

answer = np.triu(cnt_c,1).sum()
print(answer)

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

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

Solution (Python):
def d_triangles():
    from bisect import bisect_left
    N = int(input())
    L = sorted([int(i) for i in input().split()])

    ans = 0
    for min_index in range(N - 2):
        for second_index in range(min_index + 1, N - 1):
            a, b = L[min_index], L[second_index]
            max_index = bisect_left(L, a + b)
            ans += max_index - (second_index + 1)
    return ans

print(d_triangles())

Solution (Python):
import bisect

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):
    c = bisect.bisect_left(L, L[a]+L[b])
    ans += c - (b+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):
      a = L[i]
      for j in range(i+1,N):
          b = L[j]
          m = bisect.bisect_right(L,a+b-1)
          ans += m-j-1

print(ans)

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

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

Solution (Python):
import bisect
import sys

input = sys.stdin.readline

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

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

Solution (Python):
import bisect
N = int(input())
l = list(map(int, input().split()))
ans = 0
tmp = 0
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):
import bisect
import itertools
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        L = list(map(int, input().split()))

        ans = 0
        L.sort()

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

        print(ans)



if __name__ == '__main__':
    AtCoder().main()

Solution (Python):
import bisect
import itertools
import sys

input = sys.stdin.readline


class AtCoder:
    def main(self):
        N = int(input())
        L = list(map(int, input().split()))

        ans = 0
        L.sort()

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

        print(ans)



if __name__ == '__main__':
    AtCoder().main()

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

Solution (Python):
import sys
import bisect

N = int(sys.stdin.readline())
L = [int(x) for x in sys.stdin.readline().split()]
L.sort()

answer = 0

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

print(answer)

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

Solution (Python):
import bisect
import random

def test(L):
    lst = []
    N = len(L)
    for a in range(N):
        for b in range(a):
            for c in range(b):
                if L[a] < L[b] + L[c]:
                    lst.append((c, b, a))
    return lst

def test_len(L):
    return len(test(L))

def test2(L):
    return len(set((l[0], l[2]) for l in test(L)))

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







S = 0
for a in range(2, N):
    c = bisect.bisect_left(L, (L[a] + 2) // 2)
    while c >= 0:
        if L[a] < L[c] * 2:
            c -= 1
        else:
            break
    b = c + 1
    assert L[a] < L[b] * 2
    if b == 0:
        
        S += (a - 1) * (a) // 2
        continue
    if c >= a - 1:
        continue
    assert L[a] < L[b] + L[b]
    assert L[a] >= L[c] + L[c], (a, c)
    S += (a - b) * (a - b - 1) // 2
    while c >= 0:
        while L[a] >= L[b] + L[c]:
            b += 1
        assert L[a] < L[b] + L[c]
        assert L[a] >= L[b - 1] + L[c]
        
        S += a - b
        c -= 1
print(S)

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

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

count = 0 
L.sort()

for i in range(N-2):
    k = i + 2
    for j in range(i+1,N):
        while (k<N and L[i] + L[j] > L[k]):
            k += 1

        if k > j:
            count += k - j - 1


print(count)

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

Solution (Python):

import sys

from bisect import bisect_left, bisect_right



def solve(N: int, L: "List[int]"):
    ans = 0
    L.sort()

    for x in range(N):
        for y in range(x+1,N):
            ze = bisect_left(L, L[x]+L[y])            
            ans += (ze-y-1)
            
    print(ans)

    return
 



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    L = [int(next(tokens)) for _ in range(N)]  
    solve(N, L)

if __name__ == '__main__':
    main()

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))

from bisect import bisect_left

def main():
    n = ni()
    l = sorted(na())
    
    ans = 0
    for i in range(n):
        for j in range(i+1, n):
            t = l[i] + l[j]
            ind = bisect_left(l, t)
            ans += max(0, ind - j - 1)
    print(ans)

main()

Solution (Python):
n=int(input())
da=list(map(int,input().split()))
sum=[0 for i in range(2001)]
ans=0
da=sorted(da)
for i in range(n):
    for j in range(2001):
        if j > da[i]:
            ans+=sum[j]
    for j in range(i):
        sum[da[i]+da[j]]+=1
print(ans)

Solution (Python):
n=int(input())
da=list(map(int,input().split()))
sum=[0 for i in range(2001)]
ans=0
da=sorted(da)
for i in range(n):
    for j in range(2001):
        if j > da[i]:
            ans+=sum[j]
    for j in range(i):
        sum[da[i]+da[j]]+=1
print(ans)

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

Solution (Python):
n=int(input())
da=list(map(int,input().split()))
ans=0
da=sorted(da)
for i in range(1,n-1):
    for j in range(i):
        ok,ng=0,0
        ok+=i
        ng+=n
        while ng-ok!=1:
            mi=(ok+ng)//2
            if da[mi]<da[i]+da[j]:
                ok=0
                ok+=mi
            else:
                ng=0
                ng+=mi
        ans+=ok-i
print(ans)

Solution (Python):
import bisect
N = int(input())
L = [int(i) for i in input().split()]

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

print(ans)

Solution (Python):
import bisect

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

Solution (Python):


import sys
from bisect import bisect_left

n = int(sys.stdin.readline().strip())
l = list(map(int,sys.stdin.readline().strip().split()))
    
l.sort()
c = 0
n1 = n - 1
for i in range(n-2):
    li = l[i]
    for j in range(i+1,n1):
        k = bisect_left(l,l[j]+li,lo=j)
        c += k-j-1

print(c)

Solution (Python):
import copy
import bisect

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

triangles = 0

for i in range(N - 2):
    P = L[i]
    for j in range(i + 1, N - 1):
        Q = L[j]
        if P + Q <= L[j + 1]:
            continue
        maxR = P + Q - 1
        triangles += bisect.bisect_right(L, maxR) - (j + 1)

print(triangles)

Solution (Python):
import bisect
N=int(input())
ls=[int(s) for s in input().split()]
ls.sort()
ans=0
for i in range(1,N-1):
  for j in range(i+1,N):
    k = bisect.bisect_right(ls[:i],ls[j]-ls[i])
    ans+=i-k
print(ans)

Solution (Python):
import copy
import bisect

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

triangles = 0

for i in range(N - 2):
    P = L[i]
    for j in range(i + 1, N - 1):
        Q = L[j]
        if P + Q <= L[j + 1]:
            continue
        maxR = P + Q - 1
        triangles += bisect.bisect_right(L, maxR) - (j + 1)

print(triangles)

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

def bina(x,ok,ng):
    global da
    while ng-ok!=1:
        mi=(ok+ng)//2
        if da[mi]<x:
            ok=0
            ok+=mi
        else:
            ng=0
            ng+=mi
    return ok


ans=0
da=sorted(da)
for i in range(1,n-1):
    for j in range(i):
        ans+=bina(da[i]+da[j],i,n)-i
print(ans)

Solution (Python):
import bisect

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

L.sort()

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

print(num_triangles)

Solution (Python):
def main():
    from bisect import bisect_left
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            ab = L[i] + L[j]
            ri = bisect_left(L, ab)
            le = j + 1
            ans += ri - le

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input=sys.stdin.readline
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):
        x=bisect.bisect_left(l,l[i]+l[j],lo=j+1)
        ans+=x-j-1
print(ans)

Solution (Python):
def d_triangles():
    from bisect import bisect_right
    N = int(input())
    L = sorted([int(i) for i in input().split()])

    ans = 0
    for max_index in range(2, N):
        for second_index in range(1, max_index):
            b, c = L[second_index], L[max_index]
            
            
            
            third_index = bisect_right(L, c - b)
            if third_index < second_index:
                ans += second_index - third_index
    return ans

print(d_triangles())

Solution (Python):
import bisect
a = int(input())
l = list(map(int,input().split()))
l.sort()
s = 0
for n,x in enumerate(l):
    for m,y in enumerate(l[n+1:]):
        
        t = x + y
        s += bisect.bisect_left(l[n+m+2:],t)


print(s)

Solution (Python):

import sys

def solve(N: int, L: "List[int]"):
    L.sort()
    sum = 0

    for i in range(N-2):
        k = 0
        for j in range(i+1,N-1):
            tansum = L[i] + L[j]
            while k < len(L) and L[k] < tansum: k += 1
            if tansum <= L[j+1]:
                continue

            sum = sum + k - j - 1
    print(sum)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    L = [int(next(tokens)) for _ in range(N)]  
    solve(N, L)

if __name__ == '__main__':
    main()

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

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

print(count)

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

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

print(count)

Solution (Python):
from bisect import bisect_right
N = int(input())
D = list(map(int, input().split()))
D.sort(reverse=False)

count = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):

        ind = bisect_right(D, D[i] + D[j] - 1)
        count += max(ind - j - 1, 0)

print(count)

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

ans = 0
for i in range(len(l)):
    right = i+1
    for left in range(i+1, len(l)):
        while right+1 < len(l) and l[right+1] < l[left] + l[i]:
            right += 1
        ans += right - left
        if right == left:
            right += 1

print(ans)

Solution (Python):
import bisect
N = int(input())
L = list(int(l) for l in input().split())
L.sort()
num=0

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

        num += idx-(j+1)
print(num)

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

print(ans)

Solution (Python):
from bisect import bisect_right
N = int(input())
D = list(map(int, input().split()))
D.sort(reverse=False)

count = 0

for i in range(N - 2):
    for j in range(i + 1, N - 1):
        
        
        
        ind = bisect_right(D, D[i] + D[j] - 1)
        count += max(ind - j - 1, 0)

print(count)

Solution (Python):
n = int(input())
L = list(map(int, input().split()))
x = [0] * 1010
for l in L:
  x[l] += 1

ans = 0;
for a in range(1, 1001):
  if x[a] == 0:
    continue
  for b in range(a, 1001):
    if x[b] == 0:
      continue
    for c in range(b, 1001):
      if x[c] == 0:
        continue
      if c >= a + b:
        break
      if a == b == c:
        ans += x[a] * (x[b]-1) * (x[c]-2) // 6
      elif a == b:
        ans += x[a] * (x[b]-1) * x[c] // 2
      elif b == c:
        ans += x[a] * x[b] * (x[c]-1) // 2
      else:
        ans += x[a] * x[b] * x[c]

print(ans)

Solution (Python):
def main():
    from bisect import bisect
    N = int(input())
    L = list(map(int,input().split()))
    L.sort()
    cnt = 0

    for i in range(N-1,1,-1):
        for j in range(i-1,0,-1):
            temp = L[i] - L[j]
            l = bisect(L,temp,lo=0,hi=j)
            cnt += j - l
    print(cnt)
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()
    L_pair = []
    for i in range(N):
        for j in range(i+1, N):
            L_pair.append((i, j))

    def is_ok(mid, i, j):
        if L[mid] < L[i] + L[j]:
            return True
        else:
            return False

    def binary_search_meguru(i, j):
        left = j
        right = N
        while right - left > 1:
            mid = left + ((right - left) // 2)
            if is_ok(mid, i, j):
                left = mid
            else:
                right = mid
        return right

    ans = 0
    for i, j in L_pair:
        r = binary_search_meguru(i, j)
        ans += r - (j + 1)
    print(ans)


if __name__ == '__main__':
    main()

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


if __name__ == '__main__':
    main()

Solution (Python):
import bisect

n = int(input())
li = list(map(int,input().split()))
count = 0
li.sort()
for i in range(n - 2):
  for j in range(i + 1, n - 1):
    ab = li[i] + li[j]
    x = bisect.bisect_left(li, ab, lo=0, hi=n)
    count += (x - j - 1)
 
print(count)

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

v = L[:]
v.sort()

res = 0

import bisect

for i in range(N-2):
    a = v[i]
    for j in range(i+1,N-1):
        b = v[j]
        sup = a + b
        
        iterS = bisect.bisect_left(v,sup)
        
        
        res += iterS - (j+1)

print(res)

Solution (Python):
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()

ans=0
for i in range(n):
    for j in range(i+1,n):

        ans+=max(bisect_left(l,l[i]+l[j])-j-1,0)
print(ans)

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

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