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

N, K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
scores = []
for i in range(N):
  pos = P[i]-1
  s = [C[i]]
  j = 1
  while j < K and pos != i:
    s.append(s[-1]+C[pos])
    pos = P[pos]-1
    j += 1
  smax = max(s)
  length = len(s)
  if length < K and s[-1] > 0:
    if K%length != 0:
      smod = s[:K%length]
      smodmax = max(smod)
    else:
      smodmax = 0
    scores.append(max(s[-1] * ((K//length)-1) + smax, s[-1] * (K//length), s[-1] * (K//length) + smodmax))
  else:
    scores.append(smax)
print(max(scores))


### FIXED CODE ###

N, K = map(int,input().split())
P = list(map(int,input().split()))
C = list(map(int,input().split()))
scores = []
for i in range(N):
  pos = P[i]-1
  s = [C[i]]
  j = 1
  while j < K and pos != i:
    s.append(s[-1]+C[pos])
    pos = P[pos]-1
    j += 1
  smax = max(s)
  length = len(s)
  if length < K and s[-1] > 0:
    if K%length != 0:
      smod = s[:K%length]
      smodmax = max(smod)
    else:
      smodmax = 0
    scores.append(max(s[-1] * ((K//length)-1) + smax, s[-1] * (K//length), s[-1] * (K//length) + smodmax))
  else:
    scores.append(smax)
print(max(scores))
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

table = [[0 for i in range(max(M)+1)] for j in range(n)]

def solve(i, m): 
    if m == 0:
        return True
    elif i >= n or m < 0:
        return False
    elif table[i][m] != 0:
        return table[i][m]
    else:
        res = solve(i + 1, m) or solve(i + 1, m - A[i])
        table[i][m] = res
        return res

for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')

### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
q = int(input())
M = list(map(int, input().split()))

table = [[-1 for i in range(max(M)+1)] for j in range(n)]

def solve(i, m):
    if m == 0:
        return True
    elif i >= n:
        return False
    elif table[i][m] != -1:
        return table[i][m]
    elif m - A[i] >= 0:
        res = solve(i + 1, m) or solve(i + 1, m - A[i])
        table[i][m] = res
        return res
    else:
        res = solve(i + 1, m)
        table[i][m] = res
        return res

for m in M:
    if solve(0, m):
        print('yes')
    else:
        print('no')
### BUGGY CODE ###

def main():
    N = int(input())
    S = list(input())
    Q = int(input())
    K = list(map(int, input().split()))
    d_list = [0] * N
    m_list = [0] * N
    c_list = [0] * N
    M_list = [0] * N
    for index, s in enumerate(S):
        if s == 'D':
            d_list[index] += 1
        elif s == 'M':
            m_list[index] += d_list[index - 1]
            M_list[index] += 1
        elif s == 'C':
            c_list[index] += m_list[index - 1]
        d_list[index] += d_list[index - 1]
        m_list[index] += m_list[index - 1]
        c_list[index] += c_list[index - 1]
        M_list[index] += M_list[index - 1]
    
    # print (d_list)
    # print (m_list)
    # print (c_list)
    # print (M_list)



    def calc(k):
        # d = 0
        # m = 0
        # c = 0
        # M = 0
        # for s in S[:k]:
        #     if s == 'D':
        #         d += 1
        #     elif s == 'M':
        #         m += d
        #         M += 1
        #     elif s == 'C':
        #         c += m
        # print (d, m, c)

        d = d_list[k - 1]
        m = m_list[k - 1]
        c = c_list[k - 1]
        M = M_list[k - 1]

        for index, x in enumerate(S[k:]):
            if S[index] == 'D':
                m -= M
                d -= 1
            elif S[index] == 'M':
                M -= 1

            if x == 'D':
                d += 1
            elif x == 'M':
                m += d
                M += 1
            elif x == 'C':
                c += m            
            # print (d, m, c)
                
        return c

    for k in K:
        print (calc(k))

    # print (calc(5))

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    N = int(input())
    S = list(input())
    Q = int(input())
    K = tuple(map(int, input().split()))
    d_list = [0] * N
    m_list = [0] * N
    c_list = [0] * N
    M_list = [0] * N
    for index, s in enumerate(S):
        if s == 'D':
            d_list[index] += 1
        elif s == 'M':
            m_list[index] += d_list[index - 1]
            M_list[index] += 1
        elif s == 'C':
            c_list[index] += m_list[index - 1]
        d_list[index] += d_list[index - 1]
        m_list[index] += m_list[index - 1]
        c_list[index] += c_list[index - 1]
        M_list[index] += M_list[index - 1]



    for k in K:
        d = d_list[k - 1]
        m = m_list[k - 1]
        c = c_list[k - 1]
        M = M_list[k - 1]

        for index, x in enumerate(S[k:]):
            if S[index] == 'D':
                m -= M
                d -= 1
            elif S[index] == 'M':
                M -= 1

            if x == 'D':
                d += 1
            elif x == 'M':
                m += d
                M += 1
            elif x == 'C':
                c += m            
        print (c)                


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

import bisect


def main():
    n, m = map(int, input().split(' '))
    a_s = list(sorted(map(int, input().split(' '))))
    
    for _ in range(m):
        value = int(a_s[n-1]/2)
        i = bisect.bisect_left(a_s[:n-1], value)
        a_s = a_s[:i] + [value] + a_s[i:n-1]
    print(sum(a_s))


if __name__ == '__main__':
    main()


### FIXED CODE ###

import heapq


def main():
    n, m = map(int, input().split(' '))
    a_s = list(sorted(map(lambda x: -1 * int(x), input().split(' '))))

    heapq.heapify(a_s)
    for _ in range(m):
        e = heapq.heappop(a_s)
        heapq.heappush(a_s, int(e/2))
    print(-1*sum(a_s))
    
    

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

from numpy import ceil,array,sum
import sys

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

def is_ok(m):

    tmp = h - m*B

    if not any(tmp):
        return True

    tmp = tmp[tmp>0]

    rem = 0
    for i in tmp:
        rem += ceil(i/(A-B))
    
    if rem > m:
        return False

    return True

N,A,B = map(int,input().split())
h = array(list(int(input()) for _ in range(N)))
s = sum(h)
##二分散策

r = 10**9
l = 0

while abs(l-r) > 1:

    m = (r+l)//2

    if is_ok(m):
        r = m
    else:
        l = m

print(r)

### FIXED CODE ###

import sys
from math import ceil

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

def check(k):
    tmp = list(h)
    for i in range(N):
        tmp[i] -= B*k
    if sum([int(i<=0) for i in tmp]) == N:
        return True

    lf = 0
    for i in tmp:
        if i > 0:
            lf += ceil(i/(A-B))

    if lf <= k:
        return True
    else:
        return False

N,A,B = map(int,input().split())
h = tuple(int(input()) for _ in range(N))

left = -1
right = 1e9

while abs(left-right) > 1:

    mid = (left+right)//2
    if check(mid):
        right = mid
    else:
        left = mid

print(int(right))
### BUGGY CODE ###

n,*o=open(0)
n=int(n)
r=list(range(n))
print(sum((1-any(o[(i+b)%n][(a+j)%n]!=o[(j+b)%n][(a+i)%n]for i in r for j in r[i:]))*(n-a-b) for a,b in zip(r+[0]*n,[0]*~-n+r)))

### FIXED CODE ###

n,*o=open(0)
n=int(n)
r=list(range(n))
print(sum((n-a-b)*all(o[(i+b)%n][(a+j)%n]==o[(j+b)%n][(a+i)%n]for i in r for j in r[i:])for a,b in zip(r+[0]*n,[0]*~-n+r)))
### BUGGY CODE ###

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

A_sunuke = [a - (i+1) for (i,a) in enumerate(A)]

sunuke_cnts = set(A_sunuke) 

idx, cnt = 0, 0
for i in sunuke_cnts:
    a_cnt = A_sunuke.count(i)
    if cnt < a_cnt:
        cnt = a_cnt
        idx = i 

rtn = sum([abs(a - idx) for a in A_sunuke])
print(rtn)

### FIXED CODE ###

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

A_sunuke = sorted(a - (i+1) for (i,a) in enumerate(A))

center = A_sunuke[len(A) // 2]

rtn = sum([abs(a - center) for a in A_sunuke])
print(rtn)
### BUGGY CODE ###

# coding: utf-8

if __name__ == "__main__":
    N = int(input())
    s = []
    for i in range(N):
        s.append("".join(sorted(input())))
    s.sort()
    ans = 0
    for i in range(len(s)):
        for j in range(i+1, len(s)):
            if s[i] == s[j]:
                ans += 1
            else:
                break
    print(ans)

### FIXED CODE ###

# coding: utf-8

def fact(n, m):
    ans = 1
    for i in range(n-m+1, n+1):
        ans *= i
    return ans

if __name__ == "__main__":
    N = int(input())
    s = []
    c = {}
    for i in range(N):
        a = "".join(sorted(input()))
        s.append(a)
        try:
            c[a] += 1
        except KeyError:
            c[a] = 1
    
    ans = 0
    for k,v in c.items():
        ans += fact(v,2)/2
    
    print(int(ans))
### BUGGY CODE ###

H,W = list(map(int,input().split()))

S = [input() for i in range(H)]

L,R,U,D = [[[0]*W for i in range(H)] for i in range(4)]

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            if i != 0:
                U[i][j] = U[i-1][j]+1
            else:
                U[i][j] = 1
            if j != 0:
                L[i][j] = L[i][j-1]+1
            else:
                L[i][j] = 1               
            
        else:
            U[i][j] = 0
            L[i][j] = 0

        if S[H-i-1][W-j-1] == ".":
            if i != 0:
                D[H-i-1][W-j-1] = D[H-i][W-j-1]+1
            else:
                D[H-i-1][W-j-1] = 1
            if j != 0:
                R[H-i-1][W-j-1] = R[H-i-1][W-j]+1
            else:
                R[H-i-1][W-j-1] = 1 
        else:
            D[H-i-1][W-j-1] = 0
            R[H-i-1][W-j-1] = 0        


ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans,L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)

print(ans)

### FIXED CODE ###

H,W = list(map(int,input().split()))

S = [input() for i in range(H)]

L,R,U,D = [[[0]*W for i in range(H)] for i in range(4)]

for i in range(H):
    for j in range(W):
        if S[i][j] == ".":
            if i != 0:
                U[i][j] = U[i-1][j]+1
            else:
                U[i][j] = 1
            if j != 0:
                L[i][j] = L[i][j-1]+1
            else:
                L[i][j] = 1
            
        else:
            U[i][j] = 0
            L[i][j] = 0

        ri = H-i-1
        rj = W-j-1
        if S[ri][rj] == ".":
            if i != 0:
                D[ri][rj] = D[ri+1][rj]+1
            else:
                D[ri][rj] = 1
            if j != 0:
                R[ri][rj] = R[ri][rj+1]+1
            else:
                R[ri][rj] = 1
        else:
            D[ri][rj] = 0
            R[ri][rj] = 0


ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans,L[i][j]+R[i][j]+U[i][j]+D[i][j]-3)

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

from collections import deque

H, W = map(int, input().split())
G = [[i for i in input()] for j in range(H)]

ls = deque([])
dp = [[-1 for i in range(W)] for j in range(H)]
for h in range(H):
    for w in range(W):
        if G[h][w] == "#":
            ls.append([h,w])
            dp[h][w] = 0

dir = [[1,0],[-1,0],[0,1],[0,-1]]
while len(ls) > 0:
    h, w = ls.popleft()

    for dx, dy in dir:
        if h + dx < 0 or H - 1 < h + dx: continue
        if w + dy < 0 or W - 1 < w + dy: continue
        if dp[h+dx][w+dy] == -1 and G[h+dx][w+dy] == ".":
            dp[h+dx][w+dy] = dp[h][w] + 1
            ls.append([h+dx,w+dy])

ans = 0
for i in range(H):
    for j in range(W):
        ans = max(ans, dp[i][j])

print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

H, W = map(int, input().split())
G = [[i for i in input()] for j in range(H)]

ls = []
dp = [[-1 for i in range(W)] for j in range(H)]
for h in range(H):
    for w in range(W):
        if G[h][w] == "#":
            ls.append([h,w])
            dp[h][w] = 1

ans = -1
while len(ls) > 0:
    ans += 1
    temp = []
    for h, w in ls:
        if 0 <= h+1 <= H-1 and dp[h+1][w] == -1:
            dp[h+1][w] = 1
            temp.append([h+1,w])
        if 0 <= h-1 <= H-1 and dp[h-1][w] == -1:
            dp[h-1][w] = 1
            temp.append([h-1,w])
        if 0 <= w+1 <= W-1 and dp[h][w+1] == -1:
            dp[h][w+1] = 1
            temp.append([h,w+1])
        if 0 <= w-1 <= W-1 and dp[h][w-1] == -1:
            dp[h][w-1] = 1
            temp.append([h,w-1])
    ls = temp

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

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

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[i][j][3]
dp = [[[0] * (C+5) for _ in range(R + 5)] for _ in range(4)]
for i in range(1, R + 3):
    for j in range(1, C + 3):
        # 行移動
        dp[0][i][j] = max(dp[0][i][j], max(dp[k][i - 1][j] for k in range(4)))
        dp[1][i][j] = max(dp[1][i][j], max(dp[k][i - 1][j] for k in range(4)) + V[i][j])
        for k in range(4):
            # 列移動
            if k > 0:
                dp[k][i][j] = max(dp[k][i][j], dp[k-1][i][j-1] + V[i][j])
            dp[k][i][j] = max(dp[k][i][j], dp[k][i][j-1])
print(max(dp[k][R][C] for k in range(4)))


### FIXED CODE ###

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

R, C, k = (int(x) for x in input().split())
V = [[0] * (C + 5) for _ in range(R + 5)]
for _ in range(k):
    r, c, v = (int(x) for x in input().split())
    V[r][c] = v

# dp[3][i][j]
dp = [[[0] * (C + 5) for _ in range(R + 5)] for _ in range(4)]
for i in range(R + 3):
    for j in range(C + 3):
        for k in range(4):
            # 行移動
            dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[k][i][j])
            dp[1][i + 1][j] = max(dp[1][i + 1][j], dp[k][i][j] + V[i + 1][j])
            # 列移動
            dp[k][i][j + 1] = max(dp[k][i][j + 1], dp[k][i][j])
            if k < 3:
                dp[k + 1][i][j + 1] = max(dp[k + 1][i][j + 1], dp[k][i][j] + V[i][j + 1])
print(max(dp[k][R][C] for k in range(4)))
### BUGGY CODE ###

K = int(input())

N = 7
count = -1

for i in range(K):
    amari = N%K
    if amari == 0:
        count = i+1
        break
    N = N*10 + 7
print(count)

### FIXED CODE ###

K = int(input())

N = 0
count = -1

for i in range(K):
    N = (N*10+7)%K
    if N == 0:
        count = i+1
        break
    
print(count)
### BUGGY CODE ###

N=int(input())
X=[int(input())]

def insert_search(List,a,l,r):
  if r==l:
    List[l-1]=a
    return List
  m=(l+r)//2
  if List[m]<a:
    return insert_search(List,a,m+1,r)
  else:
    return insert_search(List,a,l,m)
for i in range(N-1):
  a=int(input())
  if a>X[-1]:
    X[-1]=a
  elif X[0]>=a:
    X.insert(0,a)
  else:
    X=insert_search(X,a,0,len(X)-1)
print(len(X))

### FIXED CODE ###

N=int(input())
from collections import deque
d = deque([int(input())]) 
import bisect

for i in range(N-1):
  a=int(input())
  if a>d[-1]:
    d[-1]=a
  elif d[0]>=a:
    d.appendleft(a)
  else:
    i=bisect.bisect_left(d,a)
    d[i-1]=a
print(len(d))
### BUGGY CODE ###

from sys import stdin


def mod(val):
    res = val % (10 ** 9 + 7)
    if (res < 0):
        res += m
    return res


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    A_arr = list(map(int, _in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    A_arr_mod = []
    for a in A_arr:
        A_arr_mod.append(mod(a))

    ans = 0
    for i in range(N-1):
        for j in range(i+1, N):
            ans += mod(A_arr_mod[i]*A_arr_mod[j])
            ans = mod(ans)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

from sys import stdin


def mod(val):
    res = val % (10 ** 9 + 7)
    if (res < 0):
        res += (10 ** 9 + 7)
    return res


def main():
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    A_arr = list(map(int, _in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    A_arr_mod = [0] * len(A_arr)
    for i, a in enumerate(A_arr):
        A_arr_mod[i] = mod(a)

    ans = 0
    cache = sum(A_arr_mod)
    for i in range(N-1):
        cache -= A_arr_mod[i]
        ans += A_arr_mod[i] * cache
        ans = mod(ans)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(ans)


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

from collections import defaultdict
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[] for _ in range(N+1)]
	rail_graph = [[] for _ in range(N+1)]
	ans_list = [""]*N
	for i in range(K):
		road[i] = list(map(int, input().split()))
		road_graph[road[i][0]].append(road[i][1])
		road_graph[road[i][1]].append(road[i][0])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		rail_graph[rail[i][0]].append(rail[i][1])
		rail_graph[rail[i][1]].append(rail[i][0])

	def dfs(start, graph):
		this_ans = set()
		stack = []
		stack.extend(graph[start])
		this_ans.add(start)
		while stack:
			cur = stack.pop()
			if cur in this_ans:
				continue
			this_ans.add(cur)
			stack.extend(graph[cur])
		return this_ans

	road_appeared = defaultdict(set)
	rail_appeared = defaultdict(set)
	for i in range(1, N+1):
		if i not in road_appeared:
			this_roads = dfs(i, road_graph)
			for this_road in this_roads:
				road_appeared[this_road] = this_roads

		if i not in rail_appeared:
			this_rails = dfs(i, rail_graph)
			for this_rail in this_rails:
				rail_appeared[this_rail] = this_rails

		both_connected = road_appeared[i] & rail_appeared[i]
		this_ans = str(len(both_connected))
		ans_list[i-1] = this_ans

	ans = " ".join(ans_list)
	print(ans)


if __name__ == '__main__':
	main()

### FIXED CODE ###

from collections import defaultdict
def main():
	N, K, L = map(int, input().split())
	road = [0]*K
	rail = [0]*L
	road_graph = [[i, 0] for i in range(N+1)]
	rail_graph = [[i, 0] for i in range(N+1)]

	def root(graph, i):
		parent = graph[i][0]
		if parent == i:
			return i
		return root(graph, parent)

	def is_same_tree(graph, i, j):
		return root(graph, i) == root(graph, j)

	def unite(graph, i, j):
		if is_same_tree(graph, i, j):
			return
		l = graph[root(graph, i)]
		r = graph[root(graph, j)]
		if l[1] < r[1]:
			l[0] = r[0]
			r[1] += 1
		else:
			r[0] = l[0]
			l[1] += 1

	ans_list = [""]*N

	for i in range(K):
		road[i] = list(map(int, input().split()))
		unite(road_graph, road[i][0], road[i][1])
	for i in range(L):
		rail[i] = list(map(int, input().split()))
		unite(rail_graph, rail[i][0], rail[i][1])

	ans_memo = defaultdict(int)
	road_memo = [0]*(N+1)
	rail_memo = [0]*(N+1)
	for i in range(1, N+1):
		road_root = root(road_graph, i)
		rail_root = root(rail_graph, i)
		road_memo[i] = road_root
		rail_memo[i] = rail_root
		ans_memo[(road_root, rail_root)] += 1

	for i in range(1, N+1):
		road_root = road_memo[i]
		rail_root = rail_memo[i]
		this_ans = str(ans_memo[road_root, rail_root])
		ans_list[i-1] = this_ans

	ans = " ".join(ans_list)
	print(ans)


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

H, W = map(int, input().split())
N = int(input())
color = [0]
data = list(map(int, input().split()))
for i in range(N):
    color.append(color[i] + data[i])

def num(i):
    for j in range(N):
        if color[j] <= i < color[j + 1]:
            return str(j + 1)

grid = [[num(h * W + w) for w in range(W)] for h in range(H)]

for h in range(H):
    if h % 2 == 0:
        print(" ".join(grid[h]))
    else:
        print(" ".join(grid[h][::-1]))


### FIXED CODE ###

H, W = map(int, input().split())
N = int(input())
# color = [0]
data = list(map(int, input().split()))
# for i in range(N):
#    color.append(color[i] + data[i])

# def num(i):
#    for j in range(N):
#        if color[j] <= i < color[j + 1]:
#            return str(j + 1)

# grid = [[num(h * W + w) for w in range(W)] for h in range(H)]
col = 1
count = 1
grid = [["" for w in range(W)] for h in range(H)]
for h in range(H):
    for w in range(W):
        grid[h][w] = str(col)
        if count == data[col - 1]:
            count = 1
            col += 1
        else:
            count += 1

for h in range(H):
    if h % 2 == 0:
        print(" ".join(grid[h]))
    else:
        print(" ".join(grid[h][::-1]))
### BUGGY CODE ###

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

c = [0 for _ in range(N)]
cnt = 0
for i, (l, r) in enumerate(LR):
    for j in range(l - 1, r):
        c[j] += i + 1
    cnt += i + 1

s = [1 if k == cnt else 0 for k in c]

print(sum(s))


### FIXED CODE ###

N, M = map(int, input().split())
LR = [list(map(int, input().split())) for _ in range(M)]

left = 1
right = N

for l, r in LR:
    left = max(left, l)
    right = min(right, r)

ans = right - left

print(ans + 1 if ans >= 0 else 0)
### BUGGY CODE ###

import sys
from math import cos, radians, pi, sqrt
import numpy as np

head = input().split()
N = int(head[0])
M = int(head[1])
inputs = [input() for i in range(M)]

i = 1
dic = {}
for i in range(0, M):
#	print("探索%d回目" % i)
	param = inputs[i].split()
	A = param[0]
	B = param[1]

	if A in dic.keys():
		dic[A].append(B)
	else:
		dic[A] = []
		dic[A].append(B)

	if B in dic.keys():
		dic[B].append(A)
	else:
		dic[B] = []
		dic[B].append(A)
	
depth = {'1': 0}
que = dic['1']
count=1

while not que == []:
	next_que = []
	for room in que:
		if room in depth.keys():
			continue

		depth[room] = count
		for next_room in dic[str(room)]:
			if not next_room in next_que:
				next_que.append(next_room)
	count += 1
	que = next_que
print("Yes")
for i in range(1, N+1):
	for room, dep in depth.items():
		if depth[str(i)]-1 == dep:
			print(room)
			break

### FIXED CODE ###

n,m = map(int,input().split())
visited = [-1 for _ in range(n+1)]
visited[1] = 0
will = []
root = [[] for _ in range(n+1)] 
for i in range(m):
    a,b = map(int,input().split())
    root[a].append(b)
    root[b].append(a)

will.append(1)

while will:
    visit = will.pop(0)
    for i in root[visit]:
        if visited[i] != -1:
            continue
        will.append(i)
        visited[i] = visit

if visited[1:] == -1:
    print('No')
else:
    print('Yes')
    for i in range(2,len(visited)):
        print(visited[i])
### BUGGY CODE ###

n = int(input())
A = list(map(int, input().split()))
A_i = {}
A_j = {}
for i, a in enumerate(A):
    A_i[i] = a + i
    A_j[i] = i - a

r = 0
for i in range(n):
    for j in range(i + 1, n):
        if A_i[i] == A_j[j]:
            r += 1

print(r)


### FIXED CODE ###

n = int(input())
A = list(map(int, input().split()))
A_j = {}
for i, a in enumerate(A):
    if A_j.get(i - a):
        A_j[i - a] += 1
    else:
        A_j[i - a] = 1

r = 0
for i, a in enumerate(A):
    t = A_j.get(a + i)
    if t:
        r += t

print(r)
### BUGGY CODE ###

import numpy as np
from numba import jit
     
MOD = 998244353
N, K = list(map(int, input().split()))
     
dp = np.zeros(N+1, dtype=np.int64)
mv = np.zeros(N+1, dtype=np.int64)
for i in range(K):
    l, r = list(map(int, input().split()))
    for j in range(l, r+1):
        mv[j] = 1

@jit(nopython=True)
def shift_and_add(dp, mv, i):
    for i in range(1, N+1):
        dp += np.concatenate((np.zeros(i+1, dtype=np.int64), mv[1:N-i+1]*dp[i]))
        dp %= MOD
            
dp[1] = 1
shift_and_add(dp, mv, i)
     
print(dp[N])


### FIXED CODE ###

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

a = [0] * (N+1)
f = [0] * (N+1)
mv = list()
for i in range(K):
    mv.append(list(map(int, input().split())))

def solve(N, a, f):
    for i in range(1, N+1):
        f[i] = (f[i-1] + a[i]) % MOD
        for j in range(len(mv)):
            l, r = mv[j]
            if i+l < N+1: a[i+l] = (a[i+l] + f[i]) % MOD
            if i+r+1 < N+1: a[i+r+1] = (a[i+r+1] - f[i]) % MOD

a[1] = 1
mv.sort()
solve(N, a, f)

print(a[N])