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

from collections import defaultdict
n,p = map(int,input().split())
s = list(map(int,list(input())))[::-1]
ans = 0
if p == 2 or p == 5:
    for i in range(n):
        if s[i]%p==0:
            ans += n-i
    print(ans)
else:
    d = defaultdict(int)
    check = 0
    ten = 1
    for i in range(n):
        check += s[i]*ten
        check %= p
        ans += d[check]
        if check == 0:
            ans+=1
        d[check] += 1
        ten *= 10
    print(ans)

### FIXED CODE ###

from collections import defaultdict
n,p = map(int,input().split())
s = list(map(int,list(input())))[::-1]
ans = 0
if p == 2 or p == 5:
    for i in range(n):
        if s[i]%p==0:
            ans += n-i
    print(ans)
else:
    d = defaultdict(int)
    check = 0
    ten = 1
    for i in range(n):
        check += s[i]*ten
        check %= p
        ans += d[check]
        if check == 0:
            ans+=1
        d[check] += 1
        ten *= 10
        ten %= p
    print(ans)
### BUGGY CODE ###

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)


### FIXED CODE ###

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)
### BUGGY CODE ###

[n, k] = [int(x) for x in input().split()]
num_list = [x for x in input().split()]
num_dict = {}
for num in num_list:
    num_dict[num] = num_dict.get(num, 0) + 1
for _ in range(0, k):
    if num_dict:
        num_dict.pop(max(num_dict, key=num_dict.get))
    else:
        print(0)
        break
else:
    print(sum(num_dict.values()))


### FIXED CODE ###

[n, k] = [int(x) for x in input().split()]
num_list = [int(x) for x in input().split()]
num_dict = {}
for num in num_list:
    num_dict[num] = num_dict.get(num, 0) + 1
sorted_count = sorted(num_dict.values())
if len(sorted_count) - k > 0:
    print(sum(sorted_count[:len(sorted_count)-k]))
else:
    print(0)
### BUGGY CODE ###

def ff(x,y,z):
    return(x**2+y**2+z**2+x*y+y*z+z*x)

n=int(input())

an=[ff(i,j,k) for i in range(1,42) for j in range(1,42) for k in range(1,42)]

for i in range(1,n+1):
    print(an.count(i))

### FIXED CODE ###

def ff(x,y,z):
    return(x**2+y**2+z**2+x*y+y*z+z*x)

n=int(input())
an=[0]*n
for i in range(1,100):
    for j in range(1,100):
        for k in range(1,100):
            wk=ff(i,j,k)
            if wk<=n:
                an[wk-1]+=1
for i in an:
    print(i)
### BUGGY CODE ###

n,m = map(int,input().split())
a_list = []
c = []

for i in range(m):
    a,b = map(int,input().split())
    a_list += [a]
    c += [sum(1 << j-1 for j in list(map(int,input().split())))]

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

for i in range(1 << n):
    for A,C in zip(a_list,c):
        dp[C|i] = min(dp[C|i],dp[i]+A)

if dp[-1] == inf:
    print(-1)
else:
    print(dp[-1])

### FIXED CODE ###

n,m = map(int,input().split())
a_list = []
c = []

for i in range(m):
    a,b = map(int,input().split())
    a_list.append(a)
    c.append(sum(1 << j-1 for j in list(map(int,input().split()))))

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

for i in range(1 << n):
    for j in range(m):
        dp[c[j]|i] = min(dp[c[j]|i],dp[i]+a_list[j])

if dp[-1] == inf:
    print(-1)
else:
    print(dp[-1])
### BUGGY CODE ###

import itertools

def triangleChecker(combi):
    a, b, c = combi
    if a >= b + c :
        return False
    else:
        if b >= c + a:
            return False
        else :
            if c >= a + b:
                return False
            else :
                return True

N = int(input())
numbers = [int(i) for i in input().split()]

combinations = list(itertools.combinations(numbers, 3))

counter = 0
for combi in combinations:
    if triangleChecker(combi):
        # print(combi)
        counter += 1
print(counter)

### FIXED CODE ###

import bisect

N = int(input())
numbers = [int(i) for i in input().split()]
numbers = sorted(numbers)
counter = 0

for a in range(N):
    for b in range(a+1, N):
        c = bisect.bisect_left(numbers, numbers[a] + numbers[b])
        if c > b:
            counter += c - b -1
        
print(counter)
### BUGGY CODE ###

import numpy as np
N = int(input())
dp = np.zeros((N+1, 3), dtype=int)
for i in range(1, N+1):
    lst = np.array(list(map(int, input().split(' '))))
    dp[i][0] = np.max(dp[i-1][[1,2]] + lst[[1,2]])
    dp[i][1] = np.max(dp[i-1][[0,2]] + lst[[0,2]])
    dp[i][2] = np.max(dp[i-1][[0,1]] + lst[[0,1]])
print(np.max(dp[-1]))

### FIXED CODE ###

import numpy as np
N = int(input())
dp = np.zeros((N+1, 3), dtype=int)
for i in range(1, N+1):
    a, b, c = list(map(int, input().split(' ')))
    dp[i][0] = max(dp[i-1][1], dp[i-1][2]) + a
    dp[i][1] = max(dp[i-1][0], dp[i-1][2]) + b
    dp[i][2] = max(dp[i-1][0], dp[i-1][1]) + c
print(np.max(dp[-1]))
### BUGGY CODE ###

import numpy as np


from collections import deque
class Graph:
    edges = {}

    def append_edge(self, sv, ev, weight = 1):
        if sv in self.edges:
            self.edges[sv].append((ev, weight))
        else:
            self.edges[sv] = [(ev, weight)]

    def bfs(self, sv):
        dist = np.full(len(self.edges.keys()), -1)
        q = deque([])   # キュー
        q.append(sv)
        dist[sv] = 0
        connect_vtxs = []
        while len(q) > 0:
            p = q.popleft()     # キュー取りだし（先頭）
            connect_vtxs.append(p)
            for (ev, w) in self.edges[p]:
                if dist[ev] != -1:
                    continue
                q.append(ev)    # キュー追加
                dist[ev] = dist[p] + 1
        return dist, connect_vtxs

N, X, Y = list(map(int, input().split()))
g = Graph()
for i in range(N - 1):
    g.append_edge(i, i + 1)
    g.append_edge(i + 1, i)
g.append_edge(X - 1, Y - 1)
g.append_edge(Y - 1, X - 1)
dist_cnt = np.zeros(N)
for sv in range(N):
    dist, connect_vtxs = g.bfs(sv)
    for j in range(dist.shape[0]):
        if dist[j] == -1: continue
        dist_cnt[dist[j]] += 1

for i in range(N-1):
    print(int(dist_cnt[i+1]/2))


### FIXED CODE ###

from collections import deque
class Graph:
    edges = {}

    def append_edge(self, sv, ev, weight = 1):
        if sv in self.edges:
            self.edges[sv].append((ev, weight))
        else:
            self.edges[sv] = [(ev, weight)]

    def bfs(self, sv):
        dist = [-1]*len(self.edges.keys())
        q = deque([])   # キュー
        q.append(sv)
        dist[sv] = 0
        connect_vtxs = []
        while len(q) > 0:
            p = q.popleft()     # キュー取りだし（先頭）
            connect_vtxs.append(p)
            for (ev, w) in self.edges[p]:
                if dist[ev] != -1:
                    continue
                q.append(ev)    # キュー追加
                dist[ev] = dist[p] + 1
        return dist, connect_vtxs


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

g = Graph()
for i in range(N - 1):
    g.append_edge(i, i + 1)
    g.append_edge(i + 1, i)
g.append_edge(X - 1, Y - 1)
g.append_edge(Y - 1, X - 1)

dist_cnt = [0]*N
for sv in range(N):
    dist, connect_vtxs = g.bfs(sv)
    for j in range(len(dist)):
        if dist[j] == -1: continue
        dist_cnt[dist[j]] += 1

for i in range(N-1):
    print(int(dist_cnt[i+1]/2))
### BUGGY CODE ###

while True:
    try:
        n = int(input())
        count = 0
        if n == 1:
            print("0")
        else:
            a = [2*i for i in range(2, 1000000)]
            b = [3*i for i in range(2, 1000000)]
            c = [5*i for i in range(2, 1000000)]
            d = [7*i for i in range(2, 1000000)]
            for i in range(2, n+1):
                if i not in a and i not in b and i not in c and i not in d:
                    count += 1
            print(count)
    except:
        break

### FIXED CODE ###

import bisect

primes = [0, 0] + [1] * 999999
for i in range(2, 1000):
    if primes[i]:
        for j in range(i*i, 1000000, i):
            primes[j] = 0

primes = [i for i, v in enumerate(primes) if v]
while True:
    try:
        n = int(input())
    except:
        break
    print(bisect.bisect(primes, n))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
"""
Created on Sun Aug 18 21:19:43 2019
ABC138D
@author: maezawa
"""

n, q = list(map(int, input().split()))
graph = [[] for _ in range(n+1)]
for _ in range(n-1):
    ai, bi = list(map(int, input().split()))
    graph[ai].append(bi)
    graph[bi].append(ai)
    
parent = [[] for _ in range(n+1)]
child = [[] for _ in range(n+1)]
current = 1
while current <= n:
    for j in graph[current]:
        if current in child[j]:
            continue
        child[current].append(j)
    current += 1
#print(child)
parray = [-1]*(n+1)
parray[1] = 0
for i in range(1,n+1):
    for j in child[i]:
        parray[j] = i
#print(parray)
tpoint = [0]*(n+1)
for i in range(q):
    pi, xi = list(map(int, input().split()))
    tpoint[pi] += xi
tpmem = [-1]*(n+1)
tpmem[1] = tpoint[1]
for i in range(1, n+1):
    pi = 0
    j = i
    while True:
        if tpmem[j] != -1:
            pi += tpmem[j]
            break
        pi += tpoint[j]
        if parray[j] == -1:
            break
        j = parray[j]
    print(pi)
    tpmem[i] = pi
    

### FIXED CODE ###

n, q = list(map(int, input().split()))
nodes = [[] for _ in range(n)]
for _ in range(n-1):
    ai, bi = list(map(int, input().split()))
    nodes[ai-1].append(bi-1)
    nodes[bi-1].append(ai-1)
    
cnts = [0]*n
for _ in range(q):
    pi, xi = list(map(int, input().split()))
    cnts[pi-1] += xi
    
stack = []
visited = set()
stack.append(0)
while stack:
    current = stack.pop(-1)
    visited.add(current)
    for j in nodes[current]:
        if j in visited:
            continue
        cnts[j] += cnts[current]
        stack.append(j)
        
print(*cnts)
### BUGGY CODE ###

n=int(input())
a=[int(input()) for _ in range(n)]
m=max(a)
for i in range(n):
  if a[i]>=m:
    print(max(a[:i]+a[i+1:]))
  else:
    print(m)


### FIXED CODE ###

n=int(input())
a=[int(input()) for _ in range(n)]
s=sorted(a, reverse=True)
m=s[0]
for i in range(n):
  if a[i]>=m:
    print(s[1])
  else:
    print(m)
### BUGGY CODE ###

n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[float('inf') for i in range(n+1)]
dp[0]=0
for i in range(len(dp)):
    for j in range(i+1,i+k+1):
        if(j<len(l)):
      	    dp[j]=min(dp[j],dp[i]+abs(l[j]-l[i]))
print(dp[n-1])

### FIXED CODE ###

n,k=map(int,input().split())
l=list(map(int,input().split()))
dp=[float('inf') for i in range(n+1)]
dp[0]=0
for i in range(len(dp)):
    for j in range(i+1,i+k+1):
        if(j<len(l)):
      	    dp[j]=min(dp[j],dp[i]+abs(l[j]-l[i]))
print(dp[n-1])
### BUGGY CODE ###

import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

n = int(input())
s = input()
# combinationの準備
md = 10 ** 9 + 7
n_max = 3005
fac = [1]
inv = [1] * (n_max + 1)
k_fac_inv = 1
for i in range(1, n_max + 1):
    k_fac_inv = k_fac_inv * i % md
    fac.append(k_fac_inv)
k_fac_inv = pow(k_fac_inv, md - 2, md)
for i in range(n_max, 1, -1):
    inv[i] = k_fac_inv
    k_fac_inv = k_fac_inv * i % md

def com(com_n, com_r):
    return fac[com_n] * inv[com_r] * inv[com_n - com_r] % md

def main():
    sn=len(s)
    dp=[[1]*sn for _ in range(sn)]
    for w in range(1,sn):
        for l in range(sn-w):
            value=0
            if s[l]=="<":value+=dp[l+1][l+w]
            if s[l+w]==">":value+=dp[l][l+w-1]
            for i in range(l,l+w):
                if s[i]==">" and s[i+1]=="<":
                    value+=dp[l][max(i-1,l)]*dp[min(i+2,l+w)][l+w]*com(w+1,i-l+1)
            dp[l][l+w]=value%md
    #p2D(dp)
    print(dp[0][-1])

main()


### FIXED CODE ###

md = 10 ** 9 + 7
n = int(input())
s = input()
dp = [1] * n
for i in range(n - 1):
    if s[i] == "<":
        for dpi in range(1, n - i - 1):
            dp[dpi] = (dp[dpi] + dp[dpi - 1]) % md
        dp = dp[:-1]
    else:
        for dpi in range(n - i - 2, 0, -1):
            dp[dpi] = (dp[dpi] + dp[dpi + 1]) % md
        dp = dp[1:]
print(dp[0])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N = int(input())
A = input().split()

ans_all = 0
for x in set(A):
    m = A.count(x)
    ans_all += m * (m - 1) // 2

for x in A:
    m = A.count(x) - 1
    print(ans_all - m)


### FIXED CODE ###

import sys
from collections import Counter
input = sys.stdin.readline

N = int(input())
A = input().split()
a = Counter(A)

ans_all = 0
for x in a.keys():
    m = a[x]
    ans_all += m * (m - 1) // 2

for x in A:
    m = a[x] - 1
    print(ans_all - m)
### BUGGY CODE ###

import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n,M = i_map()
    l = [i_list() for _ in range(n)]
    l.sort(reverse = True)

    dp = [0]*(M+1)
    for i,k in l:
        if i > M:
            continue
        m = min(dp[:(-i)])
        d = dp.index(m)
        if k > m:
            dp[d] = k
    print(sum(dp))





if __name__=="__main__":
    main()


### FIXED CODE ###

import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    import heapq
    n,m = i_map()
    t = [[] for _ in range(m+1)]
    for i in range(n):
        a,b = i_map()
        if a> m:
            continue
        t[a].append(b)

    ans = 0
    h = []
    for l in t:
        for i in l:
            heapq.heappush(h, -i)
        if h:
            ans -= heapq.heappop(h)
    print(ans)






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

import sys
sys.setrecursionlimit(10**7)
MOD = 10**9+7

n = int(input())
g = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)

lim = 10**6
fact = [1, 1] + [0] * (lim-1)
fact_inv = [1, 1] + [0] * (lim-1)
inv = [0, 1] + [0] * (lim-1)
for i in range(2, lim+1):
    fact[i] = fact[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD % i] * (MOD//i) % MOD
    fact_inv[i] = fact_inv[i - 1] * inv[i] % MOD

def dfs(v, pv):
    res = 1
    size = 1
    for nv in g[v]:
        if nv == pv: continue
        nr, ns = dfs(nv, v)
        res *= nr * fact_inv[ns]
        res %= MOD
        size += ns
    res *= fact[size-1]
    return res % MOD, size

for i in range(n):
    print(dfs(i, -1)[0])


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
input = lambda: sys.stdin.readline().rstrip()
MOD = 10**9+7

n = int(input())
g = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split())
    a -= 1; b -= 1
    g[a].append(b)
    g[b].append(a)

lim = 10**5*2
fact = [1, 1] + [0] * (lim-1)
fact_inv = [1, 1] + [0] * (lim-1)
inv = [0, 1] + [0] * (lim-1)
for i in range(2, lim+1):
    fact[i] = fact[i-1] * i % MOD
    inv[i] = MOD - inv[MOD % i] * (MOD//i) % MOD
    fact_inv[i] = fact_inv[i-1] * inv[i] % MOD
def comb(n, m):
    if n < m or n < 0 or m < 0: return 0
    return fact[n] * (fact_inv[m] * fact_inv[n-m] % MOD) % MOD
def icomb(n, m):
    if n < m or n < 0 or m < 0: return 0
    return fact_inv[n] * (fact[m] * fact[n-m] % MOD) % MOD

dp = [1] * n
size = [0] * n

def dfs(v, pv):
    for nv in g[v]:
        if nv == pv: continue
        dfs(nv, v)
        ns = size[nv]+1
        size[v] += ns
        dp[v] *= dp[nv] * comb(size[v], ns) % MOD
        dp[v] %= MOD

def dfs2(v, pv):
    for nv in g[v]:
        if nv == pv: continue
        d, sz = dp[v], size[v]
        d *= pow(dp[nv], MOD-2, MOD) * icomb(sz, size[nv]+1) % MOD
        d %= MOD
        sz -= size[nv]+1

        size[nv] += sz+1
        dp[nv] *= d * comb(size[nv], sz+1) % MOD
        dp[nv] %= MOD
        dfs2(nv, v)

dfs(0, -1)
dfs2(0, -1)
print(*dp, sep="\n")
### BUGGY CODE ###

N,M = map(int,input().split())
L_list = []
R_list = []
count = 0
for i in range(M):
    L,R=map(int,input().split())
    L_list.append(L)
    R_list.append(R)

for i in range(1,N+1):

    for j in range(M):
        if(i in range(L_list[j],R_list[j]+1)):
            if j==M-1:
                count+=1
        else:
            break
print(count)

### FIXED CODE ###

N,M = map(int,input().split())
L_list = []
R_list = []
count = 0
min_mn=float('inf')
max_mn=0
for i in range(M):
    L,R=map(int,input().split())
    min_mn = min(min_mn,R)
    max_mn = max(max_mn,L)

for i in range(1,N+1):
    if(i>=max_mn and i<=min_mn):
        count += 1
print(count)
### BUGGY CODE ###

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

def resolve():
    n,m=map(int, input().split())
    if m==0:
        print(0,0)
    else:
        ansl=[[] for i in range(n)]
        for i in range(m):
            p,s=input().split()
            p=int(p)
            if 'AC' not in ansl[p-1]:
                ansl[p-1].append(s)
        wacnt=0
        accnt=0
        for i in range(n):
            if 'AC' in ansl[i]:
                accnt+=1
                wacnt+=len(ansl[i])-1
        print(accnt,wacnt)

resolve()

### FIXED CODE ###

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

def resolve():
    n,m=map(int, input().split())
    if m==0:
        print(0,0)
    else:
        l=[list(input().split()) for i in range(m)]
        wacnt=[0]*n
        accheck=[False]*n
        pcnt=0
        for p,s in l:
            p=int(p)
            if accheck[p-1]:
                continue
            if s=='WA':
                wacnt[p-1]+=1
            elif s=='AC':
                accheck[p-1]=True
                pcnt+=wacnt[p-1]
        print(sum(accheck),pcnt)
resolve()
### BUGGY CODE ###

def main():
    N = int(input())
    X = input()
    X_count = X.count("1")
    X = int(X, 2)
    ans = []
    for i in range(N-1, -1, -1):
        count = 1
        target = 1 << i
        Xi = X ^ target

        if X_count == 1 and target & X:
            ans.append(0)
            continue

        if target & X:
            Xi %= (X_count - 1)
        else:
            Xi %= (X_count + 1)

        # process
        while Xi:
            count += 1
            popcount = bin(Xi).count("1")
            Xi %= popcount

        ans.append(count)
    
    print(*ans, sep="\n")
if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    N = int(input())
    X = input()
    
    def popcount(x):
        '''xの立っているビット数をカウントする関数
        (xは64bit整数)'''

        # 2bitごとの組に分け、立っているビット数を2bitで表現する
        x = x - ((x >> 1) & 0x5555555555555555)

        # 4bit整数に 上位2bit + 下位2bit を計算した値を入れる
        x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)

        x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f # 8bitごと
        x = x + (x >> 8) # 16bitごと
        x = x + (x >> 16) # 32bitごと
        x = x + (x >> 32) # 64bitごと = 全部の合計
        return x & 0x0000007f

    ans = []
   
    f = [0] * N
    # preprocess 1
    for i in range(1, N):
        pc = popcount(i)
        j =  i % pc
        f[i] = f[j] + 1
    
    # preprocess 2
    X_COUNT = X.count("1")
    X = tuple(map(int, tuple(X)))
    rem_plus = [0] * N
    rem_minus = [0] * N
    two_factor = 1
    for i in range(N):
        rem_plus[i] = two_factor
        two_factor *= 2
        two_factor %= (X_COUNT+1)

    if X_COUNT > 1:
        two_factor = 1
        for i in range(N):
            rem_minus[i] = two_factor
            two_factor *= 2
            two_factor %= (X_COUNT-1)


    X_rem_plus = 0
    X_rem_minus = 0
    two_factor = 1
    for c in X[::-1]:
        X_rem_plus += two_factor*c
        X_rem_plus %= (X_COUNT+1)
        two_factor *= 2
        two_factor %= (X_COUNT+1)
    
    if X_COUNT > 1:
        two_factor = 1
        for c in X[::-1]:
            X_rem_minus += two_factor*c
            X_rem_minus %= (X_COUNT-1)
            two_factor *= 2
            two_factor %= (X_COUNT-1)
    
    for i, c in enumerate(X):
        if c:
            if X_COUNT > 1:
                rem = X_rem_minus - rem_minus[N-1-i]
                rem %= (X_COUNT-1)
                ans.append(f[rem]+1)
            elif X_COUNT == 1:
                ans.append(0)
            else:
                ans.append(0)
        else:
            rem = X_rem_plus + rem_plus[N-1-i]
            rem %= (X_COUNT+1)
            ans.append(f[rem]+1)
   
    print(*ans, sep="\n")

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

N = int(input())
A = [input() for _ in range(N)]

for i in range(0,N,2):
    if i == N-1:
        print(max(A[0:i]+A[i+1:len(A)]))
    else:
        print(max(A[0:i]+A[i+1:len(A)]))
        print(max(A[0:i+1]+A[i+2:len(A)]))

### FIXED CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]
A_max = max(A)
a = A.index(A_max)
A.sort(reverse=True)
for i in range(N):
    if i == a:
        print(A[1])
    else:
        print(A[0])
### BUGGY CODE ###

import sys
import math
import itertools
import collections
import numpy as np

rl = sys.stdin.readline

def a():
    N, M = map(int, rl().split())
    li = [[] for _ in range(N)]
    deg = [0] * N
    for _ in range(M):
        a, b = map(int, rl().split())
        a -= 1
        b -= 1
        li[a].append(b)
        deg[b] = 1
    deg = collections.deque([i for i in range(N) if deg[i] == 0])
    dp = [0] * N
    while deg:
        i = deg.popleft()
        for j in li[i]:
            dpj = dp[j]
            dpi = dp[i]
            if dpj < dpi + 1:
                dp[j] = dpi + 1
            deg.appendleft(j)
    print(max(dp))
a()




        




### FIXED CODE ###

import sys
import math
import itertools
import collections
import numpy as np

rl = sys.stdin.readline

def a():
    N, M = map(int, rl().split())
    li = [[] for _ in range(N)]
    deg = [0] * N
    for _ in range(M):
        a, b = map(int, rl().split())
        a -= 1
        b -= 1
        li[a].append(b)
        deg[b] += 1
    d = collections.deque([i for i in range(N) if deg[i] == 0])
    dp = [0] * N
    while d:
        i = d.popleft()
        for j in li[i]:
            deg[j] -= 1
            if deg[j] == 0:
                dp[j] = max(dp[j] ,dp[i] + 1)
                d.append(j)
    print(max(dp))
a()
### BUGGY CODE ###

P = 10**9+7
N, K = map(int, input().split())
S = list(map(int, input().split()))
S.sort()
ans = 0
c = 1
for i in range(N-K+1):
    ans += (S[K-1+i]-S[N-K-i])*c
    ans %= P
    c = c*(K+i)//(i+1)
print(ans)

### FIXED CODE ###

P = 10**9+7
N, K = map(int, input().split())
S = list(map(int, input().split()))
S.sort()
ans = 0
c = 1
for i in range(N-K+1):
    ans += (S[K-1+i]*c)%P
    ans -= (S[N-K-i]*c)%P
    ans %= P
    c = c*(K+i)*pow(i+1, P-2, P)%P
print(ans)