Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

n = int(input())
s = input()
sum = 0
for i in range(n):
    if s[i] == 'R':
        for j in range(i+1,n):
            if s[j] == 'G':
                for k in range(j+1,n):
                    if s[k] == 'B' and j-i!=k-j:
                        sum += 1
            if s[j] == 'B': 
                for k in range(j+1,n):
                    if s[k] == 'G' and j-i!=k-j:
                        sum += 1
for i in range(n):
    if s[i] == 'G':
        for j in range(i+1,n):
            if s[j] == 'R':
                for k in range(j+1,n):
                    if s[k] == 'B' and j-i!=k-j:
                        sum += 1
            if s[j] == 'B':
                for k in range(j+1,n):
                    if s[k] == 'R' and j-i!=k-j:
                        sum += 1
for i in range(n):
    if s[i] == 'B':
        for j in range(i+1,n):
            if s[j] == 'R':
                for k in range(j+1,n):
                    if s[k] == 'G' and j-i!=k-j:
                        sum += 1
            if s[j] == 'G': 
                for k in range(j+1,n):
                    if s[k] == 'R' and j-i!=k-j:
                        sum += 1
print(sum)

### FIXED CODE ###

n = int(input())
s = input()
r = s.count('R')
g = s.count('G')
b = s.count('B')

cnt = r*g*b

for i in range(n):
    j = 1
    while i+2*j < n:
        if s[i] != s[i+j] and s[i+j] != s[i+2*j] and s[i+2*j] != s[i]:
            cnt -= 1
            j += 1
        else:
            j += 1
print(cnt)
### BUGGY CODE ###

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

outs = [[] for i in range(N)]
ins = [0] * N


for _ in range(M):
    x, y = (int(i) for i in input().split())
    x -= 1
    y -= 1
    outs[x].append(y)
    ins[y] += 1

q = []
for i in range(N):
    if ins[i] == 0:
        q.append(i)

result = []
while q != []:
    qn = q.pop(0)
    result.append(qn)
    for v in outs[qn]:
        ins[v] -= 1
        if ins[v] == 0:
            q.append(v)


last = [0] * N
# last[i] = iが最後だったときに最長のパス
for sq in result:
    for v in outs[sq]:
        last[v] = max(last[v], last[sq] + 1)

print(max(last))


     



### FIXED CODE ###

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

outs = [[] for i in range(N)]
ins = [0] * N


for _ in range(M):
    x, y = (int(i) for i in input().split())
    x -= 1
    y -= 1
    outs[x].append(y)
    ins[y] += 1

q = []
for i in range(N):
    if ins[i] == 0:
        q.append(i)

result = []
while q != []:
    qn = q.pop()
    result.append(qn)
    for v in outs[qn]:
        ins[v] -= 1
        if ins[v] == 0:
            q.append(v)


last = [0] * N
# last[i] = iが最後だったときに最長のパス
for sq in result:
    for v in outs[sq]:
        last[v] = max(last[v], last[sq] + 1)

print(max(last))
### BUGGY CODE ###

s = input()
t = input()
dp = []
for i in range(len(s)+1):
  L = ['']*(len(t)+1)
  dp.append(L)
for i in range(len(s)):
  for j in range(len(t)):
    if s[i] == t[j]:
      dp[i+1][j+1] = dp[i][j]+s[i]
    else:
      if len(dp[i][j+1]) >= len(dp[i+1][j]):
        dp[i+1][j+1] = dp[i][j+1]
      else:
        dp[i+1][j+1] = dp[i+1][j]
print(dp[len(s)][len(t)])

### FIXED CODE ###

s = input()
t = input()
dp = []
for i in range(len(s)+1):
  L = [0]*(len(t)+1)
  dp.append(L)
for i in range(len(s)):
  for j in range(len(t)):
    if s[i] == t[j]:
      dp[i+1][j+1] = dp[i][j]+1
    else:
      dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1])
n = len(s)
m = len(t)
ans = ''
while n > 0 and m > 0:
  if dp[n][m] == dp[n-1][m]:
    n -= 1
  elif dp[n][m] == dp[n][m-1]:
    m -= 1
  else:
    n -= 1
    m -= 1
    ans = s[n]+ans
print(ans)
### BUGGY CODE ###

import sys
def sol():
  input=sys.stdin.readline
  H, W = map(int, input().split())
  black = []
  white = []
 
  for i in range(H):
    a = input()
    for j in range(W):
      if (a[j] == '#'):
        black.append([i, j])
      else:
        white.append([i, j])
  if (len(white) == 0):
    print(0)
    exit()
  ans = 0
  for w in white:
    m = 10 ** 6
    for b in black:
      m = min(m, abs(b[0] - w[0]) + abs(b[1] - w[1]))
      if (m == 1):
        pass
    ans = max(ans, m)
  print(ans)

if __name__ =="__main__":
  sol()

### FIXED CODE ###

def sol():
    h,w=map(int,input().split())
    l=[]
    d=set()
    for i in range(h):
        s=list(input())
        for j in range(w):
            if s[j]=="#": d.add((i,j))
        l.append(s)
    ans=-1
    for i in range(2000):
        if len(d)==0: break
        ans+=1
        t=set()
        for j in d:
            if 0<=j[0]+1<h and l[j[0]+1][j[1]]==".":
                l[j[0]+1][j[1]]="#"
                t.add((j[0]+1,j[1]))
            if 0<=j[0]-1<h and l[j[0]-1][j[1]]==".":
                l[j[0]-1][j[1]]="#"
                t.add((j[0]-1,j[1]))
            if 0<=j[1]+1<w and l[j[0]][j[1]+1]==".":
                l[j[0]][j[1]+1]="#"
                t.add((j[0],j[1]+1))
            if 0<=j[1]-1<w and l[j[0]][j[1]-1]==".":
                l[j[0]][j[1]-1]="#"
                t.add((j[0],j[1]-1))
        d=t
    print(ans)

if __name__ == "__main__":
    sol()
### BUGGY CODE ###

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

max_ans = M // N

for a in reversed(range(1, max_ans + 1)):
    mod = M % a
    if mod == 0:
        print(a)
        exit()

### FIXED CODE ###

from itertools import product

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

max_ans = M // N

ans = 1
tmp = 2
tmpM = M
max_prime = int(M ** 0.5)
all_list = []
i = -1
k = 0
div = 2
while tmpM != 1:
    if tmpM % div == 0:
        tmpM = tmpM // div
        if k == 0:
            i += 1
            all_list.append(set())
            all_list[i].add(div ** k)
            k += 1
        all_list[i].add(div ** k)
        k += 1

    else:
        k = 0
        if div == 2:
            div = 3
        elif div > max_prime:
            div = tmpM
        else:
            div += 2

if all_list == []:
    print(1)
    exit()

# print(all_list)
ans = 1

for div_tuple in product(*all_list):
    tmp = 1
    for k in div_tuple:
        tmp *= k
        if tmp > max_ans:
            break
    else:
        ans = max(ans, tmp)

print(ans)
### BUGGY CODE ###

from collections import defaultdict, deque, Counter


def main():
    n = int(input())
    A = list(map(int, input().split()))
    dd = defaultdict(int)
    ans = []
    for i in A:
        dd[i] += 1
    for i in range(n):
        ng = A[i]
        dd[ng] -= 1
        cnt = 0
        for key, val in dd.items():
            cnt += val * (val-1) // 2
        ans.append(cnt)
        dd[ng] += 1
    print(*ans, sep='\n')



if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import Counter


n = int(input())
A = list(map(int, input().split()))
c = Counter(A)
cc = c.most_common()
ans = 0
for i in range(len(cc)):
    n = cc[i][1]
    if n < 2:
        break
    ans += n * (n-1) // 2

for i in A:
    cnt = c[i]
    if cnt == 1:
        print(ans)
    else:
        ans_ = ans + (cnt-1)*(cnt-2) // 2 - cnt*(cnt-1)//2
        print(ans_)
### BUGGY CODE ###

a, b, c = map(int, input().split())
# print(a ** 0.5, b ** 0.5, c ** 0.5)
digit = 10000
right = (c - a - b) * (10 ** digit)
ab = a * b * (10 ** (digit * 2))
sqrt_ab = 0
ans = False
for i in range(10 ** 5):
    # print(i, i * i * (10 ** (digit * 2)), ab, (i - 1) * (10 ** digit))
    if i * i * (10 ** (digit * 2)) > ab:
        sqrt_ab += (i - 1) * (10 ** digit)
        break
# print(sqrt_ab)
for i in range(1, digit):
    # if int(sqrt_ab + 1) * 2 < right:
    #     ans = True
    #     break
    for j in range(1, 10):
        # print(i, j, sqrt_ab + j * (10 ** (digit - i)), )
        if (sqrt_ab + j * (10 ** (digit - i))) ** 2 > ab:
            sqrt_ab += (j - 1) * (10 ** (digit - i))
            break
        elif j == 9:
            sqrt_ab += j * (10 ** (digit - i))
            break
if 2 * sqrt_ab < right:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

a, b, c = map(int, input().split())
if c - a - b > 0 and 4 * a * b < ((c - a - b) ** 2):
    print("Yes")
else:
    print("No")
### BUGGY CODE ###

import numpy as np
N = int(input())
A = list(map(int, (input().split())))
A.sort()
cnt = np.zeros(10**6+10, dtype=np.int32)
for i in range(N):
    cnt[A[i]] += 1
for i in range(N-1):
    cnt[A[i]*2::A[i]] += 2
ans = np.count_nonzero(cnt==1)
print(ans)

### FIXED CODE ###

import numpy as np
import sys
input = sys.stdin.buffer.readline

N = int(input())
A = list(map(int, (input().split())))
A.sort()
cnt = np.zeros(10**6+10, dtype=np.int32)
for x in A:
    if cnt[x] != 0:
        cnt[x] = 2
        continue
    cnt[x::x] += 1
ans = 0
for x in A:
    if cnt[x] == 1:
        ans += 1
print(ans)
### BUGGY CODE ###

S = list(input())
Q = int(input())
rev = 0
for i in range(Q):
  query = input()
  if query == "1": rev = 1 - rev 
  elif int(query[2]) + rev == 2: S.append(query[4])
  else:
    S.reverse()
    S.append(query[4])
    rev = 1 - rev
if rev == 1: S.reverse()  
print("".join(S))    

### FIXED CODE ###

from collections import deque
S = deque(input())
Q = int(input())
rev = 0
for i in range(Q):
  query = input()
  if query == "1": rev = 1 - rev 
  elif int(query[2]) + rev == 2: S.append(query[4])
  else:S.appendleft(query[4])
if rev == 1: S.reverse()  
print("".join(S))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)
n,m = map(int,input().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
    a,b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
ans = 0
def dfs(node):
    todo = set()
    for nextnode in graph[node]:
        if nextnode not in seen:
            seen.add(nextnode)
            todo.add(nextnode)
    for nextnode in todo:
        dfs(nextnode)
for i in range(1,n+1):
    seen = set()
    seen.add(i)
    dfs(i)
    ans = max(ans,len(seen))
print(ans)

### FIXED CODE ###

from collections import deque

n,m = map(int,input().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
    a,b = map(int,input().split())
    graph[a].append(b)
    graph[b].append(a)
ans = 1

def dfs(node):
    while len(todo) > 0:
        node = todo.pop()
        if tf[node] == 0:
            tf[node] = 1
            seen.add(node)
            for nextnode in graph[node]:
                todo.append(nextnode)

todo = deque()
done = set()
tf = [0 for _ in range(n+1)]
for i in range(1,n+1):
    if i in done:
        continue
    else:
        todo.append(i)
        seen = set()
        dfs(i)
        #print(i,seen)
        ans = max(ans,len(seen))
        done |= seen
print(ans)
### BUGGY CODE ###

n = int(input())
a = [list(map(int, input().split())) for _ in range(n)]
mod = 10 ** 9 + 7

dp = [0] * (1 << n)
dp[0] = 1

for bit in range(1,1 << n):
    i = bin(bit).count('1')
    for j in range(n):
        if (bit >> j) & 1 and a[i - 1][j] :
            dp[bit] += dp[bit ^ (1 << j)]
        dp[bit] %= mod
print(dp[-1])


### FIXED CODE ###

mod = 10**9+7
n = int(input())
a = [list(map(int, input().split())) for i in range(n)]
dp = [0]*(1 << n)
dp[0] = 1
for i in range(1, 1 << n):
    k = bin(i).count("1")
    for j in range(n):
        if i >> j & 1 and a[k-1][j]:
            dp[i] += dp[i ^ (1 << j)]
    dp[i] %= mod
print(dp[(1 << n)-1])
### BUGGY CODE ###

N = int(input())
S = input()
ss = [0]*N

ss[0] += S[1:].count('E')
ss[N-1] += S[:N-1].count('W')

for i in range(1, N-1):
  ss[i] += S[:i].count('W')
  ss[i] += S[i+1:].count('E')
  
print(max(min(ss), 1))

### FIXED CODE ###

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

ss1 = S[1:].count('E')
ss = ss1

for i in range(1, N):
  ss1 = ss1 + (S[i-1]=='W') - (S[i]=='E')
  if ss1 < ss:
    ss = ss1
  
print(ss)
### BUGGY CODE ###

"""
~~ Author : Bhaskar
~~ Dated : 31~05~2020
"""

import sys
from bisect import *
from math import floor,sqrt,ceil,factorial as F,gcd,pi
from itertools import chain,combinations,permutations,accumulate
from collections import Counter,defaultdict,OrderedDict,deque
from array import array
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)-1
mod = 1000000007
lcm = lambda a,b : (a*b)//gcd(a,b)
setbit = lambda x : bin(x)[2:].count("1")

def solve():
    n = int(sys.stdin.readline())
    a = list(map(int,sys.stdin.readline().split()))
    ans=  1
    flag = False
    for i in a:
        ans *= i
    print(ans if ans<=10**18 else -1)

if __name__ == "__main__":
    solve()


### FIXED CODE ###

"""
~~ Author : Bhaskar
~~ Dated : 31~05~2020
"""

import sys
from bisect import *
from math import floor,sqrt,ceil,factorial as F,gcd,pi
from itertools import chain,combinations,permutations,accumulate
from collections import Counter,defaultdict,OrderedDict,deque
from array import array
INT_MAX = sys.maxsize
INT_MIN = -(sys.maxsize)-1
mod = 10**18
lcm = lambda a,b : (a*b)//gcd(a,b)
setbit = lambda x : bin(x)[2:].count("1")

def solve():
    n = int(sys.stdin.readline())
    a = list(map(int,sys.stdin.readline().split()))
    ans =  1
    flag = False
    for i in a:
        ans *= i
        # print(ans)
        if ans > mod:
            flag = True
            break
    if 0 in a:
        print(0)
    else:
        if not flag:
            print(ans)
        else:
            print(-1)

if __name__ == "__main__":
    solve()
### BUGGY CODE ###

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

n = int(raw_input())

power = 1

for i in range(n):
    power *= (i+1)

print(power%(10**9 + 7))

### FIXED CODE ###

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

n = int(raw_input())

power = 1
m = 10**9 + 7

for i in range(n):
    power *= (i+1)
    power %= m

print(power)
### BUGGY CODE ###

N, X, Y = map(int, input().split())

dis = []
for i in range(1, N):
    for j in range(i + 1, N + 1):
        d = min(abs(i - j),
                abs(i - X) + 1 + abs(j - Y),
                abs(i - Y) + 1 + abs(j - X))
        dis.append(d)

for k in range(1, N):
    print(dis.count(k))

### FIXED CODE ###

N, X, Y = map(int, input().split())

dis = [0 for _ in range(N)]
for i in range(1, N):
    for j in range(i + 1, N + 1):
        d = min(abs(i - j),
                abs(i - X) + 1 + abs(j - Y),
                abs(i - Y) + 1 + abs(j - X))
        dis[d] += 1

for k in range(1, N):
    print(dis[k])
### BUGGY CODE ###

from collections import deque

def main():
  n, m = map(int, input().split())
  graph = [[] for _ in range(n)]

  for i in range(m):
    a, b = map(int, input().split())
    if b-1 not in graph[a-1]:
      graph[a-1].append(b-1)
      graph[b-1].append(a-1)
  
  unsearched = list(range(n))
  result = 0
  border = n//2
  while len(unsearched) > 0:
    if result > border:
      break
    s = unsearched[0]
    tmp, cnt = dfs(graph, unsearched, s)
    if cnt > result:
      result = cnt
      
  print(result)

def dfs(graph, unsearched, s):
  stack = deque([s])
  searched = [s]
  while len(stack):
    cur = stack.pop()
    unsearched.remove(cur)
    for i in graph[cur]:
      if i not in searched:
        stack.append(i)
        searched += [i]
  return searched, len(searched)

main()

### FIXED CODE ###

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

def main():
  n, m = map(int, input().split())
  uf = UnionFind(n)

  for i in range(m):
    a, b = map(int, input().split())
    a -= 1; b-= 1;
    uf.union(a, b)
  
  ans = 0
  for i in range(n):
    ans = max(ans, uf.size(i))
      
  print(ans)
  
main()
### BUGGY CODE ###

from collections import deque
h, w, k = map(int, input().split())
y1, x1, y2, x2 = map(int, input().split())
c = []
for _ in range(h):
    c.append(input())
q = deque([(x1-1, y1-1)])
dxdy = [(-1,0), (0,-1), (0,1), (1,0)]
dist = [[1000000]*w for _ in range(h)]
dist[y1-1][x1-1] = 0
while q:
    x, y = q.popleft()
    for ddx, ddy in dxdy:
        for i in range(1, k+1):
            dx = ddx*i
            dy = ddy*i
            if not (0<=x+dx<w and 0<=y+dy<h):
                break
            if c[y+dy][x+dx]=='@' or dist[y+dy][x+dx]<=dist[y][x]:
                break
            if x+dx==x2-1 and y+dy==y2-1:
                print(dist[y][x]+1)
                exit(0)
            dist[y+dy][x+dx] = dist[y][x]+1
            q.append((x+dx, y+dy))
print(-1)

### FIXED CODE ###

from collections import deque
h, w, k = map(int, input().split())
y1, x1, y2, x2 = map(int, input().split())
c = []
for _ in range(h):
    c.append(input())
q = deque([(x1-1, y1-1)])
dxdy = [(-1,0), (0,-1), (0,1), (1,0)]
dist = [[1000000]*w for _ in range(h)]
dist[y1-1][x1-1] = 0
while q:
    x, y = q.popleft()
    for ddx, ddy in dxdy:
        for i in range(1, k+1):
            dx = ddx*i
            dy = ddy*i
            if not (0<=x+dx<w and 0<=y+dy<h):
                break
            if c[y+dy][x+dx]=='@' or dist[y+dy][x+dx]<=dist[y][x]:
                break
            if x+dx==x2-1 and y+dy==y2-1:
                print(dist[y][x]+1)
                exit(0)
            if dist[y+dy][x+dx]>dist[y][x]+1:
                dist[y+dy][x+dx] = dist[y][x]+1
                q.append((x+dx, y+dy))
print(-1)
### BUGGY CODE ###

def getinputdata():

    array_result = []    
    data = input()
    array_result.append(data.split(" "))

    flg = True

    try:
        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = True

            else:

                flg = False
    finally:

        return array_result

arr_data = getinputdata()

x=arr_data[0][0]


while x.count("ST")>0:
    
   x=x.replace("ST","")

print(len(x))

### FIXED CODE ###

from collections import deque
def getinputdata():

    array_result = []    
    data = input()
    array_result.append(data.split(" "))

    flg = True

    try:
        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = True

            else:

                flg = False
    finally:

        return array_result

arr_data = getinputdata()

x=arr_data[0][0]

b = deque([])

for v in x:
    
    if v=="S":
        
        b.appendleft(v)
        
    elif v=="T" and (len(b)==0 or b[0]=="T"):
        
        b.appendleft(v)
        
    else:
            
        b.popleft()
            
print(len(b))
### BUGGY CODE ###

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

c = 0
for i in range(N):
    for j in range(N):
        if i < j:
            if A[i] == j+1 and A[j] == i+1:
                c += 1
print(c)
    

### FIXED CODE ###

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

c = 0
for i in range(N):
    if A[A[i]-1] == i+1:
        c += 1
print(int(c//2))
### BUGGY CODE ###

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

divs = []
maxA = sum(A)
for i in range(1, maxA + 1):
    if maxA % i == 0:
        divs.append(i)
        divs.append(maxA // i)

divs.sort(reverse=True)

for d in divs:
    rest = [a % d for a in A]
    rest.sort(reverse=True)
    restSum = sum(rest) // d
    cnt = 0
    for i in range(restSum):
        cnt += d - rest[i]
    if cnt <= K:
        print(d)
        exit()


### FIXED CODE ###

from heapq import heappush, heappop


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

divs = []
maxA = sum(A)
for i in range(1, int(maxA ** 0.5) + 1):
    if maxA % i == 0:
        heappush(divs, -i)
        j = maxA // i
        if i != j:
            heappush(divs, -j)

while True:
    d = -heappop(divs)
    q = []
    asum = 0
    for a in A:
        rest = a % d
        asum += rest
        heappush(q, -rest)
    cnt = 0
    for i in range(asum // d):
        cnt += d - (-heappop(q))
    if cnt <= K:
        print(d)
        exit()
### BUGGY CODE ###

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce



INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

n = I()
s = S()



left = 0
right = n//2
flag = 0
while left <= right:
    mid = (left + right) // 2
    str_len = mid
    for sousa_point in range(n - str_len + 1):
        string = s[sousa_point: sousa_point + str_len]
        if s.count(string) > 1:
            left = mid + 1
            flag = 1
            break
    else:
        right = mid - 1
        flag = False


if flag:
    print(mid)
else:
    print(mid - 1)

### FIXED CODE ###

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007

n = I()
s = S()


def z_algorithm(S):
    l = len(S)
    A = [0] * l
    i = 1; j = 0
    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


    ret = 0
    for p in range(l):
        if p >= A[p] > ret:
            ret = A[p]
    return ret


ans = 0
for i in range(n):
    ans = max(ans, z_algorithm(s[i:]))



print(ans)
### BUGGY CODE ###

# ABC138D

import sys
sys.setrecursionlimit(1000000000)

def dfs(adj,v,p,px,x,ct):
    x+=px[v]
    for nv in adj[v]:
        if nv==p:
            continue
        else:
            dfs(adj,nv,v,px,x,ct)
    ct[v]+=x

N,Q=map(int,input().split())
adj=[[] for i in range(N)]
for i in range(N-1):
    a,b=map(int,input().split())
    adj[a-1]+=[b-1]
    adj[b-1]+=[a-1]
px=[0]*N
for i in range(Q):
    p,x=map(int,input().split())
    px[p-1]+=x
    
ct=[0]*N
dfs(adj,0,-1,px,0,ct)


print(ct[0], end="")
for i in range(1,N):
    print(" {}".format(ct[i]),end="")
print("")

### FIXED CODE ###

# ABC138D

import sys
input=sys.stdin.readline
sys.setrecursionlimit(1000000000)

def dfs(adj,v,p,px,x,ct):
    x+=px[v]
    for nv in adj[v]:
        if nv==p:
            continue
        else:
            dfs(adj,nv,v,px,x,ct)
    ct[v]+=x

def main():
    N,Q=map(int,input().split())
    adj=[[] for i in range(N)]
    for i in range(N-1):
        a,b=map(int,input().split())
        adj[a-1]+=[b-1]
        adj[b-1]+=[a-1]
    px=[0]*N
    for i in range(Q):
        p,x=map(int,input().split())
        px[p-1]+=x
        
    ct=[0]*N
    dfs(adj,0,-1,px,0,ct)
    
    
    print(ct[0], end="")
    for i in range(1,N):
        print(" {}".format(ct[i]),end="")
    print("")
    
if __name__=="__main__":
    main()