Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
def search(arr,target,start,last):
    while(last > start):
        mid = (last + start)//2
        #print("start:",start, "last:" ,last, "mid:",mid)
        if arr[mid] < target:
            start = mid + 1 
        else:
            last = mid 
    return start

for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a + b 
        f1 = search(l,target,j+1,n)
        #print(f1)
        cnt+= f1 - j -1
print(cnt)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
def search(arr,target,start,last):
#    print("target ==",target,arr[start:last+1])
    if last - start == 1:
        if(arr[last] < target):
            return last
        elif(arr[start] < target):
            return start
        else:
            return start -1
    if last < start:
        return start -1
    if start == last:
        if arr[start] < target:
            return start
        else:
            return start -1
    mid = (last - start)//2 + start
    if arr[mid] < target:
        return search(arr,target,mid + 1,last)
    else:
        return search(arr,target,start,mid)
    return start -1


for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a + b 
 #       print("#",a,b)
        f1 = search(l,target,j+1,n-1)
  #      print("arr[f1]",l[f1])
        cnt+= f1 - j 
  #      print("cnt: ",cnt)
print(cnt)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
def search(arr,target,start,last):
 #   print("target ==",target,arr[start:last+1])
    if last - start == 1:
        if(arr[last] < target):
            return last
        elif(arr[start] < target):
            return start
        else:
            return -1
    if last < start:
        return -1
    if start == last:
        if arr[start] < target:
            return start
        else:
            return -1
    mid = (last - start)//2 + start
    if arr[mid] < target:
        return search(arr,target,mid + 1,last)
    else:
        return search(arr,target,start,mid)
    return -1


for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a + b 
  #      print("#",a,b)
        f1 = search(l,target,j+1,n-1)
        if f1 == -1: 
            continue
   #     print("arr[f1]",l[f1])
        cnt+= f1 - j 
   #     print("cnt: ",cnt)
print(cnt)


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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
cnt = 0 
def search(arr,target,start,last):
#    print(target,start,last)
    if len(arr[start:last]) == 1 or start == last:
        if arr[start] > target:
            return start
        else:
            return -1
    mid = (last - start)//2 + start
    if arr[mid] <= target:
        return search(arr,target,mid,last)
    else:
        return search(arr,target,start,mid)
    return -1

for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        target = a - b 
        f1 = search(l,target,j+1,n)
 #       print(f1)
        if f1 == -1: 
            continue
        target = b - a 
        f2 = search(l,target,f1,n)
  #      print(f2)
        if f2 == -1: 
            continue
        target = a 
        for k in range(f2,n):
            if a + b > l[k]:
                cnt+=1

print(cnt)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
l.reverse()
cnt = 0 
for i in range(0,n-2):
    flag = False
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        if i == j:
            continue
        for k in range(j+1,n):
            if i == k and j == k:
                continue
            c = l[k]
            if a >= b + c:
                flag=True
                break
            if a < b + c and b < a + c and c < a + b:
                cnt+=1
            else:
                flag=True
                break
        if flag:
            break
print(cnt)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
l.reverse()
cnt = 0 
flag = False
for i in range(0,n-2):
    a = l[i]
    for j in range(i+1,n-1):
        b = l[j]
        if i == j:
            continue
        for k in range(j+1,n):
            if i == k and j == k:
                continue
            c = l[k]
            if a >= b + c:
                break
            if a < b + c and b < a + c and c < a + b:
                cnt+=1
            else:
                flag=True
                break
print(cnt)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
l.reverse()
cnt = 0 
flag = False
for i in range(0,n-2):
    for j in range(i+1,n-1):
        if i == j:
            continue
        for k in range(j+1,n):
            if i == k and j == k:
                continue
            a = l[i]
            b = l[j]
            c = l[k]
            if a < b + c and b < a + c and c < a + b:
                cnt+=1
            else:
                flag=True
                break
print(cnt)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
l.sort()
l.reverse()
cnt = 0 
for i in range(0,n):
    for j in range(i+1,n):
        if i == j:
            continue
        for k in range(j+1,n):
            if i == k and j == k:
                continue
            a = l[i]
            b = l[j]
            c = l[k]
            if a < b + c and b < a + c and c < a + b:
                cnt+=1
            else:
                break
print(cnt)

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

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
l = list(map(int,input().split(" ")))
cnt = 0 
for i in range(0,n):
    for j in range(i+1,n):
        if i == j:
            continue
        for k in range(j+1,n):
            if i == k and j == k:
                continue
            a = l[i]
            b = l[j]
            c = l[k]
            if a < b + c and b < a + c and c < a + b:
                cnt+=1
print(cnt)

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