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: Runtime Error

Solution code:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    S = input()
    
    ans = float('inf')
    
    W = {'W':0, 'E':0}
    n = S.count('E')
    E = {'W':N-n, 'E':n}
    
    for s in S:
        E[s] -= 1
        
        ans = min(ans, E['E'] + W['W'])
        
        W[s] += 1
        
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

West = []
East = []
count = 1
if S[0] == "W":
    WE = "W"
else:
    WE = "E"
for i in range(1, N):
    if S[i] == WE:
        count += 1
    else:
        if S[i] == "W":
            East.append(count)
            WE = "W"
        else:
            West.append(count)
            WE = "E"
        count = 1
if S[N-1] == "E":
    East.append(count)
else:
    West.append(count)

print(min(sum(West) - max(East) + 1, sum(East) - max(West) + 1))

Submission status: Runtime Error

Solution code:
_ = int(input())
members = input()

counts = []
for i in range(len(members)):
    count=0
    for j in range(len(members)):
        if i == j:
            continue
        else:
            if (members[j] == 'W' and i<j) or (members[j] == 'E' and i>j):
                continue
            else:
                count+=1
        if count > min(counts):
            break
    counts.append(count)
print(min(counts))

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
s = input()

v,e,w = np.zeros(N)

for i in range(1,N):
  if s[i-1] == 'W':
    w[i] = w[i-1] + 1
  else:
    w[i] = w[i-1]
    
for i in reversed(range(0,N-1)):
  if s[i+1] == 'E':
    e[i] = e[i+1] + 1
  else:
    e[i] = e[i+1]
    
for i in range(N):
  v[i] = e[i] + w[i]
  
print(np.min(v))

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
s = input()

v,e,w = np.zeros(N)

for i in range(1,N):
  if s[i-1] == 'W':
    w[i] = w[i-1] + 1
  else:
    w[i] = w[i-1]
    
for i in reversed(range(0,N-1)):
  if s[i+1] == 'E':
    e[i] = e[i+1] + 1
  else:
    e[i] = e[i+1]
    
for i in range(N):
  v[i] = e[i] + w[i]
  
print(v.min())

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    S = input()
    from collections import Counter
    ri = dict(Counter(S))
    le = {'W': 0, 'E': 0}
    pre = ''
    ans = 1 << 60
    for s in S:
        if pre:
            le[pre] += 1
        ri[s] -= 1
        ans = min(ans, le['W'] + ri['E'])
        pre = s
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
S = input()
count = 0
answer = n - 1
for i in range(n):
    print(S[:i], S[i + 1:])
    count = S[:i].count('W') + S[i + 1:].count('E')
    answer = min(answer, count)
    count = 0

print(answer)

Submission status: Runtime Error

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

P = []

for i in range(N):
    l = L[:i]
    if i+1 < N:
    r = R[i+1:]

    tmp = l.count('W') + r.count('E')
    P.append(tmp)

print(min(P))

Submission status: Runtime Error

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

# print(N, S)
ans = N

for i, h in enumerate(range(N)):
    skipflg = False
    f = S[0: i]
    l = S[i]
    s = S[i+1:]
    print(f, l, s)
    nw = 0
    ne = 0
    nw = f.count('W')
    ne = s.count('E')
    print(nw, ne)
    # for r in f:
    #     if r != 'E':
    #         nw += 1
    #         if nw >= ans:
    #             skipflg = True
    #             break
    #         # print('not e')

    # if skipflg:
    #     break

    # for w in s:
    #     if w != 'W':
    #         ne += 1
    #         if nw+ne >= ans:
    #             skipflg = True
    #             break
    #         # print('not w')

    # if skipflg:
    #     break

    ans = min(ans, nw+ne)

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(e)
e = np.add.accumulate(e)
e = np.flip(e)
ans = []
for i in range(N):
    num = 0
    ans.append(num)
print(min(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
# from itertools import accumulate

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(e)
e = np.add.accumulate(e)
e = np.flip(e)
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
# from itertools import accumulate

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(np.add.accumulate(np.flip(e)))
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

Submission status: Runtime Error

Solution code:
import numpy as np
from itertools import accumulate

N = int(input())
s = input()
w = np.array(list(map(lambda x: 1 if x == 'W' else 0, s)))
e = np.array(list(map(lambda x: 1 if x == 'E' else 0, s)))
w = np.add.accumulate(w)
e = np.flip(np.add.accumulate(np.flip(e)))
# print(w)
# print(e)
ans = []
for i in range(N):
    num = 0
    if 0 < i: num += w[i - 1]
    if i < N - 1: num += e[i + 1]
    ans.append(num)
# print(ans)
print(min(ans))

Submission status: Runtime Error

Solution code:
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input())
    S = list(_ for _ in input())
    l = cnt_E
    r = 0
    ans = N
    for i in range(N):
        if i != N-1:
            if S[i] == 'E':
                l = l - 1
        if i != 0:                        
            if prev == 'W':
                r = r + 1
        ans = min(ans,l+r)
        prev = S[i]
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
%%time
from collections import Counter

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

count_E = Counter(S)['E']
count_W = Counter(S)['W']

right = 0
left = count_E
tmp = N
for i in range(N):
    if right + left < tmp:
        tmp = right + left
    if S[i] == 'W':
        right += 1
    else:
        left -= 1
print(tmp)

Submission status: Runtime Error

Solution code:
# 写経

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


cnt = s[1:].count("E")
ans = cnt
for i in range(1, n):
    if s[i-1] == "W":
        cnt_e += 1
    if s[i] == "E":
        cnt_e -= 1
    ans = min(ans, cnt)


print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
hdg = S[1:N].count('E')
for i in range(1,N):
    if S[i] = 'E':
        hdg_chgd -= 1
    if S[i-1] = 'W':
        hdg_chgd += 1
print(min(hdg, hdg_chgd))

Submission status: Runtime Error

Solution code:
from itertools import accumulate
from collections import Counter

n=int(input())
a=list(map(int,input().split()))

A=[0]+list(accumulate(a))
res=0
for e in Counter(A).values():
	res+=e*(e-1)//2
print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
ans = 300000
lst = []
for i in range(n):
    tmp = s[:i].count("W") + s[i+1:].count("E")
    ans = min(ans,tmp)
    
print(min(ans))

Submission status: Runtime Error

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

lst = []
for i in range(n):
    total = s[:i].count("W") + s[i+1:].count("E")
    lst = lst + [total]
    
print(min(total))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
l = [0]*n
for i in range(n):
    l[i] = s[0:i].count('W') + s[i+1:]count('E')
print(min(l))

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
w=ans=9**9
for i in range(N):
    e=S[:i].count("W")
    w=S[i:].count("E")
    ans=min(ans,e+w)
    if w=0:break
print(ans)

Submission status: Runtime Error

Solution code:
n,s=int(input()),input()
a=[s[0]=="E"]
for i in s[1:]:a.append(a[-1]+(i=="E"))
m=999999
for i in range(n):m=min(m,a[:i]+((n-i-1)-(a[-1]-a[i])))
print(m)

Submission status: Runtime Error

Solution code:
n,s=int(input()),input()
a=[s[0]=="E"]
for i in s[1:]:a.append(a[-1]+i=="E")
m=999999
for i in range(n):m=min(m,a[:i]+((n-i-1)-(a[-1]-a[i])))
print(m)

Submission status: Runtime Error

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

w = [0]
e = [0]

for i in range(1, n):

    j = (n - 1) - i

    if (s[i-1] == 'W'):
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])

    if (s[j+1] == 'E'):
        e.insert(0, e[0] + 1)
    else:
        e.insert(0, e[0])

result = []

ans = 10 ** 6 
for i in range(n):
    ans = w[i] + e[i] if w[i] + e[i] > ans else ans

print(min(result))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
S="E"+S+"W"
E=[0]*(N+2)
W=[0]*(N+2)
for i in range(len(S)-1):
    if S[i]=="W":
        E[i+1]=E[i]+1
    else:
        E[i+1]=E[i]
 
for i in range(len(S)-1,0,-1):
    if S[i]=="E":
         W[i-1]=W[i]+1
    else:
         W[i-1]=W[i]
ans=10**9
for i in range(N):
    ans=min(ans,E[i+1]+W[i+1])
print(ans)
N=int(input())
S=input()
S="E"+S+"W"
E=[0]*(N+2)
W=[0]*(N+2)
for i in range(len(S)-1):
    if S[i]=="W":
        E[i+1]=E[i]+1
    else:
        E[i+1]=E[i]

for i in range(len(S)-1,0,-1):
    if S[i]=="E":
         W[i-1]=W[i]+1
    else:
         W[i-1]=W[i]
ans=10**9
for i in range(N):
    ans=min(ans,E[i+1]+W[i+1])
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
A = [1 if(S[i]=='E') else 0 for i in range(N)]
for i in range(1,N):
  ans = min(ans, i - sum(A[:i])+ sum(A[i+1:]))
ans = min(ans, sum(A[1:]), N-sum(A[:]))
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
w=0
e=0
ans=0
for i in s:
  if(i="W"):
    ans=min(ans,ans-e+w)
    w+=1
  else:
    e+=1
    ans=min(ans,ans-e+w)
print(ans+e)

Submission status: Runtime Error

Solution code:
n = int(input())
a = str(input())

c = 0
p = n

for l in range(n):
    c += a.count('W', 0, l)
    elif c > p:
        break
    c += a.count('E', l+1)
    if p > c:
        p = c
    elif c == 0:
        p = c
        break
    c = 0
print(p)

Submission status: Runtime Error

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

lst_w = []
lst_e = []

count = 0
for i in string:
    if i == 'W':
        lst_w.append(count)
        count += 1
    else:
        lst_w.append(count)
        
count = 0
for i in string[::-1]:
    if i == 'E':
        lst_e.append(count)
        count += 1
    else:
        lst_e.append(count)
lst_e = lst_e[::-1]

print(min(np.array(lst_w) + np.array(lst_e)))

Submission status: Runtime Error

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

E = [0]*(N+2)
W = [0]*(N+2)
for i in range(1,N+1):
    if S[i-1] == 'E':
        E[i] = E[i-1] + 1
        W[i] = W[i-1]
    else:
        W[i] = W[i-1] + 1
        E[i] = E[i-1]
#print(E)
#print(W)
ans = N
for l in range(1,N+2):
    num = E[N]-E[l+1]
    num += W[l-1]-W[0]
    if ans >= num:
        ans = num

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
S_list = list(S)
face = [-1, 1]
ans_index = N // 2
while ans_index != N - 1 and S[ans_index] == S[ans_index + face[S[ans_index] == "E"]]:
    ans_index += face[S[ans_index] == "E"]
# print(ans_index)
ans = 0
for i in range(len(S)):
    if i < ans_index and S[i] == "W":
        ans += 1
    elif ans_index == i:
        continue
    elif i > ans_index and S[i] == "E":
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
def solve(string):
    n, s = string.split()
    n = int(n)
    num_e = len(s.count("E"))
    count_e = 0
    count_w = 0
    ans = n
    for _s in s:
        tmp = count_w
        if _s == "E":
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
s=input()
half=n//2
if s[half]=="E":
    for i in range(n//2):
        if s[half+1]!=s[half]:
            break
        else:
            half+=1
else:
    for i in range(n//2):
        if s[half-1]!=s[half]:
            break
        else:
            half-=1
before=dict(Counter(s[0:half])).get("W",0)
after=dict(Counter(s[half+1:])).get("E",0)
print(before+after)

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
s=input()
half=n//2
if s[half]=="E":
    for i in range(n//2+1):
        if s[half+1]!=s[half]:
            break
        else:
            half+=1
else:
    for i in range(n//2+1):
        if s[half-1]!=s[half]:
            break
        else:
            half-=1
before=dict(Counter(s[:half])).get("W",0)
after=dict(Counter(s[half+1:])).get("E",0)
print(before+after)

Submission status: Runtime Error

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

d=np.zeros(N)
d[0]=S[1:].count("E")

for i in range(1,N):
    if S[i]=="W" and S[i-1]=="W":
        d[i]=d[i-1]+1
    elif S[i]=="E" and S[i-1]=="W":
        d[i]=d[i-1]
    elif S[i]=="W" and S[i-1]=="E":
        d[i]=d[i-1]
    else:
        d[i]=d[i-1]-1
        
print(int(np.min(d)))

Submission status: Runtime Error

Solution code:
n = int(input())
pp = list(input())
cnt = 0
lis = []
for i in range(n):
    for j in range(i):
        if pp[j] == "E":
            cnt += 1
    for k in range(i+1,n):
        if pp[k] =="W":
            cnt += 1
    lis.apped(cnt) 
    cnt = 0
print(min(lis))

Submission status: Runtime Error

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

listSeeWSum=[]
index=0
wSum=0
while index < n:
    if index==0:
        listSeeWSum.append(0)
    else:
        if listS[index-1] == "W":
            wSum+=1
        listSeeWSum.append(wSum)
    index+=1

listSeeESum=[]
listS.reverse()
index = 0
eSum = 0
while index < n:
    if index == 0:
        listSeeESum.append(0)
    else:
        if listS[index-1] == "E":
            eSum += 1
        listSeeESum.append(eSum)
    index += 1
listSeeESum.reverse()

listSum=[]
index=0
while index < n:
    listSum.append(listSeeESum[index]+listSeeWSum[index])
    index+=1

print(min(listSum))v

Submission status: Runtime Error

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

for i in s:
    L = s[:i]
    R = s[i+1:]
    turn = L.count("W") + R.count("E")
    if turn < ans:
        ans = turn

print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
line=str(input())
line=line.replace('E','0')
line=line.replace('W','1')
line=list(map(int,line))
v=0
for i in range(n-1):
  if line[i]==0 or line[i+1]==0:
    pass
  else:
    v=line[0:i].count(o)+line[i+1:n].count(1)
    break
print(v)

Submission status: Runtime Error

Solution code:
n=int(input())
line=str(input())
line=line.replace('E','0')
line=line.replace('W','1')
line=list(map(int,line))
v=0
for i in range(n-1):
  if line[i]==0 or line[i+1]:
    pass
  else:
    v=line[0:i].count(o)+line[i+1:n].count(1)
    break
print(v)

Submission status: Runtime Error

Solution code:
n=int(input())
line=list(str(input()))
line.replace('E',0)
line.replace('W',1)
def turn_e(x):
  count=0
  for i in range(x):
    if line[i]==1:
      count+=1
  return count
def turn_w(x):
  count=0
  for i in range(x+1,n):
    if line[i]==0:
      count+=1
  return count
v=n
for i in range(n):
  if (turn_e(i)+turn_w(i))<v:
    v=turn_e(i)+turn_w(i)
print(v)

Submission status: Runtime Error

Solution code:
=int(input())
a=str(input())
b=N
for i in range(N):
    L=a[:i-1]
    R=a[i-1:]
    n=L.count('W')+R.count('E')
    if b>n:
        b=n
    else:
        b+=0
print(b)

Submission status: Runtime Error

Solution code:
N=int(input())
a=str(input())
b=N
for i in range(N):
    L=a[:i-1]
    R=a[i-1:]
    n=append(L.count('W')+R.count('E'))
    if b>n:
        b=n
    else:
        b+=0
print(b)

Submission status: Runtime Error

Solution code:
N=int(input())
a=str(input())
b=N
for i in range(N):
    L=a[:i-1]
    R=a[i-1:]
    n=append(L.count('W')+R.count('E'))
    if b>n:
        b=n
    else:
        b+=0
print(b[0])

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
ls = []
for i in range(n):
  ls.append(s[:i].count("W")+s[i+1:].count("E")
print(min(ls))

Submission status: Runtime Error

Solution code:
[carbon:~/src/github.com/hilolih/atcoder:148](! master )% cat ABC098/c.py
# -*- coding: utf-8 -*-
import sys
# ----------------------------------------------------------------
# Use Solve Function

def solve(lines):
    N = int(lines.pop(0))
    S = lines.pop(0)
    RS = S[::-1]
    S = 'x' + S
    RS = 'x' + RS
    slist = []
    rslist = []
    ss = 0
    rss = 0
    for i in range(N):
        if S[i] == 'W':
            ss += 1
        if RS[i] == 'E':
            rss += 1
        slist.append(ss)
        rslist.insert(0,rss)
    #print(S)
    #print(RS)
    #print(list( zip(slist, rslist)))
    print(min([s + r for s, r in zip(slist, rslist)]))

#
# main
#
# 2. use stdin

lines = [x.strip() for x in sys.stdin.readlines()]

#
# solve !!
#
solve(lines)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
count = 0
count_ = []
for i in range(N):
    E = S[:i]
    W = S[i+1:]
    print(E, W)
    count = E.count('W') + W.count('E')
    count_.append(count)
print(min(count_))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
ans = s[1:].count("E")
a = ans
for i in range(n-1):
  if s[i] == "W"
    a += 1
  if s[i+1] == "E"
    a -= 1
  if a < ans:
    ans = a
print(ans)

Submission status: Runtime Error

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

l = len(s)
ans = l
for i in range(l):
  l_we = s[0:i] if(i >= 0)else None
  l_ea = s[i+1:l] if(i+1 <= l) else None
  print(l_we)
  print(l_ea)
  tmp = 0
  tmp += l_we.count('W')
  tmp += l_ea.count('E')
  ans = min(ans, tmp)
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
e_count = S.count('E')
w_count = 0
ans = e_count

for s in S:
    if s == 'E':
        e_count -= 1
    else:
        w_count += 1
    if e_count + w_count < ans:
        e_count+w_count = ans

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
import math

n=int(input())
s=list(input())
ls=[0 if i=='W' else 1 for i in s]
a=sum(ls)
#print(a)
l=[math.inf]
for i in range(n):
    if ls[i]==1:
        a-=1
        l.append(a)
    elif ls[i]==0:
        a+=1
        l.append(a-1)
print(min(l))

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
import math

n=int(input())
s=list(input())
ls=[0 if i=='W' else 1 for i in s]
a=sum(ls)
#print(a)
l=[math.inf]
for i in range(n):
    if ls[i]==1:
        a-=1
        l.append(a)
    elif ls[i]==0:
        a+=1
        l.append(a-1)
print(min(l))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
c=[]
cnt=1
for i in range(n-1):
    if s[i] = s[i+1]:
        cnt += 1
    else:
        c.append(cnt)
        cnt=1
if s[n-2]==s[n-1]:
    c.append(cnt)
else:
    c.append(1)
print(min(c))

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
c=[]
cnt=1
for i in range(n-1):
    if s[i] = s[i+1]:
        cnt += 1
    else:
        c+=cnt
        cnt=1
if s[n-2]==s[n-1]:
    c+=cnt
else:
    c+=1
print(min(c))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
num = []

for i in range(N):
    East = S[:i]
    West = S[(i+1):]
    num.append(S[:i].count("W")+S[(i+1)].count("E"))
print(min(num))

Submission status: Runtime Error

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

minc = 100000000

for i in range(len(s)):
  minc = min(minc,(left_wrong = s[0:i].count('W')+s[i:].count('E')))

print(minc)

Submission status: Runtime Error

Solution code:
s = list(input())

m = n

for i in range(n):
	t = 0
	for j in range(n):
		if (j<i and s[j]=="W") or (j>i and s[j]=="E"):
			t += 1
		if t>m:
			break

	m = min(m,t)
print(m)

Submission status: Runtime Error

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

min_num = 
for i in range(n):
    min_num = min(s[:i].count("W") + s[i+1:].count("E"), min_num)
print(min_num)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
sum_s = [0] * N
# [0, i) のWの数
for i in range(1, N + 1):
    if S[i - 1] == 'W':
        sum_s[i] = sum_s[i - 1] + 1
    else:
        sum_s[i] = sum_s[i - 1]
        
ans = sum_s[N + 1]
for i in range(0, N):
    lc = sum_s[i]
    rc = sum_s[N + 1] - sum_s[i + 1] 
    min(ans, lc + rc)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
direction = list(input())
change = [0]*N
change[0] = direction.count("E")
 
for i in range(1, N):
    f_n = 0
    f_p = 0
    if direction[i] == "E":
        f_n = 1
    if direction[i1] == "W":
        f_p = 1
    change[i] = change[i-1] - f_n + f_p
print(min(change))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;


int main(){
  int N;
  cin >> N;
  string S;
  cin >> S;

  int counter = 0;
  int ans = 100;
  for(int i = 0; i < N; i++){
	
    counter = 0;
    for(int j = 0; j < N; j++){
		if(i == j){
          continue;
        } else if(j < i){
		  if(S[j] == 'W') counter++;
        } else {
		  if(S[j] == 'E') counter++;
        }
    }
    if(ans > counter) ans = counter;
  }

  cout << ans << endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# --------------------------------------------

dp = None
S = None

def main():
    global S

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

    if N == 2:
        if S == "WE":
            print(1)
        else:
            print(0)
        sys.exit()

    E, W = [], []
    e, w = 0, 0

    for s in S:
        if s == "E":
            e += 1
        else:
            w += 1
        E.append(e)
        W.append(w)

    ans = min(ans, E[-1] - E[0 + 1])
    for leader in range(1, N):
        if leader == N - 1:
            ans = min(ans, W[leader - 1])
        else:
            ans = min(ans, W[leader - 1] + E[-1] - E[leader + 1])

    print(ans)


main()

Submission status: Runtime Error

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(10000)

# Debug output
def chkprint(*args):
    names = {id(v):k for k,v in inspect.currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

# Binary converter
def to_bin(x):
    return bin(x)[2:]

# --------------------------------------------

dp = None
S = None

def main():
    global S

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

    if N == 2:
        if S == "WE":
            print(1)
        else:
            print(0)
        sys.exit()

    E, W = [], []
    e, w = 0, 0

    for s in S:
        if s == "E":
            e += 1
        else:
            w += 1
        E.append(e)
        W.append(w)

    ans = min(ans, E[-1] - E[0 + 1])
    for leader in range(1, N):
        elif leader == N - 1:
            ans = min(ans, W[leader - 1])
        else:
            ans = min(ans, W[leader - 1] + E[-1] - E[leader + 1])

    print(ans)


main()

Submission status: Runtime Error

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


def sarch(i, e_num_in_right, w_num_in_left, ans):
    # print(i, e_num_in_right, w_num_in_left, ans)
    if i == 0:
        w_num_in_left = 0
        e_num_in_right = s[1:].count("E")
        if s[0] == "W":
            ans = e_num_in_right
            sarch(i + 1, e_num_in_right, w_num_in_left + 1, ans)
        else:
            sarch(i + 1, e_num_in_right, w_num_in_left, ans)
    elif i == n:
        print(ans)
    else:
        if s[i] == "W":
            ans = min(ans, e_num_in_right + w_num_in_left)
            sarch(i + 1, e_num_in_right, w_num_in_left + 1, ans)
        else:
            ans = min(ans, e_num_in_right - 1 + w_num_in_left)
            sarch(i + 1, e_num_in_right - 1, w_num_in_left, ans)


sarch(0, 0, 0, 0)

Submission status: Runtime Error

Solution code:
N = int(input())
row = list(input())
cou = []
for i in range(N):
  l = cou[i-N-1]
  r = cou[i+1]
  if i == 0:
    s = r.count("E")
  elif i == N - 1:
    s = l.count("W")
  else:
    s = l.count("W") + r.count("E")
  cou.append(s)
cou.sort()
print(cou[0])

Submission status: Runtime Error

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


for i in range(1, N):
	if S[i - 1] == "W":
		left_w += 1
	if S[i] == "E":
		right_e -= 1
ans=abs(left_w+right_e)
    
print(ans)

Submission status: Runtime Error

Solution code:
#include<iostream>
#include<string.h>
using namespace std;

int main(void){
	int N;
	cin>>N;
	char S[N];
	for(int i=0;i<N;i++){
		cin>>S[i];
	}
	int num_arr[N];
	int num=0;
	for(int i=0;i<N-1;i++){
		if(S[i+1]=='E'){
			num++;
		}
	}
	num_arr[0]=num;
	for(int i=0;i<N-1;i++){
		if(S[i]=='W'){
			if(S[i+1]=='W'){
				num_arr[i+1]=num_arr[i]+1;
			}else{
				num_arr[i+1]=num_arr[i];
			}
		}else{
			if(S[i+1]=='W'){
				num_arr[i+1]=num_arr[i];
			}else{
				num_arr[i+1]=num_arr[i]-1;
			}
		}
	}
	int ans=num_arr[0];
	for(int i=0;i<N;i++){
		if(num_arr[i]<ans){
			ans=num_arr[i];
		}
	}
	cout<<ans<<endl;
	return 0;
}

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
result = []
 
for i in range(n):
    count = 0
    for j in range(n):
        if (i < j) and (s[j] == "E"):
            count += 1
        elif (i > j) and (s[j] == "W"):
            count += 1
    result.append(count)
 
pprint(min(result))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
res = 3*100000

for i in range(n):
    res1 = s[0:i].count('W') + s[i+1:n]count('E')
    res = min(res,res1)

print(res)

Submission status: Runtime Error

Solution code:
# ANSHUL GAUTAM
# IIIT-D

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

L = []
for i in S:
	if(i not in L):
		L.append(i)

if(len(L) == 1):
	print(1)

else:
	ans = 9999999999
	for i in range(0,N-1):
		cur = 0
		left = S[:i]
		right = S[i+1:]
		e = right.count('E')
		w = left.count('W')
		cur = e+w
#		print(cur,i)
		ans = min(ans,cur)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
//https://qiita.com/minemine0505/items/9aeae0f651f19f2ac1e2
N = int(input())
S = input()
min = S[1:N].count("E") #最小値を保存しておく
now = S[1:N].count("E") #i-1 を見ているときのそっぽ向いてる子の数

for i in range (1,N):
    if S[i-1] == "E" and S[i] == "E":
        now -= 1
    if S[i-1] == "W" and S[i] == "W":
        now += 1
    if now < min:
        min = now
print(min)

Submission status: Runtime Error

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

c = 0
for i in s:
  if i == "E":
    c+=1

min = c


for i in range(n):
  if s[i] == s[i+1]:
    if s[i] == "W":
      c+=1
    else:
      c-=1
  if c < min:
    min = c

temp = 0
for i in range(n-1):
  if s[i] == "W":
    temp += 1
  
if temp < min:
  min = temp 

print(min)

Submission status: Runtime Error

Solution code:
N = int(raw_input())
S = str(raw_input())
opt_cnt = N
for i in range(N):
    first = S[:i]
    second = S[i+1:]
    cnt = 0
    for f in first:
        if f is not 'E':
            cnt = cnt + 1
        if cnt >= opt_cnt
            break
    for s in second:
        if s is not 'W':
            cnt = cnt + 1
        if cnt >= opt_cnt
            break
    if opt_cnt > cnt:
        opt_cnt = cnt
print(opt_cnt)

Submission status: Runtime Error

Solution code:
n = input()
l = list(raw_input(x))
ans = n

for i in range(0, n):
  tmp = l[:i].count("W") + l[i+1:].count("E")
  if tmp < ans:
    ans = tmp

print ans

Submission status: Runtime Error

Solution code:

N = input()
S = raw_input()

ans = 0
ans_w = 0
ans_e = 0

for i in S:
    if i == 'E':
        ans_e += 1

ans = ans_e

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

print ans

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
ans=[]
alle=list(s).count("E")
allw=n-alle
ecnt=wcnt=0
for i in range(n):
    ans.append(wcnt+alle-(i-wcnt+ecnt)
    if s[i] == "W":
        wcnt+=1
    else:
        ecnt+=1
print(min(ans))