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_list = list(map(int, input().split()))

L_list = sorted(L_list)

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

print(total)

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

Solution (Python):
from bisect import bisect_left

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

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

print(res)

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())
L = list(map(int, input().split()))
L.sort()

ans = 0

for i in range(N-1):
    for j in range(i+1, N):
        a = L[i]
        b = L[j]
        l = bisect.bisect_right(L, abs(a-b))
        r = bisect.bisect_left(L, a+b)
        ans += r-l
        if L[l] <= a <= L[r-1]:
            ans -= 1
        if L[l] <= b <= L[r-1]:
            ans -= 1
        
print(ans//3)

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):
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
total = []
for a in range(N-2):
    for b in range(a+1, N-1):
        la = L[a]
        lb = L[b]
        ab = la + lb
        sa = abs(la-lb)
        first = bisect.bisect_left(L, ab) -1 
        second = bisect.bisect_right(L, sa)
        if first > second and first >= b+1:
            count += first - max(second, b+1) +1
print(count)

Solution (Python):
import sys
from bisect import bisect_left

input = sys.stdin.readline


def main():
    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):
            k = bisect_left(L, a + L[j])
            ans += (k - 1 - j)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from bisect import bisect_left

input = sys.stdin.readline


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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from bisect import bisect_left

input = sys.stdin.readline


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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from bisect import bisect_left

input = sys.stdin.readline


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

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import bisect
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) 

def LI(): return list(map(int, sys.stdin.readline().split()))

"""
nC3
|b - c| < a < |b + c|

nC2
b_i, c_iを除くiの中で|b-c|< a <b+cを満たすaの数をタス
"""

N = int(input())
L = LI()
L.sort()

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

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

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        p=bisect.bisect_right(l,l[i]+l[j]-1)
        cnt+=p-j-1
print(cnt)

Solution (Python):

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

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        p=bisect.bisect_right(l,l[i]+l[j]-1)
        cnt+=max(p-j-1,0)
print(cnt)

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

cnt=0
for i in range(0,n-2):
    for j in range(i+1,n-1):
        p=bisect.bisect_right(l,l[i]+l[j]-1)
        cnt+=max(p-j-1,0)
print(cnt)

Solution (Python):
import bisect

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

    ans = 0
    for i in range(n-2):
        e1 = ll[i]
        for j in range(i+1, n-1):
            e2 = ll[j]
            
            comb = bisect.bisect_left(ll, e1+e2)
            comb -= (j+1)
            ans += max(comb,0)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

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

ans = 0
for i in range(n-2):
    e1 = ll[i]
    for j in range(i+1, n-1):
        e2 = ll[j]
        ans += bisect.bisect_left(ll[j+1:], e1+e2)

print(ans)

Solution (Python):
import sys
import itertools

import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
L = list(sorted(map(int, readline().split())))

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

Solution (Python):
import bisect
 

def main():
    n = int(input())
    ls = sorted(list(map(int, input().split())))
 
    ans = 0
    for i in range(n-1):
        for j in range(i+1, n):
            a = ls[i]
            b = ls[j]
            k = bisect.bisect_left(ls, a+b)
            if k > j:
                ans += k - j - 1
    print(ans)
 
 
if __name__ == "__main__":
    main()

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

print(ans)

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

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


import bisect

ans=0
l.sort()

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

print(ans)

Solution (Python):
from sys import stdin
import numpy as np
def main():
    
    readline=stdin.readline
    n=int(readline())
    l=list(map(int,readline().split()))
    l.sort()

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

    l=np.array(l,dtype=np.int64)
    s=np.array(s,dtype=np.int64)
    indexs=np.array(indexs,dtype=np.int64)
    ans=(np.searchsorted(l,s)-indexs).sum()
    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
import bisect


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

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


if __name__ == "__main__":
    main()

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

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

counter=0
for i in range(2,n):
	now=i+1
	tmpcounter=0
	for j in range(1,i):
		while now <= n:
			if llist[j-1] + llist[i-1] > llist[now-1]:
				tmpcounter += 1
				now += 1
			else:
				break
		counter += tmpcounter

print(counter)

Solution (Python):

"""
D - Triangles
https://atcoder.jp/contests/abc143/tasks/abc143_d

"""
import sys

from bisect import bisect_right

def solve(N, lines):
    return sum(b - bisect_right(lines[:b], lines[c] - lines[b]) for c in range(N - 1, -1, -1) for b in range(c - 1, -1, -1))


def main(args):
    N = int(input())
    lines = map(int, input().split())
    ans = solve(N, sorted(lines))
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):

"""
D - Triangles
https://atcoder.jp/contests/abc143/tasks/abc143_d

"""
import sys

from bisect import bisect_right

def solve(N, lines):
    ans = 0
    for c in range(N-1, -1, -1):
        for b in range(c-1, -1, -1):
            i = bisect_right(lines[:b], lines[c] - lines[b])
            ans += b-i
    return ans


def main(args):
    N = int(input())
    lines = map(int, input().split())
    ans = solve(N, sorted(lines))
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):

"""
D - Triangles
https://atcoder.jp/contests/abc143/tasks/abc143_d

"""
import sys

from itertools import combinations
from bisect import bisect_left, bisect_right

def solve(N, lines):
    ans = 0
    for c in range(N-1, -1, -1):
        for b in range(c-1, -1, -1):
            i = bisect_right(lines[:b], lines[c] - lines[b])
            ans += max(0, (b-i))
    return ans


def main(args):
    N = int(input())
    lines = map(int, input().split())
    ans = solve(N, sorted(lines))
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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

Solution (Python):

from bisect import bisect_left


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

Solution (Python):
from bisect import bisect_left


N = int(input())
L = sorted(map(int, input().split()))
ans = 0
for i in range(2, N):
    c = L[i]
    for j in range(i):
        b = L[j]
        a_min_idx = bisect_left(L, c - b + 1, 0, j)
        ans += j - a_min_idx
print(ans)

Solution (Python):
import sys
from collections import defaultdict
from queue import deque
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():

    n = gete(int)
    l = list(geta(int))
    l.sort()

    l_max = 2000
    
    p = [0] * (l_max + 1)

    cur = 0
    acc = n
    for i in range(l_max + 1):
        p[i] = acc

        if cur < n and i == l[cur]:
            while cur < n and l[cur] == i:
                acc -= 1
                cur += 1

    ans = 0
    for i, a in enumerate(l):
        for b in l[i + 1:]:
            ans += p[abs(a - b) + 1] - p[a + b]

            if abs(a - b) < a:
                ans -= 1

            if abs(a - b) < b:
                ans -= 1
    print(ans // 3)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect

def main():
    n = int(input())
    l = sorted(list(map(int, input().split())))
    count = 0
    for i in range(n-2):
        a = l[i]
        for j in range(i+1, n-1):
            b = l[j]
            kmax = bisect.bisect_left(l, a+b)
            
            count += max(kmax-(j+1),0)
    print(count)
    
if __name__ == "__main__":
    main()

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-1):
    k = L[i]+L[j]
    l,r = 0,N
    while (r-l) > 1:
      m = (l+r)//2
      if L[m] >= k:
        r = m
      else:
        l = m

    ans += r-j-1

print(ans)

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

L.sort()
ans = 0
"""


for a in range(len(L)):
    for b in range(a+1,len(L)):
        c_max = L[a]+L[b]-1
        left = b+1
        right = len(L)
        mid = (right+left)//2
        while left<right:
            mid = (right+left)//2
            if c_max < L[mid]:
                right = mid
            else:
                left = mid+1
        ans += left-(b+1)
"""
for i in range(0,n-2):
    j = i+1
    k = j+1
    while j<n-1:
        while(k<n):
            if (L[j]-L[i]<L[k] and L[k]<L[i]+L[j]):
                k+=1
            else:
                break
        ans += k-(j+1)
        j += 1

        
print(ans)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.sort()
cnt = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        if l[i] + l[j] > l[n - 1]:
            cnt += n - j - 1
            continue
        le = j + 1
        ri = n - 1
        while ri - le > 0:
            m = (le + ri) // 2
            if l[m] < l[i] + l[j]:
                le = m + 1
            else:
                ri = m
        cnt += le - j - 1
print(cnt)

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

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

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

print(count)

Solution (Python):
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):
    if L[a] + L[b] > L[n-1]:
      count += n - b - 1
      continue
    left = b + 1
    right = n - 1
    while right - left > 0:
      c = (left+right) // 2
      if L[c] < L[a] + L[b]:
        left = c + 1
      else:
        right = c
    count += left - b - 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-1):
        idx = bisect.bisect_left(L, L[i]+L[j])
        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):
        idx = bisect_left(l, l[i] + l[j])
        ans += idx - i - 1
print(ans)

Solution (Python):

def can_make(i, j, k):
    if L[i] < L[j] + L[k] and L[j] < L[k] + L[i] and L[k] < L[j] + L[i]:
        return True
    return False


def find(i, j, L):
    left = j
    right = N
    mid = (right + left)//2
    flg = False
    while right - left > 1:
        mid = (right + left)//2
        
        if can_make(i, j, mid):
            
            left = mid
            flg = True
        else:
            right = mid
    
    
    if flg:
        return left-j
    else:
        return 0


N = int(input())
L = list(map(int, input().split()))
cnt = 0
L = sorted(L, reverse=True)
for i in range(N):
    for j in range(i+1, N):
        
        
        cnt += find(i, j, L)
print(cnt)

Solution (Python):
from bisect import bisect_left

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

l.sort()

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

print(ans)

Solution (Python):
def resolve():
    import sys
    input = sys.stdin.readline
    
    n = int(input())
    
    
    
    
    
    L = list(map(int, input().split()))
    
    

    L.sort()
    import bisect

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

    print(cnt)


resolve()

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

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

import bisect

L.sort()


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

print(ans)

Solution (Python):
import bisect

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

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

Solution (Python):
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for b in range(N):
    for a in range(b):
        ab = L[a]+L[b]
        r = bisect.bisect_left(L, ab)
        l = b+1
        ans += max(0, r-l)
print(ans)

Solution (Python):
import bisect
N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for b in range(N):
    for a in range(b):
        ab = L[a]+L[b]
        r = bisect.bisect_left(L, ab)
        l = b+1
        ans += max(0, r-l)
print(ans)

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

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

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

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


print(cnt)

Solution (Python):
import bisect

n = int(input())

in_ar = [0 for _ in range(n)]
in_ar = [int(s) for s in input().split()]
in_ar.sort()
res = 0

for i in range(n):
    for j in range(i,n):
        if(i == j):
            continue

        a = in_ar[i]
        b = in_ar[j]
        st = bisect.bisect_left(in_ar, abs(a-b))
        en = bisect.bisect_left(in_ar, abs(a+b))
        
        res += en - j - 1
        '''
        if(abs(a-b) < a):
            res -= 1
        if(abs(a-b) < b):
            res -= 1
        '''

print(res)

Solution (Python):
import sys
import itertools
import bisect

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


def solve():
    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):
            a, c = L[i], L[j]
            amc = abs(a - c)
            apc = a + c
            low = bisect.bisect_right(L, amc)
            low = max(j + 1, low)
            upp = bisect.bisect_left(L, apc)
            upp -= 1
            if low <= upp:
                ans += (upp - low) + 1

    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
n=int(input())
p=list(map(int,input().split()))
p.sort()
ans=0
for i in range(n-2):
    for j in range(i+1,n-1):
        if j<n-2:
            l=j+1
            r=n-1
            while r-l>1:
                c=(l+r)//2
                if p[c]>=p[i]+p[j]:
                    r=c
                else:
                    l=c
            if p[l]<p[i]+p[j]:
                ans+=l-j
            if p[r]<p[i]+p[j]:
                ans+=1
        if j==n-2:
            if p[i]+p[n-2]>p[n-1]:
                ans+=1
print(ans)

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

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

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

print(ans)

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

counts = 0
for i in range(n-2):
    a = L[i]
    ci = i+2
    for bi in range(i+1, n-1):
        while ci < n and L[ci] <  a + L[bi]:
            ci += 1
        counts += ci - 1 - bi
print(counts)

Solution (Python):
n = int(input())
L = tuple(map(int, input().split()))
L = sorted(L, reverse=True)

counts = 0
for i in range(n-2):
    a = L[i]
    for j in range(i+1, n-1):
        cc = a - L[j]
        left = j
        right = n
        while right - left > 1:
            mid = (right+left)//2
            if L[mid] > cc:
                left = mid
            else:
                right = mid
        counts += left - j
print(counts)

Solution (Python):
def main():
  n=int(input())
  l=list(map(int,input().split()))
  l.sort()
  import bisect
  ans=0
  for i in range(n-1,1,-1):
    for j in range(i-1,0,-1):
      a=l[i]
      b=l[j]
      idxl=bisect.bisect_right(l,a-b)
      ans+=max(j-idxl,0)
  print(ans)
if __name__=='__main__':
  main()

Solution (Python):
"""
素直に実装するとN**9
"""

import bisect
 
N = int(input())
L = list(map(int, input().split()))
 
L.sort()
ans = 0
for ai in range(N):
    for bi in range(ai+1, N):
        ci = bisect.bisect_left(L, L[ai]+L[bi])
        if ci > bi:
            ans += ci - bi - 1
        else:
            pass
 
print(ans)

Solution (Python):
n = int(raw_input())
nums = map(int, raw_input().split(' '))
nums.sort()
r = 0


for c in range(2, n):
	a,b = 0, c - 1

	for a in range(c - 1):
		lo,hi = a+1, c-1
		while(lo < hi):
			med = (lo + hi) / 2
			if nums[a] + nums[med] > nums[c]:
				hi = med
			else:
				lo = med + 1
		
		r += (c - lo) if (nums[a] + nums[lo] > nums[c]) else (0)
print r

Solution (Python):



import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect




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

L.sort(reverse=True)
acm_l = []

for i in range(1, N-1):
    acm_l.append([ -(L[i] + L[j]) for j in range(i+1, N)])





ans = 0
for i, l in enumerate(L[:-2]):
    for j in range(i, N-2):
        idx = bisect.bisect_left(acm_l[j], -l)
        
        ans += idx

print(int(ans))

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


for j in range(2,n):
    for i in range(j+1,n+1):
        if l_s[i] < l_s[j] + l_s[j-1]:
            
            OK = j-1
            NG = 0
            while (OK - NG) > 1:
                mid = (OK+NG)//2
                if l_s[i] < l_s[j] + l_s[mid]:
                    OK = mid
                else:
                    NG = mid
            ans = ans + j - OK 
print(ans)

Solution (Python):
from bisect import bisect_left

N = int(input())
L = 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(L, L[i]+L[j]) - (j + 1)

print(ans)

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


for i in range(3,n+1):
    for j in range(2,i):
        if l_s[i] < l_s[j] + l_s[j-1]:
            
            OK = j-1
            NG = 0
            while (OK - NG) > 1:
                mid = (OK+NG)//2
                if l_s[i] < l_s[j] + l_s[mid]:
                    OK = mid
                else:
                    NG = mid
            ans = ans + j - OK 
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):
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()
count = 0
for i in range(n - 1, 1, -1):
    l = 0
    r = i - 1
    while l < r:
        if L[i] < L[r] + L[l]:
            count += r - l
            r -= 1
        else:
            l += 1
print(count)

Solution (Python):
def main():
    from bisect import bisect_right as bs

    n, *l = map(int, open(0).read().split())
    l.sort()
    ans = 0
    for i, x in enumerate(l):
        for y in l[i + 1:]:
            k = y - x
            z = bs(l, k)
            if i - z >= 1:
                ans += (i - z)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import bisect

def solve():
  n = int(input())
  L = [int(i) for i in input().split()]
  L.sort()
  total = 0
  for s in range(n-2):
    for m in range(s+1, n-1):
      
      l = bisect.bisect_left(L,L[s]+L[m], 0, n)
      if l>m: total += l-m-1
  print(total)

  return 0

if __name__ == "__main__":
  solve()

Solution (Python):
from bisect import bisect_left, bisect_right

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

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

Solution (Python):
import bisect

n = int(input())
L = sorted(map(int, input().split()))
ans = 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)
        if k > j:
            ans += k - j - 1
print(ans)

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

for a in range(n-2):
    for b in range(a+1,n-1):
        p = bisect.bisect_left(L,L[a] + L[b])
        
        ans += p - (b+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,1,-1):
    for j in range(i-1,0,-1):
        x = j - bisect.bisect(l,l[i]-l[j])
        if x>0:
            ans += x

print (ans)

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

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

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

for l in L_:
    d[l] += 1

n = 0
for i in range(2001):
    n += d[i]
    d[i] += n - d[i]

ans = 0

for a, b in combinations(L_, 2):
    
    n =  d[a+b-1] - d[abs(a-b)]
    if abs(a-b) < a:
        ans -= 1
    if abs(a-b) < b:
        ans -= 1
    ans += n
    
print(ans//3)

Solution (Python):
from bisect import bisect_left, bisect_right

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

L.sort()
len_L = len(L)

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

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    l = list(map(int, input().split()))
    l.sort()

    l_c = [0] * 2001

    for li in l:
        l_c[li] += 1
    for i in range(2000):
        l_c[i + 1] += l_c[i]

    ans = 0

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

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
inf = 1001001001
n = int(input())
lis = list(map(int, input().split()))
lis.sort()
lis.append(inf)
ans = 0
for i in range(n-2):
    a = lis[i]
    for j in range(i + 1, n-1):
        b = lis[j]
        l = j
        r = n
        while r > l+1:
            mid = (l+r) // 2
            if lis[mid] < a + b:
                l = mid
            else:
                r = mid
        ans += l - j
print(ans)

Solution (Python):

from bisect import bisect_left

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

l_list.sort()

ans = 0

for i in range(n-2):
  a = l_list[i]
  
  for j in range(i+1,n-1):
    b = l_list[j]
    
    x = bisect_left(l_list,a+b)
    
    ans += x-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 small in range(n - 2):
    for mid in range(small + 1, n - 1):
        div = bisect_left(l, l[small] + l[mid], 0, n)
        if div > mid + 1:
            ans += div - mid - 1

print(ans)

Solution (Python):
from bisect import bisect_left
import copy

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


ans = 0
for small in range(n - 2):
    for mid in range(small + 1, n - 1):
        div = bisect_left(l, l[small] + l[mid], 0, n)
        if div > mid + 1:
            ans += div - mid - 1

print(ans)

Solution (Python):
from bisect import bisect_left
import copy

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


ans = 0
for small in range(n - 2):
    for mid in range(small + 1, n - 1):
        div = bisect_left(l, l[small] + l[mid], 0, n)
        if div > mid + 1:
            ans += div - mid - 1

print(ans)

Solution (Python):
import bisect

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





for i in range(n-1,1,-1):
    for j in range(i-1, 0, -1):
        kagen=l[i]-l[j]
        if l[j]>kagen:
            idx_jogen=j
            idx_kagen=bisect.bisect_right(l,kagen)
            cnt+=idx_jogen-idx_kagen
            

print(cnt)

Solution (Python):
"""
keyword: 二分探索

初めにLをソートして、a<b<cとしてa,bを決めたとき、a+b>cとなるようなcの個数を計算する
cの最大値を求めるには二分探索が高速
さらに、二分探索の探索範囲の左端は、前のループで求めたcの最大値で十分である（∵前のループのa+b <= 今ループのa+b）ことを考慮すれば、さらに高速化できる

pythonでTLE, pypyでAC
"""

import sys
sys.setrecursionlimit(10**6)

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

L.sort()
ans = 0

for i in range(n-2):
    l_prev = 0 
    for j in range(i+1,n-1):
        ab = L[i]+L[j]
        if not l_prev:
            l = j; r = n
        else:
            l = l_prev; r = n
        while l+1 < r:
            c = (l+r)//2
            if L[c] < ab:
                l = c
            else:
                r = c
        ans += l-j
        l_prev = l
        
print(ans)