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

import sys
sys.setrecursionlimit(100000)
N,Q = list(map(int,input().split()))
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

for _ in range(N-1):
    a,b=map(int,input().split())
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    px = list(map(int,input().split()))
    ans[px[0]] += px[1]

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])


### FIXED CODE ###

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

N,Q = map(int,input().split())
to = [[] for _ in range(N+1)]
ans = [0 for _ in range(N+1)]

for _ in range(N-1):
    a,b=map(int,input().split())
    to[a].append(b)
    to[b].append(a)
for _ in range(Q):
    p,x = map(int,input().split())
    ans[p] += x

def dfs(v, p):
    for u in to[v]:
        if u == p:
            continue
        ans[u] += ans[v]
        dfs(u, v)
dfs(1, -1)
print(*ans[1:])
### BUGGY CODE ###

s = input()
n = len(s)

def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x


mod = [0]*2019
mod[0] = 1
pre = 0
for i in reversed(range(n)):
    pre = (pre + int(s[i])*pow_k(10, n-i-1))%2019
    mod[pre] += 1
print(sum([m*(m-1)//2 for m in mod]))

### FIXED CODE ###

s = input()[::-1]
n = len(s)

mod = [0]*2019
pre = 0
mod[pre] = 1
x = 1
for i in range(n):
    pre += int(s[i])*x
    pre %= 2019
    mod[pre] += 1
    x *= 10
    x %=2019
print(sum([m*(m-1)//2 for m in mod]))
### BUGGY CODE ###

N,K=map(int,input().split())
H=[]
for i in range(N):
  H.append(int(input()))
H.sort()
ans=[]
for i in range(N-K+1):
  A=H[i:i+K]
  ans.append(A[K-1]-A[0])
print(min(ans))



### FIXED CODE ###

N,K=map(int,input().split())
H=[]
for i in range(N):
  H.append(int(input()))
H.sort()
print(min(H[i+K-1]-H[i] for i in range(N-K+1)))
### BUGGY CODE ###

N, M = map(int, input().split())
A = []
for i in range(N):
    a, b = map(int, input().split())
    A.append([a, b])
A = sorted(A, key = lambda x : (x[1], x[0]), reverse = True)
# print(A)
ans = 0

for i in range(M - 1, -1, -1):
    for j in range(len(A)):
        if A[j][0] <= M - i:
            ans += A[j][1]
            A.pop(j)
            break
print(ans)

### FIXED CODE ###

import heapq

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

A = [[] for _ in range(M)]
Q = []

for _ in range(N):
	day, money = map(int, input().split())
	if day <= M:
		A[day - 1].append(money)

ans = 0

for i in range(0, M):
	for money in A[i]:
		heapq.heappush(Q, -money)
	# print(Q)
	if Q:
		ans += heapq.heappop(Q)
		
print(-ans)
### BUGGY CODE ###

from collections import namedtuple
from heapq import heappush, heappop


class UnionFind:
  def __init__(self, n):
    self.par = [i for i in range(n + 1)]
    self.size = [1] * (n + 1)

  def is_same(self, a, b):
    return self.find(a) == self.find(b)

  def unite(self, a, b):
    a = self.find(a)
    b = self.find(b)  
    if self.size[a] < self.size[b]:
      a, b = b, a
    self.par[b] = a
    self.size[a] += self.size[b]

  def size_of(self, a):
    return self.size[self.find(a)]

  def find(self, a):
    if a == self.par[a]:
      return a
    self.par[a] = self.find(self.par[a])
    return self.par[a]

Edge = namedtuple('Edge', 'w s t')
X = namedtuple('X', 'x i')
Y = namedtuple('Y', 'y i')

N = int(input())

edges = []
xg = []
yg = []

for i in range(N):
  x, y = map(int, input().split())
  xg.append(X(x, i))
  yg.append(Y(y, i))

xg.sort()
yg.sort()

for i in range(N - 1):
  x1, a = xg[i]
  x2, b = xg[i + 1]
  heappush(edges, Edge(abs(x1 - x2), a, b))

for i in range(N - 1):
  y1, a = yg[i]
  y2, b = yg[i + 1]
  heappush(edges, Edge(abs(y1 - y2), a, b))

u = UnionFind(N)
cost = 0

while edges:
  if u.size_of(0) >= N:
    break

  w, s, t = heappop(edges)
  if u.is_same(s, t):
    continue
  u.unite(s, t)
  cost += w

print(cost)


### FIXED CODE ###

from collections import namedtuple


class UnionFind:
  def __init__(self, n):
    self.par = [i for i in range(n + 1)]
    self.size = [1] * (n + 1)

  def is_same(self, a, b):
    return self.find(a) == self.find(b)

  def unite(self, a, b):
    a = self.find(a)
    b = self.find(b)  
    if self.size[a] < self.size[b]:
      a, b = b, a
    self.par[b] = a
    self.size[a] += self.size[b]

  def size_of(self, a):
    return self.size[self.find(a)]

  def find(self, a):
    if a == self.par[a]:
      return a
    self.par[a] = self.find(self.par[a])
    return self.par[a]

Edge = namedtuple('Edge', 'w s t')
X = namedtuple('X', 'x i')
Y = namedtuple('Y', 'y i')

N = int(input())

edges = []
xg = []
yg = []

for i in range(N):
  x, y = map(int, input().split())
  xg.append(X(x, i))
  yg.append(Y(y, i))

xg.sort()
yg.sort()

for i in range(N - 1):
  x1, a = xg[i]
  x2, b = xg[i + 1]
  edges.append(Edge(abs(x1 - x2), a, b))

for i in range(N - 1):
  y1, a = yg[i]
  y2, b = yg[i + 1]
  edges.append(Edge(abs(y1 - y2), a, b))

edges.sort()

u = UnionFind(N)
cost = 0

for w, s, t in edges:
  if u.is_same(s, t):
    continue
  u.unite(s, t)
  cost += w

print(cost)
### BUGGY CODE ###

import math

def comb(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

num = [0] * N

for i in range(N):
    num[A[i] - 1] = num[A[i] - 1] + 1

a = [0] * N
b = [0] * N

for i in range(N):
    if num[i] >= 3:
        a[i] = comb(num[i] - 1, 2)
        b[i] = comb(num[i] - 1, 1)
    if num[i] == 2:
        a[i] = 0
        b[i] = 1

for i in range(N):
    print(sum(a) + sum(b) - b[A[i] - 1])

### FIXED CODE ###

import math

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

num = [0] * N

for i in A:
    num[i - 1] = num[i - 1] + 1

a = [0] * N
b = [0] * N

for i in range(N):
    if num[i] >= 2:
        a[i] = (num[i] - 1) * (num[i] - 2) // 2
        b[i] = num[i] - 1

sum_a = sum(a)
sum_b = sum(b)

for i in A:
    print(sum_a + sum_b - b[i - 1])
### BUGGY CODE ###

import itertools

while True:
    n, s = map(int, input().split())
    count = 0
    if n == 0:
        break
    for c in itertools.combinations([i for i in range(100)], n):
        if sum(c) == s:
            count += 1
    print(count)



### FIXED CODE ###

ans = [[0 for i in range(1001)] for j in range(11)]
ans[0][0] = 1

for i in range(101):
     for n in range(9, -1, -1):
         for s in range(1001 - i):
             ans[n + 1][s + i] += ans[n][s]

while True:
    n, s = map(int, input().split())
    if n == 0:
        break
    print(ans[n][s])
### BUGGY CODE ###

import copy
n, k = map(int, input().split())
a = list(map(int, input().split()))
memo = copy.copy(a)
for i in range(k, n+1, 1):
    for j in range(i-k, i-1, 1):
        memo[i-1] *= a[j]
for i in range(k, n, 1):
    if memo[i-1] < memo[i]:
        print("Yes")
    else:
        print("No")

### FIXED CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))
for i in range(k, n):
    if a[i-k] < a[i]:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

N = int(input())
C = dict()
i, cp, lr = 0, 0, []
for _ in range(N):
    c = int(input())
    if c != cp:
        if c in C.keys():
            lr.append([C[c], i])
        C[c] = i
        cp = c
        i += 1
length = i
lr.sort()
hist, add, i, ans = [0] * length, [0] * length, 0, 0
for l, r in lr:
    while i <= l:
        hist[i] = hist[i-1] + add[i]
        ans += add[i]
        i += 1
    add[r] += hist[l] + 1
if len(lr) != 0:
    for i in range(l+1, length):
        ans += add[i]
print((ans+1) % (10**9+7))

### FIXED CODE ###

N = int(input())
C = dict()
i, cp, hist, ans = 0, 0, [0]*N, 0
for _ in range(N):
    c = int(input())
    if c != cp:
        if c in C.keys():
            temp = hist[C[c]] + 1
            hist[i] = hist[i-1] + temp
            ans += temp
        else:
            hist[i] += hist[i-1]
        C[c], cp = i, c
        i += 1
print((ans+1) % (10**9+7))
### BUGGY CODE ###

import math

def is_prime(n):
    if n == 1:
        return False
    if not isinstance(n,int):
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

Q=int(input())
L,R=[],[]
for i in range(Q):
    l,r = list(map(int,input().split()))
    L.append(l)
    R.append(r)

list_2017_similar=[]
for i in range(1,100000):
    if is_prime(i) == True and is_prime(int((i+1)/2)) == True and i != 2:
        list_2017_similar.append(i)

for i in range(Q):
    cnt=0
    for j in list_2017_similar:
        if L[i] <= j <= R[i]:
            cnt += 1
    print(cnt)


### FIXED CODE ###

import math

def is_prime(n):
    if n == 1:
        return False
    if not isinstance(n,int):
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

Q=int(input())
L,R=[],[]
for i in range(Q):
    l,r = list(map(int,input().split()))
    L.append(l)
    R.append(r)

list_2017_similar_num=[0]
for i in range(1,100001):
    if i % 2 == 0:
        list_2017_similar_num.append(list_2017_similar_num[i-1])
    else:
        if is_prime(i) == True and is_prime(int((i+1)/2)) == True:
            list_2017_similar_num.append(list_2017_similar_num[i-1]+1)
        else:
            list_2017_similar_num.append(list_2017_similar_num[i-1])

for i in range(Q):
    print(list_2017_similar_num[R[i]] - list_2017_similar_num[L[i]-1])
### BUGGY CODE ###

import itertools
import numpy as np

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

n = int(input())

cnt = 0
i = 2
a = prime_factorize(n)
if len(a) == 1:
    a.append(1)
b = []
for i in range(1,len(a) // 2 + 2):
    b.append(list(itertools.combinations(a, i)))

m1 = 10**12
m2 = 10**12
for i in range(len(b)):
    for j in range(len(b[i])):
        c = np.prod(b[i][j])
        d = n // c
        if m1+m2 > c+d:
            m1 = c
            m2 = d

print(m1+m2-2)

### FIXED CODE ###

import sys

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors


n = int(input())
a = make_divisors(n)
#print(a)
ans = sys.maxsize
for x in a:
    y = n // x
    #print(x, y, x+y)
    ans = min(ans, x + y - 2)
print(ans)
### BUGGY CODE ###

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

ans = 'YES'
for i in range(n):
    for j in range(i+1,n):
        if a[i] == a[j]:
            ans = 'NO'



print(ans)

### FIXED CODE ###

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

b = set(a)

if len(a) == len(b):
    print('YES')
else:
    print('NO')
### BUGGY CODE ###

from sys import stdin, setrecursionlimit


def cmb(n, r, mod=10 ** 9 + 7):
    r = min(r, n - r)
    x = y = 1
    for i in range(r):
        x *= n - i
        x %= mod
        y *= i + 1
        y %= mod
    return x * pow(y, mod - 2, mod) % mod


def main():
    mod = 10 ** 9 + 7
    n, k = map(int, stdin.readline().split())
    a = list(map(int, stdin.readline().split()))
    a.sort()
    ans = 0
    for i in range(n - k + 1):
        ans -= a[i] * cmb(n - 1 - i, k - 1)
        ans %= mod
    for i in range(k - 1, n):
        ans += a[i] * cmb(i, k - 1)
        ans %= mod
    print(ans % mod)


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


### FIXED CODE ###

from sys import stdin, setrecursionlimit


def main():
    mod = 10 ** 9 + 7
    n, k = map(int, stdin.readline().split())
    a = list(map(int, stdin.readline().split()))
    a.sort()
    ans = 0

    m = n + 1

    fac = [0] * m
    finv = [0] * m
    inv = [0] * m

    def initialize_cmb(m):
        fac[0] = 1
        finv[0] = 1
        if m > 1:
            fac[1] = 1
            finv[1] = 1
            inv[1] = 1
            for i in range(2, m):
                fac[i] = fac[i-1] * i % mod
                inv[i] = mod - inv[mod % i] * (mod // i) % mod
                finv[i] = finv[i - 1] * inv[i] % mod

    def cmb(n, k, mod=10 ** 9 + 7):
        if n < k:
            return 0
        if n < 0 or k < 0:
            return 0
        return fac[n] * (finv[k] * finv[n - k] % mod) % mod

    initialize_cmb(m)

    for i in range(n - k + 1):
        ans -= a[i] * cmb(n - 1 - i, k - 1)
        ans %= mod
    for i in range(k - 1, n):
        ans += a[i] * cmb(i, k - 1)
        ans %= mod
    print(ans % mod)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
OKlist = [0]*(10**6+100)
A.sort()
for i in range(len(A)-1):
    if A[i] == A[i+1]:
        OKlist[A[i]] += 1
    else:
        for j in range(10**6+10):
            if A[i]*j > 10**6:
                break
            else:
                OKlist[A[i]*j] += 1
OKlist[max(A)] += 1
cnt = 0
for i in A:
    if OKlist[i] == 1:
        cnt += 1
        
print(cnt)

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
OKlist = [0]*(10**6+100)
A.sort()
for i in range(len(A)-1):
    if A[i] == A[i+1]:
        OKlist[A[i]] += 1
    else:
        for j in range(10**6+10):
            if A[i]*j > 10**6:
                break
            else:
                OKlist[A[i]*j] += 1
OKlist[max(A)] += 1
cnt = 0
for i in A:
    if OKlist[i] == 1:
        cnt += 1
        
print(cnt)
### BUGGY CODE ###

import numpy as np

h, w = map(int, input().split())
a = [input() for _ in range(h)]

a = np.array([[(ai[j] == "#") for j in range(w)] for ai in a])

a_arr = np.zeros((h + 2, w + 2), dtype="bool")
a_arr[1:-1, 1:-1] = a

for _ in range(h + w):
    if np.all(a_arr[1:-1, 1:-1]):
        print(_)
        break
    a_arr[1:-1, 1:-1] = (
        a_arr[1:-1, 1:-1]
        + a_arr[:-2, 1:-1]
        + a_arr[2:, 1:-1]
        + a_arr[1:-1, :-2]
        + a_arr[1:-1, 2:]
    )



### FIXED CODE ###

import numpy as np
from scipy.ndimage.morphology import distance_transform_cdt

h, w = map(int, input().split())
a = [input() for _ in range(h)]

a = np.array([[(ai[j] != "#") for j in range(w)] for ai in a])
d = distance_transform_cdt(a, metric="taxicab").max()
print(d)
### BUGGY CODE ###

import sys
import math

S = list(input())
Q = input()

for i in range(int(Q)):
  Query = input().split()
  if len(Query) == 1:
    S.reverse()
  else:
    C = Query[2]
    if int(Query[1]) == 1:
      S.insert(0,C)
    else:
      S.insert(len(S),C)

print(''.join(S))

### FIXED CODE ###

from collections import deque
import sys

S = input()
Q = int(input())
t = deque(S)
rev = False

for _ in range(Q):
  Query = input().split()
  if Query[0] == '1':
    rev = not rev
    
  else:
    F = Query[1]
    C = Query[2]
    if F == '1':
      if rev:
        t.append(C)
      else:
        t.appendleft(C)
    else:
      if rev:
        t.appendleft(C)
      else:
        t.append(C)

result = ''.join(t) 
if rev:
  print(result[::-1])
else:
  print(result)
### BUGGY CODE ###

s = input()
t = input()

alphabet = 'abcdefghijklmnopqrstuvwxyz'
alphaset = set(s)
cache = []

def search(origin, c, offset):
    if c not in alphaset:
        return None
    offset = offset % len(origin)
    o = origin[offset:] + origin
    try:
        return o.index(c)
    except ValueError:
        return None


cache.append({c2: 0 for c2 in alphabet})
for c2 in alphabet:
    offset2 = search(s, c2, 1)
    if offset2 is None:
        cache[0][c2] = None
        continue
    cache[0][c2] = offset2 + 1
for i in range(1, len(s)):
    cache.append({c2: 0 for c2 in alphabet})
    for c2 in alphabet:
        if s[i] == c2:
            offset2 = search(s, c2, i + 1)
            if offset2 is None:
                cache[i][c2] = None
                continue
            cache[i][c2] = offset2 + 1
        else:
            offset2 = cache[i-1][c2]
            if offset2 is None:
                cache[i][c2] = None
                continue
            cache[i][c2] = offset2 - 1


a = search(s, t[0], 0)
if a is None:
    print(-1)
    exit()

result = a
for i in range(1, len(t)):
    k = cache[result % len(s)][t[i]]
    if k is None:
        print(-1)
        exit()
    result += k

print(result+1)


### FIXED CODE ###

from bisect import bisect
s = input()
t = input()

alphabet = 'abcdefghijklmnopqrstuvwxyz'
char = {c: [] for c in alphabet}
for i, c in enumerate(s):
    char[c].append(i)

if len(char[t[0]]) == 0:
    print(-1)
    exit()

result = char[t[0]][0]

for c in t[1:]:
    if len(char[c]) == 0:
        print(-1)
        exit()
    idx = bisect(char[c], result % len(s))
    result = result - result % len(s)
    try:
        result += char[c][idx]
    except IndexError:
        result += len(s) + char[c][0]

print(result+1)
### BUGGY CODE ###

N = int(input())
A = [int(x) for x in input().split()]
B = []
for a in A:
    B.append(a)
    B = B[::-1]
print(*B)

### FIXED CODE ###

N = int(input())
A = [int(x) for x in input().split()]
A = [0] + A
B = []

i = N
if N%2==1:    
    for i in range(N, 0, -2):
        B.append(A[i])
    for i in range(2, N, 2):
        B.append(A[i])
else:
    for i in range(N, 1, -2):
        B.append(A[i])
    for i in range(1, N, 2):
        B.append(A[i])

print(*B)
### BUGGY CODE ###

import math

# doubling

n, k = list(map(int, input().split()))
nums = [-1] + list(map(int, input().split()))
doubling =[[0 for _ in range(n + 1)] for _ in range(62)]

for i in range(62):
	for j in range(1, n + 1):
		if i == 0:
			doubling[i][j] = nums[j]
		else:
			doubling[i][j] = doubling[i - 1][doubling[i - 1][j]]

cur = 1
for i, x in enumerate(bin(k)[2:][::-1]):
	if x == "1":
		cur = doubling[i][cur]
print(cur)


### FIXED CODE ###

import sys
n, k = list(map(int, input().split()))
nums = [-1] + list(map(int, input().split()))

visited = [0 for _ in range(n + 1)]
path = [1]
cur = 1
visited[1] = 1
for i in range(k):
	cur = nums[cur]
	path.append(cur)
	if visited[cur] == 1:
		for idx, val in enumerate(path):
			if val == cur:
				break
		# print(k, idx, path)
		# print((k - idx) % (len(path) - idx - 1))
		print(path[idx + (k - idx) % ((len(path) - idx - 1))])
		sys.exit()
	else:
		visited[cur] = 1
print(cur)
### BUGGY CODE ###

def solve():
    N = int(input())
    array = list(map(int, input().split()))
    for each in array:
        if array.count(each) > 1:
            print("NO")
            return
    print("YES")

solve()

### FIXED CODE ###

def solve():
    N = int(input())
    array = sorted(map(int, input().split()))
    for index in range(1, N):
        if array[index] == array[index - 1]:
            print("NO")
            return
    print("YES")
solve()
### BUGGY CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = [[0 for i in range(n+1)] for j in range(n+1)]
p = []
for i in range(n):
    p.append([a[i],i])
p.sort(reverse=True)

for i in range(1,n+1):
    ans[0][i] = ans[0][i-1] + p[i-1][0]*abs(p[i-1][1]-i+1)
for i in range(1,n+1):
    ans[i][0] = ans[i-1][0] + p[i-1][0]*abs(n-i-p[i-1][1])

for i in range(1,n):
    for j in range(1,n-i+1):
        ans[i][j] = max(ans[i][j-1] + p[i+j -1][0]*abs(p[i+j -1][1]-j+1),
        ans[i-1][j] + p[i+j - 1][0]*abs(n-i-p[i+j-1][1]))

ma = 0
for i in range(n):
    ma = max(ma,ans[0+i][n-i])
print(ma)

### FIXED CODE ###

n = int(input())
a = list(map(int,input().split()))
ans = [[0 for i in range(n+1)] for j in range(n+1)]
p = []
for i in range(n):
    p.append([a[i],i])
p.sort(reverse=True)

for i in range(1,n+1):
    ans[0][i] = ans[0][i-1] + p[i-1][0]*abs(p[i-1][1]-i+1)
for i in range(1,n+1):
    ans[i][0] = ans[i-1][0] + p[i-1][0]*abs(n-i-p[i-1][1])

for i in range(1,n):
    for j in range(1,n-i+1):
        ans[i][j] = max(ans[i][j-1] + p[i+j -1][0]*abs(p[i+j -1][1]-j+1),
        ans[i-1][j] + p[i+j - 1][0]*abs(n-i-p[i+j-1][1]))

ma = 0
for i in range(n):
    ma = max(ma,ans[0+i][n-i])
print(ma)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int,input().split())) for i in range(q)]
l1 = []
l2 = []
ans = sum(a)
for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    cnt = a.count(b)
    cnt2 = cnt + l2.count(b) - l1.count(b)
    for j in range(cnt):
        l1.append(b)
        l2.append(c)
    dif = c - b
    ans += dif * cnt2
    print(ans)

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
q = int(input())
bc = [list(map(int,input().split())) for i in range(q)]
x = [0]*10**5
for i in a:
    x[i-1] += 1
ans = sum(a)
for i in range(q):
    b = bc[i][0]
    c = bc[i][1]
    x[c-1] += x[b-1]
    cnt = x[b-1]
    dif = c - b
    ans += dif * cnt
    x[b-1] = 0
    print(ans)
### BUGGY CODE ###

def lcs(s1, s2):
    dp = []
    for s2_k in s2:
        bgn_idx = 0
        for i, cur_idx in enumerate(dp):
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if not chr_idx:
                break
            dp[i] = min(cur_idx, chr_idx)
            bgn_idx = cur_idx
        else:
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if chr_idx:
                dp.append(chr_idx)
    return len(dp)

def solve():
    N = int(input())
    S = input()
    ans = 0
    for i in range(1,N):
      S1 = S[:i]
      S2 = S[i:]
      ans = max(ans,lcs(S1, S2))
    print(ans)

if __name__ == '__main__':
    solve()

### FIXED CODE ###

N = int(input())
S = input()
dp = [[0]*(N+1) for i in range(N+1)]
ans = 0
for i in range(N-1,-1,-1):
  for j in range(i,-1,-1):
    if S[i] == S[j]:
      dp[i][j] = dp[i+1][j+1] + 1
      tmp = min(dp[i][j],i-j)
      ans = max(ans,tmp)
print(ans)
### BUGGY CODE ###

x,y,a,b,c=map(int,input().split()) #x,yだけ見る。
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
s=sorted(p+q+r, reverse=True)
cntr,cntg,ans=0,0,0
for i in range(a+b+c):
    if cntr!=x and (s[i] in p or s[i] in r):
        ans+=s[i]
        cntr+=1
    elif cntg!=y and ( s[i] in q or s[i] in r):
        ans+=s[i]
        cntg+=1
print(ans)

### FIXED CODE ###

x,y,a,b,c=map(int,input().split()) #x,yだけ見る。
p=list(map(int,input().split()))
q=list(map(int,input().split()))
r=list(map(int,input().split()))
p=sorted(p,reverse=True)[:x]
q=sorted(q,reverse=True)[:y]
print(sum(sorted(p+q+r, reverse=True)[:x+y]))
### BUGGY CODE ###

import bisect
import numpy as np
N,M,Q = map(int,input().split())
lr = [0]*M
for i in range(M):
    l,r = map(int,input().split())
    lr[i] = [l,r]
lr = sorted(lr,key=lambda x:x[0])
lr_t = list(np.array(lr).transpose())
index_dict={}
r_dict={} 
d = {}
for i in range(N):
    index_dict[i+1]=bisect.bisect_left(lr_t[0],i+1)
    r_dict[i+1]=sorted(lr_t[1][index_dict[i+1]:])
for i in range(Q):
    p,q = map(int,input().split())
    if p not in d:
        d[p]={}
        I = index_dict[p]
        r = r_dict[p]
        I = bisect.bisect(r,q)
        print(I)
        d[p][q]=I
    else:
        if q not in d[p]:
            I = index_dict[p]
            r = r_dict[p]
            I = bisect.bisect(r,q)
            print(I)
            d[p][q]=I
        else:
            print(d[p][q])

### FIXED CODE ###

import numpy as np
N,M,Q = map(int,input().split())
c = [[0]*(N+1) for i in range(N+1)]
for i in range(M):
    l,r = map(int,input().split())
    c[l][r] += 1
c = np.cumsum(np.cumsum(c,axis=0),axis=1)
for i in range(Q):
    p,q = map(int,input().split())
    print(c[q][q]-c[q][p-1]-c[p-1][q]+c[p-1][p-1])
### BUGGY CODE ###

import numpy
S = numpy.array([0 if s == 'B' else 1 for s in input()])
ans = [sum(S[i + 1:]) for i in numpy.where(S == 0)[0]]
print(sum(ans))

### FIXED CODE ###

import numpy
from numpy.core.defchararray import count
S = numpy.array([0 if s == 'B' else 1 for s in input()])

count = 0
bcount = 0

for i, s in enumerate(S):
    if s == 0:
        bcount += 1
    else:
        count += bcount

print(count)