alexjercan/bugnet huggingface dataset:
Problem p02911 (Python) — buggy submission, verdict: Time Limit Exceeded

# -*- coding: utf-8 -*-
N, K, Q = map(int, input().split())
A = [int(input()) for _ in range(Q)]

dp = [K] * N


def calc_dp(n):
    return n - Q


for i in A:
    # dp = list(map(calc_dp, dp))
    dp[i - 1] += 1

dp = list(map(calc_dp, dp))

for j in dp:
    if j > 0:
        print("Yes")
    else:
        print("No")


Error: TLE

Fixed submission:

# -*- coding: utf-8 -*-
N, K, Q = map(int, input().split())
A = [int(input()) for _ in range(Q)]

dp = [K] * N


def calc_dp(n):
    return n - 1


for i in A:
    dp = list(map(calc_dp, dp))
    dp[i - 1] += 1


for j in dp:
    if j > 0:
        print("Yes")
    else:
        print("No")
Problem p02911 (Python) — buggy submission, verdict: Time Limit Exceeded

import numpy as np

N, K, Q = list(map(int, input().strip().split()))
As = [int(input()) for _ in range(Q)]

points = np.asarray([K for _ in range(N)])

points -= Q
for A in As:
    points[A - 1] += 1

for p in points:
    if p > 0:
        print("Yes")
    else:
        print("No")


Error: TLE

Fixed submission:

import numpy as np

N, K, Q = list(map(int, input().strip().split()))
As = [int(input()) for _ in range(Q)]

points = np.asarray([K for _ in range(N)])
for A in As:
    points -= 1
    points[A - 1] += 1

for p in points:
    if p > 0:
        print("Yes")
    else:
        print("No")
Problem p02911 (Python) — buggy submission, verdict: Time Limit Exceeded

from collections import Counter

N, K, Q = map(int, input().split())
A = list(int(input()) for i in range(Q))
c = Counter(A)
for i in range(N):
    if Q - c[i + 1] < K:
        print("Yes")
    else:
        print("No")


Error: TLE

Fixed submission:

N, K, Q = map(int, input().split())
A = list(int(input()) for i in range(Q))
for i in range(N):
    if Q - A.count(i + 1) < K:
        print("Yes")
    else:
        print("No")
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

N, M = map(int, input().split())
A = list(map(lambda x: -1 * int(x), input().split()))

heapq.heapify(A)
for _ in range(M):
    a = -1 * heapq.heappop(A)
    a //= 2
    heapq.heappush(A, -1 * a)

print(-1 * sum(A))


Error: TLE

Fixed submission:

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

for _ in range(M):
    A[0] /= 2

    p = 0
    while p < N - 1 and A[p] < A[p + 1]:
        A[p], A[p + 1] = A[p + 1], A[p]
        p += 1

print(sum(map(int, A)))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

n, m = map(int, input().split())
a = list(map(lambda x: int(x) * (-1), input().split()))
heapq.heapify(a)

for _ in range(m):
    y = heapq.heappop(a)
    heapq.heappush(a, (-1) * (-y // 2))

print(-sum(a))


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

for _ in range(m):
    a[-1] //= 2
    a.sort()

print(sum(a))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

# priority queueで楽勝だった
import heapq

n, m = map(int, input().split())
a = list(map(lambda x: int(x) * -1, input().split()))
heapq.heapify(a)

for _ in range(m):
    x = -(-a[0] // 2)
    heapq.heapreplace(a, x)
    # print(a)
print(sum(a) * -1)


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort(reverse=True)

for _ in range(m):
    a[0] = a[0] // 2
    a.sort(reverse=True)


print(sum(a))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

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

while M > 0:
    A.sort(reverse=True)
    if A[0] == 0:
        break
    ham = A[0] // 2
    for i, a in enumerate(A):
        if ham < a and M > 0:
            A[i] = A[i] // 2
            M -= 1
        else:
            break
print(sum(A))


Error: TLE

Fixed submission:

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

while M > 0:
    max_index = A.index(max(A))
    A[max_index] = A[max_index] // 2
    M -= 1
print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

from heapq import heappush, heappop

n, m = map(int, input().split())
a_list = list(map(int, input().split()))

hqueue = []
for a in a_list:
    heappush(hqueue, -a)

for i in range(m):
    max_item = -(-heappop(hqueue) // 2)
    heappush(hqueue, max_item)

cost = 0
for i in range(n):
    cost += -heappop(hqueue)

print(cost)


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a_list = list(map(int, input().split()))

a_list.sort(reverse=True)
swapped = False
while m > 0:
    a_list[0] = a_list[0] // 2
    m -= 1
    if (n > 1) and (a_list[0] < a_list[1]) and not swapped:
        temp = a_list[0]
        a_list[0] = a_list[1]
        a_list[1] = temp
        swapped = True
    else:
        a_list.sort(reverse=True)
        swapped = False

cost_sum = 0
for a in a_list:
    cost_sum += a

print(cost_sum)
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq
import math

n, m = list(map(int, input().split()))
a = list(map(lambda x: int(x) * (-1), input().split()))
heapq.heapify(a)

for _ in range(m):
    heapq.heappush(a, math.ceil(heapq.heappop(a) / 2))

print(-sum(a))


Error: TLE

Fixed submission:

import bisect as b
import math

n, m = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))

for _ in range(m):
    half = a.pop(-1) / 2
    a.insert(b.bisect_left(a, half), half)

print(sum(list(map(lambda x: math.floor(x), a))))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

# 優先度付きキューの考え方が必須
import heapq

N, M = map(int, input().split())
# 読み込み時に * -1 を乗算している
A_list = list(map(lambda x: int(x) * (-1), input().split()))
# A_listを優先度付きキューにする
heapq.heapify(A_list)

while M > 0:
    temp_max = heapq.heappop(A_list)
    # //2 で余りは切り捨てのため -1 を 2回 乗算している
    heapq.heappush(A_list, (-1) * ((-1 * temp_max) // 2))
    M -= 1
print(-1 * sum(A_list))


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A_list = list(map(int, input().split()))
A_list.sort()
if N == 1:
    A_list[-1] = A_list[-1] // (2**M)
elif N > 1:
    while M > 0:
        A_list[-1] = A_list[-1] // 2
        if A_list[-1] < A_list[-2]:
            A_list.sort()
        M -= 1
print(sum(A_list))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

n, m = map(int, input().split())
a = list(map(lambda x: int(x) * (-1), input().split()))
heapq.heapify(a)  # aを優先度付きキューに

for _ in range(m):
    tmp_min = heapq.heappop(a)
    heapq.heappush(a, (-1) * (-tmp_min // 2))  # 計算誤差の関係で-1を2回かけてます
print(-sum(a))


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = list(map(int, input().split()))
while M > 0:
    M -= 1
    A[A.index(max(A))] //= 2
print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

N, M = map(int, input().split())
A = list(map(float, input().split()))

heap = [-Ai for Ai in A]
heapq.heapify(heap)
for _ in range(M):
    heapq.heapreplace(heap, heap[0] / 2)
A = [int(-p) for p in heap]
print(sum(A))


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = list(map(float, input().split()))

for _ in range(M):
    i = max(range(len(A)), key=A.__getitem__)
    A[i] /= 2
A = list(map(int, A))
print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

N, M = map(int, input().split())  # 品物の数、割引券の枚数
A = list(map(int, input().split()))  # 品物の値段
A.sort(reverse=True)
i = 0
for _ in range(M):
    A[i] = A[i] // 2
    if i <= N - 2 and A[i] < A[i + 1] and A[0] < A[i + 1]:
        i += 1
    else:
        A.sort(reverse=True)
        i = 0
print(str(sum(A)))


Error: TLE

Fixed submission:

N, M = map(int, input().split())  # 品物の数、割引券の枚数
A = list(map(int, input().split()))  # 品物の値段
for _ in range(M):
    tmp = A.index(max(A))
    A[tmp] = A[tmp] // 2
print(str(sum(A)))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

n, m = map(int, input().split())
A = list(map(int, input().split()))

pq = []
for a in A:
    heapq.heappush(pq, -a)  #'-'にすることで最大値を考えることができる！
# print(pq)
for _ in range(m):
    a = (-heapq.heappop(pq)) // 2
    heapq.heappush(pq, -a)
print(-sum(pq))


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a = list(map(int, input().split()))
while m > 0:
    M = max(a)
    tmp = a.index(M)
    a[tmp] = a[tmp] // 2
    m -= 1
print(sum(a))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

N, M = map(int, input().split())
A = [-int(i) for i in input().split()]

heapq.heapify(A)

for _ in range(M):
    x = heapq.heappop(A) / 2
    heapq.heappush(A, x)

ans = 0
for a in A:
    ans -= int(a)

print(ans)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = [int(i) for i in input().split()]

A = sorted(A, reverse=True)

for _ in range(M):
    A[0] = A[0] / 2
    A = sorted(A, reverse=True)

ans = 0
for a in A:
    ans += int(a)

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

import heapq

N, M = map(int, input().split())
A = [-int(n) for n in input().split()]
heapq.heapify(A)

for _ in range(M):
    a = heapq.heappop(A)
    a = a // 2 if a % 2 == 0 else a // 2 + 1
    heapq.heappush(A, a)

print(-sum(A))


Error: TLE

Fixed submission:

import numpy as np

N, M = map(int, input().split())
A = np.array([int(n) for n in input().split()])

for _ in range(M):
    A[np.argmax(A)] //= 2

print(int(np.sum(A)))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

N, M = map(int, input().split())
A = list(map(lambda x: int(x) * -1, input().split()))

heapq.heapify(A)

for _ in range(M):
    heapq.heappush(A, (heapq.heappop(A) + 1) // 2)

ans = sum(A) * -1

print(ans)


Error: TLE

Fixed submission:

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

for _ in range(M):
    A.sort()
    A[-1] //= 2

print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Runtime Error

from heapq import heapify, heappop, heappush

N, M = map(int, input().split())
A = []
for i in input().split():
    A.append(-int(i))
heapify(A)
for _ in range(M):
    a = -heappop(A)
    heappush(A, (a // 2) * -1)
# print('A', A)
ans = -sum(A)
print(ans)


Error: TypeError: heapify() argument must be list, not map
Traceback (most recent call last):
  File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02912/Python/s538766589.py", line 5, in <module>
    heapq.heapify(A)
TypeError: heapify() argument must be list, not map


Fixed submission:

import heapq  # heapqライブラリのimoprt

N, M = map(int, input().split())
A = map(lambda x: int(x) * (-1), input().split())
heapq.heapify(A)

for _ in range(N):
    a = heapq.heappop(A)
    a = (-a // 2) * (-1)
    heapq.heappush(A, a)
print(-sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3
import heapq

N, M = list(map(int, input().split()))
a_list = list(map(int, input().split()))

# Pythonのheapqは最小値しかとれないので、負の値に変換して扱う
a_list = list(map(lambda x: x * (-1), a_list))
heapq.heapify(a_list)

for _ in range(M):
    max_value = -heapq.heappop(a_list)
    heapq.heappush(a_list, -(max_value // 2))

ans = -sum(a_list)
print(ans)


Error: TLE

Fixed submission:

#!/usr/bin/env python3

N, M = list(map(int, input().split()))
a_list = list(map(int, input().split()))

for _ in range(M):
    a_list = sorted(a_list, reverse=True)
    a_list[0] = a_list[0] // 2

ans = sum(a_list)
print(ans)
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

N, M = map(int, input().split())
a_list = list(map(lambda x: int(x) * (-1), input().split()))
heapq.heapify(a_list)
for _ in range(M):
    min_num = heapq.heappop(a_list)
    heapq.heappush(a_list, (-1) * (-min_num // 2))
print(-sum(a_list))


Error: TLE

Fixed submission:

N, M = map(int, input().split())
a_list = sorted(map(int, input().split()))
for _ in range(M):
    a_list[-1] //= 2
    a_list.sort()
print(sum(a_list))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

from sys import stdin
import heapq


def main():
    N, M = [int(x) for x in stdin.readline().rstrip().split()]
    As = []
    for i in [int(x) for x in stdin.readline().rstrip().split()]:
        heapq.heappush(As, -i)
    for _ in range(M):
        x = heapq.heappop(As)
        heapq.heappush(As, x / 2)
    print(sum([-1 * int(x) for x in As]))


if __name__ == "__main__":
    main()


Error: TLE

Fixed submission:

from sys import stdin
from bisect import insort


def main():
    N, M = [int(x) for x in stdin.readline().rstrip().split()]
    As = [int(x) for x in stdin.readline().rstrip().split()]
    As.sort()
    for _ in range(M):
        x = As[-1] / 2
        insort(As, x)
        del As[-1]
    sum_ = 0
    for x in As:
        sum_ += int(x)
    print(sum_)


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

N, M = map(int, input().split())
A = list(map(int, input().split()))
A.sort(reverse=True)
it = 0
for i in range(M):
    A[it] = A[it] // 2
    if it < N - 1:
        if A[it] < A[it + 1] and A[0] < A[it + 1]:
            it += 1
        else:
            A.sort(reverse=True)
            it = 0
    if it == N - 1:
        A.sort(reverse=True)
        it = 0
print(sum(A))


Error: TLE

Fixed submission:

n, m = input().split()
N = int(n)
M = int(m)

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

for _ in range(M):
    A[0] = A[0] // 2
    A.sort(reverse=True)
print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

#!/usr/bin/env python3

from bisect import insort_right

n, m, *a = map(int, open(0).read().split())
a.sort()
for _ in range(m):
    insort_right(a, a.pop() >> 1)
print(sum(a))


Error: TLE

Fixed submission:

#!/usr/bin/env python3

from bisect import insort_right
from collections import deque

n, m, *a = map(int, open(0).read().split())

a = deque(sorted(a))
for _ in range(m):
    insort_right(a, a.pop() >> 1)
print(sum(a))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq

n, m = map(int, input().split())
a = []
for i in list(map(int, input().split())):
    heapq.heappush(a, -i)

for _ in range(m):
    p = heapq.heappop(a)
    heapq.heappush(a, int(p / 2))

print(-sum(a))


Error: TLE

Fixed submission:

n, m = map(int, input().split())
a = list(map(int, input().split()))

for _ in range(m):
    a[a.index(max(a))] = a[a.index(max(a))] // 2

print(sum(a))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq


class Reverse:
    def __init__(self, val):
        self.val = val

    def __lt__(self, other):
        return self.val > other.val

    def __repr__(self):
        return repr(self.val)


class Heapq:
    def __init__(self, arr, desc=False):
        if desc:
            for i in range(len(arr)):
                arr[i] = Reverse(arr[i])
        self.desc = desc
        self.hq = arr
        heapq.heapify(self.hq)

    def pop(self):
        if len(self.hq) == 0:
            return None
        if self.desc:
            return heapq.heappop(self.hq).val
        else:
            return heapq.heappop(self.hq)

    def push(self, a):
        if self.desc:
            heapq.heappush(self.hq, Reverse(a))
        else:
            heapq.heappush(self.hq, a)


N, M = map(int, input().split())
q = Heapq(list(map(int, input().split())), desc=True)

while M > 0:
    a = q.pop() // 2
    q.push(a)
    M -= 1

tmp = 0
while True:
    try:
        a = q.pop()
        tmp += a
    except:
        break
print(tmp)


Error: TLE

Fixed submission:

N, M = map(int, input().split())
A = sorted(list(map(int, input().split())), reverse=True)

while M > 0:
    A[0] //= 2
    for i in range(1, N):
        if A[0] > A[i]:
            A = A[1:i] + [A[0]] + A[i:]
            break
    else:
        A = A[1:] + [A[0]]
    M -= 1
print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import heapq


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

A = [a * -1 for a in A]

heapq.heapify(A)
for _ in range(M):
    a = heapq.heappop(A)
    heapq.heappush(A, (-a // 2) * (-1))

print(-sum(A))


Error: TLE

Fixed submission:

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

while M > 0:
    maxv = max(A)
    max_index = A.index(maxv)
    A[max_index] = A[max_index] // 2
    M -= 1

print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

import sys
from heapq import heappush, heappop


def input():
    return sys.stdin.readline().strip()


N, M = map(int, input().split())
A = []
for i in input().split():
    heappush(A, -1 * int(i))
# print(A)

for _ in range(M):
    val = heappop(A)
    if val == 0:
        break
    heappush(A, (val + 1) // 2)

print(-1 * sum(A))


Error: TLE

Fixed submission:

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

for _ in range(M):
    A.sort(reverse=True)
    A[0] //= 2

print(sum(A))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

from sys import stdin, setrecursionlimit
from heapq import heapify, heappop, heappush


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a = [-ai for ai in a]
    heapify(a)

    for _ in range(m):
        tmp = -heappop(a)
        heappush(a, -(tmp // 2))

    print(-sum(a))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()


Error: TLE

Fixed submission:

from sys import stdin, setrecursionlimit
from bisect import insort


def main():
    input = stdin.buffer.readline
    n, m = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    for _ in range(m):
        tmp = a[n - 1] // 2
        insort(a, tmp)

    print(sum(a[:n]))


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

import math
import heapq

n, m = map(int, input().split())
prices = list(map(int, input().split()))
hq = []


def _heappush_max(heap, item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap) - 1)


def _heappop_max(heap):
    """Maxheap version of a heappop."""
    lastelt = heap.pop()  # raises appropriate IndexError if heap is empty
    if heap:
        returnitem = heap[0]
        heap[0] = lastelt
        heapq._siftup_max(heap, 0)
        return returnitem
    return lastelt


# 優先度付キューの作成
for price in prices:
    _heappush_max(hq, price)

# 割引適用
for _ in range(m):
    target = _heappop_max(hq)
    _heappush_max(hq, target / 2)

total = 0
for price in hq:
    total += math.floor(price)

print(total)


Error: TLE

Fixed submission:

import math
import numpy as np

n, m = map(int, input().split())
prices = np.array(list(map(int, input().split())))

for _ in range(m):
    max_index = np.argmax(prices)
    prices[max_index] = prices[max_index] / 2

total = 0
for price in prices:
    total += math.floor(price)

print(total)
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

# 毎回ソートしても別にいいんじゃないの -> ダメでした
# 毎回最大のものがほしいならheapq使うか
import heapq

n, m = map(int, input().split())
# heapは最小のものを取り出すので予め-1をかけて大小を見かけ上逆転しておく
arr = list(map(lambda x: int(x) * -1, input().split()))
heapq.heapify(arr)
for _ in range(m):
    a = heapq.heappop(arr)
    # 負数にしたときの誤差がめんどくさい
    a *= -1
    a //= 2
    heapq.heappush(arr, a * -1)
print(sum(arr) * -1)


Error: TLE

Fixed submission:

# 毎回ソートしても別にいいんじゃないの -> ダメでした

n, m = map(int, input().split())
arr = list(map(int, input().split()))

while m > 0:
    arr = list(sorted(arr))
    arr[-1] = arr[-1] // 2
    m -= 1

print(sum(arr))
Problem p02912 (Python) — buggy submission, verdict: Time Limit Exceeded

n, m = map(int, input().split())
A = list(map(int, input().split()))

A = [a * (-1) for a in A]
import heapq

heapq.heapify(A)

for _ in range(m):
    a = heapq.heappop(A) * (-1)
    heapq.heappush(A, a // 2 * (-1))

print(abs(sum(A)))


Error: TLE

Fixed submission:

n, m = map(int, input().split())
A = list(map(int, input().split()))

for _ in range(m):
    i = A.index(max(A))
    A[i] /= 2
A = [int(a) for a in A]

print(sum(A))
Problem p02913 (Python) — buggy submission, verdict: Time Limit Exceeded

def z_algo(S):
    N = len(S)

    A = [0] * N
    A[0] = N
    i = 1
    j = 0

    while i < N:
        while i + j < N and S[j] == S[i + j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while N - i > k < j - A[k]:
            A[i + k] = A[k]
            k += 1
        i += k
        j -= k
    return A


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

ans = 0
for i in range(N):
    A = z_algo(S[i:])
    for idx, ls in enumerate(A):
        if idx - ls >= 0:
            ans = max(ans, ls)

print(ans)


Error: TLE

Fixed submission:

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

ans = 0
for i in range(N):
    for j in range((N + i) // 2, i, -1):
        if S[i:j] in S[j:]:
            ans = max(ans, j - i)
            break

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

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

ans = 0
left, right = 0, 1

while right < N:
    s = S[left:right]
    if s in S[right:]:
        right += 1
        ans = max(ans, len(s))
    else:
        left += 1

print(ans)


Error: TLE

Fixed submission:

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

ans = 0
left, right = 0, 1

while right < N:
    s = S[left:right]
    if s in S[right:]:
        right += 1
        ans = max(ans, len(s))
    else:
        left = right - 1

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

def Z_algo(S):
    n = len(S)
    LCP = [0] * n
    c = 0  # 最も末尾側までLCPを求めたインデックス
    for i in range(1, n):
        # i番目からのLCPが以前計算したcからのLCPに含まれている場合
        if i + LCP[i - c] < c + LCP[c]:
            LCP[i] = LCP[i - c]
        else:
            j = max(0, c + LCP[c] - i)
            while i + j < n and S[j] == S[i + j]:
                j += 1
            LCP[i] = j
            c = i
    LCP[0] = n
    return LCP


n = int(input())
s = input()
ans = 0
for i in range(n):
    lcp = Z_algo(s[i:])
    for j, p in enumerate(lcp):
        tmp = min(j, p)
        ans = max(ans, tmp)
print(ans)


Error: TLE

Fixed submission:

def Z_algo(S):
    n = len(S)
    LCP = [0] * n
    i = 1
    j = 0
    c = 0  # 最も末尾側までLCPを求めたインデックス
    for i in range(1, n):
        # i番目からのLCPが以前計算したcからのLCPに含まれている場合
        if i + LCP[i - c] < c + LCP[c]:
            LCP[i] = LCP[i - c]
        else:
            j = max(0, c + LCP[c] - i)
            while i + j < n and S[j] == S[i + j]:
                j += 1
            LCP[i] = j
            c = i
    LCP[0] = n
    return LCP


n = int(input())
s = input()
ans = 0
for i in range(n):
    lcp = Z_algo(s[i:])
    for j in range(len(lcp)):
        tmp = min(j, lcp[j])
        ans = max(ans, tmp)
print(ans)
Problem p02913 (Python) — buggy submission, verdict: Time Limit Exceeded

def z_algorithm(s):
    n = len(s)
    prefix = [0] * n
    j = 0
    ret = 0
    for i in range(1, n):
        if i + prefix[i - j] < j + prefix[j]:
            prefix[i] = prefix[i - j]
        else:
            k = max(0, j + prefix[j] - i)
            while i + k < n and s[k] == s[i + k]:
                k += 1
            prefix[i] = k
            ret = max(ret, min(i, k))
            j = i
    return ret


n = int(input())
s = input()
print(max(z_algorithm(s[i:]) for i in range(n - 1)))


Error: TLE

Fixed submission:

def z_algorithm(s):
    n = len(s)
    prefix = [0] * n
    j = 0
    ret = 0
    for i in range(1, n):
        if i + prefix[i - j] < j + prefix[j]:
            prefix[i] = prefix[i - j]
        else:
            k = max(0, j + prefix[j] - i)
            while i + k < n and s[k] == s[i + k]:
                k += 1
            prefix[i] = k
            ret = min(i, k)
            j = i
    return ret


n = int(input())
s = input()
print(max(z_algorithm(s[i:]) for i in range(n - 1)))