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
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


sys.setrecursionlimit(1000000)
MOD = int(1e09) + 7
INF = int(1e30)


def main():
    
    N = Scanner.int()
    L = Scanner.map_int()
    L.sort()

    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) - j - 1
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

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

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)

solve()

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

Solution (Python):
import sys
from bisect import bisect_left

read=sys.stdin.buffer.readline

N=int(read())
L=list(map(int,read().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):
import bisect

n = int(input())
l = sorted(list(map(int,input().split())))
count = 0
for i in range(2,n):
	for j in range(1,i):
		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()))
L.sort()
cnt = 0

for i in range(N):

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

print(cnt)

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

for i in range(N):
    r = i+1
    for j in range(i):
        t = A[i] + A[j]
        while r < N and A[r] < t:
            r += 1
        l = i+1
        ans += max(0, r-l)
print(ans)

Solution (Python):
import math
from bisect import bisect_right
from bisect import bisect_left

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):
        lg = L[i] + L[j]
        st = L[j] - L[i]
        if lg > L[j+1] and st < L[n-1]:
            if lg > L[n-1]:
                m1 = n - (j+1)
            else:
                m1 = bisect_left(L[j+1:],lg)
            c += max(0,m1)
print (c)

Solution (Python):
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):
        val = l[i]+l[j]
        ng = n
        ok = j
        while abs(ok - ng) > 1:
            mid = (ok+ng)//2
            if val > l[mid]:
                ok = mid
            else:
                ng = mid


        

        ans += ok - j


print(ans)

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

ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        a, b = x[i], x[j]
        left, right = j + 1, n
        while right - left > 1:
            k = (left + right) // 2
            if a < b + x[k]:
                left = k
            else:
                right = k
        if a < b + x[left]:
            ans += (left - j)

print(ans)

Solution (Python):
import bisect

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

'''
リストをソートし、a, b <= cとなるようにa, bを取り出せば、
a < b + c, b < c + a という条件は満たすので、
a, bを取り出したとき、c < a + b を満たすcを二分探索で探す
'''

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

Solution (Python):
from bisect import bisect_left

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):
		ans += 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()
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 itertools import accumulate

def main():
    N=int(input())
    L=list(map(int,input().split()))
    long=[0 for i in range(0,1001)]
    for i in range(0,N):
        long[L[i]]+=1
    netlong=[]
    for i in range(1,1001):
        if long[i]>=1:
            netlong.append(i)
    X=list(accumulate(long))
    subans=[0 for i in range(0,2001)]
    ans=0
    for i in range(0,len(netlong)):
        a=netlong[i]
        for j in range(netlong[i]+1,2*netlong[i]-1):
            ans+=subans[j]*long[a]
        ans=ans+X[a-1]*long[a]*(long[a]-1)//2+long[a]*(long[a]-1)*(long[a]-2)//6
        for j in range(0,i):
            subans[a+netlong[j]]+=long[a]*long[netlong[j]]
        subans[2*a]+=long[a]*(long[a]-1)//2
    print(ans)

if __name__ == '__main__':
    main()

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

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



for i in range(N):
  for j in range(i+1,N):
    a,b = L[i],L[j]
    k = bisect.bisect_left(L,a+b)
    count += k-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):
  for j in range(1+i,n):
    a,b = l[i],l[j]
    cnt += bisect.bisect_left(l,a+b)-(j+1)
    
print(cnt)

Solution (Python):
import bisect

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

ans=0
for i in range(N):
    a=L[i]
    for j in range(i+1, N):
        b=L[j]
        ans+=bisect.bisect_left(L, a+b)-(j+1)
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,N):
   if(i != j):
    a,b = L[i],L[j]
    a_plus_b = a+b
    k = bisect.bisect_left(L,a+b)
    tmp = max(0, k-(j+1))
    count += tmp
    
print(count)

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

cnt = 0

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	L = LI();L.sort();
	

	def BS(a,c,i,j):
		s,t = i,j
		while s+1 < t:
			mid = (s+t)//2
			if (a+L[mid]>c):
				t = mid
			else:
				s = mid
		return j-t


	ans = 0

	for i in range(N-1):
		for j in range(i+2,N):
			a = L[i]
			c = L[j]
			ans += BS(a,c,i,j)

	print(ans)

main()

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

Solution (Python):
def binary_search(a,b,j):
   left=j
   right=N
   while left<right-1:
      mid=(left+right)//2
      if L[mid]<a+b:
         left=mid
      else:
         right=mid

   return left

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

count=0

for i in range(N):
   for j in range(N):
      if j<=i:
         continue
      if j==N-1:
         continue
      a=L[i]
      b=L[j]

      c_index=binary_search(a,b,j)
      count+=c_index-j

print(count)

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

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

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

Solution (Python):
from bisect import bisect_left

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

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

print(ans)

Solution (Python):
import bisect
n = int(input())
length_list = sorted(list(map(int, input().split())))
result = 0
 
for i in range(n-2):
  for j in range(i+1, n-1):
    upper = length_list[i] + length_list[j] - 1
    search_target = length_list[j+1:]
    less_than_upper = bisect.bisect_right(search_target, upper)
    result += less_than_upper
    
print(result)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
x = []
for i in range(N-2):
    check = 0
    for j in range(i+1,N-1):
        for k in range(j+1+check,N):
            if L[k] < L[i]+L[j]:
                check += 1
            else:
                break
        ans += check
        check -= 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):
        k = bisect.bisect_left(L, L[i]+L[j])
        ans += max(0, k-j-1)
print(ans)

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

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

Solution (Python):
import bisect

n = int(input())
l = sorted(list(map(int, input().split())))
count = 0
for b in range(n-2):
  for c in range(b+1, n-1):
    count += max(0, bisect.bisect_left(l, l[b]+l[c])-c-1)

print(count)

Solution (Python):

import sys
import bisect


def solve(N: int, L: "List[int]"):
    L.sort()
    cnt=0
    for a_i in range(N):
        for b_i in range(a_i+1, N):
            c_r = bisect.bisect_left(L, L[a_i]+L[b_i])
            cnt += c_r - b_i - 1
    return cnt


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)]  
    print(solve(N, L))

if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left

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

count = 0
length_list.sort()

for a_idx in range(n - 2):
    for b_idx in range(a_idx + 1, n - 1):
        a = length_list[a_idx]
        b = length_list[b_idx]

        left_count = bisect_left(length_list, a+b)
        count += max(0, left_count - b_idx - 1)


print(count)

Solution (Python):





import bisect

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


def lower_bound(arr, x):
    l, r = 0, len(arr)
    while l < r:
        mid = (l + r) // 2
        if arr[mid] < x:
            l = mid + 1
        else:
            r = mid
    return l


ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        idx_l = lower_bound(arr, arr[i] + arr[j]) - 1
        
        
        
        
        
        
        
        ans += len(arr[j + 1 : idx_l + 1])
print(ans)

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

ans = 0
def find(x, arr):
  a = len(arr)//2
  cur = a
  while True:
    a //= 2
    if arr[cur] < x:
      if cur == len(arr)-1:
        return cur+1
      elif arr[cur+1] >=x:
        return cur+1
      else:
        cur += max(1,a)
    else:
      if cur == 0:
        return 0
      else:
        cur -= max(1, a)
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):
N = int(input())
L = list(map(int, input().split()))
L.sort()

ans = 0
def find(x, arr):
  a = len(arr)//2
  cur = a
  while True:
    a //= 2
    if arr[cur] < x:
      if cur == len(arr)-1:
        return cur+1
      elif arr[cur+1] >=x:
        return cur+1
      else:
        cur += max(1,a)
    else:
      if cur == 0:
        return 0
      else:
        cur -= max(1, a)
for i in range(N-2):
  for j in range(i+1, N-1):
    ans += find(L[i] + L[j], L)-j-1
print(ans)

Solution (Python):
import bisect

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

SL = sorted(L)
ans = 0
for i in range(N):
  if i+1 == N-1:
    break
  a = SL[i]
  for j in range(i+1, N):
    b = SL[j]
    c_max = bisect.bisect_left(SL, a+b)
    ans += c_max - (j+1)

print(ans)

Solution (Python):
from bisect import bisect_left
n = int(input())
nums = sorted([int(i) for i in input().split()])

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        tmp = nums[i] + nums[j]
        idx = bisect_left(nums, tmp)
        ans += idx - j - 1
print(ans)

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

L_list.sort()
import bisect
res = 0
for i in range(N):
    a= L_list[i]
    for j in range(i+1,N):
        b= L_list[j]
        ind = bisect.bisect_left(L_list,a+b)
        res+=(ind-j-1)

print(res)

Solution (Python):
n = int(input())
nums = list(map(int, input().split(' ')))
nums.sort()
total = 0

for i in range(n-1, -1, -1):
    l = 0
    r = i - 1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += r - l
            r -= 1
        else:
            l += 1
print(total)

Solution (Python):
n = int(input())
nums = list(map(int,input().split(" ")))
nums.sort()
total = 0
for i in range(n-1,-1,-1):
    l = 0
    r = i-1
    while l < r:
        if nums[l] + nums[r] > nums[i]:
            total += r - l
            r -= 1
        else:
            l += 1
print(total)

Solution (Python):
import bisect

n = int(input())
l = sorted([int(i) for i in input().split()])
ans = 0
for d in range(n):
    for e in range(d+1, n):
        a, b = l[d], l[e]
        idx = bisect.bisect_left(l, a + b)
        ans += max(idx - e - 1, 0)
print(ans)

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

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

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

ans = 0
for i in range(N):  
    for j in range(i + 1, N):  
        ans += bisect_left(L, L[i] + L[j]) - (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 a in range(0, N-2):
  for b in range(a+1, N-1):
    t = L[a] + L[b]
    idx = bisect_left(L,t)
    ans += max(0, idx-b-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-2):
  for j in range(i+1, n-1):
    ans += 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()
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):
import bisect
N=int(input())
d=sorted(list(map(int,input().split())))
cnt=0
for i in range(N-1,1,-1):
  for j in range(i-1,0,-1):
    k=bisect.bisect_right(d,d[i]-d[j])
    if j-k>0:
      cnt+=j-k
print(cnt)

Solution (Python):
import itertools
import bisect

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

l = sorted(l)

combinations = list(itertools.combinations(range(n),2))

cnt = 0

for i in combinations:
    a_b = l[i[0]] + l[i[1]]
    max_ab = max(i[0], i[1])
    c = bisect.bisect_left(l,a_b)
    
    cnt += (c - (max_ab+1))


print(cnt)

Solution (Python):

import sys, re, os
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 S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT()
    L = LIST()
    L.sort()
    ans = 0
    
    for i in range(0, N - 2):
        a = L[i]
        for j in range(i + 1, N - 1):
            b = L[j]
            x = a + b - 1
            
            
            
            index = bisect(L, x)
            
            
            if j < index:
                
                
                
                ans += (index - j - 1)
    print(ans)
if __name__ == "__main__":
    main()

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


import random  
def I():
    return int(input())
def MI():
    return map(int,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 show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')



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

show_flg=False
show_flg=True

n=I()
a=sorted(LI())[::-1]

def solve(a):
    s=set()
    for i in a:
        s.add(i)
    s=sorted(list(s))[::-1]
    L=1010
    k=[0]*(L)
    ak=[0]
    for i in a:
        k[i]+=1
    for i in range(L):
        ak.append(ak[-1]+k[i])
    ans,a1,a2,a3,a4=0,0,0,0,0
    un=len(s)
    for i in range(un):
        c=s[i]
        if k[c]>=3:
            a1+=k[c]*(k[c]-1)*(k[c]-2)//6
        if k[c]>=2:
            a2+=ak[c]*k[c]*(k[c]-1)//2
        for j in range(i+1,un):
            sc=s[j]
            a3+=(1 if 2*sc>c else 0)*k[c]*k[sc]*(k[sc]-1)//2
            mm=c-sc+1
            if mm<0:
                continue
            elif mm>sc:
                break
            else:
                a4+=k[c]*k[sc]*(ak[sc]-ak[mm])
            
    ans=a1+a2+a3+a4
    return ans,a1,a2,a3,a4

def naive(a):
    b=sorted(a)[::-1]
    n=len(b)
    ans=0
    for i in range(n):
        for j in range(i+1,n):
            for k in range(j+1,n):
                if b[j]+b[k]>b[i]:
                    ans+=1
    return ans

print(solve(a)[0])

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

li = list(map(int,input().split()))
li.sort()
cnt = 0
import bisect
for a in range(len(li)):
    for b in range(a + 1, len(li)):
       i = bisect.bisect_left(li, li[a]+li[b])
       cnt += i - b - 1

print(cnt)

Solution (Python):

n=int(input())
l=list(map(int,input().split()))
l=sorted(l,reverse=True)
ans=0

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

        
        ok=j
        ng=n
        while ng-ok>1:
            mid=(ok+ng)//2
            if l[mid]>b-c:
                
                ok=mid

            else:
                ng=mid

        ans+=ok-j
print(ans)

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

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

Solution (Python):
import bisect
n = int(input())
cnt = 0
l = sorted(list(map(int, input().split())))
for a in range(n - 2):
    for b in range(a + 1, n - 1):
        c_min = b + 1
        c_max = bisect.bisect_left(l, l[a] + l[b]) - 1
        cnt += c_max - c_min + 1
print(cnt)

Solution (Python):
import itertools
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):
        b = L[j]
        ind_cmin = j+1
        ind_cmax = bisect.bisect_left(L,a+b)-1
        ans += ind_cmax - ind_cmin + 1
print(ans)

Solution (Python):
import itertools
import bisect
            

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):
        b = L[j]

        
        ind_cmin = j+1

        
        
        ind_cmax = bisect.bisect_left(L,a+b) - 1

        ans += ind_cmax - ind_cmin + 1

print(ans)

Solution (Python):
import itertools

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









L = sorted(L)


X = []
for j in range(L[-1]+1):
    flag = False
    for i in range(N-1,-1,-1):
        if L[i] < j:
            X.append(i)
            flag = True
            break
    
    if not flag: X.append(None)


Y = []
for j in range(L[-1]+1):
    flag = False
    for i in range(N):
        if L[i] > j:
            Y.append(i)
            flag = True
            break
    
    if not flag: Y.append(None)



ind1 = 0
ind2 = N-1
ans = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1,N-1):
        b = L[j]

        

        
        cmin = Y[b-a]
        if a+b <= L[-1]:
            cmax = X[a+b]
        else:
            cmax = N - 1

        
        if cmin is not None and cmin <= j:
            cmin = j + 1

        

        
        
        
        
            
        
        if cmin is not None and cmax is not None and cmin <= cmax:
            ans += cmax - cmin + 1

print(ans)

Solution (Python):
import bisect

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

if __name__ == '__main__':
    main()

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

L.sort()

def bsearch(array, value):
  left = 0
  right = len(array)
  while left + 1 < right:
    idx = int(left + (right - left) / 2)
    if array[idx] < value:
      left = idx
    else:
      right = idx
  return left

sum = 0
ai = 0
while ai < len(L) - 2:
  bi = ai + 1
  while bi < len(L) - 1:
    a = L[ai]
    b = L[bi]
    i = bsearch(L, a + b)
    sum += (i - bi)

    bi += 1
  ai += 1

print(sum)

Solution (Python):
import bisect

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

for i in range(N-1):
    a = L[i]
    for j in range(i+1, N):
        b = L[j]
        up_bound = bisect.bisect_left(L, a+b)
        ct += max(0, up_bound - (j+1))

print(ct)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
from collections import defaultdict
D = [0]*2001




A = sorted(A)

ans = 0

for i in range(N):
    tmp = 0
    for j in range(A[i]+1,2001):
        tmp += D[j]
        ans += D[j]
    
    for j in range(i):
        D[A[i]+A[j]] += 1
print(ans)

Solution (Python):

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

from bisect import bisect_left

ans = 0
for c in range(2, N):
  for a in range(c-1):
    b_index = bisect_left(L, L[c]-L[a]+1)

    if b_index <= a:
      b_index = a + 1
    if c - b_index > 0:
      ans += c - b_index

print(ans)

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

ans = 0
i = 0
while i < N-2:
    j = i+1
    while j < N-1:
        kNG = bisect.bisect_left(L,L[i]+L[j])
        if kNG > j+1:
            ans += kNG-1-j
        j += 1
    i += 1

print(ans)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]



def bin_gte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None

def bin_lte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        
        c = (l+r+1)//2
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None

n = int(input())
lll = get_nums_l()
lll.sort()

ans=0

maxs = [None] * 2002
mi = 0
prev = lll[0]
for l in lll[1:]:
    for j in range(prev, l+1):
        maxs[j] = mi
    mi += 1
    prev = l
for j in range(prev, 2002):
    maxs[j] = mi

mins = [None] * 2002
mi = 0
prev = 0
for l in lll:
    for j in range(prev+1, l+1):
        mins[j] = mi
    mi += 1
    prev = l

for ai, a in enumerate(lll):
    if ai == n-2:
        break
    for bi in range(ai+1, n-1):
        b = lll[bi]

        _min = abs(a-b) + 1
        _max = a+b - 1

        left = mins[_min]
        right = maxs[_max]
        
        

        if left is None or right is None:
            continue
        
        if right < bi:
            continue
        left = max(bi+1, left)

        
        ans += right-left+1

print(ans)

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_nums_n(n):
    return [ int(input()) for _ in range(n)]



def bin_gte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        c = (l+r)//2
        if arr[c] >= n:
            r = c
        else :
            l = c+1
    c = max(0, (l+r)//2)
    if arr[c] >= n:
        return (c, arr[c])
    else:
        return None

def bin_lte(arr, n, l):
    r = len(arr) - 1
    c = (l+r)//2
    while l < r:
        
        c = (l+r+1)//2
        if arr[c] <= n:
            l = c
        else :
            r = c-1
    c = max(0, (l+r)//2)
    if arr[c] <= n:
        return (c, arr[c])
    else:
        return None

n = int(input())
lll = get_nums_l()
lll.sort()

ans=0
























for ai, a in enumerate(lll):
    if ai == n-2:
        break
    for bi in range(ai+1, n-1):
        b = lll[bi]

        _min = abs(a-b) + 1
        _max = a+b - 1

        
        
        left = bin_gte(lll, _min, bi+1)
        right = bin_lte(lll, _max, bi+1)
        if left is None or right is None:
            continue
        
        ans += right[0]-left[0]+1
        

print(ans)

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

def binary(a,b,mid):
  if  L[mid]+L[j]>L[i]:
    return True
  else:
    return False

ans=0
for i in range(n-2):
  for j in range(i+1,n-1):
    l=j
    r=n
    while r-l>1:
      mid=(r+l)//2
      if binary(i,j,mid):
        l=mid
      else:
        r=mid
    ans+=l-j
print(ans)

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