Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
import string

S = input()

abc = [chr(i) for i in range(97, 97+26)]

if len(S) < 26:
    for s in S:
        abc.remove(s)
    add = abc[0]
    print('wahaha')
    print(S + add)
elif S == ''.join(sorted(abc, reverse=True)):
    print(-1)
else:
    N = len(S)
    for i in range(1, N):
        now = S[N-i]
        fow = S[N-i+1:]
        fows = sorted(list(fow))
        for f in fows:
            if now < f:
                now = f
                break
        if now != S[N-i]:
            ans = S[:N-i] + now
            break
    print(ans)



Fixed submission:
import string

S = input()

abc = [chr(i) for i in range(97, 97+26)]

if len(S) < 26:
    for s in S:
        abc.remove(s)
    add = abc[0]
    print(S + add)
elif S == ''.join(sorted(abc, reverse=True)):
    print(-1)
else:
    N = len(S)
    for i in range(1, N):
        now = S[N-i-1]
        fow = S[N-i:]
        fows = sorted(list(fow))
        for f in fows:
            if now < f:
                now = f
                break
        if now != S[N-i-1]:
            ans = S[:N-i-1] + now
            break
    print(ans)

Buggy python submission (verdict: Runtime Error):
import string

S = input()

abc = [chr(i) for i in range(97, 97+26)]

if len(S) < 26:
    for s in S:
        abc.remove(s)
    add = abc[0]
    print(S + add)
elif S == ''.join(sorted(abc, reverse=True)):
    print(-1)
else:
    N = len(S)
    for i in range(1, N):
        now = S[N-i]
        fow = S[N-i+1:]
        fows = sorted(list(fow))
        for f in fows:
            if now < f:
                now = f
                break
        if now != S[N-i]:
            ans = S[:N-i] + now
            break
    print(ans)



Fixed submission:
import string

S = input()

abc = [chr(i) for i in range(97, 97+26)]

if len(S) < 26:
    for s in S:
        abc.remove(s)
    add = abc[0]
    print(S + add)
elif S == ''.join(sorted(abc, reverse=True)):
    print(-1)
else:
    N = len(S)
    for i in range(1, N):
        now = S[N-i-1]
        fow = S[N-i:]
        fows = sorted(list(fow))
        for f in fows:
            if now < f:
                now = f
                break
        if now != S[N-i-1]:
            ans = S[:N-i-1] + now
            break
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from bisect import bisect_left

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
L = 30
ans = 0
for l in range(L):
    P = []
    g = 1<<l
    P1 = []
    P2 = []
    for b in B:
        if b&g:
            P1.append(b)
        else:
            P2.append(b)
    B = P2 + P1
    P = []
    for b in B:
        P.append(b%(2*g))
    count = 0
    for a in A:
        count += (bisect_left(P, (2*g-a)%(2*g)) - bisect_left(P, (g-a)%(2*g)))%N
    if count%2 == 1:
        ans += g
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
from bisect import bisect_left

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


L = 30
ans = 0
for l in range(L):
    P = []
    g = 1<<l
    g2 = 1<<(1+l)
    B1 = []
    B2 = []
    for b in B:
        if b&g:
            B1.append(b)
        else:
            B2.append(b)
    B = B2 + B1

    A1 = []
    A2 = []
    for a in A:
        if a&g:
            A1.append(a)
        else:
            A2.append(a)
    A = A1 + A2

    count = 0
    ind1 = 0
    ind2 = 0
    ind3 = 0
    for a in A:
        while ind1 < N and B[ind1]%g2 + a%g2 < g:
            ind1 += 1
        while ind2 < N and B[ind2]%g2 + a%g2 < 2*g:
            ind2 += 1
        while ind3 < N and B[ind3]%g2 + a%g2 < 3*g:
            ind3 += 1
        count += ind2 - ind1 + N - ind3
    if count%2 == 1:
        ans += g
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from bisect import bisect_left

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

L = 30
ans = 0
for l in range(L):
    P = []
    g = 1<<l
    for b in B:
        P.append(b%(2*g))
    P.sort()
    count = 0
    for a in A:
        count += (bisect_left(P, (2*g-a)%(2*g)) - bisect_left(P, (g-a)%(2*g)))%N
    if count%2 == 1:
        ans += g
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline
from bisect import bisect_left

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


L = 30
ans = 0
for l in range(L):
    P = []
    g = 1<<l
    g2 = 1<<(1+l)
    B1 = []
    B2 = []
    for b in B:
        if b&g:
            B1.append(b)
        else:
            B2.append(b)
    B = B2 + B1

    A1 = []
    A2 = []
    for a in A:
        if a&g:
            A1.append(a)
        else:
            A2.append(a)
    A = A1 + A2

    count = 0
    ind1 = 0
    ind2 = 0
    ind3 = 0
    for a in A:
        while ind1 < N and B[ind1]%g2 + a%g2 < g:
            ind1 += 1
        while ind2 < N and B[ind2]%g2 + a%g2 < 2*g:
            ind2 += 1
        while ind3 < N and B[ind3]%g2 + a%g2 < 3*g:
            ind3 += 1
        count += ind2 - ind1 + N - ind3
    if count%2 == 1:
        ans += g
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

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

# resort list X O(len(X))
def sorting(X, l):
    eX = []
    oX = []
    for x in X:
        if x&(1<<l):
            oX.append(x)
        else:
            eX.append(x)
    return eX + oX

ans = 0
for shift in range(30):
    A = sorting(A, shift)
    B = sorting(B, shift)
    div = (1<<(shift+1))
    count = 0
    l1 = N
    r1 = N
    l2 = N
    r2 = N
    for a in A:
        while a%div + B[l1-1]%div >= div//2 and l1 > 0:
            l1 -= 1
        while a%div + B[r1-1]%div >= div and r1 > 0:
            r1 -= 1
        while a%div + B[l2-1]%div >= div//2*3 and l2 > 0:
            l2 -= 1
        while a%div + B[r2-1]%div >= 2*div and r2 > 0:
            r2 -= 1
        count += (r1-l1) + (r2-l2)
    if count%2 == 1:
        ans += (1<<shift)

print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

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

# resort list X O(len(X))
def sorting(X, l):
    eX = []
    oX = []
    for x in X:
        if x&(1<<l):
            oX.append(x)
        else:
            eX.append(x)
    return eX + oX

ans = 0
for shift in range(30):
    A = sorting(A, shift)
    B = sorting(B, shift)
    div = (1<<(shift+1))
    count = 0
    l1 = N
    r1 = N
    l2 = N
    r2 = N
    for a in A:
        while a%div + B[l1-1]%div >= div//2 and l1 > 0:
            l1 -= 1
        while a%div + B[r1-1]%div >= div and r1 > 0:
            r1 -= 1
        while a%div + B[l2-1]%div >= div//2*3 and l2 > 0:
            l2 -= 1
        while a%div + B[r2-1]%div >= 2*div and r2 > 0:
            r2 -= 1
        count += (r1-l1) + (r2-l2)
    if count%2 == 1:
        ans += (1<<shift)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    L = 2*K
    state = [[[0, 0] for _ in range(L+1)] for _ in range(L+1)]

    for _ in range(N):
        x, y, c = map(str, input().split())
        if c == 'W':
            state[int(x)%L+1][int(y)%L+1][0] += 1
        else:
            state[int(x)%L+1][int(y)%L+1][1] += 1

    for i in range(L):
        for j in range(L):
            for k in [0, 1]:
                state[i+1][j+1][k] += state[i+1][j][k] + state[i][j+1][k] - state[i][j][k]

    ans = 0
    for x in range(K):
        for y in range(K):
            for k in [0, 1]:
                count = 0
                count += state[x][y][k]
                count += state[x+K][y+K][k] - state[x][y+K][k] - state[x+K][y][k] + state[x][y][k]
                count += state[L][y][k] - state[x+K][y][k]
                count += state[x][L][k] - state[x][y+K][k]
                count += state[L][L][k] - state[x+K][L][k] - state[L][y+K][k] + state[x+K][y+K][k]

                l = k ^ 1
                count += state[x+K][y][l] - state[x][y][l]
                count += state[x][y+K][l] - state[x][y][l]
                count += state[L][y+K][l] - state[x+K][y+K][l] - state[L][y][l] + state[x+K][y][l]
                count += state[x+K][L][l] - state[x+K][y+K][l] - state[x][L][l] + state[x][y+K][l]

                ans = max(ans, count)
    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    L = 2*K
    state = [[[0, 0] for _ in range(L+1)] for _ in range(K+1)]

    for _ in range(N):
        x, y, c = map(str, input().split())
        a, b = int(x)%L, int(y)%L
        k = 0 if c == 'W' else 1
        if a >= K:
            if b >= K:
                a -= K
                b -= K
            else:
                a -= K
                b += K
        state[a+1][b+1][k] += 1


    for i in range(K):
        for j in range(L):
            for k in [0, 1]:
                state[i+1][j+1][k] += state[i+1][j][k] + state[i][j+1][k] - state[i][j][k]

    ans = 0
    for x in range(K):
        for y in range(K):
            for k in [0, 1]:
                count = 0
                count += state[x][y][k]
                count += state[x][L][k] - state[x][y+K][k]
                count += state[K][y+K][k] - state[x][y+K][k] - state[K][y][k] + state[x][y][k]

                l = k ^ 1
                count += state[K][y][l] - state[x][y][l]
                count += state[x][y+K][l] - state[x][y][l]
                count += state[K][L][l] - state[K][y+K][l] - state[x][L][l] + state[x][y+K][l]

                ans = max(ans, count)
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
K = int(input())
A = list(map(int, input().split()))

M = 2
L = 2
ok = True
for i in range(K-1, 0, -1):
    a, b = A[i], A[i-1]
    nM = (M//b)*b
    if M%b != 0:
        nM += b
    if nM >= M + a:
        ok = False
        break
    
    nL = (L+a-1)//b * b
    L, M = nL, nM

if not ok:
    print(-1)
else:
    L = L + b-1
    print(M, L)

Fixed submission:
K = int(input())
A = list(map(int, input().split()))


if K == 1:
    if A[0] == 2:
        print(2, 3)
    else:
        print(-1)
else:
    M = 2
    L = 2
    ok = True
    for i in range(K-1, -1, -1):
        a = A[i]
        nM = (M//a)*a
        if M%a != 0:
            nM += a
        nL = (L//a)*a + a - 1
        if nM >= M + a or nM > nL:
            ok = False
            break
        
        L, M = nL, nM
        #print(L, M, a, b)

    if not ok:
        print(-1)
    else:
        print(M, L)
Buggy python submission (verdict: Runtime Error):
X, Y = map(int, input().split())

for i in range(60):
    if X * 2**i > Y:
        ans = i
        break
print(ans)

Fixed submission:
X, Y = map(int, input().split())

for i in range(300):
    if X * 2**i > Y:
        ans = i
        break
print(ans)
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

for l in range(N+1):
    B = A[:]
    ok = True
    ans = []
    if l < N:
        if A[l] <= 0:
            ok = False
        for r in range(l, N-1):
            a, b = B[r], B[r+1]
            if a <= b:
                count = 0
            else:
                count = (a-b)//a
                if b%a != 0: count += 1
            B[r+1] += a*count
            if count > 2:
                ok = False
                break
            ans += [(r+1, r+2)]*count
    
    while l > 0:
        if A[l-1] != 0:
            break
        l -= 1
    
    if l > 0:
        if A[l-1] >= 0:
            ok = False
        for r in range(l-1, 0, -1):
            a, b = B[r], B[r-1]
            if b <= a:
                count = 0
            else:
                count = (a-b)//a
                if b%a != 0: count += 1
            B[r-1] += a*count
            if count > 2:
                ok = False
                break
            ans += [(r+1, r)]*count
    if ok:
        print(len(ans))
        for a, b in ans:
            print(a, b)

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

mina = 10**10
maxa = -10**10
for i, a in enumerate(A):
    if a < mina:
        l = i+1
        mina = a
    if a > maxa:
        r = i+1
        maxa = a


if maxa <= 0:
    print(N-1)
    for n in range(N, 1, -1):
        print(n, n-1)
elif mina >= 0:
    print(N-1)
    for n in range(1, N):
        print(n, n+1)
else:
    print(2*N-1)
    if abs(maxa) > abs(mina):
        for i in range(1, N+1):
            print(r, i)
        for n in range(1, N):
            print(n, n+1)
    else:
        for i in range(N, 0, -1):
            print(l, i)
        for n in range(N, 1, -1):
            print(n, n-1)
Buggy python submission (verdict: Runtime Error):
N = int(input())
F = [list(map(int, input().split())) for _ in range(N)]
P = [list(map(int, input().split())) for _ in range(N)]

def dfs(i, A, ans):
    if i == 10:
        ans.append(A)
        return ans
    B = A[:]
    B.append(0)
    ans = dfs(i+1, B, ans)
    C = A[:]
    C.append(1)
    ans = dfs(i+1, C, ans)
    return ans

def main()
    D = dfs(0, [], [])
    for ind, A in enumerate(D):
        if ind == 0:
            continue
        gain = 0
        for i in range(N):
            c = 0
            for day in range(10):
                c += F[i][day]*A[day]
            gain += P[i][c]
        if ind == 1:
            ans = gain
            continue
        ans = max(ans, gain)

    print(ans)

if __name__ == "__main__":
    main()

Fixed submission:
N = int(input())
F = [list(map(int, input().split())) for _ in range(N)]
P = [list(map(int, input().split())) for _ in range(N)]

def dfs(i, A, ans):
    if i == 10:
        ans.append(A)
        return ans
    B = A[:]
    B.append(0)
    ans = dfs(i+1, B, ans)
    C = A[:]
    C.append(1)
    ans = dfs(i+1, C, ans)
    return ans

def main():
    D = dfs(0, [], [])
    for ind, A in enumerate(D):
        if ind == 0:
            continue
        gain = 0
        for i in range(N):
            c = 0
            for day in range(10):
                c += F[i][day]*A[day]
            gain += P[i][c]
        if ind == 1:
            ans = gain
            continue
        ans = max(ans, gain)

    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
S = input()

K = ['A', 'K', 'I', 'H', 'B', 'R']

ans = 'YES'
p = 0
cont = False
for s in S:
    if not s in K:
        ans = 'NO'
    elif s == 'A':
        if (p == 0 or p == 3 or p == 4 or p == 5) and not cont:
            cont = True
        else:
            ans = 'NO'
    elif s == K[p+1]:
        p += 1
        cont = False
    else:
        ans = 'NO'
if p != 5:
    ans = 'NO'
print(ans)

Fixed submission:
S = input()

K = ['A', 'K', 'I', 'H', 'B', 'R']

ans = 'YES'
p = 0
cont = False
for s in S:
    if not s in K:
        ans = 'NO'
    elif s == 'A':
        if (p == 0 or p == 3 or p == 4 or p == 5) and not cont:
            cont = True
        else:
            ans = 'NO'
    elif p == 5:
        ans = 'NO'
        break
    elif s == K[p+1]:
        p += 1
        cont = False
    else:
        ans = 'NO'
if p != 5:
    ans = 'NO'
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
from numpy.fft import rfft, irfft
import numpy as np

K = int(input())

tmp = 1
dp = np.zeros(K, dtype=int)
while dp[tmp] == 0:
    dp[tmp] = 1
    tmp = tmp * 10 % K

init = dp[:]
KDP = rfft(init, 2*K)
for c in range(1,50):
    if dp[0] > 0:
        print(c)
        break
    NDP = rfft(dp, 2*K)
    Tmp = (irfft(KDP*NDP)[:2*K]+0.5).astype(int)
    dp = np.minimum(Tmp[:K] + Tmp[K:] + init, np.ones(K, dtype=int))

Fixed submission:
import sys
input = sys.stdin.readline
from collections import deque

K = int(input())

D = [-1]*K

q = deque()
q.append(1)
D[1] = 1
while q:
    p = q.pop()
    n1 = (p*10)%K
    if D[n1] == -1 or D[n1] > D[p]:
        D[n1] = D[p]
        q.append(n1)
    n2 = (p+1)%K
    if D[n2] == -1:
        D[n2] = D[p] + 1
        q.appendleft(n2)
print(D[0])
Buggy python submission (verdict: Time Limit Exceeded):
N, M, R = map(int, input().split())
Rs = list(map(int, input().split()))

INF = 100000*200*200
dp = [[INF for _ in range(N)] for _ in range(N)]
for _ in range(M):
    a, b, c = map(int, input().split())
    dp[a-1][b-1] = c
    dp[b-1][a-1] = c

for i in range(N):
    for j in range(N):
        for k in range(N):
            dp[j][k] = min(dp[j][k], dp[j][i]+dp[i][k])


def ind(i, R, rem, l, ans):
    if i == R:
        ans.append(l)
        return ans
    for r in rem:
        nl = l[:]
        nl.append(r)
        nr = rem[:]
        nr.remove(r)
        ans = ind(i+1, R, nr, nl, ans)
    return ans

inds = ind(0, R, [i for i in range(R)], [], [])

for k, ind in enumerate(inds):
    D = 0
    for j in range(R-1):
        D += dp[Rs[ind[j]]-1][Rs[ind[j+1]]-1]
    if k == 0:
        ans = D
    ans = min(ans, D)
print(ans)

Fixed submission:
import sys
input = sys.stdin.readline

def main():
    N, M, R = map(int, input().split())
    Rs = list(map(int, input().split()))

    INF = 100000*200*200
    dp = [[INF for _ in range(N)] for _ in range(N)]
    for _ in range(M):
        a, b, c = map(int, input().split())
        dp[a-1][b-1] = c
        dp[b-1][a-1] = c

    for i in range(N):
        for j in range(N):
            for k in range(N):
                dp[j][k] = min(dp[j][k], dp[j][i]+dp[i][k])


    def ind(i, R, rem, l, ans):
        if i == R:
            ans.append(l)
            return ans
        for r in rem:
            nl = l[:]
            nl.append(r)
            nr = rem[:]
            nr.remove(r)
            ans = ind(i+1, R, nr, nl, ans)
        return ans

    inds = ind(0, R, [i for i in range(R)], [], [])

    for k, ind in enumerate(inds):
        D = 0
        for j in range(R-1):
            D += dp[Rs[ind[j]]-1][Rs[ind[j+1]]-1]
        if k == 0:
            ans = D
        ans = min(ans, D)
    print(ans)

if __name__ == "__main__":
    main()
Buggy python submission (verdict: Runtime Error):
from collections import Counter
import sys
sys.setrecursionlimit(5000000)

S = list(input())
L = len(S)
A = Counter(S)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

ans = cmb(L, 2) + 1
for v in A.values():
    if v == 1: continue
    ans -= cmb(v, 2)
print(ans)

Fixed submission:
from collections import Counter
import sys
sys.setrecursionlimit(5000000)

S = list(input())
L = len(S)
A = Counter(S)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

if L == 1:
    print(1)
else:
    ans = cmb(L, 2) + 1
    for v in A.values():
        if v == 1: continue
        ans -= cmb(v, 2)
    print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import Counter
import sys
sys.setrecursionlimit(1000000)

S = list(input())
L = len(S)
A = Counter(S)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

ans = cmb(L, 2) + 1
for v in A.values():
    if v == 1: continue
    ans -= cmb(v, 2)
print(ans)

Fixed submission:
from collections import Counter
import sys
sys.setrecursionlimit(5000000)

S = list(input())
L = len(S)
A = Counter(S)

nCr = {}
def cmb(n, r):
    if r == 0 or r == n: return 1
    if r == 1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)] = cmb(n-1,r) + cmb(n-1,r-1)
    return nCr[(n,r)]

if L == 1:
    print(1)
else:
    ans = cmb(L, 2) + 1
    for v in A.values():
        if v == 1: continue
        ans -= cmb(v, 2)
    print(ans)
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split()) # 横に2個
A = list(map(int, input().split()))

A.sort(reverse=True)

dif = []
for i in range(N-1):
    d = A[i] - A[i+1]
    if d != 0:
        dif.append(d)
mind = min(dif)

ans = 'IMPOSSIBLE'
for a in A:
    if a < K:
        break
    if a == K:
        ans = 'POSSIBLE'
        break
    if (a-K) % mind == 0:
        ans = 'POSSIBLE'
        break

print(ans)

Fixed submission:
N, K = map(int, input().split()) # 横に2個
A = list(map(int, input().split()))

A.sort(reverse=True)

dif = []
for i in range(N-1):
    d = A[i] - A[i+1]
    if d != 0:
        dif.append(d)
if dif:
    mind = min(dif)
else:
    mind = A[0]

ans = 'IMPOSSIBLE'
for a in A:
    if a < K:
        break
    if a == K:
        ans = 'POSSIBLE'
        break
    if (a-K) % mind == 0:
        ans = 'POSSIBLE'
        break

print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split()) # 横に2個
A = list(map(int, input().split()))

A.sort(reverse=True)

dif = []
for i in range(N-1):
    dif.append(A[i] - A[i+1])
mind = min(dif)

ans = 'IMPOSSIBLE'
for a in A:
    if a < K:
        break
    if a == K:
        ans = 'POSSIBLE'
        break
    if (a-K) % mind == 0:
        ans = 'POSSIBLE'
        break

print(ans)

Fixed submission:
N, K = map(int, input().split()) # 横に2個
A = list(map(int, input().split()))

A.sort(reverse=True)

dif = []
for i in range(N-1):
    d = A[i] - A[i+1]
    if d != 0:
        dif.append(d)
if dif:
    mind = min(dif)
else:
    mind = A[0]

ans = 'IMPOSSIBLE'
for a in A:
    if a < K:
        break
    if a == K:
        ans = 'POSSIBLE'
        break
    if (a-K) % mind == 0:
        ans = 'POSSIBLE'
        break

print(ans)
Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)

N = int(input())

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

def dfs(p, checked, now):
    #print(p, checked, now)
    if p == N-1:
        return now, True
    ok = False
    for q in graph[p]:
        if not checked[q]:
            now.append(q)
            checked[q] = True
            bw, ok = dfs(q, checked, now)
    if not ok:
        now.pop()
        return now, False
    return bw, True

checked = [False for _ in range(N)]
checked[0] = True
bw, ok = dfs(0, checked, [0])
#print(bw)
cb = len(bw) - 2

between = [False for _ in range(N)]
for b in bw:
    between[b] = True

def dfs2(p, checked, c):
    c += 1
    for q in graph[p]:
        if not checked[q]:
            checked[q] = True
            c = dfs2(q, checked, c)
    return c

c0 = dfs2(0, between, 0)-1
cN = dfs2(N-1, between, 0)-1
#print(c0, cN)

if cb % 2 == 0:
    c0 += cb//2
    cN += cb//2
else:
    c0 += cb//2+1
    cN += cb//2

if cN < c0:
    print('Fennec')
else:
    print('Snuke')

Fixed submission:
import sys
sys.setrecursionlimit(600000)

N = int(input())

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

def dfs(p, checked, now):
    #print(p, checked, now)
    if p == N-1:
        return now, True
    ok = False
    for q in graph[p]:
        if not checked[q]:
            now.append(q)
            checked[q] = True
            nbw, ok1 = dfs(q, checked, now)
            if ok1:
                bw = nbw
                ok = True
    if not ok:
        now.pop()
        return now, False
    return bw, True

checked = [False for _ in range(N)]
checked[0] = True
bw, ok = dfs(0, checked, [0])
#print(bw)

between = [0 for _ in range(N)]
k = len(bw)
if k % 2 == 0:
    s0 = bw[k//2-1]
    sN = bw[k//2]
else:
    s0 = bw[k//2]
    sN = bw[k//2+1]
between[s0] = True
between[sN] = True

def dfs2(p, checked, c):
    c += 1
    for q in graph[p]:
        if not checked[q]:
            checked[q] = True
            c = dfs2(q, checked, c)
    return c

c0 = dfs2(s0, between, 0)
cN = dfs2(sN, between, 0)
#print(c0, cN)



if cN < c0:
    print('Fennec')
else:
    print('Snuke')