Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N, K, *H = map(int, open(0).read().split())
H.extend([float('inf')] * K)
dp = [float('inf')] * (N + K)
dp[0] = 0
for i, hi in enumerate(H[1:], 1):
    dp[i] = min(dp[i - k] + abs(H[i - k] - hi) for k in range(1, K + 1))
print(dp[N - 1])

Fixed submission:
N, K, *H = map(int, open(0).read().split())
H.extend([float('inf')] * K)
dp = [float('inf')] * (N + K)
dp[0] = 0
for i, hi in enumerate(H[1:], 1):
    dp[i] = min(dp[i - k] + abs(H[i - k] - hi) for k in range(1, K + 1))
print(dp[N - 1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split()))
dp = [float('inf')] * N
dp[0] = 0
for i in range(1, N):
    for j in range(1, K + 1):
        if i - j < 0:
            break
        dp[i] = min(dp[i], dp[i - j] + abs(H[i - j] - H[i]))
print(dp[N - 1])

Fixed submission:
N, K, *H = map(int, open(0).read().split())
H.extend([float('inf')] * K)
dp = [float('inf')] * (N + K)
dp[0] = 0
for i, hi in enumerate(H[1:], 1):
    dp[i] = min(dp[i - k] + abs(H[i - k] - hi) for k in range(1, K + 1))
print(dp[N - 1])

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
H = list(map(int, input().split())) + [float('inf')] * K
dp = [float('inf')] * (N + K)
dp[0] = 0
for i in range(1, N):
    for j in range(1, K + 1):
        dp[i] = min(dp[i], dp[i - j] + abs(H[i - j] - H[i]))
print(dp[N - 1])

Fixed submission:
N, K, *H = map(int, open(0).read().split())
H.extend([float('inf')] * K)
dp = [float('inf')] * (N + K)
dp[0] = 0
for i, hi in enumerate(H[1:], 1):
    dp[i] = min(dp[i - k] + abs(H[i - k] - hi) for k in range(1, K + 1))
print(dp[N - 1])
Buggy python submission (verdict: Time Limit Exceeded):
import sys
LI = lambda: list(map(int, sys.stdin.readline().split()))
N, W = LI()
dp = [0] * (W + 1)
for weight, value in sorted(LI() for _ in range(N)):
    for w in range(W - weight, -1, -1):
        if dp[w + weight] < dp[w] + value:
            dp[w + weight] = dp[w] + value
print(dp[-1])

Fixed submission:
import sys
LI = lambda: list(map(int, sys.stdin.readline().split()))
N, W = LI()
dp = [0] * (W + 1)
for weight, value in sorted(LI() for _ in range(N)):
    for w in range(W - weight, -1, -1):
        if dp[w + weight] < dp[w] + value:
            dp[w + weight] = dp[w] + value
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
from collections import namedtuple
Item = namedtuple('Item', ['weight', 'value'])
N, W = map(int, input().split())
items = [Item(*map(int, input().split())) for _ in range(N)]
dp = [[0] * (W + 1) for _ in range(N + 1)]
for i, item in enumerate(items, 1):
    for cap in range(1, W + 1):
        if cap - item.weight >= 0:
            dp[i][cap] = max(dp[i - 1][cap],
                             dp[i - 1][cap - item.weight] + item.value)
        else:
            dp[i][cap] = dp[i - 1][cap]
print(dp[-1][-1])

Fixed submission:
import sys
LI = lambda: list(map(int, sys.stdin.readline().split()))
N, W = LI()
dp = [0] * (W + 1)
for weight, value in sorted(LI() for _ in range(N)):
    for w in range(W - weight, -1, -1):
        if dp[w + weight] < dp[w] + value:
            dp[w + weight] = dp[w] + value
print(dp[-1])
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
S = input()
T = input()


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


k = n * m // gcd(n, m)


for i in range(k, n * m + 1, k):
    g = i * i // n // m // gcd(i // n, i // m)
    flag = True
    for c in range(0,i,g):
        if S[c * n // i] != T[c * m // i]:
            flag = False
            break
    if flag:
        print(i)
        exit()
print(-1)

Fixed submission:
n, m = map(int, input().split())
S = input()
T = input()


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


k = n * m // gcd(n, m)


for i in range(k, n * m + 1, k):
    g = i * i // n // m // gcd(i // n, i // m)
    flag = True
    for c in range(0, i, g):
        if S[c * n // i] != T[c * m // i]:
            flag = False
            break
    if flag:
        print(i)
        exit()
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
S = input()
T = input()


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


k = n * m // gcd(n, m)


for i in range(k, n * m + 1, k):
    ss = {a * (i // n) for a in range(n)}
    tt = {a * (i // m) for a in range(m)}
    st = ss & tt
    flag = True
    for c in st:
        if S[c * n // i] != T[c * m // i]:
            flag = False
            break
    if flag:
        print(i)
        exit()
print(-1)

Fixed submission:
n, m = map(int, input().split())
S = input()
T = input()


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


k = n * m // gcd(n, m)


for i in range(k, n * m + 1, k):
    g = i * i // n // m // gcd(i // n, i // m)
    flag = True
    for c in range(0, i, g):
        if S[c * n // i] != T[c * m // i]:
            flag = False
            break
    if flag:
        print(i)
        exit()
print(-1)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
S = input()
T = input()

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

k = n * m // gcd(n, m)


for i in range(k, n * m + 1, k):
    ss = {a*(i//n) for a in range(n)}
    tt = {a*(i//m) for a in range(m)}
    st = ss & tt
    x = ''.join(S[c//(i//n)]for c in st)
    y = ''.join(T[c//(i//m)]for c in st)
    if x == y:
        print(i)
        exit()
print(-1)

Fixed submission:
n, m = map(int, input().split())
S = input()
T = input()


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


k = n * m // gcd(n, m)


for i in range(k, n * m + 1, k):
    g = i * i // n // m // gcd(i // n, i // m)
    flag = True
    for c in range(0, i, g):
        if S[c * n // i] != T[c * m // i]:
            flag = False
            break
    if flag:
        print(i)
        exit()
print(-1)
Buggy python submission (verdict: Runtime Error):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c1 = Counter(A[::2])
c2 = Counter(A[1::2])
c1m = max(c1.items(), key=lambda x:x[1])
c2m = max(c2.items(), key=lambda x:x[1])
c1m[0] = 0
c2m[0] = 0
c1c2 = max(c for k,c in c2.items() if c1m[0]!=k)
c2c1 = max(c for k,c in c1.items() if c2m[0]!=k)
print(min(N - c1m[1] - c1c2, N - c2m[1] - c2c1))

Fixed submission:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
c1 = Counter(A[::2])
c2 = Counter(A[1::2])
c1[0] = 0
c2[0] = 0
c1m = max(c1.items(), key=lambda x:x[1])
c2m = max(c2.items(), key=lambda x:x[1])
c1c2 = max(c for k,c in c2.items() if c1m[0]!=k)
c2c1 = max(c for k,c in c1.items() if c2m[0]!=k)
print(min(N - c1m[1] - c1c2, N - c2m[1] - c2c1))
Buggy python submission (verdict: Runtime Error):
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
  int N, M; cin >> N >> M;
  int XMAX, YMIN; cin >> XMAX >> YMIN;
  for (int i = 0; i < N; ++i) {
    int x; cin >> x;
    XMAX = max(XMAX, x);
  }
  for (int i = 0; i < M; ++i) {
    int y; cin >> y;
    YMIN = min(YMIN, y);
  }
  cout << (XMAX < YMIN ? "No War" : "War") << endl;
}

Fixed submission:
N, M, X, Y = map(int, input().split())
x = max(map(int, input().split()))
y = min(map(int, input().split()))

for z in range(x + 1, y + 1):
    if X < z <= Y:
        print('No War')
        exit()

print('War')
Buggy python submission (verdict: Runtime Error):
print('YNeos'[1-'2'in input()::2])

Fixed submission:
A,B=map(int,input().split())
if A*B % 2:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
a=[list(map(int,input().split())) for _ in[0]*H]
res=[]
for i in range(H):
    for j in range(W-1):
        if a[i][j]%2==1:
            a[i][j]-=1
            a[i][j+1]+=1
            res.append([i,j,i,j+1])
for i in range(H-1):
    if a[i][-1]%2==1:
        a[i][-1]-=1
        a[i+1][-1]+=1
        res.append([i,j,i+1,j])
print(len(res))
for a,b,c,d in res:
    print(a+1,b+1,c+1,d+1)

Fixed submission:
H,W=map(int,input().split())
a=[list(map(int,input().split())) for _ in[0]*H]
res=[]
for i in range(H):
    for j in range(W-1):
        if a[i][j]%2==1:
            a[i][j]-=1
            a[i][j+1]+=1
            res.append([i,j,i,j+1])
for i in range(H-1):
    if a[i][-1]%2==1:
        a[i][-1]-=1
        a[i+1][-1]+=1
        res.append([i,W-1,i+1,W-1])
print(len(res))
for a,b,c,d in res:
    print(a+1,b+1,c+1,d+1)

Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
a=[list(map(int,input().split())) for _ in[0]*H]
res=[]
for i in range(H):
    for j in range(W-1):
        if a[i][j]%2==1:
            a[i][j]-=1
            a[i][j+1]+=1
            res.append([i,j,i,j+1])
for i in range(H-1):
    if a[i][-1]%2==1:
        a[i][-1]-=1
        a[i+1][-1]+=1
        res.append([i,j,i+1,j])
print(len(res))
for x in res:
    print(*x)

Fixed submission:
H,W=map(int,input().split())
a=[list(map(int,input().split())) for _ in[0]*H]
res=[]
for i in range(H):
    for j in range(W-1):
        if a[i][j]%2==1:
            a[i][j]-=1
            a[i][j+1]+=1
            res.append([i,j,i,j+1])
for i in range(H-1):
    if a[i][-1]%2==1:
        a[i][-1]-=1
        a[i+1][-1]+=1
        res.append([i,W-1,i+1,W-1])
print(len(res))
for a,b,c,d in res:
    print(a+1,b+1,c+1,d+1)

Buggy python submission (verdict: Runtime Error):
H,W=map(int,input().split())
a=[list(map(int,input().split())) for _ in[0]*H]
res=[]
for i in range(H):
    for j in range(W-1):
        if a[i][j]%2==1:
            res.append([i,j,i,j+1])
for i in range(H-1):
    if a[i][-1]%2==1:
        res.append([i,j,i+1,j])
print(len(res))
for x in res:
    print(*x)


Fixed submission:
H,W=map(int,input().split())
a=[list(map(int,input().split())) for _ in[0]*H]
res=[]
for i in range(H):
    for j in range(W-1):
        if a[i][j]%2==1:
            a[i][j]-=1
            a[i][j+1]+=1
            res.append([i,j,i,j+1])
for i in range(H-1):
    if a[i][-1]%2==1:
        a[i][-1]-=1
        a[i+1][-1]+=1
        res.append([i,W-1,i+1,W-1])
print(len(res))
for a,b,c,d in res:
    print(a+1,b+1,c+1,d+1)
Buggy python submission (verdict: Runtime Error):
N,K,*x=map(int,open(0).read().split());print(min(min(abs(x[i]),abs(x[i+k-1]))+x[i+k-1]-x[i]for i in range(n-k+1)))

Fixed submission:
n,k=map(int,input().split())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(n-k+1):
    a,b=X[i],X[i+k-1]
    if b<0:
        ans=min(ans,-a)
    elif a>0:
        ans=min(ans,b)
    else:
        tmp=min(-a,b)*2+max(-a,b)
        ans=min(ans,tmp)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(n-k+1):
    t = X[i:i+k]
    a,b=t[0],t[-1]
    if b<0:
        ans=min(ans,-a)
    elif a>0:
        ans=min(ans,b)
    else:
        tmp=min(-a,b)*2+max(-a,b)
        ans=min(ans,tmp)
print(ans)

Fixed submission:
n,k=map(int,input().split())
X=list(map(int,input().split()))
ans=float('inf')
for i in range(n-k+1):
    a,b=X[i],X[i+k-1]
    if b<0:
        ans=min(ans,-a)
    elif a>0:
        ans=min(ans,b)
    else:
        tmp=min(-a,b)*2+max(-a,b)
        ans=min(ans,tmp)
print(ans)
Buggy python submission (verdict: Runtime Error):
S=list(input())
K=int(input())
idx=[-1]*10
for i in '123456789':
    if i in S:
        idx[int(i)] = S.index(i)+1
if K < max(idx[2:]):
    print(1)
else:
    print(S[min(i for i in idx[2:]if i>0)-1])

Fixed submission:
S=list(input())
K=int(input())
idx=[-1]*10
for i in '123456789':
    if i in S:
        idx[int(i)] = S.index(i)+1
if idx[2:]==[-1]*8 or K < max(idx[2:]):
    print(1)
else:
    print(S[min(i for i in idx[2:]if i>0)-1])
Buggy python submission (verdict: Runtime Error):
N = int(input())
res = []
for i in range(32):
    digit = (-2) ** i
    if N % (digit * (-2)) != 0:
        N -= digit
        res.append('1')
    else:
        res.append('0')
while res[-1] == '0':
    res.pop()
print(''.join(res[::-1]))

Fixed submission:
N = int(input())
if N == 0:
    print('0')
    exit()
res = []
for i in range(32):
    digit = (-2) ** i
    if N % (digit * (-2)) != 0:
        N -= digit
        res.append('1')
    else:
        res.append('0')
while res[-1] == '0':
    res.pop()
print(''.join(res[::-1]))
Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7
dp = [0] * 4
dp[0] = 1
idx = {'A': 1, 'B': 2, 'C': 3}
for c in input():
    if c == '?':
        N, A, AB, ABC = dp
        dp = [N * 3, A * 3 + N, AB * 3 + A, ABC * 3 + AB]
    else:
        dp[idx[c]] += dp[idx[c] - 1]
print(dp[3] % MOD)

Fixed submission:
M = 10**9 + 7
dp = [0] * 4
dp[0] = 1
idx = {'A': 1, 'B': 2, 'C': 3}
for c in input():
    if c == '?':
        N, A, AB, ABC = dp
        dp = [(N * 3) % M, (A * 3 + N) % M, (AB * 3 + A) % M, (ABC * 3 + AB) % M]
    else:
        dp[idx[c]] += dp[idx[c] - 1]
print(dp[3] % M)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7
S = input()
dp = [[0] * 4 for _ in [0] * (len(S) + 1)]
dp[0][0] = 1
for i, c in enumerate(S):
    for j in range(4):
        dp[i + 1][j] += dp[i][j] * (1 + (c == '?') * 2)
    if c in 'A?':
        dp[i + 1][1] += dp[i][0]
    if c in 'B?':
        dp[i + 1][2] += dp[i][1]
    if c in 'C?':
        dp[i + 1][3] += dp[i][2]
print(dp[-1][3] % MOD)

Fixed submission:
M = 10**9 + 7
dp = [0] * 4
dp[0] = 1
idx = {'A': 1, 'B': 2, 'C': 3}
for c in input():
    if c == '?':
        N, A, AB, ABC = dp
        dp = [(N * 3) % M, (A * 3 + N) % M, (AB * 3 + A) % M, (ABC * 3 + AB) % M]
    else:
        dp[idx[c]] += dp[idx[c] - 1]
print(dp[3] % M)
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
E=[(1,N)]
for _ in[0]*M:
    a,b=map(int,input().split())
    E+=[(a,b)]
    F=[]
    for s,t in E:
        if s<=a and b<=t:
            F+=[(a,b)]
        else:
            F+=[(s,t)]
    E=F
cnt=0
while E:
    cnt+=1
    M, Mi = 0, 0
    A = [0]*N
    l = len(E)
    B = [set(range(l))for _ in[0]*N]
    for k,(s,t) in enumerate(E):
        for i in range(s,t):
            A[i]+=1
            B[i]-={k}
            if M<A[i]:
                M, Mi = A[i], i
    E=[E[i]for i in B[Mi]]
print(cnt)

Fixed submission:
n,m=map(int,input().split())
ab=sorted([list(map(int,input().split()))for _ in[0]*m],key=lambda x:x[1])
cnt=0
x=0
for a,b in ab:
   if x<a:x=b-1;cnt+=1
print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
E={(1,N)}
for _ in[0]*M:
    a,b=map(int,input().split())
    E|={(a,b)}
    F=set()
    for s,t in E:
        if s<=a and b<=t:
            F.add((a,b))
        else:
            F.add((s,t))
    E=F
cnt=0
E=list(E)
while E:
    cnt+=1
    M, Mi = 0, 0
    A = [0]*N
    l = len(E)
    B = [set(range(l))for _ in[0]*N]
    for k,(s,t) in enumerate(E):
        for i in range(s,t):
            A[i]+=1
            B[i]-={k}
            if M<A[i]:
                M, Mi = A[i], i
    E=[E[i]for i in B[Mi]]
print(cnt)

Fixed submission:
n,m=map(int,input().split())
ab=sorted([list(map(int,input().split()))for _ in[0]*m],key=lambda x:x[1])
cnt=0
x=0
for a,b in ab:
   if x<a:x=b-1;cnt+=1
print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[a-i-1 for i,a in enumerate(map(int,input().split()))]
k=sum(A)//N+1
print(min(sum(abs(a-x)for a in A)for x in range(min(0,k-1),max(k+1,1))))

Fixed submission:
N=int(input())//2
A=sorted(a-i-1 for i,a in enumerate(map(int,input().split())))
print(sum(abs(a-A[N])for a in A))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[a-i-1 for i,a in enumerate(map(int,input().split()))]
k=abs(sum(A)//N+1)
print(min(sum(abs(a-x)for a in A)for x in range(-k-1,k+1)))

Fixed submission:
N=int(input())//2
A=sorted(a-i-1 for i,a in enumerate(map(int,input().split())))
print(sum(abs(a-A[N])for a in A))

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=[a-i-1 for i,a in enumerate(map(int,input().split()))]
print(sum(abs(a-sum(A)//N) for a in A))

Fixed submission:
N=int(input())//2
A=sorted(a-i-1 for i,a in enumerate(map(int,input().split())))
print(sum(abs(a-A[N])for a in A))
Buggy python submission (verdict: Time Limit Exceeded):
'''WA
N,K=map(int,input().split())
A=list(map(int,input().split()))
x=A.index(1)
K-=1
print((x-1)//K+(N-x-2)//K+2)
'''
N,K=map(int,input().split())
A=list(map(int,input().split()))
cnt=0
while 1:
    idx=A.index(1)
    if idx==0:break
    if idx+1<=K:
        A[:K]=[1]*K
    else:
        A[idx-K+1:idx+1]=[1]*K
    cnt+=1
A=A[::-1]
while 1:
    idx=A.index(1)
    if idx==0:break
    if idx+1<=K:
        A[:K]=[1]*K
    else:
        A[idx-K+1:idx+1]=[1]*K
    cnt+=1
print(cnt)

Fixed submission:
N,K=map(int,input().split())
A=list(map(int,input().split()))
idx=A.index(1)
cnt=0
if idx:
    if idx+1<=K:
        cnt=1
        A[:K]=[1]*K
    else:
        cnt=(idx-1)//(K-1)+1
        num=cnt*(K-1)+1
        A[:num]=[1]*num
A=A[::-1]
idx=A.index(1)
if idx:
    if idx+1<=K:
        cnt+=1
        A[:K]=[1]*K
    else:
        cnt+=(idx-1)//(K-1)+1
        num=cnt*(K-1)+1
        A[:num]=[1]*num
print(cnt)
Buggy python submission (verdict: Runtime Error):
A,B=map(int,input(),split())
print(['Yay!',':('][A>8 or B>8])

Fixed submission:
A,B=map(int,input().split())
print(['Yay!',':('][A>8 or B>8])

Buggy python submission (verdict: Runtime Error):
A=int(input())
B=int(input())
print(['Yay!',':('][A>8 or B>8])

Fixed submission:
A,B=map(int,input().split())
print(['Yay!',':('][A>8 or B>8])
Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
cake=[list(map(int,input().split()))for _ in[0]*N]
dp=[[[0]*3]for _ in[0]*(M+1)]
for c in cake:
    for i in range(M-1,-1,-1):
        for t in dp[i]:
            dp[i+1]+=[[c[i]+t[i]for i in range(3)]]
        if len(dp[i])>10:
            a=[sum(t)for t in dp[i]]
            Max,Min=max(a),min(a)
            for s,t,u in dp[i]:
                if s>0 and t>0 and u>0 and s+t+u<Max:dp[i].remove([s,t,u])
                if s<0 and t<0 and u<0 and s+t+u>Min:dp[i].remove([s,t,u])
print(max(sum(map(abs,x))for x in dp[M]))

Fixed submission:
N,M=map(int,input().split())
sums=[[]for _ in[0]*8]
for _ in[0]*N:
    c=list(map(int,input().split()))
    for t in range(8):
        sums[t]+=[sum(c[i]*[-1,1][t&(1<<i)>0]for i in range(3))]
if M:print(max(sum(sorted(sums[i])[-M:])for i in range(8)))
else:print(0)

Buggy python submission (verdict: Time Limit Exceeded):
N,M=map(int,input().split())
cake=[list(map(int,input().split()))for _ in[0]*N]
dp=[[[0]*3]for _ in[0]*(M+1)]
for c in cake:
    for i in range(M-1,-1,-1):
        for t in dp[i]:
            dp[i+1]+=[[c[i]+t[i]for i in range(3)]]
print(max(sum(map(abs,x))for x in dp[M]))

Fixed submission:
N,M=map(int,input().split())
sums=[[]for _ in[0]*8]
for _ in[0]*N:
    c=list(map(int,input().split()))
    for t in range(8):
        sums[t]+=[sum(c[i]*[-1,1][t&(1<<i)>0]for i in range(3))]
if M:print(max(sum(sorted(sums[i])[-M:])for i in range(8)))
else:print(0)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = N
E = [0] * N
W = [0] * N
for i in range(N):
    if S[i] == 'E': E[i] = 1
    else: W[i] = 1
for i in range(N):
    LR = sum(W[:i]) + sum(E[i+1:])
    if LR < ans: ans = LR
print(ans)

Fixed submission:
N = int(input())
S = input()
ans = N
E = [0] * (N + 1)
for i in range(N):
    E[i+1] = E[i] + (S[i] == 'E')
for i in range(N):
    LR = i - E[i] + E[N] - E[i+1]
    if LR < ans: ans = LR
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
ans = N
for i in range(0, N):
    LR = S[:i].count('W') + S[i+1:].count('E')
    if LR < ans: ans = LR
print(ans)

Fixed submission:
N = int(input())
S = input()
ans = N
E = [0] * (N + 1)
for i in range(N):
    E[i+1] = E[i] + (S[i] == 'E')
for i in range(N):
    LR = i - E[i] + E[N] - E[i+1]
    if LR < ans: ans = LR
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N, *A = map(int, open(0).read().split())
xo = [0]
sm = [0]
for a in A:
    xo.append(xo[-1] ^ a)
    sm.append(sm[-1] + a)
xo.append(0)
sm.append(0)
ans = 0
for left in range(N):
    ok, ng = left, N
    while ok + 1 != ng:
        md = (ok + ng) // 2
        x = xo[md + 1] ^ xo[left]
        s = sm[md + 1] - sm[left]
        if x == s:
            ok = md
        else:
            ng = md
    ans += ok - left + 1
print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
ans = right = total = 0
for left in range(N):
    while right < N and total ^ A[right] == total + A[right]:
        total += A[right]
        right += 1
    ans += right - left
    if left == right:
        right += 1
    else:
        total -= A[left]
print(ans)
Buggy python submission (verdict: Runtime Error):
s = input()
K = int(input())
sub = {}
for d in range(1,6):
    for i in range(len(s)-d+1):
        sub |= {s[i:i+d]}
print(sorted(sub)[K-1])

Fixed submission:
s = input()
K = int(input())
sub = {s}
for d in range(1,6):
    for i in range(len(s)-d+1):
        sub |= {s[i:i+d]}
sub = list(sub)
n = len(sub)
for i in range(K):
    for j in range(n-i):
        a, b = sub[-i-1], sub[j]
        if a > b: sub[-i-1], sub[j] = b, a
print(sub[-K])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())
sub = {s}
for d in range(1,len(s)):
    for i in range(len(s)-d+1):
        sub |= {s[i:i+d]}
sub = list(sub)
n = len(sub)
for i in range(K):
    for j in range(n-i):
        a, b = sub[-i-1], sub[j]
        if a > b: sub[-i-1], sub[j] = b, a
print(sub[-K])

Fixed submission:
s = input()
K = int(input())
sub = {s}
for d in range(1,6):
    for i in range(len(s)-d+1):
        sub |= {s[i:i+d]}
sub = list(sub)
n = len(sub)
for i in range(K):
    for j in range(n-i):
        a, b = sub[-i-1], sub[j]
        if a > b: sub[-i-1], sub[j] = b, a
print(sub[-K])

Buggy python submission (verdict: Time Limit Exceeded):
s = input()
K = int(input())
sub = {s}
for d in range(1,len(s)):
    for i in range(len(s)-d+1):
        sub |= {s[i:i+d]}
print(sorted(sub)[K-1])

Fixed submission:
s = input()
K = int(input())
sub = {s}
for d in range(1,6):
    for i in range(len(s)-d+1):
        sub |= {s[i:i+d]}
sub = list(sub)
n = len(sub)
for i in range(K):
    for j in range(n-i):
        a, b = sub[-i-1], sub[j]
        if a > b: sub[-i-1], sub[j] = b, a
print(sub[-K])
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
edge = '.' * (W + 2)
field = [edge]
for _ in range(H):
    field.append('.' + input() + '.')
field.append(edge)
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if C[i][j] == '.':
            continue
        if '#' not in [C[i - 1][j], C[i][j - 1], C[i][j + 1], C[i + 1][j]]:
            print('No')
            exit()
print('Yes')

Fixed submission:
H,W=map(int,input().split())
C=['.'*(W+2)]+['.'+input()+'.'for _ in[0]*H]+['.'*(W+2)]
for i in range(1,H+1):
    for j in range(1,W+1):
        if C[i][j]=='#'and'#'not in[C[i-1][j], C[i][j-1], C[i][j+1], C[i+1][j]]:
            print('No')
            exit()
print('Yes')
Buggy python submission (verdict: Time Limit Exceeded):
from itertools import *
n=range(55555);a=list(n);a[1]=0
for i in range(2,233):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(n,a))
for x in combinations(p,int(input())):
    for y in combinations(x,5):
        if sum(y)in p:break
    else:print(*x);exit()

Fixed submission:
from itertools import *
n=range(55555);a=list(n);a[1]=0
for i in range(2,233):a[i*2::i]=[0]*len(a[i*2::i])
p=list(compress(n,a))

cnt = 0
ans = []
N = int(input())
for x in p:
    if x%5 == 2:
        ans += [x]
        cnt += 1
        if cnt == N: break
print(*ans)
Buggy python submission (verdict: Runtime Error):
a, b, c = sorted(map(int, input().split()))
diff = c - b
newa = a + diff
if (c - newa) % 2 == 0:
	print(d + (c - newa) // 2)
else:
	print(d + (c - newa + 3) // 2)

Fixed submission:
S = list(map(int, input().split()))
M = max(S)
r = sum(M - i for i in S)
print((r + 3 * (r % 2)) // 2)

Buggy python submission (verdict: Runtime Error):
A = list(map(int, input().split()))
M = max(S)
r = sum(M - a for a in A)
print((r + 3 * (r % 2)) // 2)

Fixed submission:
S = list(map(int, input().split()))
M = max(S)
r = sum(M - i for i in S)
print((r + 3 * (r % 2)) // 2)