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: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w, e = [0], [0]
for i in range(N):
    if S[i] == "W":
        if len(w) == 1:
            w.append(1)
        else:
            w.append(w[-1] + 1)
    else:
        if len(w) == 1:
            w.append(0)
        else:
            w.append(w[-1])

    if S[-i-1] == "E":
        if len(e) == 1:
            e.append(1)
        else:
            e.append(e[-1] + 1)
    else:
        if len(e) == 1:
            e.append(0)
        else:
            e.append(e[-1])
ans = 10**9
e = list(reversed(e))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[0])
    elif i == len(w)-2:
        ans = min(ans, w[-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w, e = [0], [0]
for i in range(N):
    if S[i] == "W":
        if len(w) == 0:
            w.append(1)
        else:
            w.append(w[-1] + 1)
    else:
        if len(w) == 0:
            w.append(0)
        else:
            w.append(w[-1])

    if S[-i-1] == "E":
        if len(e) == 0:
            e.append(1)
        else:
            e.append(e[-1] + 1)
    else:
        if len(e) == 0:
            e.append(0)
        else:
            e.append(e[-1])
ans = 10**9
e = list(reversed(e))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[0])
    elif i == len(w)-2:
        ans = min(ans, w[-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w, e = [], []
for i in range(N):
    if S[i] == "W":
        if len(w) == 0:
            w.append(1)
        else:
            w.append(w[-1] + 1)
    else:
        if len(w) == 0:
            w.append(0)
        else:
            w.append(w[-1])

    if S[-i-1] == "E":
        if len(e) == 0:
            e.append(1)
        else:
            e.append(e[-1] + 1)
    else:
        if len(e) == 0:
            e.append(0)
        else:
            e.append(e[-1])
ans = 10**9
e = list(reversed(e))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[0])
    elif i == len(w)-2:
        ans = min(ans, w[-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w, e = [], []
for i in range(N):
    if S[i] == "W":
        if i == 0:
            w.append(1)
        else:
            w.append(w[-1] + 1)
    else:
        if i == 0:
            w.append(0)
        else:
            w.append(w[-1])

    if S[-i-1] == "E":
        if i == 0:
            e.append(1)
        else:
            e.append(e[-1] + 1)
    else:
        if i == 0:
            e.append(0)
        else:
            e.append(e[-1])
ans = 10**9
e = list(reversed(e))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[0])
    elif i == len(w)-2:
        ans = min(ans, w[-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w, e = [0], [0]
for i in range(N):
    if S[i] == "W":
        if i == 0:
            w.append(1)
        else:
            w.append(w[-1] + 1)
    else:
        if i == 0:
            w.append(0)
        else:
            w.append(w[-1])

    if S[-i-1] == "E":
        if i == 0:
            e.append(1)
        else:
            e.append(e[-1] + 1)
    else:
        if i == 0:
            e.append(0)
        else:
            e.append(e[-1])
ans = 10**9
e = list(reversed(e))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[0])
    elif i == len(w)-2:
        ans = min(ans, w[-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w, e = [0], [0]
for i in range(N):
    if S[i] == "W":
        if i == 0:
            w.append(1)
        else:
            w.append(w[-1] + 1)
    else:
        if i == 0:
            w.append(0)
        else:
            w.append(w[-1])

    if S[-i-1] == "E":
        if i == 0:
            e.append(1)
        else:
            e.append(e[-1] + 1)
    else:
        if i == 0:
            e.append(0)
        else:
            e.append(e[-1])
ans = 10**5
e = list(reversed(e))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[0])
    elif i == len(w)-2:
        ans = min(ans, w[-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0]
wb = [0]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])
    
    if S[-i-1] == "E":
        wb.append(wb[-1] + 1)
    else:
        wb.append(wb[-1])
ans = float("inf")
w = w[1:]
e = list(reversed(wb[1:]))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[i+1])
    elif i == N-1:
        ans = min(ans, w[i-1])
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0]
wb = [0]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])
    
    if S[-i-1] == "E":
        wb.append(wb[-1] + 1)
    else:
        wb.append(wb[-1])
ans = 10**9 + 7
w = w[1:]
e = list(reversed(wb[1:]))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[i+1])
    elif i == N-1:
        ans = min(ans, w[i-1])
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0 if S[0]!="W" else 1]
wb = [0 if S[0]!="E" else 1]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])
    
    if S[-i-1] == "E":
        wb.append(wb[-1] + 1)
    else:
        wb.append(wb[-1])
ans = 10**5
# w = w[1:]
# e = list(reversed(wb[1:]))
e = list(reversed(wb))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[i+1])
    elif i == len(w)-2:
        ans = min(ans, w[i-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0]
wb = [0]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])
    
    if S[-i-1] == "E":
        wb.append(wb[-1] + 1)
    else:
        wb.append(wb[-1])
ans = 10**5
# w = w[1:]
# e = list(reversed(wb[1:]))
e = list(reversed(wb))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[i+1])
    elif i == len(w)-2:
        ans = min(ans, w[i-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0]
wb = [0]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])
    
    if S[-i-1] == "E":
        wb.append(wb[-1] + 1)
    else:
        wb.append(wb[-1])
ans = 10**5
# w = w[1:]
# e = list(reversed(wb[1:]))
e = list(reversed(wb))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[i+1])
    elif i == len(w)-1:
        ans = min(ans, w[i-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0]
wb = [0]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])
    
    if S[-i-1] == "E":
        wb.append(wb[-1] + 1)
    else:
        wb.append(wb[-1])
ans = 10**5
# w = w[1:]
# e = list(reversed(wb[1:]))
e = list(reversed(wb))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[i+1])
    elif i == N-1:
        ans = min(ans, w[i-1])
    # print(ans, i)
print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0]
wb = [0]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1] + 1)
    else:
        w.append(w[-1])
    
    if S[-i-1] == "E":
        wb.append(wb[-1] + 1)
    else:
        wb.append(wb[-1])
ans = 10**5
w = w[1:]
e = list(reversed(wb[1:]))
for i in range(len(w)-1):
    if 1 <= i < N-1:
        ans = min(ans, w[i-1] + e[i+1])
    elif i == 0:
        ans = min(ans, e[i+1])
    elif i == N-1:
        ans = min(ans, w[i-1])
print(ans)

Submission status: Wrong Answer

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

w = 0
e = 0
for i in s:
  if i =="W":
    pass
  else:
    e +=1
for i in s:
  if i=="E":
    e-=1
    n = min(n,e+w)
  if i =="W":
    w+=1
print(n)

Submission status: Wrong Answer

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

w = 0
e = 0
for i in s:
  if i =="W":
    w +=1
  else:
    e +=1
print(min(w,e))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
S = input()
w = [0]
wb = [0]
for i in range(N):
    if S[i] == "W":
        w.append(w[-1])
    else:
        w.append(w[-1]+1)
    
    if S[-i-1] == "E":
        wb.append(wb[-1])
    else:
        wb.append(wb[-1] + 1)
ans = 0
for w1, w2 in zip(reversed(w[2:-2]), wb[2:-2]):
    ans = max(ans, abs(w1-w2))
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[i for i in input()]
E_cnt=0
W_cnt=0
#print(S)
ans=0
flg=0
for i in range(N):
    if S[i]=='E':
        E_cnt+=1
    else:
        W_cnt+=1
    if E_cnt-W_cnt>ans:
        ans=E_cnt-W_cnt
        flg=i
    #print(ans)
print(ans+S[i+1:].count('E'))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=[i for i in input()]
E_cnt=0
W_cnt=0
#print(S)
ans=0
flg=0
for i in range(N):
    if S[i]=='E':
        E_cnt+=1
    else:
        W_cnt+=1
    if E_cnt>W_cnt:
        ans=E_cnt-W_cnt
        flg=i
    #print(ans)
print(ans+S[i+1:].count('E'))

Submission status: Wrong Answer

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

S = [0] * n
countW = 0
for i in range(n):
    if s[i] == "W":
        S[i] = 1
        countW += 1

ans = []
countE = n - countW
left = 0
right = 0

for i in range(n):
    if i == 0:
        ans.append(countW - S[0])
    elif i > 0:
        left += S[i-1]
        # right = countE - i - 1 + left
        right = n - i - 1 - countW + left + 1
        ans.append(left + right)
        # print("------------")
        # print("i: " + str(i))
        # print("left: " + str(left))
        # print("right: " + str(right))

# print("*********")
# print(countE)
# print(ans)
print(min(ans))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
east = [0] * (n + 1)
west = [0] * (n + 1)
for i in range(n):
    east[i + 1] = east[i]
    west[i + 1] = west[i]
    if s[i] == 'E':
        east[i + 1] += 1
    else:
        west[i + 1] += 1
ans = float('inf')
ans = min(ans, east[-1] - east[2])
for i in range(2, n):
    tmp = west[i - 1] + east[-1] - east[i + 1]
    ans = min(ans, tmp)
ans = min(ans, west[n - 1])
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
east = [0] * (n + 1)
west = [0] * (n + 1)
for i in range(n):
    east[i + 1] = east[i]
    west[i + 1] = west[i]
    if s[i] == 'E':
        east[i + 1] += 1
    else:
        west[i + 1] += 1
ans = float('inf')
for i in range(1, n):
    tmp = west[i - 1] + east[-1] - east[i + 1]
    ans = min(ans, tmp)
ans = min(ans, west[n])
print(ans)

Submission status: Wrong Answer

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

vWest = [0] * (int(N)+1)
vEast = [0] * (int(N)+1)

for i in range(int(N)):
    vWest[i+1] += vWest[i]
    if S[i] == 'W':
        vWest[i] += 1
    vEast[i+1] += vEast[i]
    if S[i] == 'E':
        vEast[i] +=1

ans = INF
for j in range(int(N)):
    now = vWest[i] + (vEast[N] - vEast[i+1])
    if ans > now:
        ans = now

print(ans)

Submission status: Wrong Answer

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

# 左から
l = 0
li = 0
lmax = -(10**8)
for i in range(N):
    if S[i] == 'W':
        l -= 1
    else:
        l += 1
    if l > lmax:
      lmax = l
      li = i

lans = 0
for i in range(N):
    if i < li:
        if S[i] == 'W':
            lans += 1
    else:
        if S[i] == 'E':
            lans += 1

# 右から
r = 0
ri = 0
rmax = -(10**8)
for i in reversed(range(N)):
    if S[i] == 'E':
        r -= 1
    else:
        r += 1
    if r > rmax:
      rmax = r
      ri = i

rans = 0
for i in range(N):
    if i < ri:
        if S[i] == 'W':
            rans += 1
    else:
        if S[i] == 'E':
            rans += 1

# print(li, ri)
print(min(lans, rans))

Submission status: Wrong Answer

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

#選んだリーダーよりも左 -> E, 右 -> W をとる
#数えるのはリーダーよりも左にいるWと右にいるE
E = []
W = []
e, w = 0, 0

for i in s:
    if i == 'E':
        e += 1
    elif i == 'W':
        w += 1
    E.append(e)
    W.append(w)

#k番目がリーダーとすると
ans = 10 ** 10
W.insert(0,0)
for k in range(n):
    can = W[k-1] + (E[-1]-E[k])
    if can <= ans:
        ans = can

print(ans)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
s=list(input())
ans=10**10
wl=0
el=0
wr=s.count('W')
er=s.count('E')
for i in range(n):
    if s[i]=='E':
        el+=1
        er-=1
    else:
        wl+=1
        wr-=1
    if wl+er<ans:
        ans=wl+er
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))

if len(dic)==1:
  print(0)
  exit()

#(左にあるWの数+右にあるEの数)の最小値が答え
#(右にあるWの数+左にあるEの数)の最小値が答えの場合もあるか
left_w = 0
right_e = dic["E"]

left_e = 0
right_w = dic["W"]

ans = n
for i in range(n):
  if s[i] == "W":
    ans = min(ans, left_w+right_e, left_e+right_w)
    left_w += 1
    right_w -= 1
    
  elif s[i] == "E":
    ans = min(ans, left_w+right_e, left_e+right_w)
    right_e -= 1
    left_e += 1
  
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))
#(右にあるWの数+左にあるEの数)の最小値が答えの場合もあるか
if len(dic)==1:
  print(0)
  exit()
left_w = 0
right_e = dic["E"]
cnt = [[] for _ in range(n)]
#(左にあるWの数+右にあるEの数)の最小値が答え
ans = n
for i in range(n):
  if s[i] == "W":
    cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    right_e -= 1
  
#(右にあるWの数+左にあるEの数)の最小値が答えの場合もあるか
left_e = 0
right_w = dic["W"]
#cnt = [[] for _ in range(n)]

for i in range(n):
  if s[i] == "E":
    cnt[i] = [left_e,right_w]
    ans = min(ans,left_e+right_w)
    left_e += 1
  elif s[i] == "W":
    cnt[i] = [left_e,right_w]
    ans = min(ans,left_e + right_w)
    right_w -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))
if len(dic)==1:
  print(0)
  exit()
left_w = 0
right_e = dic["E"]
cnt = [[] for _ in range(n)]
#(左あるWの数+右にあるEの数)の最小値が答え
ans = n
for i in range(n):
  if s[i] == "W":
    #cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    #cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    right_e -= 1
  
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))
left_w = 0
try:
  #全部Wの場合エラーが起きるので...
  right_e = dic["E"]
except KeyError:
  print(0)
  exit()
cnt = [[] for _ in range(n)]
#(左あるWの数+右にあるEの数)の最小値が答え
ans = n
for i in range(n):
  if s[i] == "W":
    #cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    #cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    right_e -= 1
  
print(ans)

Submission status: Wrong Answer

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

E=[0]*N
W=[0]*N
c=0
for i in range(N):
  if S[i]=='W':
    c +=1
  W[i] =c
c=0
for j in range(N):
  if S[-1-j]=='E':
    c +=1
  E[-1-j]=c
E=[E[0]]+E+[0]
W=[0]+W+[W[-1]]
answer=float('inf')
for i in range(N+2):
  answer=min(answer,E[i]+W[i])
print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = str(input())
S = []
for t in s:
    if t == 'E':
        S.append(1)
    elif t == 'W':
        S.append(0)

ans = 10 ** 10

for i in range(n):
    left = S[:i]
    right = S[i+1:]
    can = 0
    if S[i] == 1:
        can += (len(left) - sum(left)) + sum(right)
    else:
        can += sum(left) + (len(right) - sum(right))
    if can <= ans:
        ans = can

print(ans)

Submission status: Wrong Answer

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

cnt=S.count("E")
m=cnt
for i in S:
  if i == "E":
    cnt-=1
  else:
    cnt+=1
    m=min(m,cnt)
print(m)

Submission status: Wrong Answer

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

ans=[]
result=len(S)
for i in range(n):
  if ans == S[i]:
    result-=1
    p = S[i]
print(result)

Submission status: Wrong Answer

Solution code:
N = int(input())
str_to_int = {"W":0, "E":1}
input_ = input()
row = [str_to_int[input_[n]] for n in range(N)]

min_change_count = N+2

west_side = row[:0]
east_side = row[0:]
west_side_count = 0
east_side_count = sum(east_side)
count = west_side_count + east_side_count
if count < min_change_count:
    min_change_count = count
    
for n in range(1, N):
    if row[n-1] == 0:
        west_side_count += 1
        
    if row[n] == 1:
        east_side_count -= 1
        
    count = west_side_count + east_side_count
    if count < min_change_count:
        min_change_count = count
        
print(min_change_count)

Submission status: Wrong Answer

Solution code:
N = int(input())
str_to_int = {"W":0, "E":1}
input_ = input()
row = [str_to_int[input_[n]] for n in range(N)]

min_change_count = N+1

west_side = row[:0]
east_side = row[0:]
west_side_count = 0
east_side_count = sum(east_side)
count = west_side_count + east_side_count
if count < min_change_count:
    min_change_count = count
    
for n in range(1, N):
    if row[n-1] == 0:
        west_side_count += 1
        
    if row[n] == 1:
        east_side_count -= 1
        
    count = west_side_count + east_side_count
    if count < min_change_count:
        min_change_count = count
        
print(min_change_count)

Submission status: Wrong Answer

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

e=[1 if c=='E' else 0 for c in s]
w=[1 if c=='W' else 0 for c in s]

m=min(sum(e[1:n]),sum(w[:n-1]))
for i in range(1,n-1):
  m=min(m,sum(e[i+1:n])+sum(w[:i]))

Submission status: Wrong Answer

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

Wlist=[0]*(N+1)
Elist=[0]*(N+1)
for i in range(N):
    Wlist[i+1]=Wlist[i]
    if people[i]=='W':
        Wlist[i+1]+=1
    Elist[i+1]=Elist[i]
    if people[i]=='E':
        Elist[i+1]+=1

print(Wlist)
print(Elist)

List=[]
for i in range(N):
    List.append(Wlist[i]+(Elist[N]-Elist[i+1]))

print(min(List))

Submission status: Wrong Answer

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

Wlist=[0]*N
for i in range(N):
    if people[i]=='W':
        Wlist[i]=Wlist[i-1]+1
    else:
        Wlist[i]=Wlist[i-1]

Elist=[0]*N
for i in range(N):
    if people[i]=='E':
        Elist[i]=Elist[i-1]+1
    else:
        Elist[i]=Elist[i-1]

List=[]
for i in range(N):
    List.append(Wlist[i]-Elist[i]+Elist[-1])

print(min(List))

Submission status: Wrong Answer

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

S = list(map(lambda x: 0 if x == 'E' else 1, S))

def c(S, i):
    count = i
    index = 0
    if i < 3:
        for idx in range(i):
            s1 = S[int(idx/2):]
            s2 = S[:int(idx/2)]
            co = N - i - sum(s1) + sum(s2)
            if co < count:
                index = idx
        return idx

    s1 = S[int(i/2):]
    s2 = S[:int(i/2)]

    if (N - i - sum(s1)) < sum(s2):
        return c(s1, len(s1)) + int(i/2)
    else:
        return c(s2, len(s2)) - int(i/2)

i = c(S, N)

count = N - i - sum(S[int(i/2):]) + sum(S[:int(i/2)])

print(count)

Submission status: Wrong Answer

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

S = list(map(lambda x: 0 if x == 'E' else 1, S))

def c(S, i):
    s1 = S[int(i/2):]
    s2 = S[:int(i/2)]
    print(s2, s1)
    print(i)
    if len(S) < 3:
        return 2
    elif (N - i - sum(s1)) < sum(s2):
        return c(s1, len(s1)) + int(i/2)
    else:
        return c(s2, len(s2)) - int(i/2)

i = c(S, N)

count = N - i - sum(S[int(i/2):]) + sum(S[:int(i/2)])

print(count)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
lst1=[0]*n
lst2=[0]*n
if s[0]=='W':
  lst1[0]=1
if s[0]=='E':
  lst2[0]=1
for i in range(1,n):
  if s[i]=='W':
    lst1[i]=lst1[i-1]+1
  if s[i]=='E':
    lst2[i]=lst2[i-1]+1
l=[]
l.append(lst2[n-1]-lst2[0])
for j in range(1,n-1):
  l.append(lst1[j-1]+lst2[n-1]-lst2[j])
l.append(lst1[n-2])

print(min(l))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
lst1=[0]*n
lst2=[0]*n
if s[0]=='W':
  lst1[0]=1
if s[0]=='E':
  lst2[0]=1
for i in range(1,n):
  if s[i]=='W':
    lst1[i]=lst1[i-1]+1
  if s[i]=='E':
    lst2[i]=lst2[i-1]+1
l=[]
l.append(lst2[n-1]-lst2[0])
for j in range(1,n-1):
  l.append(lst1[j-1]+lst2[n-1]-lst2[j])
l.append(lst1[n-2])

print(min(l))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
w = s.count('W')
e = s.count('E')
if w==e:
    print(w-1)
else:
    print(min(w,e))

Submission status: Wrong Answer

Solution code:
input()
s = input()
print(min(s.count('E'),s.count('W')))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
 
num_E, num_W = 0, 0
# num_E_list = [0] * (N + 1)
# num_W_list = [0] * (N + 1)
min_sum_operation = N
argmin_i = 0
for i, c in enumerate(S):
    if i == 0:
        # This operation is safe because N is larger than 2
        right = S[i + 1:]
        num_E = right.count('E')
        num_W += 1 if c == 'W' else 0
        # num_E_list[i] = num_E
        # num_W_list[i] = num_W
    elif c == 'W':
        num_W += 1
        # num_W_list[i + 1] = num_W
    elif c == 'E':
        num_E -= 1
        # num_E_list[i] = num_E
    if min_sum_operation > num_E + num_W:
        min_sum_operation = min(min_sum_operation, num_E + num_W)
        argmin_i = i
 
    # print(min_sum_operation, num_E, num_W)
 
print(min_sum_operation)

Submission status: Wrong Answer

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

num_E, num_W = 0, 0
# num_E_list = [0] * (N + 1)
# num_W_list = [0] * (N + 1)
min_sum_operation = N
argmin_i = 0
for i, c in enumerate(S):
    if i == 0:
        # This operation is safe because N is larger than 2
        right = S[i + 1:]
        num_E = right.count('E')
        num_W += 1 if c == 'W' else 0
        # num_E_list[i] = num_E
        # num_W_list[i] = num_W
    elif c == 'W':
        num_W += 1
        # num_W_list[i + 1] = num_W
    elif c == 'E':
        num_E -= 1
        # num_E_list[i] = num_E
    if min_sum_operation > num_E + num_W:
        min_sum_operation = min(min_sum_operation, num_E + num_W)
        argmin_i = i

    # print(min_sum_operation, num_E, num_W)

if argmin_i == 0:
    print(min_sum_operation + 1)
else:
    print(min_sum_operation)

Submission status: Wrong Answer

Solution code:
n = int(input())
members = input()
m_e = members.count("E")
cost = m_e
costs = []
for i in range(n):
    if members[i]=="E":
        cost-=1
    if members[i-1]=="W":
        cost+=1
    costs.append(cost)
print(min(costs))

Submission status: Wrong Answer

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

W_frqs = [0 for _ in range(N+1)]
E_frqs = [0 for _ in range(N+1)]

for i, s in enumerate(S):
    if s == 'W':
        W_frqs[i+1] = W_frqs[i] + 1
        E_frqs[i+1] = E_frqs[i]
    else:
        E_frqs[i+1] = E_frqs[i] + 1
        W_frqs[i+1] = W_frqs[i]

# print(W_frqs)
# print(E_frqs)

if N == 1:
    print(0)
    exit()

min_ans = N+1
for n in range(1, N):
    ans = 0

    # nより右でEの人の数
    ans += E_frqs[N] - E_frqs[n+1]

    # nより左でWの人の数
    ans += W_frqs[n-1] - W_frqs[0]

    # print(n, ans)
    min_ans = min(min_ans, ans)


print(min_ans)

Submission status: Wrong Answer

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

W_frqs = [0 for _ in range(N+1)]
E_frqs = [0 for _ in range(N+1)]

for i, s in enumerate(S):
    if s == 'W':
        W_frqs[i+1] = W_frqs[i] + 1
        E_frqs[i+1] = E_frqs[i]
    else:
        E_frqs[i+1] = E_frqs[i] + 1
        W_frqs[i+1] = W_frqs[i]

# print(W_frqs)
# print(E_frqs)
n = 2


min_ans = N+1
for n in range(1, N):
    ans = 0

    # nより右でEの人の数
    ans += E_frqs[N] - E_frqs[n+1]

    # nより左でWの人の数
    ans += W_frqs[n-1] - W_frqs[0]

    # print(n, ans)
    min_ans = min(min_ans, ans)


print(min_ans)

Submission status: Wrong Answer

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

W_frqs = [0 for _ in range(N+1)]
E_frqs = [0 for _ in range(N+1)]

for i, s in enumerate(S):
    if s == 'W':
        W_frqs[i+1] = W_frqs[i] + 1
        E_frqs[i+1] = E_frqs[i]
    else:
        E_frqs[i+1] = E_frqs[i] + 1
        W_frqs[i+1] = W_frqs[i]

# print(W_frqs)
# print(E_frqs)
n = 2


min_ans = N+1
for n in range(N):
    ans = 0

    # nより右でEの人の数
    ans += E_frqs[N] - E_frqs[n]

    # nより左でWの人の数
    ans += W_frqs[n] - W_frqs[0]

    # print(n, ans)
    min_ans = min(min_ans, ans)


print(min_ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
S = list(input())
S_c = Counter(S)
turn_left = 0
turn_right = S_c["E"]
min_change = turn_right

for i in range(1, N):
    new_leader = S[i]
    turn_right -= 1 if new_leader == "E" else 0
    old = S[i - 1]
    turn_left += 1 if old == "W" else 0
    change = turn_left + turn_right
    if change < min_change:
        min_change = change

print(min_change)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
S = list(input())
S_c = Counter(S)
turn_left = 0
turn_right = S_c["E"]
min_change = turn_right
for i in range(1, N):
    turn_left = 0
    turn_right = S_c["E"]
    new_leader = S[i]
    turn_right -= 1 if new_leader == "E" else 0
    old = S[i - 1]
    turn_left += 1 if old == "W" else 0
    change = turn_left + turn_right
    if change < min_change:
        min_change = change

print(min_change)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
S = list(input())
S_c = Counter(S)
turn_left = 0
turn_right = S_c["E"]
min_change = turn_right
for i in range(1, N):
    new_leader = S[i]
    turn_right -= 1 if new_leader == "E" else 0
    old = S[i - 1]
    turn_left += 1 if old == "W" else 0
    change = turn_left + turn_right
    if change < min_change:
        min_change = change

print(min_change)

Submission status: Wrong Answer

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

e_l = [0]
w_l = [0]

for i in range(len(s)):
    if s[i] == "E":
        e_l.append(e_l[i]+1)
    else:
        e_l.append(e_l[i])

for i in range(len(s)):
    if s[i] == "W":
        w_l.append(w_l[i]+1)
    else:
        w_l.append(w_l[i])

ans = float("inf")
for i in range(len(s)):
    if i == 0:
        tmp = 0 + (e_l[-1] - e_l[i+1])
    elif i == len(s) - 1:
        tmp = w_l[i-1] 
    else:
        tmp = w_l[i -1] + (e_l[-1] - e_l[i+1])
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

e_l = [0]
w_l = [0]

for i in range(len(s)):
    if s[i] == "E":
        e_l.append(e_l[i]+1)
    else:
        e_l.append(e_l[i])

for i in range(len(s)):
    if s[i] == "W":
        w_l.append(w_l[i]+1)
    else:
        w_l.append(w_l[i])

ans = float("inf")
for i in range(len(s)):
    tmp = 0
    if i == 0:
        tmp += 0 + (e_l[-1] - e_l[i+1])
    elif i == len(s) - 1:
        tmp += w_l[i-1] 
    else:
        tmp += w_l[i -1] + (e_l[-1] - e_l[i+1])
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

N=int(input())
H=list(str(input()))

west=[]
east=[]
for i in H:
    if i == 'W':
        west.append(1)
        east.append(0)
    elif i=='E':
        west.append(0)
        east.append(1)

    
w = list(accumulate(west))
e = list(accumulate(east))

for p in range(N):
    if p==0:
        t = e[-1] 
        m=t
    elif p+1==N:
        t = w[p] 
    else:
        t = w[p-1] + (e[-1] - e[p+1])
    
    if m > t:
        m=t

print(m)

Submission status: Wrong Answer

Solution code:
N=int(input())

H=list(str(input()))


m = 0 
for i in range(N):
    c = 0
    for s in range(N):
        if i > s and H[s] == 'W':
            c += 1
        
        if i < s and H[s] == 'E':
            c += 1
    if m == 0 or m >c:
        n = i
        m = c
        


print(n)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(map(str, input()))
ret = float("inf")
west = [0 for _ in range(N + 2)]
east = [0 for _ in range(N + 2)]

for i in range(N):
	if S[i] == "W":
		west[i + 1] = west[i] + 1
	else:
		west[i + 1] = west[i]
for i in reversed(range(N)):
	if S[i] == "E":
		east[i] = east[i + 1] + 1
	else:
		east[i] = east[i + 1]
	ret = min(ret, west[i] + east[i])

print(ret)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
answer = s.count("E") if s[0] == "W" else s.count("E")-1
for i in range(1,n):
    a = answer
    a += 1 if s[i-1]=="W" else 0
    a -= 1 if s[i] == "E" else 0
    answer = min(answer,a)
print(answer)