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:
n=int(input())
S=list(input())
W=[0]*(n+1)
E=[0]*(n+1)
for i in range(n):
  W[i]=W[i-1]+S[i].count("W")
  E[n-i-1]=E[n-i]+S[n-i-1].count("E")
W = W[:n]
E = E[:n]
ans = 10000
for i in range(n):
  ans = min(ans,W[i]+E[i]-1)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
cnt = 0
ans_list = [0]
e_cnt = int()

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

if s[n-1] == 'E':
    e_cnt = (ans_list[n-1] - 1 - n) //(-2)
else:
    e_cnt = (ans_list[n-1] - n) //(-2)

if s[0] == 'E':
    e_cnt -= 1

print(min(ans_list) + e_cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
cnt = 0
ans_list = [0]
e_cnt = int()

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

if s[n-1] == 'E':
    e_cnt = (ans_list[n-1] - 1 - n) //(-2)
else:
    e_cnt = (ans_list[n-1] - n) //(-2)

print(min(ans_list) + e_cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
cnt = 0
ans_list = [0]

for i in range(1,n):
    if s[i-1] == "E":
        cnt -= 1
    else:
        cnt += 1
    ans_list.append(cnt)

if s[n-1] == "E":    
    print(min(ans_list) + (ans_list[n-1]-1-n)//(-2))
else:    
    print(min(ans_list) + (ans_list[n-1]-n)//(-2))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
cnt = 0
ans_list = []

for _ in range(n):
    if s[_] == "E":
        cnt -= 1
    else:
        cnt += 1
    ans_list.append(cnt)

print(min(ans_list) + (ans_list[n-1]-n)//(-2))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(input())
ans_list = []

for _ in range(n):
    if s[_] == "E":
        s[_] = -1
    else:
        s[_] = 1

for i in range(n):
    ans_list.append(sum(s[:i]))

print(min(ans_list) + (sum(s)+n)//2)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e = s.count("E")
w = n - e
ans = 10**9
if s[0] == "W":
    cnt = e
else:
    cnt = w
num = cnt
for i in range(n-1):
    if s[i] == s[i+1] and s[i] == "E":
        cnt -= 1
    elif s[i] == s[i+1]:
        cnt += 1
    #print(i,cnt)
    ans = min(ans,cnt,num)
    ans = max(ans,0)
print(max(ans,0))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e = s.count("E")
w = n - e
ans = 10**9
if s[0] == "W":
    cnt = e
else:
    cnt = w
num = cnt
for i in range(n-1):
    if s[i] == s[i+1] and s[i] == "E":
        cnt -= 1
    elif s[i] == s[i+1]:
        cnt += 1
    ans = min(ans,cnt,num)
print(max(ans,0))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e = s.count("E")
w = n - e
ans = 10**9
if s[0] == "W":
    cnt = e
else:
    cnt = w
num = cnt
for i in range(n-1):
    if s[i] == s[i+1] and s[i] == "E":
        cnt -= 1
    elif s[i] == s[i+1]:
        cnt += 1
    ans = min(ans,cnt,num)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
ecounter = [0 for _ in range(n)]
wcounter = [0 for _ in range(n)]
ecounter[0] = (s[0]=='E')
wcounter[n-1] = (s[n-1]=='W')
ans = n
for i in range(1,n):
    ecounter[i] = ecounter[i-1] + (s[i]=='E')
    wcounter[n-1-i] = wcounter[n-i] + (s[n-1-i]=='W')
    cnt = n-(ecounter[i]+wcounter[i])
    ans = min(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
x=[0]*n
t=0
for i in range(n):
    if s[i]=="E":
        t+=1
    x[i]=t
M=x[n-1]
c=0
ans=10**9
print(x)
for i in range(n):
    ans=min(ans,i-c+M-x[i])
    c=x[i]
print(ans)

Submission status: Wrong Answer

Solution code:

url = "https://atcoder.jp//contests/abc098/tasks/arc098_a"

def main():
    input()
    s = input()
    w_count = s.count('W')
    right_we = {"W": w_count, "E": len(s) - w_count}
    left_we = {"W": 0, "E": 0}
    ans = 100000
    for v in s:
        right_we[v] -= 1
        ans = min(ans, right_we["E"] + left_we["W"])
        left_we[v] += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
if s.count("E") >= (n+1)//2:
    print(n-s.count("E"))
else:
    print(s.count("E"))

Submission status: Wrong Answer

Solution code:
def schange(s):
    if s=="E":
        return 1
    else:
        return 0

n = int(input())
s = list(map(schange,input()))
ssum = [0]*(n+1)

for i in range(n):
    ssum[i+1] = ssum[i] + s[i]

ans = float('inf')
for i in range(1,n+1):
    turnE = (i-1)-ssum[i-1]
    turnW = ssum[-1] - ssum[i]
    print(turnE,turnW)
    ans=min(ans,turnE+turnW)
    
print(ans)

Submission status: Wrong Answer

Solution code:
def schange(s):
    if s=="E":
        return 1
    else:
        return 0

n = int(input())
s = list(map(schange,input()))
ssum = [0]*(n+1)

for i in range(n):
    ssum[i+1] = ssum[i] + s[i]

ans = float('inf')
for i in range(1,n):
    turnE = (i-1)-ssum[i-1]
    turnW = ssum[-1] - ssum[i]
    ans=min(ans,turnE+turnW)
    
print(ans)

Submission status: Wrong Answer

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

# full search
cnt = s.count('E')

ans = cnt
for i in range(n-1):
    if s[i] == 'E':
        cnt -= 1
        if ans > cnt:
            ans = cnt
    
    else:
        cnt += 1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input())
sw=[0]
se=[0]
swc=0
sec=0
for i in range(n):
    if s[i]=='W':
        swc+=1
    else:
        sec+=1
    sw.append(swc)
    se.append(sec)
m=n
for i in range(n):
    m=min(m,sw[i]+se[-1]-se[i])
print(m)

Submission status: Wrong Answer

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 += 1
  else:
    cnt -= 1
  ans = min(ans, cnt)
  
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

s = input()

e = int(s.count('E'))
w = n - e

if e >= w:
    print(w)
else:
    print(e)

Submission status: Wrong Answer

Solution code:
n = int(input())

s = list(map(str,input().split()))

e = s.count("E")
w = n - e

if e >= w:
    print(w)
else:
    print(e)

Submission status: Wrong Answer

Solution code:
import math
import sys
import os
from operator import mul
import numpy as np

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")


N = I()
S = list(_S())
ans = N

Sn = np.array(S)
S_cum = np.zeros(N+1,dtype='int')
S_cum[1:] = np.cumsum(Sn=='W')

Snr=np.flip(Sn, 0)
S_cumr = np.zeros(N+1,dtype='int')
S_cumr[1:] = np.cumsum(Snr=='E')

# print(S_cum)
# print(S_cumr)

for i in range(N):
    ans = min(ans, S_cum[i] + S_cumr[N-i])

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=list(input())
w=s.count("W")
e=s.count("E")
x=[0 for i in range(n)]
for i in range(1,n):
    if s[i-1]=="W":
        x[i]=x[i-1]+1
    else:
        x[i]=x[i-1]
ans=n
for i in range(n):
    ans=min(ans,2*x[i]+e-i)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = [_ for _ in input()]

left_w = 0
right_e = S[1:].count('E')
min_sum = left_w + right_e
ans = 1

for i in range(N-1):
    if S[i] == 'W':
        left_w -= 1
    if S[i+1] == 'E':
        right_e += 1
    if min_sum < left_w + right_e:
        min_sum = left_w + right_e
        ans = i + 1
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = [_ for _ in input()]

left_w = 0
right_e = S[1:].count('E')
ans = left_w + right_e

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

Submission status: Wrong Answer

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = [_ for _ in input()]

init_cnt = S[1:].count('E')
min_c = init_cnt
ans = 0

for i in range(1, N):
    tmp = min_c
    if S[i-1] == 'W':
        tmp = tmp + 1
    if S[i] == 'E':
        tmp = tmp - 1
    if tmp < min_c:
        min_c = tmp
        ans = i
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
w, e = S.count('W'),S.count('E')
ans = []
if w > e:
  for n in range(N):
    if S[n] == "W":
      e += 1
    else:
      e -= 1
    ans.append(e)
else:
  for n in range(N):
    if S[n] == "E":
      w += 1
    else:
      w -= 1
    ans.append(w)

print(min(ans) - 1)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
w, e = S.count('W'),S.count('E')
change = []
half = int(N / 2)
if w >= e:
  for n in range(half + 1):
    _ = S[0:n].count('W') + S[n + 1:].count('E')
    change.append(_)
else:
  for n in range(half,N):
    _ = S[0:n].count('W') + S[n + 1:].count('E')
    change.append(_)
print(min(change))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
c = [0,0]
for i in range(n):
    if (s[i]=="E"):
        c[0] += 1
    else:
        c[1] += 1
#print(c)
if (c[1]==0 or c[0]==0):
    print(1)
else:
    print(min(c)-1)

Submission status: Wrong Answer

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

Lw = 0
Re = S[1:].count("E")
ans_lst = [Lw+Re]*(N)

# i+1をリーダーとみなす
for i in range(1,N):
    if S[i-1] == "W":
        Lw += 1
    else:
        Re -= 1
    ans_lst[i] = Lw + Re

ans = min(ans_lst)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
ans=S.count("W")
for i in range(N):
  if S[i]=="W":
    ans-=1
  if i!=0:
    if S[i-1]=="E":
      ans+=1
  if i!=0:
    ans1=max(ans,ans1)
  else:
    ans1=ans
  print(ans)
print(N-1-ans1)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
s = input()
ans = float('inf')
e = int(s.count('E'))

for i in range(n):
    a = (s[:i]).count('W')
    b = e - (i-a)

    ans = min(ans, a + b)

print(ans)

Submission status: Wrong Answer

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

ans = N

left_W = 0
right_E = S.count("E")

for n in range(N):
  if n == 0:
    if S[n] == "E":
      right_E -= 1
    ans = min(ans,left_W + right_E)
        
  elif n == N-1 :
    if S[n-1] == "W":
      left_W += 1
    ans = min(ans,left_W + right_E)
      
  else:
    if S[n] == "E":
      right_E -= 1
    if S[n-1] == "W":
      left_W += 1
    ans = min(ans,left_W + right_E)

print(ans)

Submission status: Wrong Answer

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

Lw = 0
Re = S.count("E")
ans_lst = [Lw+Re]*(N)

# i+1をリーダーとみなす
for i in range(1,N):
    if S[i-1] == "W":
        Lw += 1
    else:
        Re -= 1
    ans_lst[i] = Lw + Re

ans = min(ans_lst)
print(ans)

Submission status: Wrong Answer

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

Lw = 0
Re = S.count("E")
ans_lst = [Lw+Re]*(N)

# i+1をリーダーとみなす
for i in range(1,N):
    if S[i-1] == "W":
        Lw += 1
    else:
        Re -= 1
    ans_lst[i] = Lw + Re
    print(Lw, Re ,ans_lst[i])

ans = min(ans_lst)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=list(str(input()))
wesr=S[1:].count('W')
east=S[1:].count('E')
ans_min = east
ans = east
for i in range(N-1):
  if S[i]=='W':
    ans+=1
  else:
    if ans>0:
      ans-=1
    else:
      ans=0
  if ans < ans_min:
    ans_min = ans
print(ans_min)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=list(str(input()))
wesr=S[1:].count('W')
east=S[1:].count('E')
ans_min = east
ans = east
for i in range(N-1):
  if S[i]=='W':
    ans+=1
  else:
    ans-=1
  if ans < ans_min:
    ans_min = ans
print(ans_min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
s = str(input())
sE = s.count('E')
sW = s.count('W')
l = []
e, w = 0, 0
for ch in s:
    if ch == 'E':
        e += 1
    else:
        w += 1
    m = (sE-e)+w
    l.append(sE-e+w-1 if ch == 'W' else sE-e+w)
print(l)

Submission status: Wrong Answer

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

ans=s.count('E')
pin=ans

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

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
def main():
    n = int(input())
    s = list(input())
    enum = [1 if c == 'E' else 0 for c in s]
    wnum = [1 if c == 'W' else 0 for c in s]
    enuma = tuple(accumulate(enum))
    wnuma = tuple(accumulate(wnum))
    wnumm = wnuma[-1]
    wnuma2 = [wnumm - wn for wn in wnuma]
    num = max(wnuma2[0], enuma[-1])
    for i1 in range(1, n - 1):
        num = max(num, enuma[i1 - 1] + wnuma2[i1 + 1])
    r = n - num - 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
NN = int(input())
SS = str(input())

score = [0]*NN
for i in range(1,NN):
    if SS[i] == "E":
        score[0] += 1

for pos in range(1,NN-1):
    score[pos] = score[pos-1]
    if SS[pos-1] == "W":
        score[pos] += 1
    if SS[pos] == "E":
        score[pos] -= 1

score[NN-1] = score[NN-2]
if SS[NN-2] == "W":
    score[NN-1] += 1

print(min(score))

Submission status: Wrong Answer

Solution code:
NN = int(input())
SS = str(input())

score = [0]*NN
for i in range(1,NN):
    if SS[i] == "E":
        score[0] += 1

for pos in range(1,NN-1):
    score[pos] = score[pos-1]
    if SS[pos-1] == "W":
        score[pos] += 1
    if SS[pos] == "E":
        score[pos] -= 1

score[NN-1] = score[NN-2]
if SS[NN-2] == "W":
    score[NN-1] += 1

print(score)
print(min(score))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
 
oE = S.count("E")
Wo = 0

L = []
for i in range(N):
    if i == 0:
        if S[i] == "E":
            oE -= 1
    else:
        if S[i-1] == "W":
            Wo += 1
        if S[i] == "E":
            oE -= 1
    L.append(Wo + oE)
    if Wo + oE == 1:
        break
print(min(L))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
 
oE = S.count("E")
Wo = 0

L = []
for i in range(N):
    if i == 0:
        if S[i] == "E":
            oE -= 1
    else:
        if S[i-1] == "W":
            Wo += 1
        if S[i] == "E":
            oE -= 1
    L.append(Wo + oE)
    if Wo + oE == 1:
        break
print(L)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

# ruisekiwa ba-jon

ruiseki_w = [0] * (N + 1)
ruiseki_e = [0] * (N + 1)

for i in range(0, len(S)):
    ruiseki_w[i+1] = ruiseki_w[i] + S[i].count("W")
    ruiseki_e[i+1] = ruiseki_e[i] + S[i].count("E")

res = 10**10
for i in range(0, len(S)):
    hidari_no_w_no_kazu = ruiseki_w[i]
    migi_no_e_no_kazu = ruiseki_e[-1] - ruiseki_e[i]
    res = min(hidari_no_w_no_kazu + migi_no_e_no_kazu, res)

print(res)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

# ruisekiwa ba-jon

ruiseki_w = [0] * (N + 1)
ruiseki_e = [0] * (N + 1)

for i in range(0, len(S)):
    ruiseki_w[i+1] = ruiseki_w[i] + S[i].count("W")
    ruiseki_e[i+1] = ruiseki_e[i] + S[i].count("E")

res = 10**10
for i in range(1, len(S)):
    res = min(ruiseki_w[i-1] - ruiseki_w[0] + ruiseki_e[-1] - ruiseki_e[i+1], res)
print(res)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

# ruisekiwa ba-jon

ruiseki_w = [0] * (N + 1)
ruiseki_e = [0] * (N + 1)

for i in range(0, len(S)):
    ruiseki_w[i+1] = ruiseki_w[i] + S[i].count("W")
    ruiseki_e[i+1] = ruiseki_e[i] + S[i].count("E")

res = 10**10
for i in range(0, len(S)):
    res = min(ruiseki_w[i-1] - ruiseki_w[0] + ruiseki_e[-1] - ruiseki_e[i+1], res)
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
data = []
data.append((0, 0))
for i in range(N):
    # looking left
    if S[i] == 'W':
        data.append((data[i][0]+1, data[i][1]))
    # looking right
    elif S[i] == 'E':
        data.append((data[i][0], data[i][1]+1))
# print(data)

answer = N
for i in range(N):
    count = 0
    count += data[N][1] - data[i][1]
    count += data[i][0]
    if answer > count:
        answer = count

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
direction = [{"E": 0, "W": 0} for _ in range(N + 1)]  # direcion[i]["E"] ... i番目までにEを向いている人数
ans = N
for i in range(N):
    direction[i + 1]["E"] = direction[i]["E"] + (1 if S[i] == "E" else 0)
    direction[i + 1]["W"] = direction[i]["W"] + (1 if S[i] == "W" else 0)
for i in range(N):
    ans = min(ans, direction[i]["W"] + direction[N]["E"] - direction[i]["E"])
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s="E"+input()+"W"
w=[int(i=="W") for i in s]
e=[int(i=="E") for i in s]
for i in range(1,n+2):
  w[i]+=w[i-1]
  e[i]+=e[i-1]
# print(w,e)
# e.append(e[-1])
ans=[]
for j in range(1,n+1):
  ans+=(w[j-1]-w[0]+e[-1]-e[j+1]),
print(min(ans))

Submission status: Wrong Answer

Solution code:
n=int(input())
s="x"+input()+"x"
w=[int(i=="W") for i in s]
e=[int(i=="E") for i in s]
for i in range(1,n+2):
  w[i]+=w[i-1]
  e[i]+=e[i-1]
# print(w,e)
# e.append(e[-1])
ans=[]
for j in range(1,n+1):
  ans+=(w[j-1]-w[0]+e[-1]-e[j+1]),
print(min(ans))

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
from itertools import accumulate
def main():
    n = int(input())
    s = list(input())

    if n == 2:
        if s[0] == 'E' or s[1] == 'W':
            print(0)
            sys.exit()
        else:
            print(1)
            sys.exit()
    e = [0] * n
    w = [0] * n
    for i1 in range(n):
        e[i1] = s[i1] == 'E'
        w[i1] = s[i1] == 'W'
    e.insert(0, 0)
    e.append(0)
    w.insert(0, 0)
    w.append(0)

    ea = list(accumulate(e))
    wa = list(accumulate(w))
    rt = n
    for j1 in range(1, n):
        rt = min(rt, wa[j1 - 1] + (ea[-1] - ea[j1]))
    print(rt)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
from itertools import accumulate
def main():
    n = int(input())
    s = list(input())

    if n == 2:
        if s[0] == 'E' or s[1] == 'W':
            print(0)
            sys.exit()
        else:
            print(1)
            sys.exit()
    e = [0] * n
    w = [0] * n
    for i1 in range(n):
        e[i1] = s[i1] == 'E'
        w[i1] = s[i1] == 'W'
    e.insert(0, 0)
    e.append(0)
    w.insert(0, 0)
    w.append(0)

    ea = list(accumulate(e))
    wa = list(accumulate(w))
    rt = 0
    for j1 in range(1, n):
        rt = max(rt, ea[j1 - 1] + (wa[-1] - wa[j1]))
    r = n - rt - 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
from itertools import accumulate
def main():
    n = int(input())
    s = list(input())
    e = [0] * n
    w = [0] * n
    for i1 in range(n):
        e[i1] = s[i1] == 'E'
        w[i1] = s[i1] == 'W'
    e.insert(0, 0)
    e.append(0)
    w.insert(0, 0)
    w.append(0)

    ea = list(accumulate(e))
    wa = list(accumulate(w))
    rt = 0
    for j1 in range(1, n):
        rt = max(rt, ea[j1 - 1] + (wa[-1] - wa[j1]))
    r = n - rt - 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
from itertools import accumulate
def main():
    n = int(input())
    s = list(input())
    e = [0] * n
    w = [0] * n
    for i1 in range(n):
        e[i1] = s[i1] == 'E'
        w[i1] = s[i1] == 'W'
    e.insert(0, 0)
    e.append(0)
    w.insert(0, 0)
    w.append(0)

    ea = list(accumulate(e))
    wa = list(accumulate(w))
    rt = 0
    for j1 in range(1, n):
        rt = max(rt, ea[j1 - 1] + (wa[-1] - wa[j1]))
        print(j1, rt)
    r = n - rt - 1
    print(r)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

for i in S:
    ans = min(ans, W+E)
    if i == 'W':
        W +=1
    else:
        E -= 1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

eastNumber = S.count('E')
westNumber = 0
minimum = N

for i in range(N):
    if S[i] == 'E':
        eastNumber -= 1
        number = eastNumber + westNumber
        if minimum > number:
            minimum = number
    else:
        number = eastNumber + westNumber
        if minimum > number:
            minimum = number
        westNumber -= 1

print(minimum)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect


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


def z():
    return int(input())


def S():
    return input()


def C(line):
    return [input() for _ in range(line)]


N = z()
S = S()
wester = [0] * N
west_sum = 0
easter = [0] * N
sum_ = [0]*N
east_sum = S.count('E')
for i, s in enumerate(S):
    wester[i] = west_sum
    easter[i] = east_sum
    sum_[i] = west_sum+east_sum
    if (s == 'W'):
        west_sum += 1
    else:
        east_sum -= 1
# print(wester)
# print(easter)
print(min(sum_))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
W=[0]*(n+1) #Wについての累積和
E=[0]*(n+1) #Eについての累積和
e=0
w=0
for i in range(n):
    if s[i]=="W":
        w += 1
    else:
        e += 1
    
    W[i+1] = w
    E[i+1] = e
ans = 10**10
for i in range(n):
    tmp = W[i] + (E[n]-E[i])
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
ap=[0]*(N+1)
bp=[0]*(N+1)
a=0
b=0
for i in range(N):
  if S[i]=='W':
    a+=1
  else:
    b+=1
  ap[i+1]=a
  bp[i+1]=b
ans=10**9
for j in range(N):
  ans=min(ap[j-1]+bp[N]-bp[j],ans)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
a,b=0,0
ans=10**9
for i in range(N):
  if S[i]=='W':
    a+=1
  else:
    b+=1
for j in range(N):
  if S[j]=='W':
    ans=min(ans,min(a-1,b))
  else:
    ans=min(ans,min(a,b-1))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
s = readline().decode().rstrip()

cost = [0]*n
cost[0] = s.count('E')
if s[0] == 'W':
    cost[1] += 1

for i in range(1, n):
    if s[i] == 'E':
        cost[i] += cost[i - 1] - 1
    else:
        cost[i] += cost[i - 1]
        if i < n - 1:
            cost[i + 1] += 1
print(min(cost))

Submission status: Wrong Answer

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

west = 0
for i in range(N):
  if S[i] == "W":
    west += 1

ans_list = [0] * N
for i in range(N):
  if S[i] == "W":
    west -= 1
  if i != 0:
    if S[i - 1] == "E":
      west += 1
  ans_list[i] = min(west, N - 1 - west)
  #ans_list[i] = west
#print(ans_list)
print(min(ans_list))

Submission status: Wrong Answer

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

west = 0
for i in range(N):
  if S[i] == "W":
    west += 1

ans_list = [0] * N
for i in range(N):
  if S[i] == "W":
    west -= 1
  if i != 0:
    if S[i - 1] == "E":
      west += 1
  #ans_list[i] = min(west, N - west)
  ans_list[i] = west
#print(ans_list)
print(min(ans_list))

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
S = "d"+input()+"d"

#index=0,N+1は初期化に必要
leftwest = np.array([0]*(N+2))
righteast = np.array([0]*(N+2))
for i in range(1,N+1):
    if S[i] == "W":
        leftwest[i] = leftwest[i-1] + 1
    else:
        leftwest[i] = leftwest[i-1]
        
    if S[-(i+1)] == "E":
        righteast[-(i+1)] = righteast[-i] + 1
    else:
        righteast[-(i+1)] = righteast[-i]
        
print(min(righteast[1:N+1]+leftwest[0:N]))

Submission status: Wrong Answer

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

# check
res = N - 1
for idx, s in enumerate(S):

    if idx == 0:
        target = list(S[1:])
    elif idx == N - 1:
        target = list(S[:idx])
    else:
        target = list(S[:idx] + S[idx + 1:])

    turn_cnt = min(target.count("W"), target.count("E"))

    if res > turn_cnt:
        res = turn_cnt

print(res)

Submission status: Wrong Answer

Solution code:
def solve(S,N):
    E,W = [0 for _ in range(N)],[0 for _ in range(N)]
    for i in range(N):
        if S[i] == "E":
            E[i] = 1
        else:
            W[i] = 1

    for i in range(1,N):
        E[i] += E[i-1]
        W[i] += W[i-1]

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

    return ans

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

    print(solve(S,N))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    S = iss()
    NUM = [0] * (N+1)

    for n in range(N):
        NUM[n+1] += NUM[n]
        if S[n] == 'W':
            NUM[n+1] += 1

    ret = MAX
    for n in range(N):
        ret = min(ret, NUM[n] + N - n - (NUM[-1] - NUM[n]))
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
s = input()
l = s.count("W")
r = s.count("E")
keep = r
minn = keep
for i in range(1, n):
    if s[i - 1:i + 1] == "WW":
        keep += 1
    elif s[i - 1:i + 1] == "WE":
        keep += 0
    elif s[i - 1:i + 1] == "EW":
        keep += 0
    elif s[i - 1:i + 1] == "EE":
        keep -= 1
    if minn > keep:
        minn = keep
print(minn)

Submission status: Wrong Answer

Solution code:
n,s=open(0);a=t=s[1:].count('E')
for i in s:t+=(-1 if i=='E' else 1);a=min(a,t)
print(a)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e = [0]
w = [0]
for i in range(len(s)):
    if s[i] == 'E':
        e.append(e[i]+1)
        w.append(w[i]+0)
    if s[i] == 'W':
        e.append(e[i]+0)
        w.append(w[i]+1)

ans = float('INF')
for i in range(n):
    iya = (w[i])+(e[n] - e[i])
    ans = min(ans,iya)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
w = [0]*(n + 2)
e = [0]*(n + 2)
ans = 10000

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

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

print(ans)