Buggy python submission (verdict: Time Limit Exceeded):
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]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        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)

Fixed submission:
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]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        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)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = list(map(int,input().split()))

L.sort()

s = 0
#print(L)
#print("")
for j in range(N-2):
    for k in range(j+1,N-1):
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        key = L[j]+L[k]
        l = k + 1
        r = N - 1
        while r - l > 1:
            m = l + (r - l) // 2
            if L[m] < key:
                l = m
            else:
                r = m
        if L[k+1] < key <= L[N-1]:
            s += l-k
        elif key <= L[k+1]:
            s += 0
        else:
            s += N-k-1

print(s)

Fixed submission:
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]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        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)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = list(map(int,input().split()))

L.sort()

s = 0
for j in range(N-2):
    for k in range(j+1,N-1):
        Ld = [0] + L[k+1::] + [3000]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        key = L[j]+L[k]
        l = 0
        r = N - k + 2
        while r - l > 1:
            m = (r + l) // 2
            if Ld[m] < key:
                l = m
            else:
                r = m
        s += l

print(s)

Fixed submission:
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]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        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)

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

L.sort()
print(L)

s = 0
for j in range(N-2):
    for k in range(j+1,N-1):
        Ld = [0] + L[k+1::] + [3000]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        key = L[j]+L[k]
        l = 0
        r = j + 2
        while r - l > 1:
            m = (r + l) // 2
            if Ld[m] < key:
                l = m
            else:
                r = m
        s += l

print(s)

Fixed submission:
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]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        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)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
L = list(map(int,input().split()))

L.sort(reverse=True)

s = 0
for i in range(N):
    o = []
    for j in range(i+1,N):
        for k in range(j+1,N):
            o.append(L[j]+L[k])
    o.sort()
    o = [0] + o + [3000]
    key = L[i]
    l = 0
    r = len(o)-1
    while r - l > 1:
        m = (r + l) // 2
        if o[m] > key:
            r = m
        else:
            l = m
    #print(key,o,l,len(o))
    s += len(o)-l-2

print(s)

Fixed submission:
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]
        # L[i] < L[j]+L[k] となる L[i]の数を求める
        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)