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
read = sys.stdin.readline


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


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


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


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
L = read_ints()
L.sort()


from bisect import bisect_left

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

print(cnt)

Solution (Python):
import bisect

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

Solution (Python):

import sys
import bisect

def solve(N: int, L: "List[int]"):
    L.sort()
    answer = 0
    for i in range(N):
        for j in range(i+1,N):
            max_k = bisect.bisect_left(L,L[i]+L[j])
            answer += max_k-(j+1)
    print(answer)
    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 itertools

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


C = [0] * (L[-1] + 1)
num_list = []
for key, group in itertools.groupby(L):
    C[key] = len(list(group))
    num_list.append(key)

C_sum = list(itertools.accumulate(C))

ans = 0
for idx, a in enumerate(num_list):
    for b in num_list[idx:]:
        m = max(b - a + 1, L[0], b)
        M = min(b + a - 1, L[-1])
        val = 0
        if a == b and m == b:
            val = (C[a] * (C[a] - 1) * (C[a] - 2) // 6) + (C[a] * (C[a] - 1) // 2) * (C_sum[M] - C_sum[m])
        elif a == b and m > b:
            val = (C[a] * (C[a] - 1) * (C[a] - 2) // 6) + (C[a] * (C[a] - 1) // 2) *(C_sum[M] - C_sum[m] + C[m])
        elif a < b and m == b:
            val = C[a] * (C[b] * (C[b] - 1) // 2 + C[b] * (C_sum[M] - C_sum[m]))
        elif a < b and m > b:
            val = C[a] * (C[b] * (C_sum[M] - C_sum[m] + C[m]))

        
        ans += val
print(ans)

Solution (Python):
def CountTriangles( A): 
  
    n = len(A); 
  
    A.sort();  
  
    count = 0; 
      
    for i in range(n - 1, 0, -1): 
        l = 0; 
        r = i - 1; 
        while(l < r): 
            if(A[l] + A[r] > A[i]): 
  
                
                
                
                count += r - l;  
  
                
                r -= 1;  
              
            else: 
  
                
                
                l += 1;  
    print(count); 
  

n = int(input())
A = [int(x) for x in input().split()]
CountTriangles(A);

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

def bisec(i,k): 
    ci = i
    ck = k
    i+=1
    k-=1
    j = int((i+k)/2)
    while k-i>1:
        if l[ci]+l[j]>l[ck]:
            k = j
            j = int((i+k)/2)
        else:
            i = j+1
            j = int((i+k)/2)
    if l[ci]+l[i]>l[ck]:
        return True,i
    elif l[ci]+l[i+1]>l[ck]:
        return True,i+1
    else:
        return False,-1

for i in range(n):
    for k in range(i+2,n):
        f,j = bisec(i,k)
        if f:
            ans += k-j
print(ans)

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
L = sorted(L)
lengthnum = [0 for _ in range(max(L) + 1)]
m = len(lengthnum)

for l in L:
	lengthnum[l] += 1

for i in range(m - 1):
	lengthnum[i + 1] += lengthnum[i]
	
ret = 0

for s in range(N - 1):
	for b in range(s + 1, N):
		
		ret += lengthnum[min(L[-1], L[b] + L[s] - 1)] - lengthnum[L[b] - L[s]] - 1
		if L[b] - L[s] < L[s]:
			ret -= 1
print(int(ret / 3))

Solution (Python):
n=int(input())
L=sorted(list(map(int,input().split())))
memo=[0]*2000
for l in L:
  memo[l-1]+=1
for i in range(1999):
  memo[i+1]=memo[i+1]+memo[i]
ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    a=L[i]
    b=L[j]
    ans+=memo[a+b-2]-j-1
print(ans)

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

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):
        a = L[i]
        b = L[j]
        add = a+b
        sub = abs(a-b)
        head = bisect_right(L, sub)
        tail = bisect_left(L, add)
        ex = 0
        if a > sub:
            ex += 1
        if b > sub:
            ex += 1
        ans += (tail - head - ex)

print(ans//3)

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):
    if l[i] * 2 > l[j]:
      idx = bisect.bisect_left(l, l[j] - l[i] + 0.5)
      ans += max(i - idx, 0)

print(ans)

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

def calc(N, sticks):
    sums = [0 for i in range(1002)]
    for stick in sticks:
        for i in range(stick + 1):
            sums[i] += 1
    total = 0
    for i in range(N):
        for j in range(N):
            if i == j: continue
            a, b = sticks[i], sticks[j]

            lo = abs(b-a)+1
            hi = min(a+b, 1001)
            
            val = sums[lo] - sums[hi] - (lo <= a) - (lo <= b)
            if val <= 0: continue
            total += val
    return total // 6


print(calc(N, sticks))

Solution (Python):
N = int(input())
L = list(map(int, input().split()))
 
L.sort()
count = 0
 
for i in reversed(range(N)):
  kmin = 0
  for j in reversed(range(i)):
    for k in range(kmin,j):
      if L[j] + L[k] > L[i]:
        count += j - k
        kmin = k
        break
        
print(count)

Solution (Python):
import sys
from pprint import pprint

import bisect

n = int(sys.stdin.readline().strip())
l = map(int, sys.stdin.readline().strip().split(" "))
l = sorted(l)


ans = 0
for i, l_i in enumerate(l[:-2]):
    for j, l_j in enumerate(l[:-1]):
        if j <= i:
            continue
        index = bisect.bisect_left(l, l_i + l_j)
        
        
        ans += index - (j+1)
        
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
import bisect
for i in range(n):
    for j in range(n):
        if i<j:
            a=l[i]
            b=l[j]
            x=bisect.bisect_left(l,a+b)
            y=bisect.bisect_right(l,b-a)
            if b-a>=a:
                ans+=max(0,x-y-1)
            else:
                ans+=max(0,x-y-2)
print(ans//3)

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

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

l.sort(reverse=True)
m = sorted(l)



import bisect

ans = 0
for i in range(n-2):
  a = l[i]
  for j in range(i+1,n-1):
    b = l[j]
    x = a-b
    ind = bisect.bisect_right(m,x)
    
    if n-1-j-ind >= 0:
      ans += n-1-j-ind

print(ans)

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

count = 0
for i in range(N):
  z = 0
  for j in range(i + 1, N):
    x = L[i] + L[j]
    if z < N:
      while L[z] < x:
        z += 1
        if z == N:
          break
    count += z - j - 1
    
print(count)

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

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

accum = [0] * (2*10**3)

prev_ind = 0
count = 0
for elm in sortedL:
    if prev_ind < elm:
        for i in range(prev_ind+1, elm):
            accum[i] = count
        count += 1
        accum[elm] = count
    else:
        count += 1
        accum[elm] = count
    prev_ind = elm

for i in range(prev_ind, 2*10**3):
    accum[i] = count

ans = 0
for i in range(n):
    for j in range(i+1, n):
        a, b = sortedL[i], sortedL[j]
        left = abs(a - b)
        right = a + b
        tmp = accum[right-1] - accum[left]
        if left < a < right:
            tmp -= 1
        if left < b < right:
            tmp -= 1
        
        ans += tmp

print(ans // 3)

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

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 sys
import bisect

input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N=int(input())
    L=[int(x) for x in input().split()]
    L.sort()
    count=0

    dic={}

    for i in range(N):
        for j in range(i+1,N):
            if i:
                try:
                    start=dic[j]
                    count+=dic[j]-j-1
                except:
                    count+=N-j-1
                    continue
            else:
                start=j+1
            for k in range(start,N):
                if L[k]>=L[i]+L[j]:
                    dic[j]=k
                    break
                count+=1
                
                
                
    print(count)

main()

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

ans=0
if n==3:
  if l[0]+l[1]<=l[2]:
    ans=0
  else:
    ans=1
if n>=4:
  for lef in range(n-1):
    for ri in range(lef+1,n):
      idx=bisect.bisect_left(l,l[lef]+l[ri])
      ans+=idx-ri-1

print(ans)

Solution (Python):
import sys
from itertools import accumulate
import bisect
input = sys.stdin.readline
def inputs():return [int(x) for x in input().split()]
N = int(input())
L= inputs()
L.sort()
b= 0
ans = 0
for i in range(N-1,-1,-1):
    for j in range(i-1,-1,-1):
        b= L[i]-L[j]
        insert_index = bisect.bisect_right(L,b)
        
        ans += max(j-insert_index,0)
        
print(ans)

Solution (Python):
from sys import stdin
import bisect

N = int(stdin.readline().rstrip())
L = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
L.sort()
for i in range(1, N):
    for j in range(i):
        ans += max(bisect.bisect_left(L, L[i] + L[j]) - i - 1, 0)

print(ans)

Solution (Python):
import bisect

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

res = 0
for i in range(N):
    np = i
    for j in range(i + 1, N):
        idx = bisect.bisect_left(L, L[i] + L[j]) - 1
        cnt = idx - j
        if idx <= j or np > idx:
            continue
        res += cnt
        np = idx

print(res)

Solution (Python):
import bisect

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

L.sort()

res = 0

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

print(res)

Solution (Python):
from heapq import heappush, heappop


def solve_d(n, l_list):
    res = 0
    l_list_s = sorted(l_list)
    h = []
    for i in range(1, n - 1):
        for j in range(0, i):
            heappush(h, l_list_s[i] + l_list_s[j])
        while len(h) > 0:
            a = heappop(h)
            if a > l_list_s[i + 1]:
                heappush(h, a)
                break
        res += len(h)

    return res


def main():
    n = int(input())
    l_list = list(map(int, input().split()))
    res = solve_d(n, l_list)
    print(res)


def test():
    assert solve_d(4, [3, 4, 2, 1]) == 1
    assert solve_d(3, [1, 1000, 1]) == 0
    assert solve_d(7, [218, 786, 704, 233, 645, 728, 389]) == 23


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

Solution (Python):




import bisect

def main():

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

            count+=a-j-1

    print(count)






if __name__ == "__main__":
    main()

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

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

N=int(input())
L=list(map(int,input().split()))
h=[]
for i in range(N):
    heappush(h,L[i])
h2=[]
for i in range(N):
    heappush(h2,heappop(h))
s=0
for i in range(N):
    for j in range(N-1-i):
        bmc=h2[j+i+1]-h2[i]
        bpc=h2[j+i+1]+h2[i]
        a=-bisect_right(h2, bmc)+bisect_left(h2, bpc)-1
        if a>0:
            s+=a
            if bmc < h2[i]:
                s-=1
print(s//3)

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

ln.sort(reverse=True)

bcs = []
for i in range(1, n-1):
  row = []
  for j in range(i+1, n):
    row.append(ln[i] + ln[j])
  bcs.append(row)

cnt = 0
ix = [0 for _ in range(n-2)]

for i in range(n-2):
  a = ln[i]
  for j in range(i, n-2):
    try:
      while ix[j] < (n - 2 - j) and bcs[j][ ix[j] ] > a:
        ix[j] += 1
    except:
      print(j, ix[j])
      exit()
  cnt += sum(ix[i:])
print(cnt)

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


L.sort(reverse=True)
dic = {}
cnt = 0

for i in range(1, N - 1):
    p = L[i]
    for j in range(i + 1, N):
        s = p + L[j]

        if s not in dic:
            dic[s] = 1
        else:
            dic[s] += 1

for i in range(N - 2):
    p = L[i]

    if i > 0:
        for x in range(i + 1, N):
            dic[p + L[x]] -= 1

    for key, value in dic.items():
        if key > p:
            cnt += value

print(cnt)

Solution (Python):
N = int(input())
ll = list(map(int,input().split()))
ll.sort()
ans = 0
for i in range(N-2):
    a = ll[0]
    b = 1
    for c in range(1,len(ll)-1):
        while (b<len(ll))and(ll[b]-ll[c] < a):
            b += 1
        ans += max(0,(b-c-1))
    del ll[0]
print(ans)

Solution (Python):
from bisect import bisect_right

n = int(input())
a = sorted(map(int, input().split()))
print(sum(j - bisect_right(a, a[i] - a[j], 0, j)
          for i in range(2, n) for j in range(1, i)))

Solution (Python):
import bisect

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

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

print(res)

Solution (Python):
import bisect
N = int(input())
l_list = sorted(map(int,input().split()))
ans = 0

for i in range(N):
    for j in range(i+1,N):
        sum_ij = l_list[i]+l_list[j]
        bi_num = bisect.bisect_left(l_list,sum_ij)
        
        ans += bi_num-j-1
print(ans)

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
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):
        A = L[i] + L[j] - 1
        sa = abs(L[i] - L[j]) + 1
        big = bisect.bisect_right(L, A)
        sml = bisect.bisect_left(L, sa)
        num = big - sml
        if sa <= L[i] <= A:
            num -= 1
        if sa <= L[j] <= A:
            num -= 1
        if num > 0:
            ans += num
print(ans//3)

Solution (Python):
a = int(input())
arr = [int(i) for i in input().split()]
arr.sort() 
count = 0
for i in range(0,a-2): 
    k = i + 2
    for j in range(i+1,a):
        while (k < a and arr[i] + arr[j] > arr[k]): 
            k += 1
        if(k>j): 
            count += k - j - 1
  
print(count)

Solution (Python):
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()
A=0
for i in range(n):
    a=l[i]
    for j in range(i+1,n):
        b=l[j]
        k1=max(j+1,bisect(l,b-a))
        k2=bisect_left(l,a+b)
        A+=max(0,k2-k1)
print(A)

Solution (Python):
import fractions
import bisect
import math
import copy


N=int(input())
A=list(map(int,input().split()))
B=copy.copy(A)
A.sort(reverse=True)
B.sort()
total=0

for i in range(N):
    for j in range(i+1,N-1):
        x=bisect.bisect_right(B, A[i]-A[j])

        if N-j>x:
            total+=N-x-(j+1)
        

print(total)

Solution (Python):
import bisect

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

total = 0
for i in range(len(ls)-1):
    for j in range(i+1, len(ls)):
        
        maxindex = bisect.bisect_left(ls, ls[i]+ ls[j], j+1)
        minindex = bisect.bisect_right(ls, ls[j]- ls[i], j+1)
        total += maxindex - minindex
            

print(total)

Solution (Python):
import bisect

def main():
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()
    count = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = bisect.bisect_left(L, L[i] + L[j])
            k -= 1
            count += k - j
    print(count)


if __name__ == "__main__":
    main()

Solution (Python):
from bisect import*
n,*l=map(int,open(0).read().split())
l.sort()
A=0
for i in range(n):
    a=l[i]
    for j in range(i+1,n):
        b=l[j]
        k1=max(j+1,bisect(l,b-a))
        k2=bisect_left(l,a+b)
        A+=max(0,k2-k1)
print(A)

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

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

print(ans)

Solution (Python):
from bisect import bisect_right

n = int(input())
a = sorted(map(int, input().split()))
r = 0
for i in range(2, n):
    j = i - 1
    while j and a[j - 1] + a[j] > a[i]:
        r += j - bisect_right(a, a[i] - a[j], 0, j)
        j -= 1
print(r)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
 
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()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
L = LIST()
L.sort()

ans = 0
for i in range(N-2):
	one = L[i]
	for j in range(i+1, N-1):
		two = L[j]
		val = one + two - 1
		idx = bisect(L, val)
		if idx-j <= 0:
			continue
		else:
			ans += (idx-j-1)
print(ans)

Solution (Python):

import bisect 

n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n-1):
    for j in range(i+1,n):
        a = l[i]
        b = l[j]
        k1 = bisect.bisect_left(l,a+b)
        ab = abs(a-b)
        k0 = bisect.bisect_right(l,ab)
        cnti = k1 - k0 
        if k0 <= i and i <= k1:
            cnti -= 1
        if k0 <= j and j <= k1:
            cnti -= 1       
        
        cnt += cnti
print(cnt//3)

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]
        b=L[j]
        count+=bisect.bisect_left(L,a+b)-(j+1)
print(count)

Solution (Python):
import bisect

def main():
    N = int(input())
    L = [int(i) for i in input().split()]
    L.sort()
    count = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            k = bisect.bisect_left(L, L[i] + L[j])
            k -= 1
            count += k - j
    print(count)


if __name__ == "__main__":
    main()

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

l.sort()
count = 0
  
for i in range(0,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
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+2, N):
            a = L[i]
            c = L[j]
            k = bisect.bisect_left(L, c-a+1)
            if k <= i:
                ans += j-i-1
            else:
                ans += j-k
    return ans

if __name__ == '__main__':
    print(main())

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

n = int(input())
ar = list(map(int, input().split()))
print(findnumberofTriangles(ar))

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

total = 0

for i in range(n-2):
    aI = arr[i]
    k = i + 2
    
    for j in range(i+1,n):
        mySum = aI+arr[j]

        while (k<n) and arr[k]<mySum:
            k+=1
            
        if k>j:
            total+= k-j-1

print(total)

Solution (Python):
import bisect
import sys
input = sys.stdin.readline
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):
        A = L[i] + L[j] - 1
        sa = abs(L[i] - L[j]) + 1
        big = bisect.bisect_right(L, A)
        sml = bisect.bisect_left(L, sa)
        num = big - sml
        if sa <= L[i] <= A:
            num -= 1
        if sa <= L[j] <= A:
            num -= 1
        
        
        
        if num > 0:
            ans += num
        
        
        
print(ans//3)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a = sorted(a)
def trian(a):
    n=len(a)
    res = 0
    for x in range(n):
        z=x+2
        for y in range(x+1,n):
            while(z<n and a[x]+a[y]>a[z]):
                z+=1
            res += z-y-1
    return res
print(trian(a))

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

Solution (Python):
N=input()
L=tuple(map(int,input().split()))
l=[0]*1001
g=[0]*1001
for i in L:
    l[i]+=1
for i in range(1,1001):
    g[i]=g[i-1]+l[i]
a=0
for i in range(1,999):
    for j in range(i+1,1000):
        a+=l[i]*l[j]*(g[min(i+j-1,1000)]-g[j])
for i in range(1,1001):
    if l[i]>1:
        t=l[i]*(l[i]-1)//2
        for j in range(1,i):
            a+=t*l[j]
        if l[i]>2:
            a+=t*(l[i]-2)//3
        for j in range(i+1,min(1001,i*2)):
            a+=t*l[j]
print(a)

Solution (Python):

N=int(input())
L=list(map(int, input().split()))
L.sort()
ans=0
for i in range(N-2):
    k=i+2
    for j in range(i+1,N-1):
        l=L[i]+L[j]
        while(L[k]<l):

            k+=1
            if k==N:
                break
        k-=1
        ans+=k-j
print(ans)

Solution (Python):
from collections import defaultdict

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


d = [0] * (l[-1] * 3)
ans = 0

for i in range(n):
  for val in d[l[i] + 1:]:
    
    ans += val

  
  for j in range(i):
    d[l[i] + l[j]] += 1

print(ans)

Solution (Python):
n = int(raw_input())
L = map(int,raw_input().split())

L = sorted(L)
ans = 0

for a in xrange(n-2):
	c = a+2
	for b in xrange(a+1,n-1):
		while c < n and L[a] + L[b] > L[c]:
			c += 1
		if c > b:
			ans += (c-b-1)
print ans

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

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

L = sorted(L)

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:
            total += k-j-1

print(total)

Solution (Python):
import bisect

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

L.sort()
ans=0

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

Solution (Python):
import bisect
def main():
    n=int(input())
    l=sorted(list(map(int,input().split(' '))))
    ans = 0
    for n,i in enumerate(l):
        for m,j in enumerate(l[n+1:]):
                ans +=bisect.bisect_left(l,i+j) - bisect.bisect_right(l,abs(i-j))
                if abs(i-j)<i<i+j:
                    ans -=1
                if abs(i-j)<j<i+j:
                    ans -=1
    print(ans//3)
if __name__ == '__main__':
    main()

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

Solution (Python):
import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort()
L2 = L[::-1]
ans = 0
for i in range(N):
    long = L2[i]
    for j in range(i + 1, N):
        d = long - L2[j]
        ans += max(N - 1 - j - bisect.bisect_right(L[:N - j], d), 0)

print(ans)

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

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

print(findnumberofTriangles(L))

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

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

lList.sort()
counter = 0
for a in range(N-2):
    for b in range(a+1, N-1):
        ab = lList[a] + lList[b]
        startIndex = b+1
        endIndex = N-1
        while endIndex - startIndex > 1:
            searchIndex = (startIndex+endIndex)//2
            if lList[searchIndex] >= ab:
                endIndex = searchIndex
            else:
                startIndex = searchIndex
        if lList[startIndex] >= ab:
            counter += 0
        elif lList[endIndex] < ab:
            counter += N-1-b
        else:
            counter += startIndex - b

print(counter)

Solution (Python):
import bisect

n = int(input())
l = list(map(int, input().split()))
l.sort()
l_copy = l.copy()
l_copy = l_copy[::-1]

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        rest = l_copy[i] - l_copy[j]
        ind = bisect.bisect_right(l, rest)
        ind = n - ind - 1
        if ind <= j:
            break
        else:
            ans += ind - j

print(ans)

Solution (Python):
from bisect import bisect_right as binser
n = int(input())
l = [int(j) for j in raw_input().split()]
l = sorted(l)
ans = 0 
for i in range(1,n):
    for j in range(i):
        a = l[i] - l[j]
        x = binser(l,a, j+1, i)
        
        
        if l[x] == a:
            x = x+1
        ans+=(i-x)
        
        
print(ans)

Solution (Python):

import sys
import math
import numpy as np
import itertools
from bisect import bisect_left


n = int(input())


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

l=sorted(l)


answer = 0
for i in range(n-1,1,-1):
    for j in range(i-1,0,-1):
        k = bisect_left(l,l[i]-l[j]+1)
        if(l[j]>=l[k]):
            answer += max(j-k,0)



print(answer)

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

L = sorted(L)

mappin = [0] * 2001
for l in L:
    mappin[l] += 1

for i in range(2000):
    mappin[i + 1] += mappin[i]

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        temp = L[i] + L[j]
        addd = mappin[temp - 1] - j - 1
        
        
        ans += max([0, addd])


print(ans)

Solution (Python):
from bisect import bisect_left
import sys
sys.setrecursionlimit(10**9)
input = lambda: sys.stdin.readline().rstrip()
inpl = lambda: list(map(int,input().split()))
N = int(input())
L = inpl()
L.sort()
ans = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        k = bisect_left(L,L[i]+L[j],j,N) - (j+1)
        ans += k
print(ans)

Solution (Python):


def find(arr):

	n = len(arr)
	arr.sort()
	count = 0
	for i in range(0,n-2):

		k = i + 2

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

	return count

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


print(find(a))

Solution (Python):
import sys
import bisect

sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return list(map(int, sys.stdin.readline().split()))
def II(): return int(sys.stdin.readline())
def LS(): return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]

def main():
    n = II()
    ls = LI()
    ls = sorted(ls)
    ans = 0
    for i in range(n):
        for j in range(i):
            ans += j - (bisect.bisect_right(ls[:j], ls[i] - ls[j]))

            
            
            
            

    print(ans)

if __name__ == '__main__':
    main()

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

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