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 bisect

N = int(input())
L = [int(li) for li in input().split(' ')]
LMAX = 10 ** 3

L.sort()

ans = 0

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

        ind = bisect.bisect_left(L, lin)
        ans += ind-j-1
print(ans)

Solution (Python):
import bisect

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

MAX=L[-1]
count=0
for i in range(N-2):
    for  j in range(i+1,N-1):
        
        
        minInd=j+1
        
        
        if L[minInd]> L[i]+L[j]:
            continue
        if MAX<L[i]+L[j]:
            count+=N-1-minInd+1
        else:
            ind = bisect.bisect_left(L, L[i]+L[j])-minInd 
            count+=ind
        
print(count)

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

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

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

Solution (Python):
import bisect

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


print(c)

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

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
ans = 0
L.sort()
lst = [0 for i in range(10**3+1)]
for i in range(N):
    lst[L[i]]+=1
for i in range(10**3+1):
    if i != 0:
        lst[i]=lst[i-1]+lst[i]
for i in range(N-1):
    for j in range(i+1,N):
        p = L[i]+L[j]
        if p-1>=10**3+1:
            ans += lst[10**3]-(j+1)
        else:
            ans += lst[p-1]-(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,N-1):
        l1 = L[N-1-i]
        l2 = L[N-2-j]
        
        l = N - 2 - j - bisect.bisect_right(L, l1-l2)
        if l >= 0:
            ans += l

print(ans)

Solution (Python):
import bisect


def solve(ls) -> int:
  n = len(ls)
  assert(3 <= n <= 2 * 10**3)

  def n_possible_cs(a: int, b: int, first_ic: int) -> int:
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    idx = bisect.bisect_left(ls, a + b, first_ic)
    return 0 if idx == first_ic else idx - first_ic

  ret = 0
  for ia in range(0, n - 2):
    for ib in range(ia + 1, n - 1):
      a, b = (ls[ia], ls[ib])
      x = n_possible_cs(a, b, ib + 1)
      ret += x
  return ret


n = int(input().strip())
ls = [int(l) for l in input().split()]

print(solve(sorted(ls)))

Solution (Python):
import bisect

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

L.sort()

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

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

counts = 0

for i in range(n):
  start, end = 0, i - 1
  while start < end:
    if nums[start] + nums[end] > nums[i]:
      counts += end - start
      end -= 1
    else:
      start += 1
print(counts)

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

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

        ind = bisect.bisect_left(L, ll)
        ans += ind - j - 1

print(ans)

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

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

print(count)

Solution (Python):

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

Solution (Python):

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

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

s = 0

ma = L[N-1]
for j in range(N-2):
    ja = L[j]
    for k in range(j+1,N-1):
        ka = L[k]
        
        key = ja + ka
        l = k + 1
        r = N - 1
        while r - l > 1:
            m = l + (r - l) // 2
            if L[m] < key:
                l = m
            else:
                r = m
        kka = L[k+1]
        if kka < key <= ma:
            s += l-k
        elif key <= kka:
            s += 0
        else:
            s += N-k-1

print(s)

Solution (Python):
import bisect

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

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a, b = LI()
    print(max(0, a - 2 * b))
    return


def B():
    n = II()
    d = LI()
    ans = 0
    for i in range(n):
        for k in range(i+1, n):
            ans += d[i] * d[k]
    print(ans)
    return


def C():
    n = II()
    s = S()
    i = 0
    ans = 1
    for i in range(1, n):
        if s[i - 1] == s[i]:
            continue
        ans += 1
    print(ans)
    return


def D():
    n = II()
    l = LI()
    l.sort()
    ans = 0
    for i in range(n - 1, 0, -1):
        for k in range(i - 1, -1, -1):
            ab = l[i] - l[k]
            c = max(0, k - bisect_right(l, ab))
            ans += c
    print(ans)
    return


def E():
    n, m, l = LI()
    dist = [[inf] * n for i in range(n)]

    for _ in range(m):
        a, b, c = LI_()
        c += 1
        if c > l:
            continue
        dist[a][b] = c
        dist[b][a] = c

    supply = [[n] * n for i in range(n)]
    for k in range(n):
        distk = dist[k]
        for i in range(n):
            if i == k:
                continue
            disti = dist[i]
            distik = dist[i][k]
            for j in range(i + 1, n):
                if distik + distk[j] < disti[j]:
                    disti[j] = distik + distk[j]
                    dist[j][i] = disti[j]

    lis = [[] for i in range(n)]
    for i in range(n):
        for j in range(i + 1, n):
            if dist[i][j] <= l:
                supply[i][j] = 0
                supply[j][i] = 0
                lis[i].append(j)
                lis[j].append(i)

    for i in range(n):
        q = []
        for k in lis[i]:
            heappush(q, (0, k))
        supplyi = supply[i]

        while q:
            time, p = heappop(q)
            for k in lis[p]:
                if supplyi[k] > time + 1:
                    supplyi[k] = time + 1
                    heappush(q, (time + 1, k))

    q = II()
    for _ in range(q):
        s, t = LI_()
        if dist[s][t] == inf:
            print(-1)
            continue
        else:
            print(supply[s][t])
    return



def F():
    return


def G():
    return


def H():
    return


if __name__ == '__main__':
    D()

Solution (Python):
from itertools import combinations
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i,j in list(combinations(L,2)):
  if j < 2*i:
    ans += max(0, bisect.bisect_left(L, i+j) - bisect.bisect_right(L, j-i)-2)
  else:
    ans += max(0, bisect.bisect_left(L, i+j) - bisect.bisect_right(L, j-i)-1)
print(ans//3)

Solution (Python):
import bisect as b

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

Solution (Python):
import bisect
N = int(input())
L0 = list(map(int, input().split()))
L = sorted(L0)
tri = 0
for left in range(N-2):
    for mid in range(left+1, N-1):
        x = bisect.bisect_left(L, L[left]+L[mid])
        tri += x - mid - 1

print(tri)

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

for a_index in range(N):
    for b_index in range(a_index+1, N):
        a_and_b = L[a_index] + L[b_index]
        c_index = bisect.bisect_left(L,L[a_index]+L[b_index])
        ans += max(0, c_index - b_index - 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(N-i-2):        
    ans+=min(N-i-j-2,N-bisect.bisect_left(L,L[-j-1]-L[i]+1)-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
def main():
    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):
            c = bisect.bisect_left(L,L[i]+L[j])
            
            count += max(0,c-1 - j)
    
    print(count)
main()

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

Solution (Python):
import bisect
n = int(input())
l_list = list(map(int,input().split()))
l_list.sort()
ans = 0
right = 0

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

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

Solution (Python):
import bisect

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

L.sort()

ans = 0

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

print(ans)

Solution (Python):
from bisect import bisect_left

n = int(input())
lst = sorted([int(i) for i in input().split()])

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

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

Solution (Python):
from bisect import bisect_left

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

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

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

print(ans)

Solution (Python):
from bisect import bisect_left

count = 0
n = int(input())
list = sorted([int(x) for x in input().split()])

for i in range(n):
    for j in range(i + 1, n) :
        sum = list[i] + list[j]
        a = bisect_left(list,sum)
        count += a - j - 1
print(count)

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

cnt = 0
for i in range(n):
  for j in range(i+1,n):
    num = L[i] + L[j]
    b = bisect.bisect_left(L, num)
    
    if b-1 > j:
      cnt += b-1-j
print(cnt)

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

Solution (Python):
import bisect

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

counter = 0

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

print(counter)

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

L = sorted(L)[::-1] 

def binary_search(descend_list, threshold):
    
    length = len(descend_list)
    
    
    if descend_list[-1] > threshold:
        return length
    
    
    if descend_list[0] <= threshold:
        return 0
    
    
    upper_bound = length
    lower_bound = 0
    index = (upper_bound - lower_bound) // 2
    
    while upper_bound > lower_bound + 1:
        if descend_list[index] > threshold:
            lower_bound = index
        else:
            upper_bound = index
        
        index = (upper_bound + lower_bound) // 2
    
    return lower_bound + 1

i = 0
ret = 0

while i < N - 2:
    edge1 = L[i]
    for j in range(i + 1, N-1):
        edge2 = L[j]
        _ret = binary_search(L[j+1:], edge1 - edge2)
        if _ret == 0:
            break
        else:
            ret += _ret
    
    i += 1

print(ret)

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

print(count)

Solution (Python):
import bisect


def read_data():
    N_ = int(input())
    L_ = list(map(int, input().split()))
    return N_, L_


def Search(N, L):
    ans = 0
    L.sort()
   
    for a_index in range(N):
       for b_index in range(a_index + 1, N):
           sum_of_a_and_b = L[a_index] + L[b_index]
           
           c_right_id = bisect.bisect_left(L, sum_of_a_and_b)
           
           ans += max(0, c_right_id - b_index - 1)
    print(ans)

N, L = read_data()
Search(N, L)

Solution (Python):
import bisect


def read_data():
   N_ = int(input())
   L_ = list(map(int, input().split()))
   return N_, L_


def Search(N, L):
   ans = 0
   L.sort()
   
   for a_index in range(N):
       for b_index in range(a_index + 1, N):
           sum_of_a_and_b = L[a_index] + L[b_index]
           
           c_right_index = bisect.bisect_left(L, sum_of_a_and_b)
           
           ans += max(0, c_right_index - b_index - 1)
   print(ans)

N, L = read_data()
Search(N, L)

Solution (Python):
import bisect
n=int(input())
*L,=map(int,input().split())
L.sort()
ans=0
for i in range(n-1):
      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


def read_data():
    N_ = int(input())
    L_ = list(map(int, input().split()))
    return N_, L_


def binarySearch(N :int, L):
    ans = 0
    L.sort()
    
    for a_index in range(N):
        for b_index in range(a_index + 1, N):
            sum_of_a_and_b = L[a_index] + L[b_index]
            
            c_right_index = bisect.bisect_left(L, sum_of_a_and_b)
            
            ans += max(0, c_right_index - b_index - 1)
    print(ans)

N, L = read_data()
binarySearch(N, L)

Solution (Python):
import bisect


def read_data():
    N_ = int(input())
    L_ = list(map(int, input().split()))
    return N_, L_


def binarySearch(N :int, L):
    ans = 0
    L.sort()
    
    for a_index in range(N):
        for b_index in range(a_index + 1, N):
            sum_of_a_and_b = L[a_index] + L[b_index]
            
            c_right_index = bisect.bisect_left(L, sum_of_a_and_b)
            
            ans += max(0, c_right_index - b_index - 1)
    print(ans)

N, L = read_data()
binarySearch(N, L)

Solution (Python):
import bisect


def read_data():
    N_ = int(input())
    L_ = list(map(int, input().split()))
    return N_, L_


def binarySearch(N :int, L):
    ans = 0
    L.sort()
    
    for a_index in range(N):
        for b_index in range(a_index + 1, N):
            sum_of_a_and_b = L[a_index] + L[b_index]
            
            c_right_index = bisect.bisect_left(L, sum_of_a_and_b)
            
            ans += max(0, c_right_index - b_index - 1)
    print(ans)

N, L = read_data()
binarySearch(N, L)

Solution (Python):
import bisect

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

L.sort()

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

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

L.sort()
X=0
for i in range(N):
  for k in range(N-(i+1)):
    j=i+1+k
    s=L[i]+L[j]
    d=max(L[i]-L[j],L[j]-L[i])
    lb=0
    ub=N-1
    while lb<ub:
      mn=(lb+ub)//2
      if L[mn]<=d:
        lb=mn+1
      else:
        ub=mn
    dmin=lb
    
    lb=0
    ub=N-1
    while lb<ub:
      mn=(lb+ub)//2
      if mn*2 != lb+ub:
        mn=mn+1
      if L[mn]>=s:
        ub=mn-1
      else:
        lb=mn
    smax=lb
    a=smax-dmin+1
    if dmin<=i and i<=smax:
      a-=1
    if dmin<=j and j<=smax:
      a-=1
    X+=a
print(int(X/3))

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

Solution (Python):


import bisect

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

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

Solution (Python):
import bisect

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

print(count)

Solution (Python):
n=int(input())
l=[int(i) for i in input().split()]
l.sort()
ans=0
ans1=0
for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+ans1,n):
      ans1=0
      if l[i]+l[j]>l[-1]:
        ans+=n-j-1
        break
      elif l[i]+l[j]<=l[k] :
        ans+=k-j-1
        ans1+=k-j-1
        break
      
      
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-1):
    for j in range(i+1,n):
        limit = L[i]+L[j]
        ans += bisect.bisect_left(L,limit) - (j+1)

print(ans)

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

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

Solution (Python):
import sys
import bisect

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline

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

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

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

        ans += max(0, index-j-1)

print(ans)

Solution (Python):

import math
import heapq
import bisect

MOD = 10**9+7

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

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

Solution (Python):

import bisect

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

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

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

Solution (Python):
import bisect

n = int(input())
l = list(map(int,input().split()))
l.sort()
count = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        short_2hen = l[i] + l[j]
        bis = bisect.bisect_left(l, short_2hen)
        count += bis - 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-1):
    for j in range(i+1,n):
        index = bisect.bisect_left(L,L[i]+L[j])
        ans += index - (j+1)

print(ans)

Solution (Python):
import bisect

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

L.sort()

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

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

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


if __name__ == "__main__":
    main()

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

def main():
    N = int(input())
    l = sorted(list(map(int, input().split())))
    memo = [-1] * (l[-2] + l[-1] + 1)
    ans = 0
    for i in range(N):
        for j in range(i+1, N):
            a = l[i]
            b = l[j]
            if memo[a + b] == -1:
                right = bisect.bisect_left(l, a + b)
                memo[a + b] = right
            else:
                right = memo[a + b]
            ans += max(0, right - j - 1)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

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

L.sort()
s=0
for i in range(n-1):
      for j in range(i+1,n):
            index=bisect.bisect_left(L,L[i]+L[j])
            s+=index-(j+1)
print(s)

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

def BinaryIndexSearch_floor(p, SL):
	"""
	search the minimum number among the larger numbers than the ginven nymber "n". 
	return the index of it under the list sorted.
	"""
	
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] > p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[l] > p:
				return l
			elif SL[r] > p:
				return r
			else:
				return -1

		center = l + ((r-l) // 2)

		if SL[center] <= p:
			l = center + 1
			continue

		else:
			r = center
			continue

def BinaryIndexSearch_ceil(p, SL):
	
	l = 0
	r = len(SL) - 1

	while True:
		if l == r:
			if SL[l] < p:
				return l
			else:
				return -1
		elif r-l == 1:
			if SL[r] < p:
				return r
			elif SL[l] < p:
				return l
			else:
				return -1
		center = l + ((r-l) // 2)

		if SL[center] < p:
			l = center 
			continue

		else:
			r = center - 1
			continue
ans = 0
for i in range(N-1):
	for j in range(i+1,N):
		wa = L[i] + L[j]
		sa = abs(L[i]-L[j])

		l = BinaryIndexSearch_ceil(wa,L)
		s = BinaryIndexSearch_floor(sa,L)

		oc = 0
		if s <= i <= l:
			oc += 1
		if s <= j <= l:
			oc += 1
		if l >= s:
			ans += max(0, l-s+1-oc)

print(ans//3)

Solution (Python):

import sys
import collections













def main():
    n, *l = [int(x) for x in sys.stdin.read().split()]
    l.sort()

    c = collections.defaultdict(int)
    for i in range(n):
        c[l[i]] += 1
    
    
    s = [0 for _ in range(l[-3] + l[-2] + 1)] 
    for i in range(l[-3] + l[-2]):
        s[i+1] = s[i] + c[i+1]

    count = 0
    
    for i in range(n-2):
        a = l[i]
        for j in range(i+1, n-1):
            b = l[j]
            
            count += s[a+b-1] - (j + 1) 

    print(count)




    

if __name__ == "__main__":
    main()

Solution (Python):

import sys



from bisect import bisect_left as bi_l, bisect_right as bi_r










def main():
    n, *l = [int(x) for x in sys.stdin.read().split()]
    l.sort()
    count = 0
    for i in range(n-2):
        a = l[i]
        for j in range(i+1, n-1):
            b = l[j]
            count += (bi_l(l, a + b) - 1) - j
    
    print(count)

if __name__ == "__main__":
    main()

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

Solution (Python):
from bisect import bisect_left

N = int(input())
L = list(map(lambda l: int(l)*(-1), input().split()))
L.sort()

result = 0
for i, a in enumerate(L[:-2:]):
    for ii, b in enumerate(L[i+1:-1:]):
        c_list = L[i+ii+2::]
        c_index = bisect_left(c_list, a-b)
        result += c_index

print(result)

Solution (Python):
from bisect import bisect_left

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

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

L.sort()

import bisect
ans = 0

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

Solution (Python):
import itertools
import bisect

N = int(input())
Llist  =[int(i) for i in input().split()]
Llist.sort()




ans=0
for i in range(len(Llist)):
    for j in range(i+1,len(Llist)):
        a = Llist[i]
        b = Llist[j]
        index = bisect.bisect_right(Llist,b-a)
        
        if i>index:
            ans+=i-index

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

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