alexjercan/bugnet huggingface dataset:
Problem p02881 (Python) — buggy submission, verdict: Runtime Error

from math import sqrt

N = int(input())

for i in range(int(sqrt(N) + 1), -1, -1):
    if N % i == 0:
        print((i - 1) + (N // i - 1))
        break


Error: 0

Fixed submission:

N = int(input())
ret = []
for i in range(1, N):
    for j in range(i, N):
        if i * j == N:
            ret.append(i + j - 2)
print(min(ret))
Problem p02883 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np


def is_good(mid, key):
    return A_sum - np.minimum(A, mid // F).sum() <= key


def binary_search(bad, good, key):
    while good - bad > 1:
        mid = (bad + good) // 2
        if is_good(mid, key):
            good = mid
        else:
            bad = mid
    return good


N, K = map(int, input().split())
A = np.array(input().split(), dtype=np.int64)
F = np.array(input().split(), dtype=np.int64)
A.sort()
F[::-1].sort()
A_sum = A.sum()
ans = binary_search(-1, np.max(A * F), K)
print(ans)


Error: TLE

Fixed submission:

def is_good(mid, key):
    return sum(max(0, a - mid // f) for a, f in zip(A, F)) <= key


def binary_search(bad, good, key):
    while good - bad > 1:
        mid = (bad + good) // 2
        if is_good(mid, key):
            good = mid
        else:
            bad = mid
    return good


N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort(reverse=True)
A_sum = sum(A)
ans = binary_search(-1, max(a * f for a, f in zip(A, F)), K)
print(ans)
Problem p02883 (Python) — buggy submission, verdict: Time Limit Exceeded

N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))
A.sort(reverse=True)
F.sort()

C = [None] * N
for i, (a, f) in enumerate(zip(A, F)):
    C[i] = (a * f, f)


def solve(x):
    global K, N
    t = 0
    for c, f in C:
        temp = ((c - x) + f - 1) // f
        t += max(0, temp)
        if t > K:
            result = False
            break
    else:
        result = True
    return result


ok = A[0] * F[N - 1]
ng = -1
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if solve(mid):
        ok = mid
    else:
        ng = mid


print(ok)


Error: TLE

Fixed submission:

from heapq import heappush, heappushpop, heappop

N, K = map(int, input().split())
A = list(map(lambda x: -int(x), input().split()))
F = list(map(int, input().split()))
A.sort()
F.sort()


hq = []
for a, f in zip(A, F):
    heappush(hq, (a * f, f))

c, f = heappop(hq)
while K > 0:
    c += f
    K -= 1
    c, f = heappushpop(hq, (c, f))

print(-c)
Problem p02883 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import numpy as np


def is_ok(mid, K, A, F):
    return np.maximum(A - mid // F, 0).sum() <= K


def main():
    N, K = input_line(int, int)
    A = input_line([int])
    F = input_line([int])
    A = np.array(sorted(A))
    F = np.array(sorted(F, reverse=True))

    ng = -1
    ok = int(1e12)
    while ok - ng > 1:
        mid = (ng + ok) // 2
        if is_ok(mid, K, A, F):
            ok = mid
        else:
            ng = mid
    print(ok)


def input_line(*types):
    if isinstance(types[0], list):
        return [types[0][0](x) for x in input().split()]
    elif len(types) == 1:
        return types[0](input())
    else:
        return [t(x) for t, x in zip(types, input().split())]


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

#!/usr/bin/env python3


def is_ok(mid, K, A, F):
    used = 0
    for a, f in zip(A, F):
        upper = int(mid / f)
        if upper < a:
            used += a - upper
        if K < used:
            return False
    return True


def main():
    N, K = input_line(int, int)
    A = input_line([int])
    F = input_line([int])
    A.sort()
    F.sort(reverse=True)

    ng = -1
    ok = int(1e12)
    while ok - ng > 1:
        mid = (ng + ok) // 2
        if is_ok(mid, K, A, F):
            ok = mid
        else:
            ng = mid
    print(ok)


def input_line(*types):
    if isinstance(types[0], list):
        return list(map(types[0][0], input().split()))
    elif len(types) == 1:
        return types[0](input())
    else:
        return [t(x) for t, x in zip(types, input().split())]


if __name__ == "__main__":
    main()
Problem p02883 (Python) — buggy submission, verdict: Time Limit Exceeded

def is_ok(x):
    trainings = 0
    for i in range(N):
        t = A[i] - x // F[i]
        if t > 0:
            trainings += t
    return trainings <= K


N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse=True)

ng = -1
ok = A[-1] * F[0]
while ok - ng > 1:
    m = ng + (ok - ng) // 2
    if is_ok(m):
        ok = m
    else:
        ng = m
print(ok)


Error: TLE

Fixed submission:

def is_ok(x, A, F):
    trainings = 0
    for i in range(N):
        t = A[i] - x // F[i]
        if t > 0:
            trainings += t
    return trainings <= K


N, K = map(int, input().split())
A = list(map(int, input().split()))
F = list(map(int, input().split()))

A.sort()
F.sort(reverse=True)

ng = -1
ok = A[-1] * F[0]
while ok - ng > 1:
    m = ng + (ok - ng) // 2
    if is_ok(m, A, F):
        ok = m
    else:
        ng = m
print(ok)
Problem p02884 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys

n, m = map(int, input().split())
links = [set() for _ in range(n)]
counts = [0] * n
for line in sys.stdin:
    s, t = map(int, line.split())
    s -= 1
    t -= 1
    links[s].add(t)
    counts[s] += 1

not_omitted = [0.0] * n
exp_get = not_omitted.__getitem__
for j in range(n - 2, -1, -1):
    not_omitted[j] = sum(map(exp_get, links[j])) / counts[j] + 1

ans = 1e6

for i in range(n - 1):
    expected = [0.0] * n
    expected[i + 1 :] = not_omitted[i + 1 :]
    e_get = expected.__getitem__

    if counts[i] == 1:
        expected[i] = sum(map(e_get, links[i])) / counts[i] + 1
    else:
        exp = list(map(e_get, links[i]))
        expected[i] = (sum(exp) - max(exp)) / (counts[i] - 1) + 1

    for j in range(i - 1, -1, -1):
        expected[j] = sum(map(e_get, links[j])) / counts[j] + 1

    ans = min(ans, expected[0])

print(ans)


Error: TLE

Fixed submission:

import sys

import numpy as np

n, m = map(int, input().split())
links = [[] for _ in range(n)]
for line in sys.stdin:
    s, t = map(int, line.split())
    s -= 1
    t -= 1
    links[s].append(t)

not_omitted = np.zeros(n, dtype=np.float64)
for j in range(n - 2, -1, -1):
    exp = not_omitted[links[j]]
    not_omitted[j] = exp.mean() + 1

ans = not_omitted[0]

for i in range(n - 1):
    expected = not_omitted.copy()

    exp = expected[links[i]]
    if exp.size == 1:
        expected[i] = exp.mean() + 1
    else:
        expected[i] = (exp.sum() - exp.max()) / (exp.size - 1) + 1

    for j in range(i - 1, -1, -1):
        exp = expected[links[j]]
        expected[j] = exp.mean() + 1

    ans = min(ans, expected[0])

print(ans)
Problem p02885 (Python) — buggy submission, verdict: Runtime Error

data = input().split()
A = int(data[0])
B = int(data[1])

remain = max(0, A - B * 2)

print(remain)


Error: ValueError: invalid literal for int() with base 10: '12 4'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02885/Python/s165633108.py", line 1, in <module>
    A = int(input())
ValueError: invalid literal for int() with base 10: '12 4'


Fixed submission:

A = int(input())
B = int(input())

remain = max(0, A - B * 2)

print(remain)
Problem p02885 (Python) — buggy submission, verdict: Runtime Error

list = input().split()

a = int(list[0])
b = int(list[1])

if a > b * 2:
    print(a - b * 2)
else:
    print(0)


Error: ValueError: invalid literal for int() with base 10: '12 4'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02885/Python/s894876689.py", line 1, in <module>
    a = int(input())
ValueError: invalid literal for int() with base 10: '12 4'


Fixed submission:

a = int(input())
b = int(input())

if a > b * 2:
    print(a - b * 2)
else:
    print(0)
Problem p02885 (Python) — buggy submission, verdict: Runtime Error

a, b = map(int, input().split())
ans = a - (b * 2)

if ans <= 0:
    ans = 0

print(ans)


Error: ValueError: invalid literal for int() with base 10: '12 4'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02885/Python/s927098253.py", line 1, in <module>
    a = int(input())
ValueError: invalid literal for int() with base 10: '12 4'


Fixed submission:

a = int(input())
b = int(input())

ans = a // (b * 2)

if ans <= 0:
    ans = 0

print(ans)
Problem p02887 (Python) — buggy submission, verdict: Runtime Error

from itertools import groupby

N = int(input())
S = input()

G = [len(tuple(v)) for k, v in groupby(S)]
print(len(G))


Error: ValueError: invalid literal for int() with base 10: 'aabbbbaaca'
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02887/Python/s984716346.py", line 1, in <module>
    N, *D = map(int, open(0).read().split())
ValueError: invalid literal for int() with base 10: 'aabbbbaaca'


Fixed submission:

N, *D = map(int, open(0).read().split())

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        ans += D[i] * D[j]

print(ans)
Problem p02887 (Python) — buggy submission, verdict: Runtime Error

#!/usr/bin/env python3
import sys
import re


def solve(N: int, S: str):
    result = 0
    for s in set(S):
        regex = re.compile(s + "+")
        # print(regex.findall(S))
        result += len(regex.findall(S))
    print(result)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)


if __name__ == "__main__":
    main()


Error: 0

Fixed submission:

#!/usr/bin/env python3
import sys
import re


def solve(N: int, S: str):
    result = 0
    for s in set(S):
        regex = re.compile(f"{s}+")
        # print(regex.findall(S))
        result += len(regex.findall(S))
    print(result)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    solve(N, S)


if __name__ == "__main__":
    main()
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect

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


Error: TLE

Fixed submission:

N = int(input())
L = sorted(map(int, input().split()))
ans = 0
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            if L[k] < L[i] + L[j]:
                ans += 1
print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import bisect


N = int(input().split()[0])
l_list = list(map(int, input().split()))
l_list = sorted(l_list)
total = 0

for a_i, a in enumerate(l_list):
    # 一番長い辺を固定
    for b_i, b in enumerate(l_list[:a_i]):
        # 2番目に長い辺を固定
        c_i = bisect.bisect_right(l_list, abs(a - b))
        total += max(b_i - c_i, 0)

ans = total
print(ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3
import bisect


N = int(input().split()[0])
l_list = list(map(int, input().split()))
l_list = sorted(l_list)
p_list = []

for a_i, a in enumerate(l_list):
    for b_i, b in enumerate(l_list):
        if a_i == b_i:
            continue

        c_kouho = a + b
        c_i = bisect.bisect_left(l_list, c_kouho)
        p_list += [
            sorted([a, b, c])
            for c_i, c in enumerate(l_list[: c_i + 1])
            if c_i != a_i and c_i != b_i and abs(b - c) < a < b + c
        ]

p_list = ["{}_{}_{}".format(p[0], p[1], p[2]) for p in p_list]
ans = len(set(p_list))

print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

n = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0
for i in range(n - 2):
    a = L[i]
    k = i
    for j in range(i + 1, n - 1):
        b = L[j]
        while k < n and L[k] < a + b:
            k += 1
        ans += k - (j + 1)
print(ans)


Error: TLE

Fixed submission:

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):
        b = L[j]
        for k in range(j + 1, n):
            c = L[k]
            if c >= a + b:
                break
            ans += 1
print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

from bisect import bisect_left, bisect_right

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_left(L, L[i] + L[j]) - j - 1

print(ans)


Error: TLE

Fixed submission:

from itertools import combinations


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

ans = 0
for a, b, c in combinations(L, 3):
    if c < a + b:
        ans += 1

print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

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)


Error: TLE

Fixed submission:

N = int(input())

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

ret = 0


def is_ok(a, b, c):
    return a < b + c and b < a + c and c < a + b


for i in range(N - 2):
    for j in range(i + 1, N - 1):
        p = j + 1
        q = N + 1
        while q - p > 2:
            k = (p + q) // 2
            if is_ok(L[i], L[j], L[k]):
                p = k
            else:
                q = k

        if is_ok(L[i], L[j], L[p]):
            ret += p - j

print(ret)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

from sys import stdin
import bisect

N = int(stdin.readline().rstrip())
L = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
L.sort()
for i in range(1, N):
    for j in range(i):
        ans += max(bisect.bisect_left(L, L[i] + L[j]) - i - 1, 0)

print(ans)


Error: TLE

Fixed submission:

from sys import stdin

N = int(stdin.readline().rstrip())
L = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
L.sort()

for i in range(2, N):
    for j in range(1, i):
        for k in range(j):
            if L[j] + L[k] > L[i]:
                ans += 1

print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

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):
        a = L[i]
        b = L[j]
        v = bisect.bisect_left(L, a + b)
        cnt += v - j - 1
print(cnt)


Error: TLE

Fixed submission:

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):
        left = j
        right = N
        v = L[i] + L[j]
        while right - left > 1:
            mid = (left + right) // 2
            if L[mid] < v:
                left = mid
            else:
                right = mid
        cnt += left - j
print(cnt)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect

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

L.sort()

n = 0
# Choose two edges
for i in range(N):
    for j in range(i + 1, N):
        a = L[i] + L[j]
        k = bisect.bisect_left(L, a, lo=j + 1)
        #   assert all(v < a for v in L[:k]) and all(v >= a for v in L[k:])
        # the number of candidates of the third edge
        n += k - (j + 1)

print(n)


Error: TLE

Fixed submission:

import bisect

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

L.sort()

n = 0

# Choose two edges
for i in range(N):
    for j in range(i + 1, N):
        a = L[i] + L[j]
        k = bisect.bisect_left(L, a, lo=j + 1)
        #   assert all(v < a for v in L[:k]) and all(v >= a for v in L[k:])
        # the number of candidates of the third edge
        n += k - (j + 1)

print(n)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import collections

N = int(input())
L = [int(i) for i in input().split()]
M = max(L)
c = collections.Counter(L)
li = [c[i] for i in range(M + 1)]

ans = 0
for i in range(1, M + 1):
    for j in range(i, M + 1):
        if i == j:
            ans += int(li[i] * (li[i] - 1) * (li[i] - 2) / 6)
            ans += int(sum(li[j + 1 : min(M + 1, i + j)]) * li[i] * (li[i] - 1) / 2)
        else:
            ans += int(li[i] * li[j] * (li[j] - 1) / 2)
            ans += li[i] * li[j] * sum(li[j + 1 : min(M + 1, i + j)])
print(int(ans))


Error: TLE

Fixed submission:

import collections

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

ans = 0
for i in c:
    for j in c:
        for k in c:
            if i + j <= k or j + k <= i or k + i <= j or i > j or j > k:
                continue
            if i == j and j == k:
                ans += c[i] * (c[i] - 1) * (c[i] - 2) / 6
            elif i == j:
                ans += c[k] * c[i] * (c[i] - 1) / 2
            elif j == k:
                ans += c[i] * c[j] * (c[j] - 1) / 2
            elif i == k:
                ans += c[j] * c[i] * (c[i] - 1) / 2
            else:
                ans += c[i] * c[j] * c[k]
print(int(ans))
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect

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):
        index = bisect.bisect_left(L, L[i] + L[j])
        ans += max(0, index - j - 1)
print(ans)


Error: TLE

Fixed submission:

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):
        for k in range(j + 1, N):
            if L[i] + L[j] <= L[k]:
                break
            ans += 1
print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect

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

Ls.sort()
S = 0
for i in range(N - 2):
    A = Ls[i]
    for j in range(i + 1, N - 1):
        B = Ls[j]
        k = bisect.bisect_left(Ls, A + B, j + 1)
        S += k - j - 1
print(S)


Error: TLE

Fixed submission:

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

Ls.sort()
S = 0
for i in range(N - 2):
    A = Ls[i]
    for j in range(i + 1, N - 1):
        B = Ls[j]
        for k in range(j + 1, N):
            C = Ls[k]
            if A + B <= C:
                break
            else:
                S += 1
print(S)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect

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


Error: TLE

Fixed submission:

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):
        b = L[j]
        for k in range(j + 1, n):
            c = L[k]
            if c < a + b:
                ans += 1
            else:
                break
print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

from bisect import bisect_left

n = int(input())
lll = list(map(int, input().split()))
lll.sort()
ans = 0


def get_bisect_left():
    cache = {}

    def _f(x):
        if x in cache:
            return cache[x]
        cache[x] = bisect_left(lll, x)
        return cache[x]

    return _f


bisect_left_cache = get_bisect_left()

for i in range(n - 2):
    li = lll[i]
    for j in range(i + 1, n - 1):
        k = bisect_left_cache(li + lll[j])
        ans += k - j - 1
print(ans)


Error: TLE

Fixed submission:

from bisect import bisect_left
from functools import lru_cache

n = int(input())
lll = list(map(int, input().split()))
lll.sort()
ans = 0


@lru_cache()
def bisect_left_cache(x):
    return bisect_left(lll, x)


for i in range(n - 2):
    li = lll[i]
    for j in range(i + 1, n - 1):
        k = bisect_left_cache(li + lll[j])
        ans += k - j - 1
print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect


def main():
    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):
            index = bisect.bisect_left(L, L[i] + L[j]) - j - 1
            ans += index

    print(ans)


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

import itertools


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

    comb = itertools.combinations(L, 3)

    ans = 0
    dp = []
    for v in comb:
        if dp.__contains__(sum(v)):
            ans += 1
        elif v[0] < v[1] + v[2] and v[1] < v[2] + v[0] and v[2] < v[0] + v[1]:
            ans += 1
            dp.append(sum(v))

    print(ans)


if __name__ == "__main__":
    main()
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

import bisect

n = int(input())
li = 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(li, li[i] + li[j]) - j - 1
print(ans)


Error: TLE

Fixed submission:

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

ans = 0
for i in range(n - 2):
    for j in range(i + 1, n - 1):
        for k in range(j + 1, n):
            if li[i] < li[j] + li[k] and li[j] < li[k] + li[i] and li[k] < li[i] + li[j]:
                # print(li[i], li[j], li[k])
                ans += 1

print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

from bisect import bisect_left

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

# a<b<c として、aとbを固定する
# 残る条件は c<a+b のみ
# aとbの固定方法がO(N^2), cの二分探索がO(logN)、結局O(N^2logN)
# 二分探索は事前にソートが必要

# ex. 2, 4(a), 4, 7(b), | 8, 9, 9 | (ここにa+b=11が挿入される), 12, 14
# cとして適切なのは8,9,9の3通り
# bisect_rightだとa+bが存在した時にバグる

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


Error: TLE

Fixed submission:

N = int(input())
L = sorted(list(map(int, input().split())))
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        for k in range(j + 1, N):
            if L[i] < L[j] + L[k] and L[j] < L[k] + L[i] and L[k] < L[i] + L[j]:
                ans += 1
print(ans)
Problem p02888 (Python) — buggy submission, verdict: Time Limit Exceeded

# -*- coding: utf-8 -*-
import bisect

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

# solve
ans = 0
L.sort()
for i in range(N):
    for j in range(i + 1, N):
        r = bisect.bisect_left(L, L[i] + L[j])
        l = j + 1
        ans += max(0, r - l)

# output
print(ans)


Error: TLE

Fixed submission:

# -*- coding: utf-8 -*-

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

# solve
ans = 0
L.sort()
for i in range(N - 2):
    for j in range(i + 1, N - 1):
        for k in range(j + 1, N):
            if L[k] < L[j] + L[i]:
                ans += 1

# output
print(ans)