Problem:


Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

2 \leq N \leq 5 \times 10^3

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5



Sample input:
5
ababa


Sample output:
2

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict
from heapq import *
sys.setrecursionlimit(200000)
input = sys.stdin.readline

N, = map(int, input().split())
# L = [int(v) for v in input().split()]
# L = [int(input()) for _ in range(Q)]
S = input().strip()

class RollingHash:
    def __init__(self, data):
        self.len = len(data)
        self.row = [0] * self.len
        for i in range(self.len):
            self.row[i] = ord(data[i]) * 73

    def build(self, count):
        self.data = [0] * (self.len - count + 1)
        first = 0
        for i in range(count):
            c = 10 ** (count - i - 1)
            first += self.row[i] * c

        self.data[0] = first

        for i in range(1, len(self.data)):
            c = self.data[i - 1] - self.row[i - 1] * (10 ** (count - 1))
            self.data[i] = c * 10 + self.row[i + count - 1]

    def get(self, index):
        return self.data[index]

roll = RollingHash(S)
t = N // 2 + 1
ans = 0
for i in range(2, t):
    roll.build(i)

    f = False
    for j in range(t - i + 1):
        ll = j + i
        rr = N - i + 1
        data = roll.data[j:ll+rr]
        data.sort()

        ta = roll.get(j)
        for _ in range(20):
            k = (ll + rr) // 2
            cu = roll.get(k)
            if ta < cu:
                rr = k
            elif ta > cu:
                ll = k
            else:
                f = True
                break
        if f:
            break
    if f:
        ans = i

print(ans)

Submission status: Runtime Error

Solution code:
import random

n = int(input())
s = list(input())

#s[i:j]のハッシュ値:(hash1[j]-hash1[i]*power1[j-i]%mod1)%mod1,hash_key2 = (hash2[j]-hash2[i]*power2[j-i]%mod2)%mod2
#長さnの文字列s
base1 = 1007
mod1 = 1000000007
base2 = 2009
mod2 = 1000000009

"""
modTank1 = [8570647,8570659,8570671,8570677,8570687,8570701,8570719,8570761,8570797,8571799]
mod1 = modTank1[random.randint(0,10)]
modTank2 = [7777709,7777727,7777753,7777769,7777801,7777811,7777813,7777843,7777849,7777853]
mod2 = modTank2[random.randint(0,10)]
"""

modTank1 = [8570647,8570659,8570671,8570677,8570687,8570701,8570719,8570761,8570797,8571799]
mod1 = modTank1[random.randint(0,10)]

hash1 = [0]*(n+1)
power1 = [1]*(n+1)


for i,e in enumerate(s):
    hash1[i+1] = (hash1[i]*base1 + ord(e))%mod1
    power1[i+1] = (power1[i]*base1)%mod1

res = 0
for i in range(n):
    for j in range(i+1,n):
        ng = min(j-i+1,n-j+1)
        ok = 0
        while ng-ok > 1:
            mid = (ok+ng)//2
            if (hash1[i+mid]-hash1[i]*power1[mid]%mod1)%mod1 != (hash1[j+mid]-hash1[j]*power1[mid]%mod1)%mod1:
                ng = mid
            else:
                ok = mid
        res = max(res,mid)
print(res)

Submission status: Runtime Error

Solution code:
s=[]
st = input()
for i in range(len(st)):
  for j in range(len(st)-i):
   s.append(st[i:j+i+1])
a=[]
for i in s:
  a.append(st.count(i))
b=[]
for i in range(len(a)):
  if a[i]>=2:
    b.append(len(s[i]))
print(max(b))

Submission status: Runtime Error

Solution code:
inpl = lambda: list(map(int,input().split()))
N = int(input())
S = input()
character_pos = [set()]
ans = 0
continue_list = dict()
for i in range(N):
    s = S[i]
    new_continue_list = dict()
    cpos = character_pos[ord(s)-ord('a')]
    for p in cpos:
        new_continue_list[p] = 1
    cpos.add(i)
    for k, v in continue_list.items():
        if s == S[k+1]  and v + 1 <= i - k - 1:
            new_continue_list[k+1] = v+1
        elif v > ans:
            ans = v
    continue_list = new_continue_list

if len(continue_list.values()) > 0:
    ans = max(ans, max(continue_list.values()))
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin

N = int(stdin.readline().rstrip())
S = stdin.readline().rstrip()
dp = [[0] * (N + 1) for _ in range(N + 1)]
res = 0

for i in range(N - 1, -1, -1):
    for j in range(N - 1, i, -1):
        if S[i] == S[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1, dp[i][j]
        res = max(res, min(j - i, dp[i][j]))

print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
ans = 0

for i in range(n):
    for j in range(i+1, n+1):
        print('s[i:j]:, s[j:]: ',s[i:j],s[j:], s[i:j] in s[j:], len(s[i:j]))
        if (s[i:j] in s[j:]):
            if ans < len(s[i:j]):
                ans = len(s[i:j])
        else:
            break
print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":
    N = int(input())
    S = input()

    dp = [[0] * 5001 for i in range(5001)]

    for i in range(N, 0, -1):
        for j in range(N, 0, -1):
            if S[i-1] != S[j-1]:
                dp[i][j] = 0
            else:
                dp[i][j] = dp[i+1][j+1] + 1

    result = 0
    for i in range(N):
        for j in range(N):
            if i >= j:
                continue
            now = min(dp[i][j], j-i)
            result = max([now, result])

    print(result)

Submission status: Runtime Error

Solution code:
n = int(intput())
s = int(intput())

ans = 0
i = j= 0

while j <= n-1:
    if s[i:j] in s[j:]:
        ans = max(ans, j-i)
        j += 1
    else:
        i += 1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
D = [[0] * n for i in range(n)]
 
for i in range(n):
    D[0][i] = int(s[0]==s[i])
    D[i][0] = int(s[0]==s[i])
D[0][0] = 0

for i in range(1,n):
    for j in range(i+1,n):
        print(f"i,j = {i,j}")
        mae = D[i-1][j-1]
        print(f"mae = {mae}")
        if s[i] == s[j]:
            if j-i <= mae:
                D[i][j] = mae
            else:
                D[i][j] = mae + 1

print(max([max(D[i]) for i in range(n)]))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()

ans = 0
for kosu in range(1, N//2+1):
    hantei = False
    for i in range(N-kosu):
        if hantei:
            break
        else:
            temp = S[i:i+kosu]
            # print("temp:", temp)
            # print("比較する文字列",S[0:i] + "|" + S[i+1:])
        if temp in S[0:i]+S[i+1:]:
            # print("Trueの時")
            ans = len(list(temp))
            hantei = True
            breaks
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
dp = [0]*n
j = 1

for i in range(n):
    while s[i:j] in s[j:s]:
        j += 1
    dp[i] = j-i-1
print(max(dp))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
dp = [0]*n
j = 1

for i in range(n):
    while S[i:j] in S[j:s]:
        j += 1
    dp[i] = j-i-1
print(max(dp))

Submission status: Runtime Error

Solution code:
n: int = input()
s: str = input()
assert n == len(s)

maxlen = 0
for offset range(1, n):
  t = s[offset:]
  d = 0
  for (a, b) in zip(s, t):
    if a == b:
      d += 1
    else:
      d = 0
    if maxlen < d and d <= offset:  # (d > offset) のとき共通部分文字列が重なってる
      maxlen = d

print(maxlen)

Submission status: Runtime Error

Solution code:
import sys
import bisect
import numpy as np


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

l = []
for a in AS.split():
    bisect.insort_left(l, int(a))

for i in range(M):
    n = l.pop()
    bisect.insort_left(l, n // 2)


print(sum(l))

Submission status: Runtime Error

Solution code:
n,s=open(0);a=i=j=0
while j<int(n):
 if s[l:j]in s[j:]:a=max(a,j-i);j+=1
 else:i+=1
print(a)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()

ans = 0

# 2文字以上
for i in range(0, N - 3):
    for j in range(i + 2, N):
        if S[i:i + 1] == S[j:j + 1]:
            length, max_length = 2, j
            i, j = i + 1, j + 1
            while True:
                if i + 1 == max_length or j + 1 == N:
                    break
                else:
                    print(i, j)
                    i, j = i + 1, j + 1
                    print(i, j)
                    if S[i] == S[j]:
                        length += 1
                    else:
                        break
            if length > ans:
                ans = length

# 1文字
if ans == 0:
    for i in range(N - 1):
        s = S[i]
        if s in S[i + 1:]:
            ans = 1

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()

ans = 0

# 2文字以上
for i in range(0, N - 3):
    for j in range(i + 2, N):
        if S[i:i + 1] == S[j:j + 1]:
            i, j = i + 1, j + 1
            length, max_length = 2, j
            while True:
                if i + 1 == max_length or j + 1 == N:
                    break
                else:
                    i, j = i + 1, j + 1
                    if S[i] == S[j]:
                        length += 1
                    else:
                        break
            if length > ans:
                ans = length

# 1文字
if ans == 0:
    for i in range(N - 1):
        s = S[i]
        if s in S[i + 1:]:
            ans = 1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np

N = int(input())
S = np.array([ord(x) for x in input().rstrip()],dtype=np.int64)

MOD = 10**9 + 7
base = 1234

def cumprod(arr):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

x = np.full(N,base,np.int64); x[0] = 1
power = cumprod(x)
x = np.full(N,pow(base,MOD-2,MOD),np.int64); x[0] = 1
power_inv = cumprod(x)

# rolling hash
H = S * power % MOD
Hcum = H.cumsum() % MOD

def test(n):
    # 長さnの文字列で2回現れるものが存在する
    x = (Hcum[n-1:] - Hcum[:N-n+1] + H[:N-n+1]) % MOD
    x *= power_inv[:N-n+1]
    x %= MOD
    # 距離n以上離れたところで連番が存在しないといけない
    ind = x.argsort(kind='stable')
    x = x[ind]
    is_left_end = np.zeros(N-n+1,np.bool)
    is_left_end[1:] = (x[1:] != x[:-1])
    is_left_end[0] = True
    is_right_end = np.zeros(N-n+1,np.bool)
    is_right_end[:-1] = (x[:-1] != x[1:])
    is_right_end[-1] = True
    gap = ind[is_right_end] - ind[is_left_end]
    return gap.max() >= n

left = 0 # 存在
right = N+1 # 非存在
while left + 1 < right:
    mid = (left + right)//2
    if test(mid):
        left = mid
    else:
        right = mid

answer = left
print(answer)

Submission status: Runtime Error

Solution code:
N = int(input())
S = str(input())
dp = [[0 for i in range(N)] for i in range(N+1)]

for i in reversed(range(N)):
    for j in reversed(range(N)):
        if S[i] == S[j]:
            dp[i][j] = dp[i+1][j+1] + 1
ans = 0
for i in range(N):
    for j in range(N):
        tmp = dp[i][j]
        if j >= i:
            tmp = min(j-i,tmp)
        ans = max(tmp,ans)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = str(input())
dp = [[0] for i in range(N+1)]

for i in reversed(range(N)):
    for j in reversed(range(N)):
        if S[i] == S[j]:
            dp[i][j] = dp[i+1][j+1] + 1
ans = 0
for i in range(N):
    for j in range(N):
        tmp = dp[i][j]
        if j >= i:
            tmp = min(j-i,tmp)
        ans = max(tmp,ans)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()

def solver(_range1, _range2):
    if _range1[1] - _range1[0] > _range2[1] - _range2[0]:
        return 0
    if _range1[1] > _range2[0]:
        return solver(_range1, (_range2[0] + 1, min(_range2[1] + 1, n)))
    if s[slice(*_range1)] in s[slice(*_range2)]:
        return 1 + solver((_range1[0], _range1[1] + 1),  _range2)
    else:
        return solver((_range1[0] + 1, _range1[1] + 1), _range2)

print(solver((0,1), (1, n)))

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
a,i,j=0
while j<N:
  t=S[i:j]
  if t in S[j:]:
    a=max(a,len(t))
    j+=1
  else:
    i+=1
print(a)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

using namespace std;

int main() {
    int N;
    int a;
    std::string S;
    cin >> N;
    cin >> S;
    int m = 0;
    for (int l = 0; l < N-1; l++) {
        char c = S[l];
        int r = l+m+1;
        while (r < N && S[r] != c) r++;
        if (r == N) break;
        int k = 1;
        while (l+k<N && l+k<r && r+k<N && S[l+k] == S[r+k]) {
            k++;
        }
        m = max(k, m);
    }
    cout << m << endl;
}

Submission status: Runtime Error

Solution code:
s = input().split()[1]
import re

print(max(map(lambda x: len(x), re.findall(r"(.*).*\1", a))))

Submission status: Runtime Error

Solution code:
n = int(input())
s =list(map(int,input().split()))

s.sort()

p = 0
for e in s:
    p = p^e

res = 0

q = 0
for e in s:
    q = q^e
    res = max(res,(p^q)+q)


q = 0
for i in range(n):
    q = q^s[n-1-i]
    res = max(res,(p^q)+q)
print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
f1 = False
t = dict()
for z in range(n):
    t[s[z]] = t.get(s[z],0) + 1
    if t[s[z]] >= 2:
        f1 = True
        break

for i in range(n//2,1,-1):
    #for j in range(n-2*i+1):
    w = set()
    for j in range(n-2*i,-1,-1):
        #for k in range(j+i,n-i+1):
        #for k in range(n-i,j+i-1,-1):
        w.add(s[j+i:j+2*i])
        print(w)
        if s[j:j+i] in w:
            print(i)
            exit(0)    
if f1:
    print(1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
# coding: utf-8


def main():
    N = int(input())
    S = input()

    mat = [[0 for _ in range(N)] for _ in range(N)]

    for i in range(1, N):
        for j in range(i):
            if S[i] == S[j]:
                mat[i][j] = 1

    ans = 0
    for i in range(1, N):
        for j in range(i):
            if mat[i][j] == 1:
                ans_cond = 1
                k = 1
                while k < N:
                    if mat[i+k][j+k] == 1:
                        ans_cond += 1
                        k += 1
                    else:
                        break
                if ans < ans_cond:
                    ans = ans_cond

    return ans


if __name__ == "__main__":
    print(main())

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()

ans = 0

# 3文字以上
for i in range(0, N - 5):
    for j in range(i + 3, N):
        if S[i:i + 3] == S[j:j + 3]:
            i += 2
            j += 2
            max_length = j
            length = 3
            while True:
                if i + 1 == max_length or j + 1 == N:
                    break
                else:
                    i += 1
                    j += 1
                if S[i] == S[j]:
                    length += 1
                else:
                    break
            if length > ans:
                ans = length

# 2文字
if ans == 0:
    for i in range(0, N - 1):
        s = S[i:i + 2]
        if s in S[i + 2:]:
            ans = 2

# 1文字
if ans == 0:
    for i in range(N):
        s = S[i]
        if s in S[i + 1:]:
            ans = 1

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
ma=0
for i in range(n//2):
    x=[]
    y=[]
    for j in range(n-i):
        s2=s[j:j+i+1]
        if s2 not in x:
            x.append(s2)
            y.append(j)
        elif y[x.find(s2)]+i<j:
            ma=i+1
print(ma)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()

tmp = n // 2
if s[0:tmp + 1] == s[tmp + 1:]:
    return tmp

res = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        tt = res
        mx = min(j - i, n - j)
        if tt >= mx:
            continue
        while tt <= mx:
            tt += 1
            if s[i:i + tt] != s[j:j + tt]:
                break
        _res = tt - 1
        res = max(res, _res)

print(res)

Submission status: Runtime Error

Solution code:
# coding: utf-8


def main():
    N = int(input())
    S = input()

    mat = [[0 for _ in range(N)] for _ in range(N)]

    for i in range(1, N):
        for j in range(i):
            if S[i] == S[j]:
                mat[i][j] = 1

    ans = 0
    for i in range(1, N):
        for j in range(i):
            if mat[i][j] == 1:
                ans_cond = 1
                k = 1
                while True:
                    if mat[i+k][j+k] == 1:
                        ans_cond += 1
                        k += 1
                    else:
                        break
                if ans < ans_cond:
                    ans = ans_cond

    return ans


if __name__ == "__main__":
    print(main())

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
m = n//2 #答えのMAX値
#始まりを絞ろう
alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
for i in range(26):
  if s.count(alphabet[i])<=1:
    alphabet.remove(alphabet[i])
    
#ngramを作る
def n_gram(target, n):
  return [ target[idx:idx + n] for idx in range(len(target) - n + 1)]
 
yet = True
ans = 0
 
for l in reversed(range(1,m+1)): #答えを上から探っていく
  if not yet:
    break
  else:
    for j in range(n-2*l+1):
      if not yet:
        break
      elif alphabet.count(s[j])<=1:
        ans = ans
      else:
        left = s[j:j+l] #最初の文字列
        target = s[j+l:]
        ngr = n_gram(target,l)
        if ngr.count(left)>0:
          ans = l
          yet = False
if yet:
  print(0)
else:
  print(ans)

Submission status: Runtime Error

Solution code:
# import math
# import numpy as np

# n, m = 4,4
# a_list = np.array([1,9,3,5])
n = int(input())
s = input()

ct = 0
ct_max = 0
for i in range(1,n):
    if(ct_max >= n-i)
    
    forward = s[:n-i]
    back = s[i:]
    for j in range(n-i):
        f = forward[j]
        b = back[j]
        if(f==b):
            ct += 1
            if(ct_max < ct):
                ct_max = ct*1
            if(ct_max >= i):
                break()
        else:
            ct = 0

print(ct_max)

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs , heapq as hq
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k][0] ==  x:
        return True
    else:
        return False

def search(a,n):
    se = dict()
    for i in range(0,len(a)-n+1):
        kkk = a[i:i+n]
        kkk = "".join(kkk)
        if kkk in se:
            if se[kkk][j] <= i-n:
                    return True
        else:
            se[kkk] = i
    return False


n = onem()

s = list(input())
l = 0
r = n//2
while True:
    mid = -(-(l+r)//2)
    ans = search(s,mid)
    if l == mid:
        break
    if ans:
        l = mid
    else:
        r = mid-1
print(l)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
flag = True
if n%2 == 0:
  for i in range(n-2*(n//2-i)-1):
    for j in range(n//2 - i):
      if s[n//2-i+j:].count(s[j:n//2-i+j]) >=1:
        print(n//2-i+1)
        flag = False
        break
    else:
      continue
    break
else:
  for i in range(n//2):
    for j in range(n-2*(n//2-i)-1):
      if s[n//2-i+j:].count(s[j:n//2-i+j]) >=1:
        print(n//2-i+1)
        flag = False
        break
    else:
      continue
    break
if flag:
  print(0)

Submission status: Runtime Error

Solution code:
def main():
  n, s = raw_input(), raw_input()
  subs = [s[i: j] for i in range(len(s)) for j in range(i + 1, len(s) + 1)] 
  _max = 0
  for x in subs:
    val = s.count(x)
    if val >= 2 and len(x) > _max:
      _max = len(x)
  print(_max)

main()

Submission status: Runtime Error

Solution code:
_ = input()
s = input()
import re

print(max(map(lambda x: len(x), re.findall(r"(.*).*\1", a))))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
m = n//2 #答えのMAX値
#始まりを絞ろう
alphabet = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
for i in range(26):
  if s.count(alphabet[i])<=1:
    alphabet.remove(alphabet[i])
    
#ngramを作る
def n_gram(target, n):
  return [ target[idx:idx + n] for idx in range(len(target) - n + 1)]
 
yet = True
ans = 0
 
for l in reversed(range(1,m+1)): #答えを上から探っていく
  if not yet:
    break
  else:
    for j in range(n-2*l+1):
      if not yet:
        break
      elif alphabet.count(s[j])<=1:
        ans = ans
      else:
        left = s[j:j+l] #最初の文字列
        target = s[j+l:]
        ngr = n_gram(target,l)
        if ngr.count(left)>0:
          ans = l
          yet = False

if yet:
  print(0)
else:
  print(ans)

Submission status: Runtime Error

Solution code:
from itertools import count

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

abc = 'abcdefghijklmnopqrstuvwxyz'

T = ['']

flag = True
for i in count():
    T2 = T.copy()
    T.clear()
    for t in T2:
        for c in abc:
            if S.count(t + c) > 1:
                T.append(t + c)
                flag = False
    if flag:
        print(i)
        break
    flag = True

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
flag = True
if n%2 == 0:
	for i in range(n-2*(n//2-i)-1):
		for j in range(n//2 - i):
			if s[n//2-i+j:].count(s[j:n//2-i+j]) >=1:
        		print(n//2-i+1)
        		flag = False
        		break
    	else:
      		continue
    	break
else:
	for i in range(n//2):
		for j in range(n-2*(n//2-i)-1):
			if s[n//2-i+j:].count(s[j:n//2-i+j]) >=1:
        	print(n//2-i+1)
        	flag = False
        	break
    else:
      	continue
    break
if flag:
	print(0)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()

ans = 0
for j in range(ans + 1, n):
    for i in range(j + ans):
        if (s[i:i+ans+1] == s[j:j+ans+1]):
            while (i + ans < n) & (i + ans < j):
                if s[i+ans] == s[j+ans]:
                    ans += 1
                else:
                    break
            else:
                break
print(ans)

Submission status: Runtime Error

Solution code:
import bisect
import numpy as np

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

a.sort

for i in range(m):
  x=a.pop(n-1)/2
  insert_index = bisect.bisect_left(a,x)
  a.insert(insert_index,x)

nums = np.array(a)
print(int(sum(np.round(nums, 0))))

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque
n = int(input())
s = input()
dic = defaultdict(list)
used = [0]*(n+1)
ans = 0


def search(lst, a):
    global ans
    n_dic = defaultdict(list)
    for i in lst:
        used[i] = 1
        if used[i+1] == 0 and i < n-1:
            n_dic[s[i+1]].append(i+1)
    for n_lst in n_dic.values():
        if len(n_lst) >= 2:
            for j in range(len(n_lst)):
                for k in range(j+1, len(n_lst)):
                    n_lst = [n_lst[j], n_lst[k]]
                    search(n_lst, a+1)
    ans = max(ans, a)

for i in range(n):
    dic[s[i]].append(i)

for lst in dic.values():
    c_lst = []
    if len(lst) >= 2:
        search(lst, 1)
    else:
        used[lst[0]] = 1
print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()

def LCS(S,T):
    dp=[[0 for i in range(len(T)+1)] for j in range(len(S)+1)]
    for i in range(len(S)):
        for j in range(len(T)):
            dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j])
            if S[i]==T[j]:
                dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1)
    return dp[len(S)][len(T)]
arr=[]
for i in range(1,N-1):
    arr.append(LCS(S[0:i],S[i:N]))

print(max(arr))

Submission status: Runtime Error

Solution code:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author gaidash
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        EWhoSaysAPun solver = new EWhoSaysAPun();
        solver.solve(1, in, out);
        out.close();
    }

    static class EWhoSaysAPun {
        public void solve(int testNumber, InputReader in, OutputWriter out) {
            int n = in.nextInt();
            String s = in.nextString();

            int ret = 0;
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    if (s.charAt(i) == s.charAt(j)) {
                        int uptoI = j;
                        int len = 0;
                        while (i < uptoI && j < n && s.charAt(i) == s.charAt(j)) {
                            len++;
                            j++;
                            i++;
                        }
                        ret = Math.max(ret, len);
                        i--;
                    }
                }
            }

            out.println(ret);
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void close() {
            writer.close();
        }

        public void println(int i) {
            writer.println(i);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()

ans = 0
for j in range(n):
    for i in range(j + ans):
        if (s[i:i+ans+1] == s[j:j+ans+1]):
            while (i + ans < n) & (i + ans < j):
                if s[i+ans] == s[j+ans]:
                    ans += 1
            else:
                break
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=sys.stdin.readline
def resolve():
    n=int(input())
    s=input()
    if n==len(set(s)):
        print(0)
        return
    for w in range(2,n): # 幅数
        flag=False
        for i in range(n-w+1):
            if s.count(s[i:i+w])>=2:
                flag=True
        if flag is False:
            break
    print(w-1)
resolve()

Submission status: Runtime Error

Solution code:
n = int(input())
s = [ord(i) - 97 for i in input()]

flag = [list()] * 26
count = 0
max_count = 0
for i in range(1, n):
    now = s[i]
    if now in flag[i]:
        count += 1
    else:
        flag[i].append(s[i])
        max_count = max(max_count, count)
        count = 0

max_count = max(max_count, count)
print(max_count)

Submission status: Runtime Error

Solution code:
import sys, bisect

def main():
    _,M,*A = map(int, open(0).read().split())

    A.sort()
    for _ in range(M):
        a = int(A.pop() / 2)
        bisect.insort_left(A,a)

    print(sum(A))

if __name__=='__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
n, m = (int(i) for i in input().split())
a = [int(i) for i in input().split()]

max_value = max(a)
l = math.ceil(math.log2(max_value))

b = [[] for i in range (l)]

for i in a:
    index = math.floor(math.log2(i))
    b[index].append(i)

ticket = m
p = l-1
while ticket > 0 and p >= 0:
    max_value = max(b[p])
    b[p].remove(max_value)
    if p > 0:
        b[p-1].append(max_value // 2)
    ticket = ticket - 1
    while len(b[p]) == 0 and p >= 0:
        p = p-1


sum_value = 0
for i in range(l):
    sum_value += sum(b[i])

print(sum_value)