Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
N = int(input())


ans_len = 0
ans_num = 0
zero = []
for i in range(1,N+1):
    temp = i * (i+1) // 2
    zero.append(temp)
    if temp == N:
        ans_len = i+1
        ans_num = i

ans_list = [[] for _ in range(ans_len)]

if N in zero:
    print('Yes')
else:
    print('No')
    exit()


low_idx = 0
up_idx = 1
for i in range(1,N+1):
    ans_list[low_idx].append(i)
    ans_list[up_idx].append(i)
    if i in zero:
        low_idx = 0
        up_idx += 1
    else:
        low_idx += 1

# print(ans_list)
print(len(ans_list))
for a in ans_list:
    print(len(a)," ".join([str(x) for x in a]))


"""
1 2 3 4 5  6  7
2 4 6 8 10 12 14 16 18 20
2   3      4           5
1   2      3           4
0 1 0 1 2  0  1  2  3  0

12
13
23

1347
1258
2369
456 10
789 10

124
135
236
456

"""


Fixed submission:
# -*- coding: utf-8 -*-
N = int(input())

ans_len = 0
ans_num = 0
zero = []
for i in range(1,N+1):
    temp = i * (i+1) // 2
    zero.append(temp)
    if temp == N:
        ans_len = i+1
        ans_num = i
        break

ans_list = [[] for _ in range(ans_len)]

if N in zero:
    print('Yes')
else:
    print('No')
    exit()


low_idx = 0
up_idx = 1
for i in range(1,N+1):
    ans_list[low_idx].append(i)
    ans_list[up_idx].append(i)
    if i in zero:
        low_idx = 0
        up_idx += 1
    else:
        low_idx += 1

# print(ans_list)
print(len(ans_list))
for a in ans_list:
    print(len(a)," ".join([str(x) for x in a]))


"""
1 2 3 4 5  6  7
2 4 6 8 10 12 14 16 18 20
2   3      4           5
1   2      3           4
0 1 0 1 2  0  1  2  3  0

12
13
23

1347
1258
2369
456 10
789 10

124
135
236
456

"""
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

from collections import Counter

N, M = map(int, input().split())
D = 1000000000 + 7

def primes(n):
    primfac = []
    d = 2
    while d*d <= n:
        while (n % d) == 0:
            primfac.append(d)  # supposing you want multiple factors repeated
            n //= d
        d += 1
    if n > 1:
       primfac.append(n)
    return primfac


f = primes(M)
cntr = Counter(f)

ans = 1
for k,v in cntr.items():
    ans *= combinations_count(v+N-1,v) %D

print(ans%D)


Fixed submission:
# -*- coding: utf-8 -*-
import math

# 高速 組み合わせ
def C(n, r):
  a = 1
  b = 1
  for i in range(r):
    a = a*(n-i)
    b = b*(r-i)
  return((a//b))

from collections import Counter

N, M = map(int, input().split())
D = 1000000000 + 7

def primes(n):
    primfac = []
    d = 2
    while d*d <= n:
        while (n % d) == 0:
            primfac.append(d)  # supposing you want multiple factors repeated
            n //= d
        d += 1
    if n > 1:
       primfac.append(n)
    return primfac


f = primes(M)
cntr = Counter(f)

ans = 1
for k,v in cntr.items():
    ans *= C(v+N-1,v) %D

print(ans%D)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

from collections import Counter

N, M = map(int, input().split())
D = 1000000000 + 7


def primes(n):
    primfac = []
    d = 2
    while d*d <= n:
        while (n % d) == 0:
            primfac.append(d)  # supposing you want multiple factors repeated
            n //= d
        d += 1
    if n > 1:
       primfac.append(n)
    return primfac


f = primes(M)
cntr = Counter(f)

ans = 1
for k,v in cntr.items():
    ans *= combinations_count(v+N-1,v)

print(ans%D)


Fixed submission:
# -*- coding: utf-8 -*-
import math

# 高速 組み合わせ
def C(n, r):
  a = 1
  b = 1
  for i in range(r):
    a = a*(n-i)
    b = b*(r-i)
  return((a//b))

from collections import Counter

N, M = map(int, input().split())
D = 1000000000 + 7

def primes(n):
    primfac = []
    d = 2
    while d*d <= n:
        while (n % d) == 0:
            primfac.append(d)  # supposing you want multiple factors repeated
            n //= d
        d += 1
    if n > 1:
       primfac.append(n)
    return primfac


f = primes(M)
cntr = Counter(f)

ans = 1
for k,v in cntr.items():
    ans *= C(v+N-1,v) %D

print(ans%D)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import math

N, X = map(int, input().split())
xn = list(map(int, input().split()))
xn2 = []

for xi in xn:
    xn2.append(xi - X)

xn2.sort()

gcd_num = max(xn2)
for xi2 in xn2:
    tmp = math.gcd(xi2,gcd_num)
    if tmp < gcd_num:
        gcd_num = tmp

print(gcd_num)


Fixed submission:
# -*- coding: utf-8 -*-
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

N, X = map(int, input().split())
xn = list(map(int, input().split()))
xn2 = []

for xi in xn:
    xn2.append(abs(xi - X))

xn2.sort()

gcd_num = max(xn2)
for xi2 in xn2:
    tmp = gcd(xi2,gcd_num)
    if tmp < gcd_num:
        gcd_num = tmp

print(gcd_num)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import math
from functools import reduce

def gcd_list(numbers):
    return reduce(math.gcd, numbers)

N, X = map(int, input().split())
xn = list(map(int, input().split()))
xn2 = []

for xi in xn:
    xn2.append(xi - X)

# print(xn2)
print(gcd_list(xn2))


Fixed submission:
# -*- coding: utf-8 -*-
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a

N, X = map(int, input().split())
xn = list(map(int, input().split()))
xn2 = []

for xi in xn:
    xn2.append(abs(xi - X))

xn2.sort()

gcd_num = max(xn2)
for xi2 in xn2:
    tmp = gcd(xi2,gcd_num)
    if tmp < gcd_num:
        gcd_num = tmp

print(gcd_num)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
import itertools

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

Nlst = []
# for i in range(1,N+1):
#     Nlst.append(i)

if K % 2 == 0:
    for i in range(1,N+1):
        if i % K == 0 or i % (K/2) == 0:
            Nlst.append(i)
else:
    for i in range(1,N+1):
        if i % K == 0:
            Nlst.append(i)

tmplst = []
for i in itertools.product(Nlst, repeat=3):
    tmplst.append(i)

ans = 0

for (a,b,c) in tmplst:
    if (a+b) % K == 0 and (b+c) % K == 0 and (c+a) % K == 0:
        ans += 1


print(ans)


Fixed submission:
# -*- coding: utf-8 -*-
import itertools

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

Nlst = []
Nlst_gu = []
# for i in range(1,N+1):
#     Nlst.append(i)

if K % 2 == 0:
    for i in range(1,N+1):
        if i % K == 0:
            Nlst.append(i)
        elif i % (K/2) == 0:
            Nlst_gu.append(i)
else:
    for i in range(1,N+1):
        if i % K == 0:
            Nlst.append(i)
#
# print(Nlst)
# print(Nlst_gu)

ans = len(Nlst)
ans_gu = len(Nlst_gu)

print(ans * ans * ans + ans_gu * ans_gu * ans_gu)

#
# tmplst = []
# for i in itertools.product(Nlst, repeat=3):
#     tmplst.append(i)
#
# ans = 0
#
# for (a,b,c) in tmplst:
#     if (a+b) % K == 0 and (b+c) % K == 0 and (c+a) % K == 0:
#         ans += 1
#
#
# print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

kouho = []
ans_list = []
for i in range(N-K+1):
    li = xi[i:i+K]
    if li[0]*li[-1] < 0:
        a = abs(li[0])*2 + abs(li[-1])
        b = abs(li[-1])*2 + abs(li[0])
        if a > b :
            ans_list.append(b)
        else:
            ans_list.append(a)
    elif li[0] < 0:
        ans_list.append(abs(li[0]))
    else: #if li[-1] > 0:
        ans_list.append(abs(li[-1]))

print(min(ans_list))


Fixed submission:
# -*- coding: utf-8 -*-

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

ans_list = []
for i in range(N-K+1):
    l = xi[i]
    r = xi[i+K-1]
    if l*r < 0:
        a = abs(l)*2 + abs(r)
        b = abs(r)*2 + abs(l)
        if a > b :
            ans_list.append(b)
        else:
            ans_list.append(a)
    elif l < 0:
        ans_list.append(abs(l))
    else: #if li[-1] > 0:
        ans_list.append(abs(r))

print(min(ans_list))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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

kouho = []
ans_list = []
for i in range(N-K+1):
    li = xi[i:i+K]
    if li[0]*li[-1] < 0:
        ans_list.append(abs(li[0])*2 + abs(li[-1]))
        ans_list.append(abs(li[-1])*2 + abs(li[0]))
    elif li[0] < 0:
        ans_list.append(abs(li[0]))
    else: #if li[-1] > 0:
        ans_list.append(abs(li[-1]))

print(min(ans_list))


Fixed submission:
# -*- coding: utf-8 -*-

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

ans_list = []
for i in range(N-K+1):
    l = xi[i]
    r = xi[i+K-1]
    if l*r < 0:
        a = abs(l)*2 + abs(r)
        b = abs(r)*2 + abs(l)
        if a > b :
            ans_list.append(b)
        else:
            ans_list.append(a)
    elif l < 0:
        ans_list.append(abs(l))
    else: #if li[-1] > 0:
        ans_list.append(abs(r))

print(min(ans_list))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

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


kouho = []
for i in range(N-K+1):
    kouho.append(xi[i:i+K])

# print(kouho)
ans_list = []
for li in kouho:
    if li[0]*li[-1] < 0:
        ans_list.append(abs(li[0])*2 + abs(li[-1]))
        ans_list.append(abs(li[-1])*2 + abs(li[0]))
    elif li[0] < 0:
        ans_list.append(abs(li[0]))
    else: #if li[-1] > 0:
        ans_list.append(abs(li[-1]))

print(min(ans_list))


Fixed submission:
# -*- coding: utf-8 -*-

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

ans_list = []
for i in range(N-K+1):
    l = xi[i]
    r = xi[i+K-1]
    if l*r < 0:
        a = abs(l)*2 + abs(r)
        b = abs(r)*2 + abs(l)
        if a > b :
            ans_list.append(b)
        else:
            ans_list.append(a)
    elif l < 0:
        ans_list.append(abs(l))
    else: #if li[-1] > 0:
        ans_list.append(abs(r))

print(min(ans_list))
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

R = int(input())

if R < 1200:
    print('ABC')
elif R < 2800:
    print('ARC')
elif:
    print('AGC')


Fixed submission:
# -*- coding: utf-8 -*-

R = int(input())

if R < 1200:
    print('ABC')
elif R < 2800:
    print('ARC')
else:
    print('AGC')
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from pprint import pprint

S = list(input())
MOD = 10**9 + 7

dp = [[0 for _ in range(4)] for _ in range(len(S)+1)]

dp[len(S)][0] = 0
dp[len(S)][1] = 0
dp[len(S)][2] = 0
dp[len(S)][3] = 1

for i in reversed( range(len(S))):
    dp[i][0] = dp[i+1][0]
    if S[i] == 'C':
        dp[i][3] = dp[i+1][3]
        dp[i][2] = dp[i+1][2] + dp[i+1][3]
        dp[i][1] = dp[i+1][1] 
        dp[i][0] = dp[i+1][0]
    elif S[i] == 'B':
        dp[i][3] = dp[i+1][3]
        dp[i][2] = dp[i+1][2]
        dp[i][1] = dp[i+1][1] + dp[i+1][2]
        dp[i][0] = dp[i+1][0]
    elif S[i] == 'A':
        dp[i][3] = dp[i+1][3]
        dp[i][2] = dp[i+1][2] 
        dp[i][1] = dp[i+1][1] 
        dp[i][0] = dp[i+1][0] + dp[i+1][1]
    elif S[i] == '?':
        dp[i][3] = 3*dp[i+1][3]
        dp[i][2] = 3*dp[i+1][2] + dp[i+1][3]
        dp[i][1] = 3*dp[i+1][1] + dp[i+1][2]
        dp[i][0] = 3*dp[i+1][0] + dp[i+1][1]

# pprint(dp)
print(dp[0][0]%MOD)


Fixed submission:
# -*- coding: utf-8 -*-
from pprint import pprint

S = list(input())
MOD = 10**9 + 7

dp = [[0 for _ in range(4)] for _ in range(len(S)+1)]

dp[len(S)][0] = 0
dp[len(S)][1] = 0
dp[len(S)][2] = 0
dp[len(S)][3] = 1

for i in reversed( range(len(S))):
    dp[i][0] = dp[i+1][0]
    if S[i] == 'C':
        dp[i][3] = dp[i+1][3]
        dp[i][2] = dp[i+1][2] + dp[i+1][3]
        dp[i][1] = dp[i+1][1] 
        dp[i][0] = dp[i+1][0]
    elif S[i] == 'B':
        dp[i][3] = dp[i+1][3]
        dp[i][2] = dp[i+1][2]
        dp[i][1] = dp[i+1][1] + dp[i+1][2]
        dp[i][0] = dp[i+1][0]
    elif S[i] == 'A':
        dp[i][3] = dp[i+1][3]
        dp[i][2] = dp[i+1][2] 
        dp[i][1] = dp[i+1][1] 
        dp[i][0] = dp[i+1][0] + dp[i+1][1]
    elif S[i] == '?':
        dp[i][3] = 3*dp[i+1][3]
        dp[i][2] = 3*dp[i+1][2] + dp[i+1][3]
        dp[i][1] = 3*dp[i+1][1] + dp[i+1][2]
        dp[i][0] = 3*dp[i+1][0] + dp[i+1][1]

    dp[i][3] %= MOD
    dp[i][2] %= MOD
    dp[i][1] %= MOD
    dp[i][0] %= MOD


# pprint(dp)
print(dp[0][0]%MOD)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import numpy as np

N, M = map(int, input().split())
a = []
b = []

for j in range(M):
    ta, tb = map(int, input().split())
    a.append(ta)
    b.append(tb)

# print(a)
# print(b)

br_tbl = np.zeros((N-1, M), dtype=np.bool)
for j in range(M):
    for i in range(a[j]-1,b[j]-1):
        br_tbl[i,j] = 1

max_list = []
skip_list = np.zeros((M), dtype=np.bool)
br_cnt = 0
for j in range(M):
    # print('j=',j)
    max_col = 0
    if skip_list[j] == 1:
        continue
    sum_list = np.zeros((N-1), dtype=np.uint)
    for i in range(N-1):
        if  br_tbl[i,j] == 1:
            # print('i=',i)
            sum = 0
            for j2 in range(j,M):
                # print('j2=',j2)
                sum += br_tbl[i,j2]
            sum_list[i] = sum
            # print(sum)
    max_col = np.argmax(sum_list)
    max_list.append(max_col)
    # print('sum_list=',sum_list)
    # print('max_col=',max_col)
    br_cnt += 1

    for j3 in range(j,M):
        # print('j3=',j3)
        if br_tbl[max_col,j3] == 1:
            skip_list[j3] = 1
#     print(skip_list)
#
# print()
# print(br_tbl)

print(br_cnt)


Fixed submission:
# -*- coding: utf-8 -*-
from collections import deque
N, M = map(int, input().split())
abn = [list(map(int, input().split())) for _ in range(M)]

abn.sort(key=lambda x:x[1])

ans = 0
bnum = -1
for a,b in abn:
    if bnum == -1:
        bnum = b - 1
        ans += 1
    elif a <= bnum < b:
        continue
    else:
        bnum = b - 1
        ans += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
N = int(input())
an = list(map(int, input().split()))

sum_cnt = 0
for a in an:
    tmp = a
    cnt = 0
    while tmp % 2 == 0:
        cnt += 1
        tmp = tmp / 2

    sum_cnt += cnt

print(sum_cnt)


Fixed submission:
# -*- coding: utf-8 -*-

# スペース区切りの整数の入力
A, B = map(int, input().split())

if A <= 8 and B <= 8:
    print('Yay!')
else:
    print(':(')
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import itertools

# スペース区切りの整数の入力
N,M = list(map(int, input().split()))
xyz = []
for i in range(N):
    xyz.append(list(map(int, input().split())))

xyz_list = itertools.combinations(xyz, M)

sum_list = []
for v in xyz_list:
    xsum = 0
    ysum = 0
    zsum = 0
    all_sum = 0
    for w in v:
        xsum += w[0]
        ysum += w[1]
        zsum += w[2]

    all_sum = abs(xsum) + abs(ysum) + abs(zsum)
    sum_list.append(all_sum)

print(max(sum_list))


Fixed submission:
# -*- coding: utf-8 -*-
from pprint import pprint
import itertools
N, M = map(int, input().split())
xyzn = [list(map(int, input().split())) for _ in range(N)]

ans = []
ope = [1,-1]
for sx, sy, sz in itertools.product(ope, repeat=3):
    xyzn.sort(key=lambda x:-(sx*x[0]+sy*x[1]+sz*x[2]))
    ans.append(sum([sx*x+sy*y+sz*z for x,y,z in xyzn[:M]]))

print(max(ans))
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

n = int(input())
s = input()

s1 = list(s)
s2 = list(s)

# print(s1)
# print(s2)
# print('')

numlist = []
for i in range(n):
    if i > 0:
        for j in range(i):
            s2[j] = 'E'
    for j in range(i+1,n):
        s2[j] = 'W'

    temp = 0
    for j in range(n):
        if s1[j] != s2[j]:
            temp += 1

    numlist.append(temp)

    # print(s1)
    # print(s2)
    # print('')

# 出力
print("{}".format(min(numlist)))


Fixed submission:
# -*- coding: utf-8 -*-

n = int(input())
s = input()

LW_cnt = 0
RE_cnt = s[1:].count('E')


numlist = []
for i in range(n):
    if i > 0:
        if s[i-1] == 'W':
            LW_cnt += 1
        if s[i] == 'E':
            RE_cnt -= 1

    numlist.append(LW_cnt+RE_cnt)

# 出力
print("{}".format(min(numlist)))
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from pprint import pprint

MAX_bin = 20
N = int(input())
An = list(map(int, input().split()))

bin_rui = [[0 for _ in range(MAX_bin)] for _ in range(N+1)]

tmp_bin = [0 for _ in range(MAX_bin)]
for i,A in enumerate(An):
    binA = bin(A)[2:]
    tmpA = "0"*(MAX_bin - len(binA)) + binA

    for k in range(MAX_bin):
        bin_rui[i+1][k] = tmp_bin[k] + int(tmpA[k])
        tmp_bin[k] += int(tmpA[k])

# 尺取
renzoku = []
cnt = 1
l = 1
r = 1
while l <= N and r <= N:
    bin_l = bin_rui[l-1]
    bin_r = bin_rui[r]
    bin_diff = [0 for _ in range(MAX_bin)]
    # bin_r - bin_l
    for k in range(MAX_bin):
        bin_diff[k] = bin_r[k] - bin_l[k]

    if sum([1 for b in bin_diff if b > 1]) == 0:
        renzoku.append(cnt)
        cnt += 1
        r += 1
    else:
        cnt -= 1 
        l += 1

# pprint(renzoku)
print(sum(renzoku))


Fixed submission:
# -*- coding: utf-8 -*-
N = int(input())
An = list(map(int, input().split()))

# 尺取
ans = 0
l = 0
r = 0
Asum = An[0]
Axor = An[0]
while l < N and r < N:
    if Asum == Axor:
        ans += r-l+1
        r += 1
        if r < N:
            Asum += An[r]
            Axor ^= An[r]
    else:
        if l < N:
            Asum -= An[l]
            Axor ^= An[l]
        l += 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from pprint import pprint

MAX_bin = 20
N = int(input())
An = list(map(int, input().split()))

rui = []
bin_rui = [[0 for _ in range(MAX_bin)] for _ in range(N+1)]

tmp = 0
tmp_bin = [0 for _ in range(MAX_bin)]
for i,A in enumerate(An):
    rui.append(A+tmp)
    tmp += A

    binA = bin(A)[2:]
    tmpA = "0"*(MAX_bin - len(binA)) + binA

    for k in range(MAX_bin):
        bin_rui[i+1][k] = tmp_bin[k] + int(tmpA[k])
        tmp_bin[k] += int(tmpA[k])

# 尺取
renzoku = []
cnt = 0
l = 1
r = 1
while l <= N and r <= N:
    bin_l = bin_rui[l-1]
    bin_r = bin_rui[r]
    bin_diff = [0 for _ in range(MAX_bin)]
    # bin_r - bin_l
    for k in range(MAX_bin):
        bin_diff[k] = bin_r[k] - bin_l[k]

    if sum([1 for b in bin_diff if b > 1]) == 0:
        cnt += 1
        r += 1
        if r > N:
            renzoku.append(cnt)
            cnt = 0
            l += 1
            r = l
    else:
        renzoku.append(cnt)
        cnt = 0
        l += 1
        r = l

# pprint(renzoku)
print(sum(renzoku))


Fixed submission:
# -*- coding: utf-8 -*-
N = int(input())
An = list(map(int, input().split()))

# 尺取
ans = 0
l = 0
r = 0
Asum = An[0]
Axor = An[0]
while l < N and r < N:
    if Asum == Axor:
        ans += r-l+1
        r += 1
        if r < N:
            Asum += An[r]
            Axor ^= An[r]
    else:
        if l < N:
            Asum -= An[l]
            Axor ^= An[l]
        l += 1

print(ans)
Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-

H, W = map(int, input().split())
s = []

for i in range(H):
    s.append(list(input()))

for i in range(H):
    for j in range(W):
        OK_cnt = 0
        if s[i][j] == '#':
            #上
            if j > 0 and s[i][j-1] == '#':
                OK_cnt += 1
            #右
            if i+1 < W and s[i+1][j] == '#':
                OK_cnt += 1
            #下
            if j+1 < H and s[i][j+1] == '#':
                OK_cnt += 1
            #左
            if i > 0 and s[i-1][j] == '#':
                OK_cnt += 1
            if OK_cnt == 0:
                print('No')
                exit()

print('Yes')


Fixed submission:
# -*- coding: utf-8 -*-

H, W = map(int, input().split())
s = []

for i in range(H):
    s.append(list(input()))

for i in range(H):
    for j in range(W):
        OK_cnt = 0
        if s[i][j] == '#':
            #上
            if j > 0 and s[i][j-1] == '#':
                OK_cnt += 1
            #右
            if i+1 < H and s[i+1][j] == '#':
                OK_cnt += 1
            #下
            if j+1 < W and s[i][j+1] == '#':
                OK_cnt += 1
            #左
            if i > 0 and s[i-1][j] == '#':
                OK_cnt += 1
            if OK_cnt == 0:
                print('No')
                exit()

print('Yes')
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
N = int(input())
xn = list(map(int, input().split()))
xn_sorted = sorted(xn)

for xi in xn:
    xn_tmp = list(xn_sorted)
    xn_tmp.remove(xi)
    print(xn_tmp[(len(xn_tmp)+1)//2 -1])


Fixed submission:
# -*- coding: utf-8 -*-
N = int(input())
xn = list(map(int, input().split()))
xn_sorted = sorted(xn)
val_a = xn_sorted[(len(xn_sorted))//2 - 1]
val_b = xn_sorted[(len(xn_sorted))//2]

for xi in xn:
    if xi <= val_a:
        print(val_b)
    else:
        print(val_a)
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

xy = {}
for h in range(H):
    for w in range(W):
        xy[A[h][w]] = (h,w)

for l,r in LR:
    if l == r:
        print(0)
        continue

    start = l
    cost = 0
    while start != r:
        x,y = xy[start]
        i,j = xy[start+D]
        cost += abs(x-i) + abs(y-j)
        start += D

    print(cost)


Fixed submission:
# -*- coding: utf-8 -*-
import math
from pprint import pprint

H, W, D = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
Q = int(input())
LR = [list(map(int, input().split())) for _ in range(Q)]

xy = {}
for h in range(H):
    for w in range(W):
        xy[A[h][w]] = (h,w)

xy[0] = xy[D]

width = math.ceil(H*W/D)
rui_xy = [[0 for _ in range(width+1)] for _ in range(D)]
for d in range(D):
    rui_cost = 0
    x,y = xy[d]
    for w in range(width+1):
        if w*D+d > H*W:
            break
        i,j = xy[w*D+d]
        rui_cost += abs(x-i) + abs(y-j)
        rui_xy[d][w] = rui_cost
        x,y = i,j

for l,r in LR:
    if l == r:
        print(0)
        continue

    print(rui_xy[r % D][r//D] - rui_xy[l % D][l//D])