Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
B = [[0]*k for i in range(k)]
W = [[0]*k for i in range(k)]
for i in range(n):
    x, y, c = input().split(); x = int(x); y = int(y)
    x %= 2*k; y %= 2*k
    if (k <= x) == (k <= y):
        (B if c is 'B' else W)[y%k][x%k] += 1
    else:
        (W if c is 'B' else B)[y%k][x%k] += 1
def calc(M, n):
    D = [[0]*(n+1) for i in range(n+1)]
    for i in range(1, n+1):
        tmp = 0
        for j in range(1, n+1):
            tmp += M[i-1][j-1]
            D[i][j] = tmp + D[i-1][j]
    return D

# [a, c) * [b, d)
def su(D, a, b, c, d):
    return D[d][c] - D[d][a] - D[b][c] + D[b][a]

DB = calc(B, k)
DW = calc(W, k)
ans = 0
for i in range(k):
    for j in range(k):
        ans = max(ans, su(DB, 0, 0, i, j) + su(DB, i, j, k, k) + su(DW, i, 0, k, j) + su(DW, 0, j, i, k))
        ans = max(ans, su(DW, 0, 0, i, j) + su(DW, i, j, k, k) + su(DB, i, 0, k, j) + su(DB, 0, j, i, k))
print(ans)


Fixed submission:
n, k = map(int, input().split())
B = [[0]*k for i in range(k)]
W = [[0]*k for i in range(k)]
for i in range(n):
    x, y, c = input().split(); x = int(x); y = int(y)
    x %= 2*k; y %= 2*k
    if (k <= x) == (k <= y):
        (B if c == 'B' else W)[y%k][x%k] += 1
    else:
        (W if c == 'B' else B)[y%k][x%k] += 1
def calc(M, n):
    D = [[0]*(n+1) for i in range(n+1)]
    for i in range(1, n+1):
        tmp = 0
        for j in range(1, n+1):
            tmp += M[i-1][j-1]
            D[i][j] = tmp + D[i-1][j]
    return D

# [a, c) * [b, d)
def su(D, a, b, c, d):
    return D[d][c] - D[d][a] - D[b][c] + D[b][a]

DB = calc(B, k)
DW = calc(W, k)
ans = 0
for i in range(k):
    for j in range(k):
        ans = max(ans, su(DB, 0, 0, i, j) + su(DB, i, j, k, k) + su(DW, i, 0, k, j) + su(DW, 0, j, i, k))
        ans = max(ans, su(DW, 0, 0, i, j) + su(DW, i, j, k, k) + su(DB, i, 0, k, j) + su(DB, 0, j, i, k))
print(ans)
Buggy python submission (verdict: Runtime Error):
z,*s=map(len,input().split('T'));l=sum(s);Y=1<<l;X=Y<<z
for t,u in zip(s[::2],s[1::2]+[0]):Y=Y>>t|Y<<t;X=X>>u|X<<u
x,y=map(int,input().split());print("Yes"*(X>>l+x&Y>>l+y&1)or"No")


Fixed submission:
s=input()
x,y=map(int, input().split())
cnt = 0; mode = 2
l = len(s) # l + 1 + l
X = Y = 1 << l
for c in s:
    if c is 'F':
        cnt += 1
    else:
        if mode == 0:
            X = (X >> cnt) | (X << cnt)
        elif mode == 1:
            Y = (Y >> cnt) | (Y << cnt)
        else:
            X = X << cnt
            mode = 0
        cnt = 0; mode ^= 1
if cnt > 0:
    if mode == 0:
        X = (X >> cnt) | (X << cnt)
    elif mode == 1:
        Y = (Y >> cnt) | (Y << cnt)
    else:
        X = X << cnt
if (X >> (l + x)) & 1 and (Y >> (l + y)) & 1:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
z,*s=map(len,input().split('T')+['']);l=sum(s,0);Y=1<<l;X=Y<<z
for t,u in zip(s[::2],s[1::2]):Y=Y>>t|Y<<t;X=X>>u|X<<u
x,y=map(int,input().split());print("Yes"*(X>>l+x&Y>>l+y&1)or"No")

Fixed submission:
s=input()
x,y=map(int, input().split())
cnt = 0; mode = 2
l = len(s) # l + 1 + l
X = Y = 1 << l
for c in s:
    if c is 'F':
        cnt += 1
    else:
        if mode == 0:
            X = (X >> cnt) | (X << cnt)
        elif mode == 1:
            Y = (Y >> cnt) | (Y << cnt)
        else:
            X = X << cnt
            mode = 0
        cnt = 0; mode ^= 1
if cnt > 0:
    if mode == 0:
        X = (X >> cnt) | (X << cnt)
    elif mode == 1:
        Y = (Y >> cnt) | (Y << cnt)
    else:
        X = X << cnt
if (X >> (l + x)) & 1 and (Y >> (l + y)) & 1:
    print("Yes")
else:
    print("No")

Buggy python submission (verdict: Runtime Error):
z,*s,=map(len,input().split('T')+['']);l=sum(s);Y=1<<l;X=Y<<z
for t,u in zip(s[::2],s[1::2]):Y=Y>>t|Y<<t;X=X>>u|X<<u
x,y=map(int,input().split());print("Yes"*(X>>l+x&Y>>l+y&1)or"No")

Fixed submission:
s=input()
x,y=map(int, input().split())
cnt = 0; mode = 2
l = len(s) # l + 1 + l
X = Y = 1 << l
for c in s:
    if c is 'F':
        cnt += 1
    else:
        if mode == 0:
            X = (X >> cnt) | (X << cnt)
        elif mode == 1:
            Y = (Y >> cnt) | (Y << cnt)
        else:
            X = X << cnt
            mode = 0
        cnt = 0; mode ^= 1
if cnt > 0:
    if mode == 0:
        X = (X >> cnt) | (X << cnt)
    elif mode == 1:
        Y = (Y >> cnt) | (Y << cnt)
    else:
        X = X << cnt
if (X >> (l + x)) & 1 and (Y >> (l + y)) & 1:
    print("Yes")
else:
    print("No")
Buggy python submission (verdict: Runtime Error):
N, C = map(int, input().split())

su = [0]*(10**5+1)
Q = [[] for i in range(C)]
for i in range(N):
    s, t, c = map(int, input().split())
    Q[c-1].append([s, t])
R = [[] for i in range(C)]
for i in range(C):
    q = Q[i]; q.sort()
    r = R[i]
    r.append(q[0])
    for j in range(1, len(q)):
        if r[-1][1] == q[j][0]:
            r[-1][1] = q[j][1]
        else:
            r.append(q[j])
    for a, b in r:
        su[a-1] += 1
        su[b] -= 1
ans = 0; tmp = 0
for i in range(10**5+1):
    tmp += su[i]
    ans = max(ans, tmp)
print(ans)



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

su = [0]*(10**5+1)
Q = [[] for i in range(C)]
for i in range(N):
    s, t, c = map(int, input().split())
    Q[c-1].append([s, t])
R = [[] for i in range(C)]
for i in range(C):
    q = Q[i]; q.sort()
    r = R[i]
    if len(q) == 0:
        continue
    r.append(q[0])
    for j in range(1, len(q)):
        if r[-1][1] == q[j][0]:
            r[-1][1] = q[j][1]
        else:
            r.append(q[j])
    for a, b in r:
        su[a-1] += 1
        su[b] -= 1
ans = 0; tmp = 0
for i in range(10**5+1):
    tmp += su[i]
    ans = max(ans, tmp)
print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
t = "AKIHABARA"
idx = 0
ok = 1
for i in range(len(s)):
    if idx == len(t):
        ok = 0
        break
    if s[i] == t[idx]:
        idx += 1
    elif s[i] != 'A' and t[idx] == 'A':
        idx += 1
        if s[i] == t[idx]:
            idx += 1
        else:
            ok = 0
    else:
        ok = 0
if idx != len(t) and idx != len(t)-1:
    ok = 0
print("YES"*ok or "NO")

Fixed submission:
s = input()
t = "AKIHABARA"
idx = 0
ok = 1
for i in range(len(s)):
    if idx == len(t):
        ok = 0
        break
    if s[i] == t[idx]:
        idx += 1
    elif s[i] != 'A' and t[idx] == 'A':
        idx += 1
        if idx < len(t) and s[i] == t[idx]:
            idx += 1
        else:
            ok = 0
    else:
        ok = 0
if idx != len(t) and idx != len(t)-1:
    ok = 0
print("YES"*ok or "NO")

Buggy python submission (verdict: Runtime Error):
s = input()
t = "AKIHABARA"
idx = 0
ok = 1
for i in range(len(s)):
    if s[i] == t[idx]:
        idx += 1
    elif s[i] != 'A' and t[idx] == 'A':
        idx += 1
        if s[i] == t[idx]:
            idx += 1
        else:
            ok = 0
    elif idx == len(t):
        ok = 0
        break
    else:
        ok = 0
print("YES"*ok or "NO")


Fixed submission:
s = input()
t = "AKIHABARA"
idx = 0
ok = 1
for i in range(len(s)):
    if idx == len(t):
        ok = 0
        break
    if s[i] == t[idx]:
        idx += 1
    elif s[i] != 'A' and t[idx] == 'A':
        idx += 1
        if idx < len(t) and s[i] == t[idx]:
            idx += 1
        else:
            ok = 0
    else:
        ok = 0
if idx != len(t) and idx != len(t)-1:
    ok = 0
print("YES"*ok or "NO")
Buggy python submission (verdict: Runtime Error):
N, Z, W = map(int, input().split())
*A, = map(int, input().split())

memo = {}
def dfs(i, turn):
    if (i, turn) in memo:
        return memo[i, turn]
    if turn == 0:
        # X
        if i == 0:
            res = abs(W - A[-1])
        else:
            res = abs(A[i-1] - A[-1])
        for j in range(i+1, N):
            res = max(res, dfs(j, 1))
    else:
        # Y
        if i == 0:
            res = abs(Z - A[-1])
        else:
            res = abs(A[i-1] - A[-1])
        for j in range(i+1, N):
            res = min(res, dfs(j, 0))
    memo[i, turn] = res
    return res
print(dfs(0, 0))


Fixed submission:
N, Z, W = map(int, input().split())
*A, = map(int, input().split())

import sys
sys.setrecursionlimit(10**6)

memo = {}
def dfs(i, turn):
    if (i, turn) in memo:
        return memo[i, turn]
    if turn == 0:
        # X
        if i == 0:
            res = abs(W - A[-1])
        else:
            res = abs(A[i-1] - A[-1])
        for j in range(i+1, N):
            res = max(res, dfs(j, 1))
    else:
        # Y
        if i == 0:
            res = abs(Z - A[-1])
        else:
            res = abs(A[i-1] - A[-1])
        for j in range(i+1, N):
            res = min(res, dfs(j, 0))
    memo[i, turn] = res
    return res
print(dfs(0, 0))
Buggy python submission (verdict: Time Limit Exceeded):
s = input()
n = len(s)
cA = ord('a')


even = [10**18]*n
odd = [[10**18]*26 for i in range(n)]
su = [0]*n
cp = 1 << (ord(s[0])-cA)
P = {cp: 0}
prev = [-1]*26
dp = [10**18 for i in range(n)]

dp[0] = 1
su[0] = cp
prev[ord(s[0])-cA] = 0
for i in range(1, n):
    code = ord(s[i]) - cA
    dp[i] = min(dp[i], dp[i-1] + 1)
    pv = prev[code]
    if pv != -1:
        need = su[i-1] ^ su[pv]
        #print(need, pv)
        state = need ^ su[pv] ^ (1 << code)
        #print(i, state, P)
        if state == 0:
            dp[i] = 1
        elif state in P:
            j = P[state]
            dp[i] = min(dp[i], dp[j] + 1)
    if cp == 0:
        dp[i] = 1
    if cp in P:
        j = P[cp]
        dp[i] = min(dp[i], dp[j] + 1)
    cp ^= 1 << code
    if cp not in P:
        P[cp] = i
    prev[code] = i
    su[i] = cp

    for j in range(26):
        pv = prev[j]
        if pv != -1:
            need = su[i] ^ su[pv]
            state = need ^ su[pv] ^ (1 << j)
            if state == 0:
                dp[i] = 1
            elif state in P:
                k = P[state]
                dp[i] = min(dp[i], dp[k] + 1)
ans = dp[n-1]
#print(dp)
for i in range(26):
    pv = prev[i]
    if pv != -1:
        need = su[n-1] ^ su[pv]
        #print(bin(need), pv)
        state = need ^ su[pv] ^ (1 << i)
        #print(i, state, P)
        if state == 0:
            ans = 1
        elif state in P:
            j = P[state]
            #print(">", i, j)
            ans = min(ans, dp[j] + 1)
print(ans)


Fixed submission:
s = input()
n = len(s)
cA = ord('a')
P = {0: 0}
cur = 0

dp = [10**18]*(n+1)
dp[-1] = 0

for i in range(n):
    c = ord(s[i]) - cA
    dp[i] = dp[i-1] + 1
    cur ^= 1 << c
    # even
    if cur in P:
        dp[i] = min(dp[i], P[cur] + 1)
    # odd
    for k in range(26):
        need = cur ^ (1 << k)
        if need in P:
            dp[i] = min(dp[i], P[need] + 1)
    P[cur] = min(P.get(cur, 10**18), dp[i])
print(dp[n-1])
Buggy python submission (verdict: Runtime Error):
S=map(int, input().split())
S.sort()
print(S[0] if S[1] == S[2] else S[2])

Fixed submission:
*S,=map(int, input().split())
S.sort()
print(S[0] if S[1] == S[2] else S[2])
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
X = set(); Y = set()
P = set()
for i in range(n):
    x, y = map(int, input().split())
    X.add(x); Y.add(y)
    P.add((x, y))

X = sorted(X); w = len(X)
Y = sorted(Y); h = len(Y)
MX = {x: i for i, x in enumerate(X)}
MY = {y: i for i, y in enumerate(Y)}
Q = {(MX[x], MY[y]) for x, y in P}
S = [[0]*(w+1) for i in range(h+1)]
for i in range(h):
    tmp = 0
    for j in range(w):
        if (j, i) in Q:
            tmp += 1
        S[i+1][j+1] = S[i][j+1] + tmp

ans = 10**100
for y1 in range(h):
    for x1 in range(w):
        for y2 in range(h):
            for x2 in range(w):
                count = S[y2+1][x2+1] - S[y2+1][x1] - S[y1][x2+1] + S[y1][x1]
                if count >= k:
                    ans = min(ans, (X[x2]-X[x1])*(Y[y2]-Y[y1]))
print(ans)


Fixed submission:
n, k = map(int, input().split())
X = set(); Y = set()
P = set()
for i in range(n):
    x, y = map(int, input().split())
    X.add(x); Y.add(y)
    P.add((x, y))

X = sorted(X); w = len(X)
Y = sorted(Y); h = len(Y)
MX = {x: i for i, x in enumerate(X)}
MY = {y: i for i, y in enumerate(Y)}
Q = {(MX[x], MY[y]) for x, y in P}
S = [[0]*(w+1) for i in range(h+1)]
for i in range(h):
    tmp = 0
    for j in range(w):
        if (j, i) in Q:
            tmp += 1
        S[i+1][j+1] = S[i][j+1] + tmp

ans = 10**100
for y1 in range(h):
    for x1 in range(w):
        for y2 in range(h):
            for x2 in range(w):
                count = S[y2+1][x2+1] - S[y2+1][x1] - S[y1][x2+1] + S[y1][x1]
                if count >= k:
                    ans = min(ans, (X[x2]-X[x1])*(Y[y2]-Y[y1]))
print(ans)
Buggy python submission (verdict: Runtime Error):
n = int(input())
A = [list(map(int, input().split())) for i in range(n)]
B = [e[:] for e in A]

for k in range(n):
    for i in range(n):
        for j in range(n):
            B[i][j] = min(B[i][k] + B[k][j], B[i][j])
ok = 1
for i in range(n):
    for j in range(n):
        if A[i][j] != B[i][j]:
            ok = 0
if not ok:
    print(-1)
    exit(0)

ans = 0
for i in range(n):
    for j in range(i+1, n):
        need = 1
        for k in range(n):
            if i == k or j == k:
                continue
            if A[i][k] + A[k][j] == A[i][j]:
                need = 0
        if need:
            ans += A[i][j]
print(ans)


Fixed submission:
import sys
sys.setrecursionlimit(100000)
a, b, c, d, e, f = map(int, input().split())
def dfs(g, a, b, can, base=100):
    if not base*g <= f:
        return
    if g in can:
        return
    can[g] = 1
    dfs(g + a, a, b, can, base)
    dfs(g + b, a, b, can, base)
can1 = {}
dfs(0, a, b, can1)
can2 = {}
dfs(0, c, d, can2, 1)

ans = (0, 0, 0)
for g in can1:
    if g == 0:
        continue
    rest = f - 100*g
    result = max([0]+[h for h in can2 if h <= min(rest, e*g)])
    ans = max(ans, (result / (100.*g + result), 100*g+result, result))
print(*ans[1:])
Buggy python submission (verdict: Time Limit Exceeded):
n, m, r = map(int, input().split())
*R, = map(int, input().split())
E = [[10**18]*n for i in range(n)]
for i in range(n):
    E[i][i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    E[a-1][b-1] = E[b-1][a-1] = c
for k in range(n):
    for i in range(n):
        for j in range(n):
            E[i][j] = min(E[i][j], E[i][k] + E[k][j])

memo = {}
ALL = 2**r-1
def dfs(state, pos):
    if (state, pos) in memo:
        return memo[state, pos]
    if state == ALL:
        return 0
    res = 10**18
    for i in range(r):
        if state & (1 << i) == 0:
            res = min(res, dfs(state | (1 << i), R[i]-1) + E[pos][R[i]-1])
    memo[state, pos] = res
    return res
print(min(dfs(0, i) for i in range(n)))

Fixed submission:
n, m, r = map(int, input().split())
*R, = map(int, input().split())
E = [[10**18]*n for i in range(n)]
for i in range(n):
    E[i][i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    E[a-1][b-1] = E[b-1][a-1] = c
for k in range(n):
    for i in range(n):
        for j in range(n):
            E[i][j] = min(E[i][j], E[i][k] + E[k][j])

memo = {}
ALL = 2**r-1
def dfs(state, pos):
    if (state, pos) in memo:
        return memo[state, pos]
    if state == ALL:
        return 0
    res = 10**18
    for i in range(r):
        if state & (1 << i) == 0:
            res = min(res, dfs(state | (1 << i), R[i]-1) + E[pos][R[i]-1])
    memo[state, pos] = res
    return res
print(min(dfs(0, i) for i in range(n)))
Buggy python submission (verdict: Time Limit Exceeded):
n,m,*T=map(int,open(0).read().split());*S,=zip(T[::2],T[1::2]);s=lambda e:set(sum([s for s in S if e in s],()));print("IM"*(len(s(1)&s(n))<1)+"POSSIBLE")

Fixed submission:
n,m,*T=map(int,open(0).read().split());*S,=zip(T[::2],T[1::2]);s=lambda e:set(s[e<2]for s in S if e in s);print("IM"*(len(s(1)&s(n))<1)+"POSSIBLE")

Buggy python submission (verdict: Runtime Error):
n,m,*T=map(int,open(0).read().split());*S,=zip(T[::2],T[1::2]);s=lambda e:{*sum([s for s in S if e in s],())};print("IM"*(len(s(1)&s(n))<1)+"POSSIBLE")

Fixed submission:
n,m,*T=map(int,open(0).read().split());*S,=zip(T[::2],T[1::2]);s=lambda e:set(s[e<2]for s in S if e in s);print("IM"*(len(s(1)&s(n))<1)+"POSSIBLE")
Buggy python submission (verdict: Time Limit Exceeded):
from collections import*
n=input();G=[[]for d in[deque([1,-1])]*-~n]
for C in[[0,1]+[0]*(n-2)+[2]]*~-n:a,b=map(int,raw_input().split());G[a]+=b,;G[b]+=a,
while d:
 v=d.popleft()
 for t in G[v]:exec"C[t]=C[v];d+=t,"*(C[t]<1)
print["Fennec","Snuke"][2*C.count(1)<=n]

Fixed submission:
n = input()
G = [[] for i in xrange(n)]
for i in xrange(n-1):
    a, b = map(int, raw_input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)

from collections import deque
deq = deque()
deq.append((0, 0))
deq.append((n-1, 1))
C = [None]*n
C[0] = 0; C[n-1] = 1
while deq:
    v, col = deq.popleft()
    for t in G[v]:
        if C[t] is None:
            C[t] = col
            deq.append((t, col))
B = C.count(0)
W = C.count(1)
if B == W:
    print "Snuke"
elif B < W:
    print "Snuke"
elif B > W:
    print "Fennec"
Buggy python submission (verdict: Runtime Error):
n=int(input());A=input().split();print(*A[-1::-2],*A[n%2::2])

Fixed submission:
n = input()
A = raw_input().split()
B = A[0::2]
C = A[1::2]
if n % 2 == 1:
    print " ".join(B[::-1] + C)
else:
    print " ".join(C[::-1] + B)
Buggy python submission (verdict: Time Limit Exceeded):
n,*A=map(int,open(0).read().split())
f=[1]*(n+2);r=f[:]
MOD=10**9+7
for i in range(n+1):f[i+1]=e=f[i]*-~i%MOD;r[i+1]=pow(e,MOD-2,MOD)
for i in range(1,n+1):
    try:
        L=A.index(i);R=A.index(i,L+1)
        for k in range(1,n+2):
            print((f[n+1]*r[k]*r[n+1-k]-(k<=L+n+1-R and f[L-1+n+1-R]*r[k-1]*r[L+n+1-k-R]))%MOD)
    except:...


Fixed submission:
n = input()
A = map(int, raw_input().split())
fact = [0]*(n+1)
frev = [0]*(n+1)
MOD = 10**9 + 7
frev[0] = fact[0] = 1

for i in xrange(1, n+1):
    fact[i] = (fact[i-1] * i) % MOD
    frev[i] = pow(fact[i], MOD-2, MOD)

def comb(n, k):
    return (((fact[n] * frev[k]) % MOD) * frev[n-k]) % MOD

left = 0; right = 0
B = [0]*(n+1)
for a in A:
    B[a] += 1
p = B.index(2)
while A[left] != p:
    left += 1
while A[-1-right] != p:
    right += 1
mid = n+1 - left - right
ans = []
for k in xrange(1, n+2):
    res = 0
    if 0 <= k <= n-1:
        res += comb(n-1, k)
    if 0 <= k-1 <= n-1:
        if k-1 <= left+right:
            res += comb(left+right, k-1) + 2*(comb(n-1, k-1) - comb(left+right, k-1))
        else:
            res += 2*comb(n-1, k-1)
    if 0 <= k-2 <= n-1:
        res += comb(n-1, k-2)
    res %= MOD
    ans.append(res)
print "\n".join(map(str, ans))
Buggy python submission (verdict: Time Limit Exceeded):
_,m,*A=map(int,open(0).read().split());S=l=3*m;D=[0]*l
for a,b in zip(A,A[1:]):
 b+=m*(b<a);k=b-a;S+=k
 if k>1:D[a+2]+=1;D[b+1]-=k;D[b+2]+=k-1
exec(("i=0;"+"D[i]+=D[i-1];i+=1;"*l)*2);print(S-l-max(D[a]+D[a+m]for a in A))

Fixed submission:
n, m = map(int, raw_input().split())
A = map(int, raw_input().split())
S = 0
for i in xrange(n-1):
    S += (m + A[i+1] - A[i]) % m

T = 2*m
D = [0]*(T+10)
data = [0]*(T+1)
def get(i):
    s = 0
    while i:
        s += data[i]
        i -= i & -i
    return s
def add(i, x):
    while i <= T:
        data[i] += x
        i += i & -i

for i in xrange(n-1):
    x = A[i]; y = A[i+1]
    if y < x:
        y += m
    k = y-x
    if k > 1:
        D[x+2] += 1
        D[y+1] -= k
        D[y+2] += k-1
for i in xrange(1, 2*m+5):
    D[i] += D[i-1]
    add(i, D[i])
res = min(S - (get(a) + get(a+m)) for a in A)
print res

Buggy python submission (verdict: Time Limit Exceeded):
_,m,*A=map(int,open(0).read().split());S=l=3*m;D=[0]*l
for a,b in zip(A,A[1:]):
 b+=m*(b<a);k=b-a;S+=k
 if k>1:D[a+2]+=1;D[b+1]-=k;D[b+2]+=k-1
exec(("i=0;"+"D[i]+=D[i-1];i+=1;"*l)*2);print(S-l-max(D[a]+D[a+m]for a in A))

Fixed submission:
n, m = map(int, raw_input().split())
A = map(int, raw_input().split())
S = 0
for i in xrange(n-1):
    S += (m + A[i+1] - A[i]) % m

T = 2*m
D = [0]*(T+10)
data = [0]*(T+1)
def get(i):
    s = 0
    while i:
        s += data[i]
        i -= i & -i
    return s
def add(i, x):
    while i <= T:
        data[i] += x
        i += i & -i

for i in xrange(n-1):
    x = A[i]; y = A[i+1]
    if y < x:
        y += m
    k = y-x
    if k > 1:
        D[x+2] += 1
        D[y+1] -= k
        D[y+2] += k-1
for i in xrange(1, 2*m+5):
    D[i] += D[i-1]
    add(i, D[i])
res = min(S - (get(a) + get(a+m)) for a in A)
print res
Buggy python submission (verdict: Time Limit Exceeded):
R, C, N = map(int, input().split())
def calc(x, y):
    if x == 0 or y == C:
        return x + y
    return 2*R + 2*C - x - y
A = [None]*(2*R + 2*C)
for i in range(N):
    x1, y1, x2, y2 = map(int, input().split())
    if not ((x1 in [0, R] or y1 in [0, C]) and (x2 in [0, R] or y2 in [0, C])):
        continue
    A[calc(x1, y1)] = i
    A[calc(x2, y2)] = i

st = []
for a in A:
    if st and st[-1] == a:
        st.pop()
    elif a is not None:
        st.append(a)
print('YES' if not st else 'NO')


Fixed submission:
R, C, N = map(int, input().split())
def calc(x, y):
    if x == 0 or y == C:
        return x + y
    return 2*R + 2*C - x - y
A = {}
for i in range(N):
    x1, y1, x2, y2 = map(int, input().split())
    if not ((x1 in [0, R] or y1 in [0, C]) and (x2 in [0, R] or y2 in [0, C])):
        continue
    A[calc(x1, y1)] = i
    A[calc(x2, y2)] = i

st = []
for i, a in sorted(A.items()):
    if st and st[-1] == a:
        st.pop()
    elif a is not None:
        st.append(a)
print('YES' if not st else 'NO')
Buggy python submission (verdict: Runtime Error):
print("YES"*(int(input()[::2])%4<1)or"NO")


Fixed submission:
print "YES"*(int(raw_input().replace(" ", ""))%4==0)or"NO"