qyliang/CodeNanoFix huggingface dataset:
Buggy code:
def binary_search(list, item):
    low = 0
    high = len(list)
    
    while low < high:
        mid = (low + high)//2
        if list[mid] < item:
            low = mid+1
        else:
            high = mid
    return low
  
N = int(input())
L = list( map(int, input().split()) )

L.sort()
print(L)

count = 0
for a in range(0, len(L)-2):
    for b in range(a+1, len(L)-1):
        count += binary_search(L[b+1:], L[a]+L[b])
print(count)

Fixed code:
def binary_search(list, item):
    low = 0
    high = len(list)
    
    while low < high:
        mid = (low + high)//2
        if list[mid] < item:
            low = mid+1
        else:
            high = mid
    return low
  
N = int(input())
L = list( map(int, input().split()) )

L.sort()

count = 0
for a in range(0, len(L)-2):
    for b in range(a+1, len(L)-1):
        count += binary_search(L[b+1:], L[a]+L[b])
print(count)
Buggy code:
N = int(input())
l = sorted(list(map(int, input().split())))
 
from bisect import bisect_left

cnt = 0
for i in range(N):
    a = l[i]
    for j in range(i+1, N):
        b = l[j]
        start = bisect_left(l, b)
        end = bisect_left(l, a+b)
        cnt += len(l[start+1:end])
print(cnt)

Fixed code:
N = int(input())
l = sorted(list(map(int, input().split())))
 
from bisect import bisect_left

cnt = 0
for i in range(N):
    a = l[i]
    for j in range(i+1, N):
        b = l[j]
        cnt += bisect_left(l[j+1:], a+b)
print(cnt)
Buggy code:
import bisect
N = int(input())
l = list(map(int,input().split()))
l.sort()
#print(l)


ans = 0
for i in range(N - 1 , 2 , -1):
    for j in range(N - 2 , 1 , -1):
        work = l[:j]
        x = l[i] - l[j]
        idx = bisect.bisect_right(work, x)
        ans += len(work) - idx
print(ans)


Fixed code:
import bisect
N = int(input())
l = list(map(int,input().split()))
l.sort()
#print(l)


ans = 0
for i in range(N - 1 , 1, -1):
    for j in range(i - 1 , 0 , -1):
        #work = l[:j]
        x = l[i] - l[j]
        #idx = bisect.bisect_right(work, x)
        idx = bisect.bisect_right(l, x)
        ans += max(j - idx , 0)
print(ans)
Buggy code:
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)):
  print(i,j)
  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)

Fixed code:
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)
Buggy code:
# a - b < c
# b - a < c
# a + b > c

import bisect

n = int(input())

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

ret = 0

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

print(ret)


Fixed code:
# a - b < c
# b - a < c
# a + b > c

import bisect

n = int(input())

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

ret = 0

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

print(ret)
Buggy code:
N=int(input())
L=sorted(list(map(int,input().split())))
print(L)
ans=0
for c in range(2,N):
    cc=L[c]
    for b in range(1,c):
        bb=L[b]
        if cc>=bb+L[b-1]: continue
        l=0
        u=b-1
        while u-l>1:
            a=(l+u)//2
            aa=L[a]
            if aa+bb>cc: u=a
            else: l=a
        ans+=(b-u)
        print(b,c,b-u)
print(ans)

Fixed code:
N=int(input())
L=sorted(list(map(int,input().split())))
ans=0
for c in range(2,N):
    cc=L[c]
    for b in range(1,c):
        bb=L[b]
        if cc>=bb+L[b-1]: continue
        l=-1
        u=b-1
        while u-l>1:
            a=(l+u+1)//2
            aa=L[a]
            if aa+bb>cc: u=a
            else: l=a
        ans+=(b-u)
print(ans)
Buggy code:
import bisect
N=int(input())
ls=[int(s) for s in input().split()]
ls.sort()
ans=0
for i in range(1,N-1):
  for j in range(i+1,N):
    k = bisect.bisect_right(ls[:i],ls[j]-ls[i])
    print(i,j,k,i-k)
    ans+=i-k
print(ans)

Fixed code:
import bisect
N=int(input())
ls=[int(s) for s in input().split()]
ls.sort()
ans=0
for i in range(1,N-1):
  for j in range(i+1,N):
    k = bisect.bisect_right(ls[:i],ls[j]-ls[i])
    ans+=i-k
print(ans)
Buggy code:
def bis_right(lis,dev):
    a=0
    b=len(lis)
    while a<b:
        c=(a+b)//2
        if lis[c]>dev:
            b=c
        else:
            a=c+1
    if lis[a]<=dev:
        return a
    return a-1



n=int(input())
l=[int(x) for x in input().split()]
l.sort()
s=0
for a in range(n-2):
    for b in range(a+1,n-1):
        c=bis_right(l[b+1:],l[a]+l[b]-1)
        s+=c+1
print(s)

Fixed code:
def bis_right(lis,dev):
    a=0
    b=len(lis)-1
    while a<b:
        c=(a+b)//2
        if lis[c]>dev:
            b=c
        else:
            a=c+1
    if lis[a]<=dev:
        return a
    return a-1



n=int(input())
l=[int(x) for x in input().split()]
l.sort()
s=0
for a in range(n-2):
    for b in range(a+1,n-1):
        c=bis_right(l[b+1:],l[a]+l[b]-1)
        s+=c+1
print(s)
Buggy code:
import bisect

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

L = sorted(L)
print(L)
for c in reversed(range(2, N)):
	for b in reversed(range(1, c)):
		a = bisect.bisect_left(L, L[c]-L[b])
		if a<b:
			s += b-a
print(s)

Fixed code:
import bisect

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

L = sorted(L)

for c in reversed(range(2, N)):
	for b in reversed(range(1, c)):
		a = bisect.bisect_right(L, L[c]-L[b])
		if a<b:
			s += b-a
print(s)
Buggy code:
from bisect import bisect_left

N = int(input())
L = list(sorted(map(int, input().split()), key=lambda x: -x))

res = 0

for i in range(N):
    for j in range(i + 1, N):
        k = bisect_left(L, L[i] + L[j])

        res += k - j - 1

print(res)


Fixed code:
from bisect import bisect_left

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

res = 0

for i in range(N):
    for j in range(i + 1, N):
        k = bisect_left(L, L[i] + L[j])

        res += k - j - 1

print(res)
Buggy code:
import bisect

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

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

print(count)

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

print(count)
Buggy code:
from bisect import*
_,l=open(0)
*e,=enumerate(sorted(map(int,l.split())))
print(sum(j-bisect(l,a-b,0,j)for i,a in e for j,b in e[:i]))

Fixed code:
from bisect import*
_,l=open(0)
l=sorted(map(int,l.split()))
e=enumerate
print(sum(j-bisect(l,a-b,0,j)for i,a in e(l)for j,b in e(l[:i])))
Buggy code:
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])-1
print(c)

Fixed code:
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)
Buggy code:
import sys
ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

a = ri()
li = sorted(rl(), reverse=True)
cnt = 0
for i in range(a-2):
    for j in range(i+1, a-1):
        left = j+1
        right = a
        lii = li[i]
        lij = li[j]
        t = 0
        while right-left > 1:
            t = (left + right)//2
            lit = li[t]
            if lii < lij + lit:
                left = t
            else:
                right = t
        if lii < lij + li[t]:
            cnt += left-j
print(cnt)

Fixed code:
import sys

ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

a = ri()
li = sorted(rl(), reverse=True)
cnt = 0
for i in range(a-2):
    for j in range(i+1, a-1):
        left = j+1
        right = a
        lii = li[i]
        lij = li[j]
        if lii >= lij + li[j+1]:
            continue
        while right-left > 1:
            t = (left + right)//2
            lit = li[t]
            if lii < lij + lit:
                left = t
            else:
                right = t
        cnt += left-j
print(cnt)
Buggy code:
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
for i in range(0,N-1,1):
    for j in range(i+1,N,1):
        left = max(L[i]-L[j],L[j]-L[i])
        right = L[i]+L[j]
        print(L[i],L[j],left,right)
        count += -bisect.bisect_right(L,left)+bisect.bisect_left(L,right)
        print(-bisect.bisect_right(L,left)+bisect.bisect_left(L,right))
        if L[i] > left and L[i] < right:
            count-=1
        if L[j] > left and L[j] < right:
            count-=1
print(count/3)


Fixed code:
import bisect

N = int(input())
L = list(map(int,input().split()))
L.sort()
count = 0
for i in range(0,N-1,1):
    for j in range(i+1,N,1):
        left = max(L[i]-L[j],L[j]-L[i])
        right = L[i]+L[j]
        count += -bisect.bisect_right(L,left)+bisect.bisect_left(L,right)
        if L[i] > left and L[i] < right:
            count-=1
        if L[j] > left and L[j] < right:
            count-=1
print(count//3)
Buggy code:
import sys

def bi_search(array, target):
    l, r = 0, len(array)
    while r-l > 1:
        mid = int((r+l)/2)
        if array[mid] > target:
            r = mid
        else:
            l = mid+1
    return l

N = int(sys.stdin.readline().strip())
l_list = list(sorted(list(map(int, sys.stdin.readline().strip().split(" ")))))
count = 0

for i in reversed(range(N)):
    a = l_list[i]
    for j in reversed(range(i)):
        b = l_list[j]
        index = bi_search(l_list, b-a)
        if index < j and l_list[index] + b > a:
            count += (j-index)

print(count)

Fixed code:
import sys

def bi_search(array, target):
    l, r = -1, len(array)-1
    while r-l > 1:
        mid = int((r+l)/2)
        if array[mid] > target:
            r = mid
        else:
            l = mid
    return r

N = int(sys.stdin.readline().strip())
l_list = list(sorted(list(map(int, sys.stdin.readline().strip().split(" ")))))
count = 0
#print(l_list)

for i in reversed(range(N)):
    a = l_list[i]
    for j in reversed(range(i)):
        b = l_list[j]
        index = bi_search(l_list[:j], a-b)
        """
        print("====")
        print(b-a)
        print(i, j, index)
        """
        if index > -1 and index < j and l_list[index] + b > a:
            count += (j-index)

print(count)
Buggy code:
import bisect
N = int(input())
L_list =  sorted(list(map(int,input().split())))
#print(L_list)
result =0

for i in range(N):
    for j in range(i+1,N):
        a = L_list[i] + L_list[j]
        idx = bisect.bisect_left(L_list,a)
        result = max(0,idx-j-1)
print(result)

Fixed code:
import bisect
N = int(input())
L_list =  sorted(list(map(int,input().split())))
#print(L_list)
result =0

for i in range(N):
    for j in range(i+1,N):
        a = L_list[i] + L_list[j]
        idx = bisect.bisect_left(L_list,a)
        result += max(0,idx-j-1)
print(result)
Buggy code:
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.bisect_left(l[j+1:], l[i]+l[j])

print(ans)


Fixed code:
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)
Buggy code:
#coding: utf-8
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,a+b)
        ans += c-b-1
            
print(ans)

Fixed code:
#coding: utf-8
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)
Buggy code:
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):
        ab=L[i]+L[j]
        r=bisect.bisect_left(L,ab)
        l=j+1
        ans+=r-l

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

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

ans = 0

l.sort()
print(l)

for i in range(n):
    for j in range(i+1, n):
        gap = l[i] + l[j]
        index = bisect.bisect_right(l, gap)

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

print(ans)

Fixed code:
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)
Buggy code:
import bisect
n = int(input())
 
l = list(map(int, input().split()))
l.sort()
a = 0
 
for i in range(len(l)-2):
    for j in range(i+1,len(l)-1):
        s = bisect.bisect_left(l[i]+l[j])
        a += n-s-1 


print(a)

Fixed code:
import bisect
n = int(input())
 
l = list(map(int, input().split()))
l.sort()
a = 0
#print(l)
for i in range(n-2):
    for j in range(i+1,n-1):
        s = bisect.bisect_left(l,l[i]+l[j])-j-1
        a+=s


print(a)
Buggy code:
N = int(input())
Lines = list(map(int, input().split(" ")))
Lines.sort()
answer = 0

import bisect

for lineI in range(N):
    for lineJ in range(lineI+1,N):
    	answer += bisect.bisect_left(Lines,Lines[lineI]+Lines[lineJ])-j-1
print(answer)

Fixed code:
N = int(input())
Lines = list(map(int, input().split(" ")))
Lines.sort()
answer = 0
 
import bisect
 
for lineI in range(N):
    for lineJ in range(lineI+1,N):
    	answer += bisect.bisect_left(Lines,Lines[lineI]+Lines[lineJ])-lineJ-1
print(answer)
Buggy code:
def find(edges):
    import bisect
    edges=sorted(edges)
    ans=0
    for i in range(len(edges)):
        for j in range(i+1,len(edges)):
            temp=edges[i]+edges[j]
            pos=bisect.bisect_left(edges,temp)
            ans+=max(0,pos-j-1)
    return ans

input()
find(list(map(int,input().strip().split(" "))))

Fixed code:
def find(edges):
    import bisect
    edges=sorted(edges)
    ans=0
    for i in range(len(edges)):
        for j in range(i+1,len(edges)):
            temp=edges[i]+edges[j]
            pos=bisect.bisect_left(edges,temp)
            ans+=max(0,pos-1-j)
            #print(edges[i],edges[j],max(0,pos-1-j))
    return ans

input()
print(find(list(map(int,input().strip().split(" ")))))
Buggy code:
from bisect import bisect_left


def p_d():
    N = int(input())
    L = list(map(int, input().split()))
    L.sort()
    ans = N * (N - 1) * (N - 2) // 6

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

    print(ans)
    
p_d()

Fixed code:
from bisect import bisect_left


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

    ans = N * (N - 1) * (N - 2) // 6
    L.sort()

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

    print(ans)


if __name__ == '__main__':
    p_d()
Buggy code:
import sys

#+++++

def is_tr(a,b,c):
	if a < b+c:
		return True
	return False


def main():
	n = int(input())
	ll = list(map(int, input().split()))
	ll.sort(reverse = True)
	pa(ll)
	
	ret = 0
	for i,l1 in enumerate(ll[:-2]):
		ok=i+2
		for j, l2 in enumerate(ll[i+1:-1]):
			pa(ret)
			if not is_tr(l1, l2, ll[j+1]):
				break
			if is_tr(l1,l2,ll[-1]):
				pa('ggg')
				ret += n-j
				continue
			ng=n-1
			while ng-ok > 1:
				mid = (ok+ng)//2
				if is_tr(l1,l2,ll[mid]):
					ok =mid
				else:
					ng = mid
			pa((ok,j))
			ret += ((n-ok) -j-1)
	
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Fixed code:
import sys

#+++++

def is_tr(a,b,c):
	if a < b+c:
		return True
	return False


def main():
	n = int(input())
	ll = list(map(int, input().split()))
	n=len(ll)
	ll.sort(reverse = True)
	pa(ll)
	
	ret = 0
	for i,l1 in enumerate(ll[:-2]):
		for j, l2 in enumerate(ll[i+1:-1]):
			pa(ret)
			if not is_tr(l1, l2, ll[j+1]):
				break
			if is_tr(l1,l2,ll[-1]):
				pa('ggg')
				ret += n-(i+1+j+1)
				continue
			ok=i+j+1
			ng=n-1
			while ng-ok > 1:
				mid = (ok+ng)//2
				if is_tr(l1,l2,ll[mid]):
					ok =mid
				else:
					ng = mid
			pa((ok,j+i+1))
			ret += (ok - (i+j+1))
	
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)
Buggy code:
from bisect import bisect_left

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

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

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

from bisect import bisect_left 
cnt = 0
for i in range(0,N):
  for j in range(i+1,N):
    t = L[i]+L[j]
    idx = bisect_left(L,t)
    cnt += max(0,idx-j-1)
print(cnt)
Buggy code:
a = list(map(int, input().split()))
 
a.sort()
count = 0
for i in range(0, n-1):
    l = 0
    r = i-1
    while(l<r):
        if a[l] + a[r] > a[i]:
            count += r-l
            r -= 1
 
        else:
            l += 1
 
print(count)

Fixed code:
n = int(input())
a = list(map(int, input().split()))
 
a.sort()
count = 0
for i in range(n-1, 0, -1):
    l = 0
    r = i-1
    while(l<r):
        if a[l] + a[r] > a[i]:
            count += r-l
            r -= 1
 
        else:
            l += 1
 
print(count)
Buggy code:
import bisect

N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0
for bi in range(1,N-1):
  for ai in range(0,bi):
    ci = bisect.bisect(L,L[bi]+L[ai]) - 1
    if bi<ci:
      ans += ci-bi
print(ans)

Fixed code:
import bisect

N = int(input())
L = sorted(list(map(int,input().split())))
ans = 0
for bi in range(1,N-1):
  for ai in range(0,bi):
    ci = bisect.bisect(L,L[bi]+L[ai]-1) - 1
    if bi<ci:
      ans += ci-bi
print(ans)
Buggy code:
N = int(input())
l = [int(x) for x in input().split()]
l.sort(reverse=True)

ans = 0

for i in range(n-2):
  j = i + 1
  k = n - 1
  if l[i] < l[j] + l[k]:
    ans += k - j
    j += 1
  else:
    k -= 1
print(ans)

Fixed code:
N = int(input())
l = [int(x) for x in input().split()]
l.sort(reverse=True)
 
ans = 0
 
for i in range(N-2):
  j = i + 1
  k = N - 1
  while j < k:
    if l[i] < l[j] + l[k]:
      ans += k - j
      j += 1
    else:
      k -= 1
print(ans)
Buggy code:
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range(len(L)-1):
    for j in range(i+1,len(L)-1):
        a = L[i]
        b = L[j]
        c = L[j+1]
        if c-a < b and b < c+a:
            ans += bisect.bisect_left(L,b+a)-j-1
print(ans)

Fixed code:
import bisect
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
for i in range(len(L)-1):
    for j in range(i+1,len(L)-1):
        a = L[i]
        b = L[j]
        c = L[j+1]
        if c-a < b and b < c+a:
            ans += bisect.bisect_left(L,b+a)-j-1
print(ans)
Buggy code:
import sys
import bisect

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
l = sorted(nm())

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

Fixed code:
import sys
import bisect

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
l = sorted(nm())

ans = 0
for i in range(n-2):
    for j in range(i+1, n-1):
        a = l[i]
        b = l[j]
        idx = bisect.bisect_left(l, a + b)
        if idx > j: ans += idx - j - 1
print(ans)
Buggy code:
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 = i+j
        ng = n+1
        ok = j
        while abs(ok - ng) > 1:
            mid = (ok+ng)//2
            if val > mid:
                ok = mid
            else:
                ng = mid


        #print(ok)
        ans += ok - j


print(ans)

Fixed code:
n = int(input())
l = list(map(int,input().split()))
l.sort()
ans = 0
#print(l)
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


        #print(i,j,ok,ng)

        ans += ok - j


print(ans)
Buggy code:
import bisect
N = int(input())
L = [int(x) for x in input().split()]
L = sorted(L)
ans = 0
for one in range(N - 2):
    for two in range(one+1, N-1):
        ans += bisect.bisect_left(L, (L[one] + L[two]))-two-1
        print(bisect.bisect_left(L, (L[one] + L[two]))-two-1)
print(ans)


Fixed code:
import bisect
N = int(input())
L = [int(x) for x in input().split()]
L = sorted(L)
ans = 0
for one in range(N - 2):
    for two in range(one+1, N-1):
        ans += bisect.bisect_left(L, (L[one] + L[two]))-two-1
        #print(bisect.bisect_left(L, (L[one] + L[two]))-two-1)
print(ans)
Buggy code:
import bisect
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
for i in range(n-1,1,-1):
  for j in range(i-1,0,-1):
    ans+=j-bisect.bisect_left(l,l[i]-l[j],hi=j)
print(ans)

Fixed code:
import bisect
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=0
for i in range(n-1,1,-1):
  for j in range(i-1,0,-1):
    ans+=j-bisect.bisect_left(l,l[i]-l[j]+1,hi=j)
print(ans)
Buggy code:
from bisect import bisect_left
N=int(input())
triangle=[]
count=0
L=list(map(int,input().split()))
L.sort()
#print(L)
for a in range(1,N):
    for b in range(a+1,N):
        c=L[a]+L[b]
        zahyo=bisect_left(L,c)
        count+=max(0,zahyo-b-1)
print(count)

Fixed code:
from bisect import bisect_left
N=int(input())
triangle=[]
count=0
L=list(map(int,input().split()))
L.sort()
#print(L)
for a in range(N):
    for b in range(a+1,N):
        c=L[a]+L[b]
        zahyo=bisect_left(L,c)
        count+=max(0,zahyo-b-1)
print(count)
Buggy code:
import bisect
n = int(input())
l_li = list(map(int,input().split()))
l_li.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        ind = bisect.bisect_left(l_li,l_li[i]+l_li[j])
        num = ind-1 - j
print(ans)


Fixed code:
import bisect
n = int(input())
l_li = list(map(int,input().split()))
l_li.sort()
ans = 0
for i in range(n-2):
    for j in range(i+1,n-1):
        ind = bisect.bisect_left(l_li,l_li[i]+l_li[j])
        num = ind-1 - j
        if num > 0:
            ans += num
print(ans)