Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
n = int(input())
d = [int(i) for i in input().split(" ")]
uni = np.unique(d)
co = [d.count(i) for i in uni]
tm = 0
for i in range(n):
    tm += co[i]
    if tm == n//2:
        print(uni[i+1]-uni[i])
        break
    if tm > n//2:
        print(0)
        break


Fixed submission:
import numpy as np
n = int(input())
d = [int(i) for i in input().split(" ")]
d = sorted(d)

if d[n//2] == d[n//2-1]:
    print(0)
else:
    print(d[n//2] - d[n//2-1])
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = [int(input()) for i in range(m)]
s = [1] + [0 for i in range(1,n+1)]
p = 1000000007
if n==1:
    print(1)
else:
    ind = 0
    if a[0] != 1:
        s[1] = 1
    else:
        ind += 1
    for i in range(2,n+1):
        if ind < m:
            if a[ind] != i:
                s[i] = (s[i-1] + s[i-2]) % p
            else:
                ind += 1
        else:
            s[i] = (s[i-1] + s[i-2]) % p
    print(s[-1])


Fixed submission:
#129C
n, m = map(int, input().split())
a = [int(input()) for i in range(m)]
s = [1] + [0 for i in range(1,n+1)]
p = 1000000007
if n==1:
    print(1)
else:
    ind = 0
    if ind < m:
        if a[0] != 1:
            s[1] = 1
        else:
            ind += 1
    else:
        s[1] = 1
    for i in range(2,n+1):
        if ind < m:
            if a[ind] != i:
                s[i] = (s[i-1] + s[i-2]) % p
            else:
                ind += 1
        else:
            s[i] = (s[i-1] + s[i-2]) % p
    print(s[-1])
Buggy python submission (verdict: Time Limit Exceeded):
def cal_cost(co, h, i, j):
    return co + abs(h[i]-h[j])

n, k = map(int, input().split())
h = [int(i) for i in input().split(" ")]
lis = [0 for i in range(n)]
for i in range(1, n):
    a = min([cal_cost(lis[i-j], h, i-j, i) for j in range(1, k+1) if j<=i])
    lis[i] = a
print(lis[-1])

Fixed submission:
def cal_cost(co, h, i, j):
    return co + abs(h[i]-h[j])

n, k = map(int, input().split())
h = [int(i) for i in input().split(" ")]
lis = [0 for i in range(n)]
for i in range(1, n):
    a = min([cal_cost(lis[i-j], h, i-j, i) for j in range(1, k+1) if j<=i])
    lis[i] = a
print(lis[-1])
Buggy python submission (verdict: Runtime Error):
n = int(input())
dp = [map(int, input().split())]
for i in range(n-1):
    a, b, c = map(int, input().split())
    a0 = dp[0]
    b0 = dp[1]
    c0 = dp[2]
    
    dp[0] = max(b0, c0) + a
    dp[1] = max(c0, a0) + b
    dp[2] = max(a0, b0) + c
print(max(dp))

Fixed submission:
n = int(input())
dp = [int(i) for i in input().split()]
for i in range(n-1):
    a, b, c = map(int, input().split())
    a0 = dp[0]
    b0 = dp[1]
    c0 = dp[2]
    
    dp[0] = max(b0, c0) + a
    dp[1] = max(c0, a0) + b
    dp[2] = max(a0, b0) + c
print(max(dp))
Buggy python submission (verdict: Runtime Error):
n, w = map(int, input().split())
a = [[int(i) for i in input().split(" ")] for j in range(n)]
dp = [[0]*(w+1) for i in range(n+1)]
for i in range(1, n+1):
    w_, v_ = a[i-1]
    for j in range(1, w+1):
        if j < w_:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = max(dp[i-1][j-w_]+v_, dp[i-1][j])
print(dp[n][w])

Fixed submission:
n, w = map(int, input().split())
a = [[int(i) for i in input().split(" ")] for j in range(n)]
dp = [[0]+[float("inf")]*(10**5) for i in range(n+1)]
for i in range(1, n+1):
    w_, v_ = a[i-1]
    for j in range(1, 10**5+1):
        if j < v_:
            dp[i][j] = dp[i-1][j]
        else:
            dp[i][j] = min(dp[i-1][j-v_]+w_, dp[i-1][j])
for i in range(10**5, -1, -1):
    if dp[n][i]<=w:
        print(i)
        break
Buggy python submission (verdict: Runtime Error):
import math
n, m = map(int, input().split())
if m%2 == 0:
    divisor = [2, m//2]
else:
    divisor = []
for i in range(3, int(math.sqrt(m))+1):
    if m%i == 0:
        divisor.append(i)
        divisor.append(m//i)
divisor = sorted(divisor, reverse=True)
a = m//n
for i in divisor:
    if i > a:
        continue
    if (m-(m//i*i))%i == 0:
        break
print(i)

Fixed submission:
import math
n, m = map(int, input().split())
if m%2 == 0:
    divisor = [1, 2, m//2, m]
else:
    divisor = [1, m]
for i in range(3, int(math.sqrt(m))+1):
    if m%i == 0:
        divisor.append(i)
        divisor.append(m//i)
divisor = sorted(divisor, reverse=True)
a = m//n
for i in divisor:
    if i > a:
        continue
    if (m-(m//i*i))%i == 0:
        break
print(i)

Buggy python submission (verdict: Runtime Error):
import math
n, m = map(int, input().split())
if m%2 == 0:
    divisor = [2, m//2]
else:
    divisor = []
for i in range(3, int(math.sqrt(m))+1, 2):
    if m%i == 0:
        divisor.append(i)
        divisor.append(m//i)
divisor = sorted(divisor, reverse=True)
a = m//n
for i in divisor:
    if i > a:
        continue
    if (m-(m//i*i))%i == 0:
        break
print(i)

Fixed submission:
import math
n, m = map(int, input().split())
if m%2 == 0:
    divisor = [1, 2, m//2, m]
else:
    divisor = [1, m]
for i in range(3, int(math.sqrt(m))+1):
    if m%i == 0:
        divisor.append(i)
        divisor.append(m//i)
divisor = sorted(divisor, reverse=True)
a = m//n
for i in divisor:
    if i > a:
        continue
    if (m-(m//i*i))%i == 0:
        break
print(i)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
for i in range(m//n, 0, -1):
    if (m-(m//i*i))%i == 0:
        break
print(i)

Fixed submission:
import math
n, m = map(int, input().split())
if m%2 == 0:
    divisor = [1, 2, m//2, m]
else:
    divisor = [1, m]
for i in range(3, int(math.sqrt(m))+1):
    if m%i == 0:
        divisor.append(i)
        divisor.append(m//i)
divisor = sorted(divisor, reverse=True)
a = m//n
for i in divisor:
    if i > a:
        continue
    if (m-(m//i*i))%i == 0:
        break
print(i)
Buggy python submission (verdict: Runtime Error):
import numpy as np
N, M = map(int, input().split())
A_list = sorted(list(map(int, input().split())))
A_ar = np.array(A_list)[::-1]
for i in range(M):
    A_ar[0] = A_ar[0]//2
    if A_ar[0] < A_ar[1]:
        A_ar = np.sort(A_ar)[::-1]
print(sum(A_ar))



Fixed submission:
import heapq
N, M = map(int, input().split())
A_list = list(map(lambda x: int(x)*(-1), input().split()))
heapq.heapify(A_list)
for i in range(M):
    max_num = heapq.heappop(A_list) * (-1)
    heapq.heappush(A_list, (-1) * (max_num // 2))
print((-1) * sum(A_list))

Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
N, M = map(int, input().split())
A_list = list(map(int, input().split()))
A_ar = np.array(A_list)
for i in range(M):
    max_pos = np.argmax(A_ar)
    A_ar[max_pos] = A_ar[max_pos]//2
print(sum(A_ar))



Fixed submission:
import heapq
N, M = map(int, input().split())
A_list = list(map(lambda x: int(x)*(-1), input().split()))
heapq.heapify(A_list)
for i in range(M):
    max_num = heapq.heappop(A_list) * (-1)
    heapq.heappush(A_list, (-1) * (max_num // 2))
print((-1) * sum(A_list))
Buggy python submission (verdict: Runtime Error):
3 3 0
100 -100 0
0 100 100
100 100 100
-100 100 100

Fixed submission:
N, M, C = map(int, input().split())
B_list = list(map(int, input().split()))
A_lists = [list(map(int, input().split())) for _ in range(N)]

cnt = 0
for A_list in A_lists:
    tmp = [a * b for a, b in zip(A_list, B_list)]
    tmp = sum(tmp) + C
    if tmp > 0:
        cnt += 1
print(cnt)
Buggy python submission (verdict: Runtime Error):
H, W = map(int, input().split())
S = [input() for i in range(H)]
 
 
b_list = []
for row in S:
    tmp_raw = []
    for j, t in enumerate(row):
        if t == '#':
            tmp_raw.append(j)
    b_list.append(tmp_raw)
tmp = -1
res = {}
raw_before = []
for i, raw in enumerate(b_list):
    res[i] = {}
    for j in raw:
        res[i][j] = []
        if not((j - 1) in raw or j == 0):
            res[i][j].append((i, j-1))
        if not((j + 1) in raw or j == W -1):
            res[i][j].append((i, j+1))
        if not(i == 0 or j in raw_before):
            res[i][j].append((i-1, j))
        if not(i == H - 1):
            if not(j in b_list[i+1]):
                res[i][j].append((i+1, j))
    raw_before = raw
 
 
 
before = 0
for i, raw in enumerate(b_list):
    for j in raw:
         before += len(res[i][j])
while True:
    for i, raw in enumerate(b_list):
        for j in raw:
            try:
                if S[i-1][j] == '.' or S[i][j-1] == '.':
                    res[i][j] += res[i-1][j-1]
            except:
                pass
            try:
                if S[i+1][j] == '.' or S[i][j-1] == '.':
                    res[i][j] += res[i+1][j-1]
            except:
                pass
            try:
                if S[i-1][j] == '.' or S[i][j+1] == '.':
                    res[i][j] += res[i-1][j+1]
            except:
                pass
            try:
                if S[i+1][j] == '.' or S[i][j+1] == '.':
                    res[i][j] += res[i+1][j+1]
            except:
                pass
    before = result
    for i, raw in enumerate(b_list):
        for j in raw:
            res[i][j] = list(set(res[i][j]))
    result = 0
    for i, raw in enumerate(b_list):
        for j in raw:
             result += len(res[i][j])
    if before == result:
        break
print(result)

Fixed submission:
N = int(input())
H = int(input())
W = int(input())
print((N - H + 1) * (N - W + 1))
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
h_list = list(map(int, input().split()))

dp_list = [0]
for n in range(1, N):
  tmp_h = h_list[n]
  if n < K:
    lst = [abs(tmp_h - h_list[i]) + dp_list[i] for i in range(n)]
    else:
      lst = [abs(tmp_h - h_list[n-k]) + dp_list[n-k] for k in range(1, K + 1)]
      dp_list.append(min(lst))
      print(dp_list[-1])

Fixed submission:
N = int(input())
h_list = list(map(int, input().split()))
def chmin(a, b):
    if a > b:
        return b
    else:
        return a
import numpy as np
dp_list = [np.inf] * N
dp_list[0] = 0
dp_list[1] = abs(h_list[1] - h_list[0])
for i in range(2, N):
    dp_list[i] = chmin(dp_list[i], dp_list[i-1] + abs(h_list[i-1] - h_list[i]))
    dp_list[i] = chmin(dp_list[i], dp_list[i-2] + abs(h_list[i-2] - h_list[i]))
print(dp_list[-1])
Buggy python submission (verdict: Runtime Error):
N = int(input())
abc_list = [list(map(int, input().split())) for _ in range(N)]

happy_list = [[0, 0, 0]] # n日目にa|b|cを選んだときの幸福のそこまでの最大値
# before_act = -1 # a: 0, b: 1, c: 2
for n in range(N):
    before_happy_a, before_happy_b, before_happy_c = happy_list[-1]
    a, b, c = abc_list[n]
    tmp_happy_a = max(before_happy_b, before_happy_c) + a
    tmp_happy_b = max(before_happy_c, before_happy_a) + b
    tmp_happy_c = max(before_happy_a, before_happy_b) + c
    happy_list.append([tmp_happy_a, tmp_happy_b, tmp_happy_c])

print(max(happy_list[-1]))s

Fixed submission:
N = int(input())
happy_list = [list(map(int, input().split())) for n in range(N)]
def chmax(a, b):
    if a > b:
        return a
    else:
        return b
dp_list = [[0, 0, 0]]
for n in range(1, N+1):
    tmp = [0, 0, 0]
    tmp[1] = chmax(tmp[1], dp_list[n-1][0] + happy_list[n-1][1])
    tmp[2] = chmax(tmp[2], dp_list[n-1][0] + happy_list[n-1][2])
    tmp[0] = chmax(tmp[0], dp_list[n-1][1] + happy_list[n-1][0])
    tmp[2] = chmax(tmp[2], dp_list[n-1][1] + happy_list[n-1][2])
    tmp[0] = chmax(tmp[0], dp_list[n-1][2] + happy_list[n-1][0])
    tmp[1] = chmax(tmp[1], dp_list[n-1][2] + happy_list[n-1][1])
    dp_list.append(tmp)
print(max(tmp))
Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
res = []
for i in range(N-K+1):
    res.append(tmp[i+K-1] - tmp[i])
print(min(res))

Fixed submission:
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
res = []
for i in range(N-K+1):
    res.append(h[i+K-1] - h[i])
print(min(res))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
res = []
for i in range(N-K+1):
    tmp = h[i:i+K]
    res.append(tmp[-1] - tmp[0])
print(min(res))

Fixed submission:
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
res = []
for i in range(N-K+1):
    res.append(h[i+K-1] - h[i])
print(min(res))

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
res = []
for i in range(N-K+1):
    tmp = h[i:i+K]
    res.append(max(tmp) - min(tmp))
print(min(res))

Fixed submission:
N, K = map(int, input().split())
h = sorted([int(input()) for i in range(N)])
res = []
for i in range(N-K+1):
    res.append(h[i+K-1] - h[i])
print(min(res))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = int(input())
b = int(input())
if n==1:
    print('Hello World')
else:
    print(a+b)

Fixed submission:
n = int(input())
if n==1:
    print('Hello World')
else:
    a = int(input())
    b = int(input())
    print(a+b)
Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())
x_lst = [int(i) for i in input().split()]
# N, X = map(int, '3 81'.split())
# x_list = [abs(int(i) - X) for i in '33 105 57'.split()]
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
res = [x_list[0]]
for i in range(1, N):
    res.append(gcd(res[i-1], x_list[i]))
print(min(res))

Fixed submission:
N, X = map(int, input().split())
x_list = [abs(int(i) - X) for i in input().split()]
# N, X = map(int, '3 81'.split())
# x_list = [abs(int(i) - X) for i in '33 105 57'.split()]
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a
res = [x_list[0]]
for i in range(1, N):
    res.append(gcd(res[i-1], x_list[i]))
print(min(res))
Buggy python submission (verdict: Runtime Error):
import numpy as np
def main():
    N, M, Q = map(int, input().split())
    LR = []
    for i in range(M):
        l, r = map(int, input().split())
        LR.append([l-1, r])
    PQ = []
    for i in range(Q):
        p, q = map(int, input().split())
        PQ.append([p-1, q-1])
    ar = np.zeros([M, N])
    for m in range(M):
        ar[m, LR[m][0]:LR[m][1]] = 1
    flags_start = [np.ones(M, dtype=np.bool)]
    flags_end = []
    for i in range(1, N):
        flags_start.append((ar[:, :i].max(axis=1)==0))# * (ar[:, i:].max(axis=1)==1))
        flags_end.append((ar[:, i:].max(axis=1)==0))# * (ar[:, :i].max(axis=1)==1))
    flags_end.append(np.ones(M, dtype=np.bool))
    for q in range(Q):
        left = PQ[q][0]
        right = PQ[q][1]
        print(sum(flags_start[left] * flags_end[right]))

if __name__=='__main__':
    main()

Fixed submission:

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

print(A*B-A-B+1)

Buggy python submission (verdict: Runtime Error):

A = int(input())
B = int(input())

print(A*B-A-B+1)


Fixed submission:

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

print(A*B-A-B+1)
Buggy python submission (verdict: Runtime Error):
def comb1(n, r):
    if n == 0 or r == 0: return 1
    return comb1(n, r-1) * (n-r+1) / r 
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
# N, M = 10, 400000000
# A = [int(i) for i in '1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000'.split()]
surplus = {0:0}
res = 0
tmp = 0
B = []
for i in range(0, N):
    tmp += A[i] % M
    B.append(tmp)
    surplus[tmp] = 0
for i in B:
    surplus[i] += 1
for tmp_key in surplus.keys():
    res += comb1(surplus[tmp_key], 2)

corner = surplus[0]
if corner == 0:
    res -= 1
else:
    res += corner
print(int(res))

Fixed submission:
N, K = map(int, input().split())
if N % K==0:
    print(0)
else:
    print(1)
Buggy python submission (verdict: Runtime Error):
import numpy as np
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
ar = np.eye(N) * A

for h in range(0, N):
    for w in range(1, h+1):
        ar[-h-1, -h-1+w] = ar[-h, -h-1+w] + ar[-h-1, -h-2+w] - ar[-h, -h-2+w]
print(int(sum(sum(ar % M == 0)) - sum(np.linspace(1, N-1, N-1))))

Fixed submission:
N = int(input())
s = ''

while N != 0:
#    print(N, s, N % (-2) == 0)
    if N % (-2) == 0:
        s = '0' + s
    else:
        s = '1' + s
        N -= 1
    N /= (-2)

if s =='':
    s = '0'
print(s)
Buggy python submission (verdict: Time Limit Exceeded):
def comb1(n, r):
    if n == 0 or r == 0: return 1
    return comb1(n, r-1) * (n-r+1) / r 
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
# N, M = 10, 400000000
# A = [int(i) for i in '1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000'.split()]
surplus = {0:0}
res = 0
B = []
for i in range(1, N + 1):
    tmp = sum(A[:i])%M
    B.append(tmp)
    surplus[tmp] = 0
for i in B:
    surplus[i] += 1
for tmp_key in surplus.keys():
    res += comb1(surplus[tmp_key], 2)

corner = surplus[0]
if corner == 0:
    res -= 1
else:
    res += corner
print(int(res))

Fixed submission:
def comb1(n, r):
    if n == 0 or r == 0: return 1
    return comb1(n, r-1) * (n-r+1) / r 
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
# N, M = 10, 400000000
# A = [int(i) for i in '1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000'.split()]
surplus = {0:0}
res = 0
tmp = 0
B = []
for i in range(0, N):
    tmp += A[i]
    tmp %= M
    B.append(tmp)
    surplus[tmp] = 0
for i in B:
    surplus[i] += 1

for tmp_key in surplus.keys():
    res += comb1(surplus[tmp_key], 2)

corner = surplus[0]
if corner == 0:
    res -= 1
else:
    res += corner
print(int(res))

Buggy python submission (verdict: Runtime Error):
import numpy as np
N, M = map(int, input().split())
A = []
res = 0
for i in input().split():
    A.append(int(i))
    if A[-1]%M==0:
        res += 1
ar = np.eye(N) * A

for h in range(0, N):
    for w in range(1, h+1):
        ar[-h-1, -h-1+w] = ar[-h, -h-1+w] + ar[-h-1, -h-2+w] - ar[-h, -h-2+w]
        if ar[-h-1, -h-1+w] % M == 0:
            res += 1
# print(int(sum(sum(ar % M == 0)) - sum(np.linspace(1, N-1, N-1))))
print(res)

Fixed submission:
def comb1(n, r):
    if n == 0 or r == 0: return 1
    return comb1(n, r-1) * (n-r+1) / r 
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
# N, M = 10, 400000000
# A = [int(i) for i in '1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000'.split()]
surplus = {0:0}
res = 0
tmp = 0
B = []
for i in range(0, N):
    tmp += A[i]
    tmp %= M
    B.append(tmp)
    surplus[tmp] = 0
for i in B:
    surplus[i] += 1

for tmp_key in surplus.keys():
    res += comb1(surplus[tmp_key], 2)

corner = surplus[0]
if corner == 0:
    res -= 1
else:
    res += corner
print(int(res))
Buggy python submission (verdict: Runtime Error):
R = int(input())
if R<1200:


Fixed submission:
R = int(input())
if R<1200:
    print('ABC')
elif R<2800:
    print('ARC')
else:
    print('AGC')
Buggy python submission (verdict: Runtime Error):

D, W = map(int, input().split())
if W == 100:
    print(101*(100**D))
else:
    print(W*(100**D))




Fixed submission:

N = int(input())

N_str = str(N)
S = sum([int(N_str[i]) for i in range(len(N_str))])

if N % S == 0:
    print('Yes')
else:
    print('No')
Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  6 17:34:40 2018

@author: Yuki
"""

N = int(input())
S = input()
res = []
for i, n_ban in enumerate(S):
    # Westからn_ban目の人をリーダーとしたときに何人を変える必要があるか
    # n_banについて全探索
    # 自分よりEast側かつEastを向いてる人の人数と
    # 自分よりWest側かつWestを向いてる人の人数の合計
    E_cnt = 0
    W_cnt = 0
    E_line = S[i:]
    W_line = S[:i]
    E_cnt = E_line.count('E')
    W_cnt = W_line.count('W')
    if n_ban == 'E':
        E_cnt -= 1
    res.append(E_cnt+W_cnt)

print(min(res))

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  6 17:34:40 2018

@author: Yuki
"""

N = int(input())
S = input()
res = []

res.append(S[1:].count('E'))
for i in range(1, len(S)):
    if S[i] == 'E':
        tmp = res[-1]-1
        if S[i-1] == 'W':
            tmp += 1
        
    elif S[i] == 'W':
        tmp = res[-1]
        if S[i-1] == 'W':
            tmp += 1
    res.append(tmp)
    

print(min(res))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = input()
res = []
for i, n_ban in enumerate(S):
    # Westからn_ban目の人をリーダーとしたときに何人を変える必要があるか
    # n_banについて全探索
    # 自分よりEast側かつEastを向いてる人の人数と
    # 自分よりWest側かつWestを向いてる人の人数の合計
    E_cnt = 0
    W_cnt = 0

    for ii, target in enumerate(S):
        if i < ii:
            if target == "E":
                E_cnt += 1
        elif ii < i:
            if target == "W":
                W_cnt += 1
    
    res.append(E_cnt+W_cnt)

print(min(res))

Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  6 17:34:40 2018

@author: Yuki
"""

N = int(input())
S = input()
res = []

res.append(S[1:].count('E'))
for i in range(1, len(S)):
    if S[i] == 'E':
        tmp = res[-1]-1
        if S[i-1] == 'W':
            tmp += 1
        
    elif S[i] == 'W':
        tmp = res[-1]
        if S[i-1] == 'W':
            tmp += 1
    res.append(tmp)
    

print(min(res))
Buggy python submission (verdict: Time Limit Exceeded):

s = input()
K = int(input())

string_cands = []
for i in range(len(s)):
    for ii in range(i, len(s)+1):
        if i < ii:
            if ii != len(s):
                string_cands.append(s[i:ii])
            else:
                string_cands.append(s[i:])
            #print(i, ii, s[i:ii])
print(sorted(set(string_cands))[K-1])



Fixed submission:
# -*- coding: utf-8 -*-
"""
Created on Tue Aug  7 23:57:38 2018

@author: Yuki
"""

s = input()
K = int(input())

string_cands = []
s_length = len(s)
for i in range(s_length):
    for ii in range(1, K+1):
        string_cands.append(s[i:i+ii])
print(sorted(set(string_cands))[K-1])