Buggy python submission (verdict: Runtime Error):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
s = input()
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
res = 0
for i in range(n-1):
    c = z_algo(s[i:])
    # print(c)
    for j,t in enumerate(c):
        if t > n//2 or j == 0: continue
        res = max(res, t)
print(res)

Fixed submission:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
s = input()
def z_algo(S):
    N = len(S)
    A = [0] * N
    i = 1; j = 0
    A[0] = l = N
    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A
res = 0
for i in range(n):
    c = z_algo(s[i:])
    # print(c)
    for j,t in enumerate(c):
        tmp = min(j,t)
        res = max(res, tmp)
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
s = input()
if n >= 666:
    ans = 2
elif n >= 27:
    ans = 1
else:
    ans = 0

for i in range(1,n//2+1)[::-1]:
    if i == ans:
        print(ans)
        quit()
    d = deque([])
    for j in range(i,n-i+1):
        d.append(s[j:j+i])
    for j in range(n-2*i+1):
        tmp = s[j:j+i]
        ll = len(d)
        for k in range(ll):
            ddd = d.popleft()
            if tmp == ddd:
                print(i)
                quit()
            d.append(ddd)
        d.popleft()
print(0)



Fixed submission:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
s = input()
def z_algo(S):
    N = len(S)
    A = [0] * N
    i = 1; j = 0
    A[0] = l = N
    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A
res = 0
for i in range(n):
    c = z_algo(s[i:])
    # print(c)
    for j,t in enumerate(c):
        tmp = min(j,t)
        res = max(res, tmp)
print(res)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
s = input()

for i in range(1,n//2+1)[::-1]:
    d = deque([])
    for j in range(i,n-i+1):
        d.append(s[j:j+i])
    for j in range(n-2*i+1):
        tmp = s[j:j+i]
        ll = len(d)
        for k in range(ll):
            ddd = d.popleft()
            if tmp == ddd:
                print(i)
                quit()
            d.append(ddd)
        d.popleft()
print(0)



Fixed submission:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
s = input()
def z_algo(S):
    N = len(S)
    A = [0] * N
    i = 1; j = 0
    A[0] = l = N
    while i < l:
        while i+j < l and S[j] == S[i+j]:
            j += 1
        if not j:
            i += 1
            continue
        A[i] = j
        k = 1
        while l-i > k < j - A[k]:
            A[i+k] = A[k]
            k += 1
        i += k; j -= k
    return A
res = 0
for i in range(n):
    c = z_algo(s[i:])
    # print(c)
    for j,t in enumerate(c):
        tmp = min(j,t)
        res = max(res, tmp)
print(res)