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())
a=[int(i) for i in input().split()]
a.sort(reverse=True)
l=len(a)
ans=0
for i in range(l-2):
    e=l-1
    for j in range(i+1,l-1):
        if e<=j:
            break
        while a[i]-a[j]>=a[e] and e>j:
            ans+=(e-j)
            e-=1
ans=(n*(n-1)*(n-2))//6-ans
print(ans)

Solution (Python):
import bisect

def main():    
    n = int(input())
    L = list(int(x) for x in input().split())
    ans = 0
    c = 0
    L.sort()

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

if __name__ == '__main__':
    main()

Solution (Python):
import bisect

def main():    
    n = int(input())
    L = list(int(x) for x in input().split())
    ans = 0
    c = 0

    L.sort()

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

if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin 
from bisect import bisect_left
n = int(stdin.readline().rstrip())
l = [int(x) for x in stdin.readline().rstrip().split()]
l.sort()
answer = 0
for i in range(len(l)):
    for j in range(i):
        if i != j:
            k = bisect_left(l, l[i]+ l[j])
            answer += k - i - 1
print(answer)

Solution (Python):
from sys import stdin 
n = int(stdin.readline().rstrip())
l = [int(x) for x in stdin.readline().rstrip().split()]
l.sort()
answer = 0
for i in range(len(l)):
    for j in range(i):
        if i != j:
            low = i+1
            high = n
            c = l[i] + l[j]
            while low < high:
                mid = (low + high) // 2
                if l[mid] < c:
                    low = mid + 1
                else:
                    high = mid
            answer += low - i - 1
print(answer)

Solution (Python):
from sys import stdin 
from bisect import bisect_left
n = int(stdin.readline().rstrip())
l = [int(x) for x in stdin.readline().rstrip().split()]
l.sort()
answer = 0
for i in range(len(l)):
    for j in range(i):
        if i != j:
            k = bisect_left(l, l[i]+ l[j])
            answer += k - i - 1
print(answer)

Solution (Python):
import bisect

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

for x in range(n):
    for y in range(x + 1, n - 1):
        ans += bisect.bisect_left(l, l[x] + l[y]) - y - 1

print(ans)

Solution (Python):
from bisect import bisect_left

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

L = sorted(L)

ans = 0

for mid in range(N - 1, -1, -1):
    for low in range(mid - 1, -1, -1):
        itr = bisect_left(L, L[low] + L[mid])
        ng = len(L) - itr
        ans += (N - 1 - mid) - ng

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


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
L = input().split()
tri = 0
for i in range(0,N):
    L[i] = int(L[i])
L.sort()
for i in range(0,N):
    k = i
    for j in range(i+1,N):
        while(k < N and L[k] < L[i] + L[j]):
            k += 1
        tri += k-j-1
print(tri)

Solution (Python):
import sys
from bisect import bisect_left

N = int(input())
A = [int(v) for v in sys.stdin.readline().split()]

A = sorted(A)

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

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

print(cnt)

Solution (Python):
input()
a = input().split(' ')
b = []
for i in a:
    b.append(int(i))
b.sort()
b.reverse()
out = 0
for i in range(len(b) - 2):
    l = i + 1
    r = len(b) - 1
    while l < r:
        if b[i] < b[l] + b[r]:
            out += r - l
            l += 1
        else:
            r -= 1

print(out)

Solution (Python):
from bisect import bisect_left, bisect_right

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

l = sorted(l)

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

print(ans)

Solution (Python):
import bisect

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

L.sort()
cnt = 0

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

print(cnt)

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

print(ans)

Solution (Python):
import bisect

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

L.sort()
cnt = 0

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


print(cnt)

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

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

l.sort(reverse = 1)
ans = 0

for i in range(n-2):
    indL = i+1
    indR = len(l)-1
    while 1:
        if indL == indR:
            break
        if l[i] < l[indL] + l[indR]:
            ans += (indR - indL)
            indL += 1
        else:
            indR -= 1
                
print(ans)

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

a = list(map(int, input().split()))
a.sort()
ans = 0
for i in range(2, n):
    k = i - 1
    for j in range(i):
        while k > j and a[k] > a[i] - a[j]:
            k -= 1
        if k < j:
            k = j
        ans += i - k - 1

print(ans)

Solution (Python):
import bisect

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

count = 0
for n in range(N-2):
    for m in range(n+1,N-1):
        count += bisect.bisect_left(L,L[m]+L[n]) -m-1

print(count)

Solution (Python):

def binary_search(cur: int, upper_bound: int = 1 << 60, lower_bound: int = -1) -> int:
    ok, ng, ret = upper_bound, lower_bound, 0
    while ok > ng:
        if L[ok] + L[ng] > cur:
            ret += ok - ng
            ok -= 1
        else:
            ng += 1
    return ret


def main():
    
    global L
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = sum(binary_search(L[i], i - 1, 0) for i in range(N - 1, 1, -1))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

def binary_search(cur: int, upper_bound: int = 1 << 60, lower_bound: int = -1) -> int:
    ok, ng, ret = upper_bound, lower_bound, 0
    while abs(ok - ng) > 0:
        if L[ok] + L[ng] > cur:
            ret += ok - ng
            ok -= 1
        else:
            ng += 1
    return ret


def main():
    
    global L
    N, *L = map(int, open(0).read().split())
    L.sort()
    ans = sum(binary_search(L[i], i - 1, 0) for i in range(N - 1, 1, -1))
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def solve():
    N, *Ll = map(int, open(0).read().split())
    Ll.sort()
    ans = 0
    
    for i in range(N-1, 0, -1):
        a = 0
        b = i - 1
        while(a < b):
            if Ll[a] + Ll[b] > Ll[i]:
                ans += b - a
                b -= 1
            else:
                a += 1
    print(ans)

if __name__ == '__main__':
    solve()

Solution (Python):

n = int(input())
l = list(map(int,input().split()))
l.sort()
ret = 0
from bisect import bisect_right

for i in range(n-1,1,-1):
    longest = l[i]
    for j in range(i-1,0,-1):
        second_longest = l[j]
        ret += max(j-bisect_right(l,longest-second_longest),0)
print(ret)

Solution (Python):
from enum import Enum
from queue import Queue
import collections
import bisect

import sys
import math

BIG_NUM = 2000000000
MOD = 1000000007
EPS = 0.000000001

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

print(ans)

Solution (Python):
import bisect

n = int(input())
l = list(int(x) for x in input().split())
l.sort()
ans = 0
for i in range(n):
    for j in range(i+1, n):
        key = bisect.bisect_left(l, l[j]-l[i]+1)
        ans += max([0, i-key])
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
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


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

    ans = 0
    for i in range(n - 2):
        for j in range(i + 1, n - 1):
            rest = ll[j + 1:]
            a_plus_b = ll[i] + ll[j]
            pos_right = bisect.bisect_left(rest, a_plus_b)
            count = pos_right

            ans += count
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import combinations
from collections import Counter

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

counter = Counter()

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

sums = [0]
L_MAX = 10**3

for i in range(1, L_MAX+1):
    sums.append(sums[i-1]+counter[i])

res = 0

for l1, l2 in combinations(L, r=2):
    upper = l1+l2
    lower = abs(l1-l2)

    tmp = sums[min(L_MAX, upper-1)] - sums[lower]
    if l1 < upper and l1 > lower:
        tmp -= 1
    if l2 < upper and l2 > lower:
        tmp -= 1

    res += tmp

print(res//3)

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

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

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

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

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

print(ans)

Solution (Python):
import bisect

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

L.sort()

count=0

for i in reversed(range(2,N)):
  for j in reversed(range(1,i)):
    index=bisect.bisect_right(L,L[i]-L[j])
    
    if index<j:
     count += j-index
            
print(count)

Solution (Python):
import bisect

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

L.sort()
ans = 0
for i in range(N):
    for j in range(i+1, N):
        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 = list(map(int, input().split()))

l.sort()
c = 0
for i in range(len(l) - 2):
    for j in range(i + 1, len(l) - 1):
        c += bisect.bisect(l[j+1:], l[i]+l[j]-1)
print(c)

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

def main():
    N=ii()
    L=sorted(lmis(),reverse=True)
    ans=0
    for i in range(N-2):
        a=L[i]
        j=i+1
        k=N-1
        while j<k:
            if a<L[j]+L[k]:
                j+=1
                ans+=k-j+1
            else:
                k-=1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1000000000)
ii = lambda: int(input())
mis = lambda: map(int,input().split())
lmis = lambda: list(mis())
INF = float('inf')

import itertools as it

def main():
    N=ii()
    L=sorted(lmis(),reverse=True)

    ans=0
    for i in range(N-2):
        a=L[i]
        j=i+1
        k=N-1
        while j<k:
            if a<L[j]+L[k]:
                j+=1
                ans+=k-j+1
            else:
                k-=1
    
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
N=int(input())
Ln=list(map(int,input().split()))
Ln.sort(reverse=True)
Ans=0
left=0
right=0
 
for i in range(N-2):
    di=i+1
    for j in range(N-di-1):
        dj=di+j+1
        A=Ln[di-1]
        B=Ln[dj-1]
        if (B+Ln[dj])<A : break
 
        right=N-1
        left=dj
        while left<=right :
            mid=int((right+left)/2)
            if (B+Ln[mid])>A :
                left=mid+1
            else :
                right=mid-1
        Ans+=(left-1)-(dj-1)
 
print(Ans)

Solution (Python):
from bisect import bisect_left

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

for i in range(n-2):
    shortest = l[i]
    for j in range(i+1, n-1):
        second = l[j]
        
        
        

        k = bisect_left(l, shortest + second)
        ans += k - j - 1

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
import itertools
import math
INF = 10**9
MOD = 10**9+7

N = int(input())
L = [int(x) for x in input().split()]
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])
        ans += k-j-1
print(ans)

Solution (Python):
import bisect

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

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

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

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect

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

Solution (Python):
import bisect

n = int(input())
l = sorted([int(li) for li in input().split()])

sum = 0
for i in range(n-1,-1,-1):
    for j in range(i-1,-1,-1):
        k = bisect.bisect_right(l,l[i]-l[j])
        sum += j-k if j>k else 0

print(sum)

Solution (Python):
import bisect

def main():
    n, *l = map(int, open(0).read().split())
    l = sorted(l, reverse=True)

    ans = 0
    for a_i in range(n-2):
        a = l[a_i]
        b_i = a_i + 1
        c_i = n - 1
        while b_i < c_i:
            b = l[b_i]
            c = l[c_i]
            if a < b + c:
                b_i += 1
                ans += c_i - b_i + 1
            else:
                c_i -= 1
    print(ans)
    return()

if __name__ == '__main__':
    main()

Solution (Python):
import bisect

def next_int():
    return int(input())

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

n = next_int()
a = next_ints()
a.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        pos = bisect.bisect_right(a, a[i] + a[j] - 1, j+1, n)
        ans += pos - j - 1
print(ans)

Solution (Python):
import bisect

def next_int():
    return int(input())

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

n = next_int()
a = next_ints()
a.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        pos = bisect.bisect_right(a, a[i] + a[j] - 1)
        ans += pos - 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)-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):
from bisect import bisect_left

def Triangles(N, L):
    asc = sorted(L)
    cnt = 0
    for i in range(N-2):
        l1 = asc[i]
        for j in range(i+1, N-1):
            l2 = asc[j]
            pos = bisect_left(asc, l1+l2)
            cnt += pos - 1 - j
    return cnt

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

Solution (Python):
from bisect import bisect_right

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

count = 0
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1, n-1):
        ab = a+l[j]
        
        
        k = bisect_right(l, ab-1, j+1, n)
        count += k-(j+1)
print(count)

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

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

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

Solution (Python):
from bisect import bisect_left

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):
        idx = bisect_left(L, L[i]+L[j])
        res += idx-(j+1)

print(res)

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

Solution (Python):
def f(list, n):
  length=len(list)
  if list[-1]<n:
    return length
  elif list[0]>=n:
    return 0
  else:
    a=length//2
    b=length//4
    while b>=1:
      if list[a]<n:
        a+=b
      else:
        a-=b
      b=b//2
      
    if list[a]<n:
      while list[a+1]<n:
        a+=1
    else:
      while list[a]>=n:
        a-=1
        
    return a+1  

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

Solution (Python):




import sys

readline = sys.stdin.readline 

n = int(input())
a = [int(i) for i in readline().split()]
a.sort()

ans = 0
for i in range(n):
    r = i+1
    for j in range(i+1,n):
        A = a[i] + a[j]
        while r < n and A > a[r]:
            r += 1
        ans += r-j-1

print(ans)

Solution (Python):
from bisect import bisect_right

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

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

L_list.sort()

from bisect import bisect_left

for i in range(N):
    for j in range(i+1, N):
        t = L_list[i]+L_list[j]
        idx = bisect_left(L_list, t)
        sum += max(0, idx-(j+1))
        
print(sum)

Solution (Python):
from bisect import bisect_left
N = int(input())
L = list(map(int,input().split()))
L_1 = sorted(L)

ans = 0

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

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

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

Solution (Python):
import bisect
n = int(input())
l = list(map(int, input().split()))
count = 0
l.sort(reverse = False)
for i in range(len(l)-1, 1, -1):
  for j in range(i-1, 0, -1):
    pt = bisect.bisect_right(l, l[i] - l[j])
    count += max(j - pt, 0)
print(count)

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

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

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

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

print(ans)

Solution (Python):
import bisect

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

l.sort()


cnt = 0

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

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

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

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

vL.sort()


res = 0
for x in range(0, N-2):
	a = vL[x]
	for y in range(x+1, N-1):
		b = vL[y]
		
		cntC = bs.bisect_left(vL, a+b)
		res += cntC - (y+1)
		

print(res)

Solution (Python):

import bisect

def lower_bound(sorted_list, value):
    return bisect.bisect_left(sorted_list, value)


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

L = sorted(L)

count = 0

for a in range(N):
    for b in range(a+1,N):
        count += lower_bound(L, L[a] + L[b]) - (b + 1)
            
print(count)

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

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

print(ans)

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

print(count)

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

l.sort()

from bisect import bisect_left

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

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

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

l.sort()

from bisect import bisect_left

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

Solution (Python):
import bisect
N=int(input())
L=list(map(int, input().split()))
ans=0
L_LENGTH=len(L)
L=sorted(L)
for i in range(L_LENGTH-2):
    for j in range(i+1, L_LENGTH-1):
        a=L[i]+L[j]
        idx=bisect.bisect_left(L, a)
        ans+=max(0, idx-j-1)
print(ans)

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

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

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

Solution (Python):

import bisect
import sys
input = sys.stdin.readline
 
 
def main():
    n = int(input())
    l = list(map(int, input().split()))
    l = sorted(l)
 
    result = 0
    for i in range(n-2):
        for j in range(i + 1, n - 1):
            result += bisect.bisect_left(l, l[i] + l[j]) - (j + 1)
 
    print(result)
 
 
if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import sys
input = sys.stdin.readline
 
 
def main():
    n = int(input())
    l = list(map(int, input().split()))
    l = sorted(l)
 
    result = 0
    for i in range(n-2):
        for j in range(i + 1, n - 1):
            result += bisect.bisect_left(l[j+1:], l[i] + l[j])
 
    print(result)
 
 
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):
    a = l[i]
    for j in range(n - i - 2):
        b = l[i + j + 1]
        index = bisect.bisect_left(l, a + b)
        if index > i + j + 2:
            ans += index - (i + j + 2)

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

print(ans)

Solution (Python):

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

Solution (Python):
def solve(string):
    n, *l = map(int, string.split())
    l.sort(reverse=True)
    nums = 0
    for i, a in enumerate(l[:-2]):
        j, k = i + 1, n - 1
        while (j < k):
            if (a < l[j] + l[k]):
                nums += k - j
                j += 1
            else:
                k -= 1
    return str(nums)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

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

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


print(a)

Solution (Python):
def solve(string):
    n, *l = map(int, string.split())
    l.sort()
    nums = 0
    for i, a in enumerate(l[:-2]):
        k = i + 2
        for j, b in enumerate(l[i + 1:-1], i + 1):
            while k < n and l[k] < a + b:
                k += 1
            nums += k - j - 1
    return str(nums)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
from bisect import bisect_left, bisect_right

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

L.sort()

count = 0
for i, l1 in enumerate(L):
    for j, l2 in enumerate(L):
        if i < j:
            s = max(j+1, bisect_right(L, abs(l1-l2)))
            e = max(j+1, bisect_left(L, l1+l2))

            
            count += max(0, e-s)
            

print(count)

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

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

Solution (Python):
from bisect import bisect_left
import sys
input = sys.stdin.readline
def solve():
  n = int(input())
  l = sorted(list(int(i) for i in input().split()))
  ans = 0
  for i in range(n):
    for j in range(i+1,n):
        ans += max(0,bisect_left(l,l[i]+l[j])-j-1)
  print(ans)
  
solve()

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

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