Problem:


Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

2 \leq N \leq 3 \times 10^5

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Accepted

Solution code:
N = int(input())
S = input()
w_count = 0
e_count = 0
w_counts = [0 for _ in range(N)]
e_counts = [0 for _ in range(N)]
for i in range(N):
    if S[i] == 'W':
        w_count += 1
    w_counts[i] = w_count
for i in range(N-1, -1, -1):
    if S[i] == 'E':
        e_count += 1
    e_counts[i] = e_count
_min = -1
for i in range(N):
    total = w_counts[i] + e_counts[i]
    if _min == -1:
        _min = total
    if _min > total:
        _min = total
print(_min-1)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

leftW = [0]*N
rightE = [0]*N
for i in range(N-1):
    if S[i] == "W":
        leftW[i+1] = leftW[i] + 1
    else:
        leftW[i+1] = leftW[i]
for i in range(N-1,0,-1):
    if S[i] == "E":
        rightE[i-1] = rightE[i] + 1
    else:
        rightE[i-1] = rightE[i]
minlw = N
for i in range(N):
    if leftW[i] + rightE[i] < minlw:
        minlw = leftW[i] + rightE[i]
print(minlw)

Submission status: Accepted

Solution code:
n = int(input())
a = list(input())
for i in range(n):
    if a[i] == 'E':
        a[i] = 1
    else:
        a[i] = 0

ans = 99999999999999999999
# print(a)
l = 0
r = sum(a)
for i in range(n):

    if i == 0:
        if a[i] == 1:
            r -= a[i]
            a[i] = 0
        else:
            a[i] = 1
        l = 0
    else:
        if a[i] == 1:
            a[i] = 0
            r -= 1
        else:
            a[i] = 1
        l += a[i-1]

    ans = min(l+r,ans)
    # print(i,l,r,ans)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
a=[]
for i in s:
    if i=='W': a.append(1)
    else: a.append(-1)
a2=[-1*i for i in a]
suma=[sum(a)-a[0]]
for i in range(len(a)-1):
    suma.append(-a[i+1]+suma[i])
suma2=[0]
for i in range(len(a2)-1):
    suma2.append(suma2[i]+a2[i])
#print(a,a2)
count=0
for i in range(len(a)):
    d=suma[i]+suma2[i]
    if count<d:count=d
    #print(sum(a[i+1:]),sum(a2[:i]),d)
print((n-1-count)//2)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

left = 0
right = N - 1

DP = [0] * N

DP[0] = S[1:].count("E")

for i in range(1, N):
    if S[i - 1] == "W":
        a = 1
    else:
        a = 0
    if S[i] == "E":
        b = -1
    else:
        b = 0
    DP[i] = DP[i - 1] + a + b

print(min(DP))

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def solve():
    n = int(input())
    s = input()
    el, wl = [0]*n, [0]*n
    ec, wc = 0, 0
    for i in range(n):
        if s[n - 1 - i] == 'E':
            ec += 1
        if s[i] == 'W':
            wc += 1
        el[n - 1 - i] = ec
        wl[i] = wc

    ans = n - 1
    for i in range(n):
        if i == 0:
            ans = min(ans, el[i + 1])
        elif i == n - 1:
            ans = min(ans, wl[i - 1])
        else:
            ans = min(ans, wl[i - 1] + el[i + 1])
    print(max(0, ans))

if __name__=="__main__":
    solve()

Submission status: Accepted

Solution code:
N = int(input())
S = input()

from collections import defaultdict
wc = defaultdict(int)
ec = defaultdict(int)

if S[0] == 'W':
    wc[0] = 1
for i in range(1, N):
    wc[i] = wc[i-1]
    if S[i] == 'W':
        wc[i] += 1

if S[N-1] == 'E':
    ec[N-1] = 1
for i in range(N-2, -1, -1):
    ec[i] = ec[i+1]
    if S[i] == 'E':
        ec[i] += 1

min_ = N
for i in range(1, N-1):
    v = wc[i-1] + ec[i+1]
    if v < min_:
        min_ = v
v0 = ec[1]
if v0 < min_:
    min_ = v0
vn = wc[N-1]
if vn < min_:
    min_ = vn
print(min_)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    [S] = [(input_raw[0])]

    min_count = 0
    for i in range(1, N):
        if S[i] != "W":
            min_count += 1
    prev_count = min_count

    for i in range(1, N):

        if S[i - 1] == "E" and S[i] == "E":
            prev_count -= 1
        elif S[i - 1] == "W" and S[i] == "W":
            prev_count += 1
        if prev_count < min_count:
            min_count = prev_count

    print min_count


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()

d = {0:0}
l = [0]*N
for i in range(1,N+1):
    if S[i-1] == "E":
        d[i] = d[i-1]+1
    else:
        d[i] = d[i-1]
    l[i-1] = i-1-d[i-1]-d[i]

print(min(l)+d[N])

Submission status: Accepted

Solution code:
import functools
a = int(input())
S = input()
l = [0] * (a+1)
r = [0] * (a+1)
for i, s in enumerate(S):
  l[i+1] = l[i] + 1 if s=="W" else l[i]
for i,s in enumerate(S[::-1]):
  r[i+1] = r[i] + 1 if s=="E" else r[i]
b = [l[i]+r[a-1-i] for i in range(a)]
c = functools.reduce(lambda x, y: min(x, y), b)
print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Contents   : ARC098 C問題
Author     : (製作者の名前) (製作者の所属)
LastUpdate : (更新日YYYY/MM/DD)
Since      : (作成日YYYY/MM/DD)
"""
N = int(input())
S = input()
cnt = 0
for s in S[1:]:
    if s == "E":
        cnt += 1
cnt_list = []
cnt_list.append(cnt)
for i in range(len(S)-1):
    if S[i] == "E" and S[i+1] == "E":
        cnt -= 1
        cnt_list.append(cnt)
    elif S[i] == "E" and S[i+1] == "W":
        cnt_list.append(cnt)
    elif S[i] == "W" and S[i+1] == "E":
        cnt_list.append(cnt)
    else:
        cnt += 1
        cnt_list.append(cnt)
print(min(cnt_list))

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

#print(S)

W_people = [0]
W_psn = 0
for i in range(N-1):
    if S[i] == 'W':
        W_psn += 1
    W_people.append(W_psn)

E_people = [0]
E_psn = 0
S.reverse()
for i in range(N-1):
    if S[i] == 'E':
        E_psn += 1
    E_people.append(E_psn)
S.reverse()
E_people.reverse()

#print(E_people)
#print(W_people)

_min = N
for i in range(N):
    _min = min(E_people[i] + W_people[i], _min)
    
print(_min)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

numE_R = S.count('E')
numW_R = N - numE_R
numE_L, numW_L = 0, 0

ans = float('inf')
for Si in S:
    if Si == 'E':
        numE_R -= 1
    else:
        numW_R -= 1

    ans = min(ans, numW_L + numE_R)

    if Si == 'E':
        numE_L += 1
    else:
        numW_L += 1

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, S):

    W = 0
    E = S.count('E')
    ans = E + W
    for s in S:
        if s == 'E':
            E -= 1

        ans = min(E + W, ans)

        if s == 'W':
            W += 1

    return ans


def main():
    N = read_int()
    S = read_str()
    print(slv(N, S))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
L = []
L.append(S[1:].count('E'))
for i in range(1,N):
    if S[i-1] =='E' and S[i]=='E':
        a = L[-1] -1 
    elif S[i-1] =='W' and S[i]=='W':
        a = L[-1] +1 
    else:
        a = L[-1]
    L.append(a)
print(min(L))

Submission status: Accepted

Solution code:
n = int(input())
lis = [0]
s = str(input())
cou = 0
co  = 0
for i in range(n-1):
    if s[i] == "W":cou += 1
    lis.append(cou)
for k in range(1,n):
    if s[n-(k)] == "E":co += 1
    lis[n-(k+1)] += co
print(min(lis))

Submission status: Accepted

Solution code:
n = int(input())
# a, b = map(int, input().split())
# c = list(map(int, input().split()))
# d = [list(map(int, input().split())) for _ in range(n)]
s = input()

W = []
S = []

if s[0] == 'W':
    W.append(1)
    S.append(0)
else:
    W.append(0)
    S.append(1)

for i in range(1, n):
    c = s[i]
    if c == 'W':
        W.append(W[i - 1] + 1)
        S.append(S[i - 1])
    else:
        W.append(W[i - 1])
        S.append(S[i - 1] + 1)


min = min(S[n - 1] - S[0], W[n - 2])


for i in range(1, n - 1):
    tmp = W[i - 1] + S[n - 1] - S[i]
    if min > tmp:
        min = tmp




print(min)

Submission status: Accepted

Solution code:
input()
S = input()
tmp = sum([1 for s in S[1:] if s == "E"])
ans = tmp
for i in range(1, len(S)):
    if S[i-1] == "W":
        tmp += 1
    if S[i] == "E":
        tmp -= 1
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

W = S.count('W')
E = N - W

numW = 0
ans = N
for i in range(N):
    numE = N - i - (W - numW)
    if S[i]=='E':
        numE -= 1
    #print(i, S[:i], S[i+1:], numW, numE)
    ans = min(ans, numW + numE)
    if S[i]=='W':
        numW += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())

e = s.count('E')
w = 0
c = 10**6
for i in range(n):
    if s[i] == 'E':
        e -= 1
        c = min(c, w + e)
    else:
        c = min(c, w + e)
        w += 1

print(c)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())


count = 0
for e,i in enumerate(s):
    if e == 0:
        continue
    if i == 'E':
        count += 1

mincount = count
for e,i in enumerate(s):
    if e == len(s)-1:
        break
    if e == 0 and i == 'E':
        continue
    if i == 'W':
        count+=1
    else:
        count-=1

    if s[e+1] == 'E':
        mincount = min(count-1,mincount)
    else:
        mincount = min(count,mincount)

print(mincount)

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
S = input()

right_EW = Counter(S)
left_EW = {"E":0,"W":0}

ans = len(S)
for s in S:
    right_EW[s] -= 1
    #print(left_EW,right_EW)
    temp = left_EW["W"] + right_EW["E"]
    if temp < ans: ans = temp
    left_EW[s] += 1
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w,e=[],[]
a,b=0,0
for i in range(n):
    if s[i]=='W':
        a+=1
    else:
        b+=1
    w.append(a)
    e.append(b)
x=n
for i in range(n):
    if s[i]=='W':
        if x>e[-1]-e[i]+w[i]-w[0]:
            x=e[-1]-e[i]+w[i]-w[0]
    else:
        if x>e[-1]-e[i]+w[i]:
            x=e[-1]-e[i]+w[i]
print(x)

Submission status: Accepted

Solution code:
n = int(input())
s = list('N' + input())
all_e = s.count('E')
all_w = s.count('W')
now_e, now_w = 0, 0
ans = float('inf')
me = ''
for i in s:
    me = i
    if i == 'E':
        now_e += 1
    elif i == 'W':
        now_w += 1
    need = (all_e - now_e) + now_w
    if ans > need:
        ans = need
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
queue = [x for x in input()]

n_wests = 0
n_easts = queue.count('E') - (1 if queue[0] == 'E' else 0)
min_i = 0
min_n_changes = n_easts
for i in range(1, N):
    prev_dir = queue[i - 1]
    cur_dir = queue[i]
    if prev_dir == 'W':
        n_wests += 1
    if cur_dir == 'E':
        n_easts -= 1
    n_changes = n_wests + n_easts
    if n_changes < min_n_changes:
        min_n_changes = n_changes
        min_i = i

print(min_n_changes)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n = int(input())
acc_e = [0]
acc_w = [0]

for (i,s) in enumerate(list(input())):
    if s=="W":
        acc_w.append(acc_w[i]+1)
        acc_e.append(acc_e[i])
    else:
        acc_w.append(acc_w[i])
        acc_e.append(acc_e[i]+1)

#print(acc_w)
#print(acc_e)

ret = 999999
for i in range(n):
    r = acc_w[i] + (acc_e[-1]-acc_e[i+1])
    if r<ret:
        ret = r

print(ret)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
cnt_w = [0]
for i in range(n):
    cnt_w.append(cnt_w[i] + (1 if s[i] == 'W' else 0))
cnt_w.append(cnt_w[n])
min_cnt = cnt_w[n-1]
for i in range(n):
    min_cnt = min(min_cnt, cnt_w[i] + (n - 1 - i) - (cnt_w[n] - cnt_w[i + 1]))
print(min_cnt)

Submission status: Accepted

Solution code:
n = input()
s = list(input())
line = [0 for _ in range(0,len(s)+1)]
for i in range(0,len(s)):
    if s[i] =="W":
        line[0] += 1
        line[i+1] -= 1
    if s[i] =="E":
        line[i] += 1
        line[len(s)] -= 1
for i in range(0,len(line)-1):
    line[i+1] += line[i]
print(len(s) - max(line))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

N = int(input())
S = str(input())

west_w = 0
west_e = 0
east_w = S[1:].count("W")
east_e = S[1:].count("E")

min_change = west_w + east_e
for i in range(1, N):
    if S[i-1] == "W":
        west_w += 1
    else:
        west_e += 1
    if S[i] == "W":
        east_w -= 1
    else:
        east_e -= 1
    change = west_w + east_e
    if change < min_change:
        min_change = change

print(min_change)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
n = int(input())
s = input()
ans = float('inf')
E_total = s.count('E')
W_total = s.count('W')
cnt = {'E': 0, 'W': 0}
for i in range(n):
    turn = cnt['W'] + (E_total - cnt['E'])
    if s[i] == 'E':
        turn -= 1
    ans = min(ans, turn)
    cnt[s[i]] += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

first = 0

for i in range (n-1):
    if s[i+1] == "E":
        first += 1
mini = first

for i in range(n-1):
    if s[i] == "W":
        first +=1
    if s[i+1] == "E":
        first -= 1
    mini = min(mini,first)

print(mini)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    S = input()

    AW = [0]*(N+1)
    AE = [0]*(N)

    wcount = 0
    ecount = 0
    for i in range(N):
        if S[i] == "W":
            wcount += 1
        else:
            ecount += 1
        AW[i] = wcount
        AE[i] = ecount

    MIN = 1000000

    for i in range(N):
        tmp = 0
        tmp += AW[i-1]
        tmp += AE[-1] - AE[i]
        MIN = min(MIN, tmp)
    print(MIN)

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())

accLeft = [0]
accRight = [0]

tmp = 0
for c in S:
  tmp += int(c == 'W')
  accLeft.append(tmp)
accLeft.append(0)

tmp = 0
for c in S[::-1]:
  tmp += int(c == 'E')
  accRight.append(tmp)
accRight.append(0)
accRight = accRight[::-1]

print(min([accLeft[i-1] + accRight[i+1] for i in range(1, N+1)]))

Submission status: Accepted

Solution code:
N = int(input())
s = input()
cnt = 0
acce = [0]*N
accw = [0]*N
if s[0] == 'E':
	acce[0] = 1
else:
	accw[0] = 1
for i in range(1,N):
	if s[i] == 'E':
		acce[i] = acce[i-1]+1
		accw[i] = accw[i-1]
	else:
		accw[i] = accw[i-1]+1
		acce[i] = acce[i-1]

m = acce[N-1] - acce[0]
for i in range(1,N):		
	if m > (accw[i-1]+(acce[N-1]-acce[i])):
		m = (accw[i-1]+(acce[N-1]-acce[i]))


print(m)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
cnt = 0
for i in S[1:]:
    if i == 'E':
        cnt += 1
min_cnt = cnt
for i in range(N-1):
    if S[i] == 'E' and S[i + 1] == 'E':
        cnt -= 1
    elif S[i] == 'W' and S[i + 1] == 'W':
        cnt += 1
    if cnt < min_cnt:
        min_cnt = cnt
print(min_cnt)

Submission status: Accepted

Solution code:
n = int(input())
s = input()

change = []
s0 = 0
for i in range(1, n):
    if s[i] == "E":
        s0 = s0 + 1
change.append(s0)

for i in range(1, n - 1):
    if s[i - 1] == "W":
        s0 = s0 + 1
    if s[i] == "E":
        s0 = s0 - 1
    change.append(s0)

s0 = 0
for i in range(n - 1):
    if s[i] == "W":
        s0 = s0 + 1
change.append(s0)

print(min(change))

Submission status: Accepted

Solution code:
N = int(input())
S = input()

ecount = [0]*N
if S[0] == "E":
    ecount[0] = 1
for i in range(1, N):
    if S[i] == "E":
        ecount[i] = ecount[i-1] + 1
    else:
        ecount[i] = ecount[i-1]
ans = float("INF")
for i in range(N):
    west = 0
    if i > 1:
        west = i+1 - ecount[i]
    east = 0
    if i < N-1:
        east = ecount[N-1] - ecount[i]
    ans = min(ans, west+east)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = list(input())

ans = n - 1
w = [0]
for i in range(n - 1):
    w.append(w[-1])
    if s[i] == 'W':
        w[-1] += 1
e = [0 for _ in range(n)]
for i in range(n - 1, 0, - 1):
    e[i - 1] = e[i]
    if s[i] == 'E':
        e[i - 1] += 1
for i in range(n):
    ans = min(ans, e[i] + w[i])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
W, E = [0], [0]
for s in S[::-1]:
    if s == "E":
        E.append(E[-1]+1)
    else:
        E.append(E[-1])

for s in S:
    if s == "W":
        W.append(W[-1]+1)
    else:
        W.append(W[-1])

ans = float('inf')
E = E[::-1]
for i in range(N):
    ans = min(ans, E[i-1]+W[i-1])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

a = S[1:].count('E')
W = 0
E = a
for i in range(1, N):
    if S[i - 1] == 'W':
        W += 1
    if S[i] == 'E':
        E -= 1
    a = min(W + E, a)

print(a)

Submission status: Accepted

Solution code:
n = input()
s = input()


e_cnt = 0
for i in range(1, len(s)):
    if s[i] == 'E':
        e_cnt += 1
res = []
res.append(e_cnt)
for i in range(1, len(s)):
    add = 0
    if s[i-1] == 'W':
        add += 1
    if s[i] == 'E':
        add -= 1
    res.append(res[i-1] + add)

print(min(res))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    s = input().strip()
    w = []
    count = 0
    for c in s:
        w.append(count)
        if c == 'W':
            count += 1
    e = []
    count = 0
    for c in s[::-1]:
        e.append(count)
        if c == 'E':
            count += 1
    e = e[::-1]
    print(min(i + j for i, j in zip(w, e)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
es = [0]
for c in S:
    es.append(es[-1] + (c=='W'))

ans = N
for i in range(N):
    tmp = (N-i-1) - (es[N] - es[i+1])
    tmp += es[i]
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np


def ii():
    return int(input())


def lii():
    return list(map(int, input().split(' ')))


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


def main():
    N = ii()
    S = input()
    e = 0
    for v in S:
        if v == 'E': e += 1

    min_n_turn = N
    ec = 0
    for i in range(N):
        ew = (N - i - 1) - (e - ec - int(S[i] == 'E'))
        n_turn = N - 1 - ec - ew
        if S[i] == 'E':
            ec += 1
        # print(ec, ew, n_turn)
        min_n_turn = min(min_n_turn, n_turn)

    return min_n_turn

if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
N = int(input())
s = input()
w = 0
e = 0
for i in s[1:]:
  if i == "E":
    e += 1
ans = e
tmp = e
for i in range(1,N):
    if s[i-1] == "E" and s[i] == "E":
        tmp -= 1
    elif s[i-1] == "W" and s[i] == "E":
        pass
    elif s[i-1] == "E" and s[i] == "W":
        pass
    elif s[i-1] == "W" and s[i] == "W":
        tmp += 1
    if ans > tmp:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8

def main():
	n = int(input())
	s = list(input())
	d = {'W':0, 'E':1}
	nums = [d[x] for x in s]

	left = 0
	right = sum(nums[1:])
	min_cost = right

	for i in range(1, n):	
		left = left + (1 - nums[i-1])
		right = right - nums[i]
		min_cost = min(min_cost, left + right)

	print(min_cost)



if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = [0] * n
if s[0] == 'W': a[0] = 1
for i in range(1, n):
    c = s[i]
    if c == 'E':
        a[i] = a[i - 1]
    else:
        a[i] = a[i - 1] + 1
b = [0] * n
if s[n - 1] == 'E': b[n - 1] = 1
for i in range(n - 2, -1, -1):
    c = s[i]
    if c == 'W':
        b[i] = b[i + 1]
    else:
        b[i] = b[i + 1] + 1
m = n
for i in range(n):
    m = min(m, a[i] + b[i])
print(m - 1)

Submission status: Accepted

Solution code:
N=int(input())
S=input()
Cost=0
for i in range(1,N):
    if S[i]=="E":
        Cost+=1
Costtmp=Cost
for i in range(1,N):
    if S[i-1]==S[i]:
        if S[i]=="W":
            Costtmp+=1
        else:
            Costtmp-=1
    if Costtmp<Cost:
        Cost=Costtmp
print(Cost)

Submission status: Accepted

Solution code:
N = int(input())
S = input()

tmp = 0
left = []
for c in S:
    if c == 'W':
        tmp += 1
    left.append(tmp)

tmp = 0
right = []
for c in reversed(S):
    if c == 'E':
        tmp += 1
    right.append(tmp)

right = list(reversed(right))

ans = min(right[0], left[N - 1])

for i in range(1, N - 1):
    ans = min(ans, left[i - 1] + right[i + 1])
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

N= int(input())
S = input()

cnt = Counter(S[1:])["E"]
ret = cnt
for i in range(1, N):
    if S[i] == "E":
        cnt -= 1
    if S[i-1] == "W":
        cnt += 1
    ret = min(ret, cnt)
print(ret)

Submission status: Accepted

Solution code:
N = input()
S = raw_input()
lcnt = [0]*N
rcnt = [0]*N
run = 0
for i,c in enumerate(S):
    lcnt[i] = run
    run+=c=='W'
run=0
for i,c in enumerate(reversed(S)):
    rcnt[N-i-1] = run
    run+=c=='E'

print min(map(lambda i:lcnt[i]+rcnt[i],range(N)))

Submission status: Accepted

Solution code:
n = int(raw_input())
line = raw_input()

west = [0] * n

for i in range(n - 1):
    west[i + 1] = west[i]
    if line[i] == 'W':
        west[i + 1] += 1

east = [0] * n

for i in range(n - 1, 0, -1):
    east[i - 1] = east[i]
    if line[i] == 'E':
        east[i - 1] += 1

min_change = 1e10

for i in range(n):
    if west[i] + east[i] < min_change:
        min_change = west[i] + east[i]

print min_change

Submission status: Accepted

Solution code:
#!python
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    S = input()

    s = 0
    for c in S[1:]:
        if c == 'E':
            s += 1

    ANS = s
    for i in range(1, N):
        if S[i] == 'E':
            s -= 1
        if S[i - 1] == 'W':
            s += 1
        ANS = min(ANS, s)

    print(ANS)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# C
N = int(input())
S = list(input())


res = 0
for i in range(1, N):
    if S[i] == "E":
        res += 1
res_min = res
for i in range(1, N):
    if S[i-1] == "W":
        res += 1
    if S[i] == "E":
        res -= 1
    res_min = min(res, res_min)
print(res_min)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())

N = int(input())
S = input()

R = [0]*(N)
L = [0]*(N)
for i in range(N-1):
    R[i+1] = R[i] + (S[i] == "W")
    L[-(i+1)-1] = L[-(i+1)] + (S[-(i+1)] == "E")

ans = 10**6

for i in range(N):
    ans = min(ans, R[i] + L[i])

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python2
# -*- coding: utf-8 -*-

import sys

def rl(proc=None):
    if proc is not None:
        return proc(sys.stdin.readline())
    else:
        return sys.stdin.readline().rstrip()

def srl(proc=None):
    if proc is not None:
        return map(proc, rl().split())
    else:
        return rl().split()

def main():
    rl()
    A = rl()
    right = A.count('E')
    if A[0] == 'E':
        right -= 1
    r = right
    left = 0
    for i in xrange(1, len(A)):
        if A[i] == 'E':
            right -= 1
        if A[i-1] == 'W':
            left += 1
        r = min(r, left + right)
    print r


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def ec(s):
    ans = [0]
    cur = 0
    for c in s:
        if c == 'W':
            cur += 1
        ans.append(cur)
    return ans


def main():
    n = int(input())
    s = input().strip()
    fr = ec(s)
    ba = ec(reversed(s))
    ans = n
    for i in range(n):
        ans = min(ans, fr[i] + (n - i - 1) - ba[n - 1 - i])
        # print(i, fr[i] + (n - i - 1) - ba[n - 1 - i])
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    s = S()
    c = collections.Counter(s)
    d = collections.defaultdict(int)
    r = n - 1
    for i in range(n):
        c[s[i]] -= 1
        t = c['E'] + d['W']
        if r > t:
            r = t
        d[s[i]] += 1

    return r




print(main())

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]


N = int(input())
S = input()

cnt = 0
ans = int(1e9)
for c in S:
    if c == "E":
        cnt += 1

for c in S:
    if c == "E":
        cnt -= 1
        ans = min(ans, cnt)
    else:
        ans = min(ans, cnt)
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline

n=int(input())

s=input().strip()

ans=s[1:].count('E')
t=ans

for i in range(1,n):
    if s[i-1]=='W': t+=1
    if s[i]=='E': t-=1
    ans=min(ans,t)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(raw_input())
    s = raw_input().strip()
    e = [0] * (n + 1)
    for i, c in enumerate(s):
        e[i+1] = e[i] + (c == 'E')
    ans = n
    x = 0
    for i, c in enumerate(s):
        if ans > x + e[n] - e[i+1]:
            ans = x + e[n] - e[i+1]
        x += c == 'W'
    print ans
main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

using namespace std;

int main(){
    int N; cin>> N;
    vector<char> d(N);
    string S; cin >> S;
    for(int i=0; i<N; i++)
        d[i] = S[i];

    int res = 10e8;

    for(int i=0; i<N; i++){
        int count = 0;

        for(int w=0; w<i; w++){
            if(d[w] == 'W') count++;
        }
        for(int e=i+1; e<N; e++){
            if(d[e] == 'E') count++;
        }

        res = min(res,count);
    }

    cout << res << endl;
}

Submission status: Runtime Error

Solution code:
N = input("N: ")
S = input("S: ")
point_list = []
for i in range(N):
	p = 0
	if i >0:
		for c in S[:i]:
			if c == 'W':
				p += 1
	if i + 1 < len(S):
		for d in S[i+1:]:
			if d == 'E':
				p += 1
	point_list.append(p)
print(min(point_list))

Submission status: Runtime Error

Solution code:
N = input("N: ")
S = input("S: ")
point_list = []
for i in range(0, len(S)):
		p = 0
		if i >0:
			for c in S[:i]:
				if c == 'W':
					p += 1
		if i + 1 < len(S):
			for d in S[i+1:]:
				if d == 'E':
					p += 1
	point_list.append(count_point(S, i))
print(min(point_list))

Submission status: Runtime Error

Solution code:
def count_point(S, i):
	p = 0
	for c in S[:i]:
		if c == 'W':
			p += 1
	if i + 1 < len(S):
		for d in S[i+1:]:
			if c == 'E':
				p += 1
	return (p)



N = input("N: ")
S = input("S: ")
point_list = []
for i in len(S):
	point_list.append(count_point(S, i))
print(min(point_list))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))

count = N
Sum = 0

for l in range(0, N-1):
    Sum = A[l]
    for r in range(l+1, N):
        if Sum + A[r] != Sum ^ A[r]:
            break
        else:
            Sum += A[r]
            #count += 1
    count += r-l-1
    #if r== l:
    #    r += 1
    #else:
    #    Sum -= A[l]
    #Sum -= A[l]
print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))

l_max = len(A)
#print(l_max)
r = 0
count = l_max
bit_max = 0
    
    
for l in range(0, l_max):
    bitSums = A[l]
    Sums = A[l]
    bit_max = max(A[l:l_max])
    for r in range(l+1, l_max):
        Sums = Sums + A[r]
        if len(bin(Sums))>len(bin(bit_max)):
            #print('break!')
            break
        bitSums = bitSums ^ A[r]
        if bitSums == Sums:
            count += 1

print(count)

Submission status: Runtime Error

Solution code:
import sys
input()
d={k:0 for k in 'AC WA TLE RE'.split()}
for ln in sys.stdin:
    d[ln.strip()]+=1
for k,v in d.items():
    print(k,'x',v)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())

CNT = []
c = 0
for s in S:
  if s == "E":
    c += 1
  CNT.append(c)
  
ans = N + 10
mn = N + 10
for i in range(N):
  c = 0
  if i > 0:
    c += i - 1 - CNT[i-1]
  if i < N - 1:
    c +=  N - i - 1 - (CNT[N] - CNT[i+1])
    
  if c < mn:
    ans = i+1

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = str(input())

count = S.count('E')

if S[0] == 'E':
	coun-=1

countmin = count

for i in range (1, N):
	if S[i] == 'E':
		count-=1
	if S[i-1] == 'W':
		count+=1
	if count < countmin:
		countmin = count

print(countmin)