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):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

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

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

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

Solution (Python):


from bisect import bisect_left
def main():
    N = int(input())
    lst = list(map(int, input().split()))
    sor = sorted(lst)
    res = 0
    for i in range(N):
        for j in range(i+1, N):
            ab = sor[i] + sor[j]
            highbound = bisect_left(sor, ab)
            res += highbound - j - 1
    print(res)
main()

Solution (Python):


from bisect import bisect_left
N = int(input())
lst = list(map(int, input().split()))
sor = sorted(lst)
res = 0
for i in range(N):
    for j in range(i+1, N):
        ab = sor[i] + sor[j]
        highbound = bisect_left(sor, ab)
        res += highbound - j - 1
print(res)

Solution (Python):


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

def BinarySearch(lst, key): 
    left = -1  
    right = len(lst)  
    
    while right != left + 1:
        mid = (left + right) // 2
        val = lst[mid]
        if val >= key:
            right = mid
        elif val < key:
            left = mid
    return right  

res = 0
for i in range(N):
    for j in range(i+1, N):
        a = sor[i]
        b = sor[j]
        ab = a+b

        highbound = BinarySearch(sor, ab)
        res += highbound - j - 1
print(res)

Solution (Python):

from bisect import bisect_left
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+1, n):
            ab = L[i]+L[j]
            r = bisect_left(L, ab)
            l = j+1
            ans += r-l
    print(ans)
main()

Solution (Python):
from bisect import bisect_left, bisect_right  

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

ans = 0
for i in range(N):
    for j in range(i+1, N):
        tmp = L[i]+L[j]  

        
        ix = bisect_left(L, tmp)
        ans += ix-(j+1)

print(ans)

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
L = lr()
L.sort()
answer = 0
for i in range(N-1, -1, -1):
    
    x = L[i]
    left = 0
    right = i - 1
    while left < right:
        if L[left] + L[right] <= x:
            left += 1
            continue
        answer += (right - left)
        right -= 1

print(answer)

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
L = lr()
L.sort()
answer = 0
for i in range(N-1, -1, -1):
    
    x = L[i]
    left = 0
    right = i - 1
    while left < right:
        if L[left] + L[right] <= x:
            left += 1
            continue
        answer += (right - left)
        right -= 1

print(answer)

Solution (Python):
def binsearch(l, r, fn, i, j):
    while r - l > 1:
        m = (l + r) // 2
        if fn(m, i, j):
            l = m
        else:
            r = m
    return l

def main():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    def fn(m, i, j):
        a = L[m]
        b = L[i]
        c = L[j]
        if a < b + c and b < c + a and c < a + b:
            return True
        return False
    r = 0
    for i in range(N-2):
        for j in range(i+1, N-1):
            if fn(j+1, i, j):
                r += binsearch(j+1, N, fn, i, j) - j
    return r
print(main())

Solution (Python):
import sys
from bisect import bisect_left

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
L = lr()
L.sort()
answer = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        index = bisect_left(L, (L[i]+L[j]))
        answer += index - j - 1

print(answer)

Solution (Python):
import sys
from bisect import bisect_left

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
L = lr()
L.sort()
answer = 0
for i in range(N-2):
    for j in range(i+1, N-1):
        index = bisect_left(L, (L[i]+L[j]))
        answer += index - j - 1

print(answer)

Solution (Python):
import bisect

def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    num_list.sort()

    ans = 0

    for i in range(n-2):
        for j in range(i+1,n-1):
            tmp = bisect.bisect_left(num_list, num_list[i]+num_list[j])
            ans += tmp - j - 1

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left

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

Solution (Python):

def inputlist(): return [int(j) for j in input().split()]
from collections import Counter


N = int(input())
L = inputlist()
L.sort()
ans = 0
from bisect import bisect_left
for i in range(N-2):
    for j in range(i+1,N-1):
        lia = L[j+1:]
        indexa = bisect_left(lia,L[i]+L[j])
        ans += indexa
print(ans)

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

import bisect

L_list = sorted(L_list)
 
ans = 0
for a_index in range(0, N):
    for b_index in range(a_index+1, N):
        a = L_list[a_index]
        b = L_list[b_index]
        c_right_index = bisect.bisect_left(L_list, a+b)
        if c_right_index > b_index:
            ans += c_right_index - b_index - 1
        else:
            pass

print(ans)

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

Solution (Python):
import sys
import bisect
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):
        check = bisect.bisect_left(l, l[i] + l[j])
        ans += max(0, check - j - 1)
print(ans)

Solution (Python):
import bisect

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

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

    print(ans)

if __name__ == "__main__":
    solve()

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

for i in range(n-2):
    a=L[i]
    for j in range(i+1,n-1):
        b=L[j]
        high=n-1
        low=j+1
        if L[high]<a+b:
            ans+=n-j-1
            continue
        if L[low]>=a+b:
            continue
        while high-low>1:
            now=(high+low)//2
            c=L[now]
            if c<a+b:
                low=now
            else:
                high=now
        ans+=low-j
print(ans)

Solution (Python):
import collections

N = int(input())
L = [int(i) for i in input().split()]
M = max(L)
c = collections.Counter(L)
li = [c[i] for i in range(M+1)]

ans = 0
for i in range(1,M+1):
    for j in range(i,M+1):
        if i == j:
            ans += int(li[i] * (li[i] - 1) * (li[i] - 2) / 6)
            ans += int(sum(li[j+1:min(M+1, i+j)]) * li[i] * (li[i] - 1) / 2)
        else:
            ans += int(li[i] * li[j] * (li[j] - 1) / 2)
            ans += li[i] * li[j] * sum(li[j+1:min(M+1, i+j)])
print(int(ans))

Solution (Python):
import collections

N = int(input())
L = [int(i) for i in input().split()]
M = max(L)
c = collections.Counter(L)
li = [c[i] for i in range(M+1)]

ans = 0
for i in range(1,M+1):
    for j in range(i,M+1):
        if i == j:
            ans += int(li[i] * (li[i] - 1) * (li[i] - 2) / 6)
            ans += int(sum(li[j+1:min(M+1, i+j)]) * li[i] * (li[i] - 1) / 2)
        else:
            ans += int(li[i] * li[j] * (li[j] - 1) / 2)
            ans += li[i] * li[j] * sum(li[j+1:min(M+1, i+j)])
print(int(ans))

Solution (Python):
import bisect


def submit():
    n = int(input())
    llist = list(map(int, input().split()))

    llist.sort()
    ans = 0
    for a in range(n):
        for b in range(a + 1, n):
            c_min = llist[b] - llist[a]
            c_max = llist[a] + llist[b]
            cs = bisect.bisect_right(llist, c_min, lo=b + 1)
            ce = bisect.bisect_left(llist, c_max, lo=b + 1)
            ans += ce - cs

    print(ans)


if __name__ == "__main__":
    submit()

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

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

a = [0 for i in range(1000)]
b = [0 for i in range(1000)]

ans = 0

for i in range(n):
    a[l[i]-1] += 1

b[0] = a[0]

for i in range(1, 1000):
    b[i] = a[i] + b[i-1]

for i in range(1000):
    ans += a[i] * (a[i]-1) * (a[i]-2) // 6
    ans += a[i] * (a[i]-1) // 2 * (b[min(2*i, 999)]-a[i])

for i in range(1, 998):
    for j in range(i+1, 999):
        ans += a[i] * a[j] * (b[min(999, i+j)]-b[j])

print(ans)

Solution (Python):
import bisect

n = int(input())
li = [int(x) for x in input().split()]
li.sort()
ans = 0
for i in range(n-1):
    for j in range(n-1):
        if i<j:
            a = bisect.bisect_left(li,li[i]+li[j])
            ans += a-1-j
print(ans)

Solution (Python):
import bisect

n = int(input())
li = [int(x) for x in input().split()]
li.sort()
ans = 0
for i in range(n-1):
    for j in range(n-1):
        if i<j:
            a = bisect.bisect_left(li,li[i]+li[j])
            ans += a-1-j
print(ans)

Solution (Python):
import bisect

_n = input()
ls = list(sorted(map(int, input().strip().split())))
total = 0

for i in range(len(ls) - 2):
    for j in range(i + 1, len(ls) - 1):
        a = ls[i]
        b = ls[j]
        left = j + 1
        num = bisect.bisect_left(ls, a + b) - left
        total += num

print(total)

Solution (Python):
import bisect


def main(ls):
    total = 0

    for i in range(len(ls) - 2):
        for j in range(i + 1, len(ls) - 1):
            a = ls[i]
            b = ls[j]
            left = j + 1
            num = bisect.bisect_left(ls, a + b) - left
            total += num

    print(total)


_n = input()
ls = list(sorted(map(int, input().strip().split())))

main(ls)

Solution (Python):
import bisect


def main(ls):
    total = 0

    for i in range(len(ls) - 2):
        for j in range(i + 1, len(ls) - 1):
            a = ls[i]
            b = ls[j]
            left = j + 1
            num = bisect.bisect_left(ls, a + b) - left
            total += num

    print(total)


_n = input()
ls = list(sorted(map(int, input().strip().split())))

main(ls)

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

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

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

Solution (Python):
from bisect import bisect_left

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

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

Solution (Python):
"""
a < b + c
b < c + a
c < a + b
↓
a < b < c && c < a+b
"""
import bisect
n = int(input())
l = list(map(int, input().split()))

ans = 0
l.sort()

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

print(ans)

Solution (Python):
import bisect

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

for b in range(N):
    for a in range(b):
        ab = L[a] + L[b]
        r = bisect.bisect_left(L, ab) - 1
        l = b
        ans += r - l
print(ans)

Solution (Python):
from bisect import bisect_left


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

Solution (Python):
import copy
import math
import string
import heapq
import bisect
import sys
sys.setrecursionlimit(10**7)



N = int(input())
L = list(map(int,input().split()))
L.append(2**63)
L.sort()

ans = 0;
for i in range(N):
    for j in range(N):
        if i >= j:
            continue
        l = bisect.bisect_left(L,max(L[i]-L[j],L[j]-L[i])+1)
        r = bisect.bisect_right(L,L[i]+L[j]-1)
        cnt = r-l;
        if L[l] <= L[i] < L[r]:
            cnt -= 1
        if L[l] <= L[j] < L[r]:
            cnt -= 1
        ans += cnt
print(ans//3)

Solution (Python):
from bisect import bisect_left

n = int(input())
l = [int(_) for _ in input().split()]
l.sort()
ans = 0

for s in range(n - 2):
    for t in range(s + 1, n - 1):
        x = bisect_left(l, l[s] + l[t])
        ans += x - t - 1

print(ans)

Solution (Python):
import bisect
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
ans = 0
for b in range(n):
    for a in range(b):
        tmp = l[a] + l[b]
        r = bisect.bisect_left(l, tmp)
        ll = b + 1
        ans += max(0, r - ll)
print(ans)

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

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

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

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

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

Solution (Python):
import bisect
x=int(input())
y=list(map(int, input().split()))
 
y.sort()
 
a=0
b=0
c=0
ans=0
for i in range(x):
	a=y[i]
	for j in range(i+1,x):
		b=y[j]
		c=bisect.bisect_left(y,a+b)
		ans=ans+c-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,n)-j-1
print(ans)

Solution (Python):
from bisect import bisect_left

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

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

print(result)

Solution (Python):
from bisect import bisect_left

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

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

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

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

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
input_nums = lambda: list(map(int, input().split()))
from bisect import bisect_left

def main():
    N = int(input())
    L = sorted(input_nums())
    count = 0
    for i in range(N-2):
        a = L[i]
        for j in range(i+1, N-1):
            count += bisect_left(L, a+L[j]) - j - 1
    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
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]

        mi = 0
        ma = n - 1
        while mi != ma:
            m = (mi + ma) // 2 + 1
            if l[m] < a:
                mi = m
            else:
                ma = m - 1
        ans += max(0, mi - j)
print(ans)

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

cnt=0
import math
for i in range(n):
    for j in range(i+1,n):
        
        x=lists[i]
        y=lists[j]
        left=i 
        right=j 
        index=bisect.bisect_right(lists,y-x)
        cnt+=min(j-i-1,j-index)

print(cnt)

Solution (Python):
import sys


def input():
    return sys.stdin.readline().strip()


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

L.sort()

ans = 0

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

from bisect import bisect_left
n = int(input())
l = sorted(list(map(int, input().split())))

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

Solution (Python):
import sys
import bisect


def input():
    return sys.stdin.readline()[:-1]


if __name__ == "__main__":
    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):
            b = bisect.bisect_left(L, L[i]+L[j])
            cnt += b-j-1
    print(cnt)

Solution (Python):
import bisect

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

    ans = 0
    L = sorted(L)

    for i in range(N):
        a = L[i]
        for j in range(i+1, N):
            b = L[j]
            target = a+b

            c = bisect.bisect_left(L, target)
            ans = ans + (c-j-1)

    print (ans)
if __name__ == "__main__":
    main()

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

ans = 0

for a in range(N - 2):
    b = a + 1
    c = a + 2
    while c < N and b < N - 1:
        if L[c] >= L[a] + L[b]:
            ans += c - 1 - b
            b += 1
            if b == c:
                c += 1
        else:
            if c == N - 1:
                ans += (c - b) * (c - b + 1) // 2 
                break
            else:
                c += 1
        
print(ans)

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = sorted(inpl())
res = 0
for i in range(n-1):
    for j in range(i+1,n):
        c = bisect.bisect_left(a,a[i]+a[j]) 
        res += c-j-1
print(res)

Solution (Python):
from bisect import bisect_left

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

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

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

print(ans)

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

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

if __name__ == '__main__':
    main()

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

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

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

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

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

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

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

Solution (Python):
import bisect

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

l.sort()
ans = 0

for b in range(n - 2):
    for c in range(b + 1,n - 1):
        left = bisect.bisect_left(l,l[c] + l[b])
        ans+=left - c - 1

print(ans)

Solution (Python):
import sys
from bisect import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
from bisect import bisect_left as bl,bisect_right as br
for i in range(n):
    for j in range(i+1,n):
        a,s=l[i],l[j]
        ans+=-1-int(s<2*a)+bl(l,a+s)-br(l,s-a)
print(ans//3)

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

Solution (Python):
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):
        if L[i]+L[j]<=L[j+1]:
            continue
        if L[i]+L[j]>L[n-1]:
            cnt+=n-j-1
            continue
        left=j+1
        right=n-1
        while right-left>1:
            mid=left+(right-left)//2
            if L[mid]<L[i]+L[j]:
                left=mid
            else:
                right=mid
        cnt+=left-j
print(cnt)

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 += index-1-j

print(ans)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def MI(): return map(int, input().split())


def S(): return input()


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat[i])


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()

nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']



show_flg = False

def f(x):
    return x



def main():
    N = I()
    L = LI()
    
    R = sorted(L, reverse=True)
    L = R[::-1]
    
    ans = 0

    for i in range(N):
        for j in range(i+1, N):
            a = R[i]
            b = R[j]
            remain = a - b + 1
            idx = bisect.bisect_left(L, remain)
            c_idx = N - (idx + 1)
            if j < c_idx:
                c = (c_idx - j)
                ans += c
                
                
                
                
                

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
c = 0
for i in range(N-2):
    for j in range(i+1,N-1):
        zum = L[i]+L[j]
        start = j+1
        goal = N-1
        now = (start + goal)//2

        if zum > L[goal]:
            c += goal - j
        elif zum <= L[start]:
            c += 0
        else:
            while not L[now] < zum <= L[now+1]:
                now = (start + goal)//2
                if zum > L[now]:
                    start = now
                else:
                    goal = now
            c += now - j

print(c)

Solution (Python):
import bisect

N = int(input())
L = list(map(int, input().split()))
L.sort()
count = 0
for a in range(N):
    for b in range(a + 1, N):
        c = bisect.bisect_left(L, L[a] + L[b])
        if c > b:
            count += c - b - 1
        else:
            break
print(count)

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

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

Solution (Python):
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):
    x=j+1
    y=n-1
    while y!=x+1 and y!=x:
      if L[i]+L[j]>L[(x+y)//2]:
        x=(x+y)//2
      else:
        y=(x+y)//2
    p=y if L[i]+L[j]>L[y] else x
    if L[i]+L[j]<=L[p]:
      count+=0
    else:
      count+=p-j
print(count)

Solution (Python):
from sys import stdin
import bisect
        
N = int(stdin.readline().rstrip())
L = [int(x) for x in stdin.readline().rstrip().split()]
L.sort()

ans = 0
for a in range(len(L)-2):
    for b in range(a + 1, len(L)-1):
        c = bisect.bisect_left(L, L[a] + L[b])
        if c > b:
            ans += c - 1 - b

print(ans)

Solution (Python):
import bisect

N = int(input())
L = [int(x) for x in input().split()]
L.sort()
ret = 0

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

print(ret)

Solution (Python):
import bisect
n = int(input())
L = sorted(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)