Problem p02888 (python)

Fixed submission:
def fnt(arr,n): 
  
     
     
     
    count = 0
  
     
    for i in range(0,n-2): 
  
         
        k = i + 2
  
        # Fix the second element 
        for j in range(i+1,n): 
  
             
            while (k < n and arr[i] + arr[j] > arr[k]): 
                k += 1
  
             
            if(k>j): 
                count += k - j - 1
  
    return count
 
 
n=int(input())
arr=input()
arr=arr.split()
arr=list(map(int,arr))
arr.sort()
print(fnt(arr,n))

Buggy submission (verdict: Runtime Error):

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

L.sort()
#print(L)

count = 0
for i in range(N-1):
    for j in range(i+1, N-1):
        L_ = L[j+1:]
        #print(L[i] + L[j], ">", L_, "?")
        key = len(L_) // 2
        #print("init key", key)
        past_key = 0
        n = 2
        while key and N > key:
            n *= 2
            if L[i] + L[j] > L_[key]:
                key += round(len(L_) / n)
            elif L[i] + L[j] <= L_[key]:
                #print(n, round(key / n))
                key -= round(len(L_) / n)
            
            if past_key == key:
                break
            else:
                past_key = key
            #print("next key", key)
            
        if L[i] + L[j] > L_[key]:
            count += key + 1
            

print(count)

Buggy submission (verdict: Runtime Error):

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

L.sort()
#print(L)

import sys

sys.setrecursionlimit(10**8)

count = 0
for i in range(N-1):
    for j in range(i+1, N-1):
        L_ = L[j+1:]
        #print(L[i] + L[j], ">", L_, "?")
        key = len(L_) // 2
        #print("init key", key)
        past_key = 0
        n = 2
        while key:
            n *= 2
            if L[i] + L[j] > L_[key]:
                key += round(len(L_) / n)
            elif L[i] + L[j] <= L_[key]:
                #print(n, round(key / n))
                key -= round(len(L_) / n)
            
            if past_key == key:
                break
            else:
                past_key = key
            #print("next key", key)
            
        if L[i] + L[j] > L_[key]:
            count += key + 1
            

print(count)

Buggy submission (verdict: Runtime Error):
N = int(input())
L = list(map(int, input().split()))

L.sort()
#print(L)

import sys

sys.setrecursionlimit(10**6)

count = 0
for i in range(N-1):
    for j in range(i+1, N-1):
        L_ = L[j+1:]
        #print(L[i] + L[j], ">", L_, "?")
        key = len(L_) // 2
        #print("init key", key)
        past_key = 0
        n = 2
        while key:
            n *= 2
            if L[i] + L[j] > L_[key]:
                key += round(len(L_) / n)
            elif L[i] + L[j] <= L_[key]:
                #print(n, round(key / n))
                key -= round(len(L_) / n)
            
            if past_key == key:
                break
            else:
                past_key = key
            #print("next key", key)
            
        if L[i] + L[j] > L_[key]:
            count += key + 1
            

print(count)

Buggy submission (verdict: Runtime Error):

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

L.sort()
#print(L)

count = 0
for i in range(N-1):
    for j in range(i+1, N-1):
        L_ = L[j+1:]
        #print(L[i] + L[j], ">", L_, "?")
        key = len(L_) // 2
        #print("init key", key)
        past_key = 0
        n = 2
        while key:
            n *= 2
            if L[i] + L[j] > L_[key]:
                key = key + round(len(L_) / n)
            elif L[i] + L[j] <= L_[key]:
                #print(n, round(key / n))
                key = key - round(len(L_) / n)
            
            if past_key == key:
                break
            else:
                past_key = key
            #print("next key", key)
            
        if L[i] + L[j] > L_[key]:
            count += key + 1
            

print(count)