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:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    #csw[i]:='W'のi番目までの登場回数
    #cse[i]:='E'のi番目までの登場回数
    csw = [0]
    cse = [0]
    for i in range(N):
        if S[i] == 'W':
            csw.append(csw[i]+1)
            cse.append(cse[i])
        else:
            csw.append(csw[i])
            cse.append(cse[i]+1)
    ans = int(1e4)
    for i in range(N):
        #Sのi番目の文字を見たときSの区間[0,i）にある'W'の数と区間(i, N)にある'E'の数の合計が「向く方向を変える人数」である
        #i番目はリーダーなので数えないことに注意
        ans = min(cse[N]-cse[i+1] + csw[i] - csw[0], ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    #csw[i]:='W'のi番目までの登場回数
    #cse[i]:='E'のi番目までの登場回数
    csw = [0]
    cse = [0]
    for i in range(N):
        if S[i] == 'W':
            csw.append(csw[i]+1)
            cse.append(cse[i])
        else:
            csw.append(csw[i])
            cse.append(cse[i]+1)
    ans = int(1e6)
    for i in range(N):
        #Sのi番目の文字を見たときSの区間[0,i）にある'W'の数と区間[i, N)にある'E'の数の合計が「向く方向を変える人数」である
        ans = min(cse[N]-cse[i] + csw[i] - csw[0], ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    N = int(readline())
    S = readline().rstrip().decode()
    #csw[i]:='W'のi番目までの登場回数
    #cse[i]:='E'のi番目までの登場回数
    csw = [0]
    cse = [0]
    for i in range(N):
        if S[i] == 'W':
            csw.append(csw[i]+1)
            cse.append(cse[i])
        else:
            csw.append(csw[i])
            cse.append(cse[i]+1)
    ans = int(1e4)
    for i in range(N):
        #Sのi番目の文字を見たときSの区間[0,i）にある'W'の数と区間[i, N)にある'E'の数の合計が「向く方向を変える人数」である
        ans = min(cse[N]-cse[i] + csw[i] - csw[0], ans)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

c_e = 0
c_w = 0

for s in S:
    if s == "E":
        c_e += 1
    elif s == "W":
        c_w += 1

print(min(c_e, c_w))

Submission status: Wrong Answer

Solution code:
import bisect
n = int(input())
s = input()
elist = []
wlist = []
for i in range(n):
	if s[i] == "E":
		elist.append(i)
	elif s[i] == "W":
		wlist.append(i)
ans = 10**18
lenelist = len(elist)
"""
西にいるWを数える
東にいるEを数える
"""
for i in range(n):
	tmp = lenelist-bisect.bisect_left(elist, i)+bisect.bisect_left(wlist, i)
	ans = min(tmp, ans)
print(ans)

Submission status: Wrong Answer

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

from collections import defaultdict
l = defaultdict(int)
r = defaultdict(int)
for si in s:
    r[si] += 1
r[s[0]] -= 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
cumsum = [0]*N
if S[0] == 'E':
    cumsum[0] += 1
for i in range(1, N):
    if S[i] == 'E':
        cumsum[i] += cumsum[i-1] + 1
    else:
        cumsum[i] += cumsum[i-1]

ans = float('inf')
ans = min(ans, cumsum[-1]-cumsum[0])

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s=str(input())
s=list(s)
tempw=s.count("W")
ans=n-tempw
cnt=n-tempw
for i in range(1,n):
  if s[i-1]=="W":
    cnt=cnt+1
  if s[i]=="E":
    cnt=cnt-1
  ans=min(ans,cnt)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = input()
re = 0
for s in S:
    if s == 'E':
        re += 1
lw = 0
c = 3*10*5
for i in range(n):
    if S[i]  == 'E':
        re -= 1
    c = min(c, re+lw)
    if S[i] == 'W':
        lw += 1
    
print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
c = s[1:].count('E')
mc = c

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

print(mc)

Submission status: Wrong Answer

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

w = s.count('W')
e = n - s.count('W')

for i in range(n):
    if (w - 1 == i and e <= w) or (e - 1 == i and w <= e):
        print(i - 1)
        exit()

Submission status: Wrong Answer

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

WtoE=0
EtoW=S[1:N+1].count('E')
min_ans = WtoE + EtoW

for i in range(1, N):

    if S[i-1] == 'W':
        WtoE += 1
    if S[i-1] == 'E':
        EtoW -= 1
    ans = WtoE + EtoW

    if min_ans > ans:
        min_ans = ans

print(min_ans)

Submission status: Wrong Answer

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

WtoE=0
EtoW=S[1:N+1].count('E')
min_ans = WtoE + EtoW
print(WtoE, EtoW, min_ans)
for i in range(1, N):
    print(i)
    if S[i-1] == 'W':
        WtoE += 1
    if S[i-1] == 'E':
        EtoW -= 1
    ans = WtoE + EtoW
    print(WtoE, EtoW, ans)
    if min_ans > ans:
        min_ans = ans

print(min_ans)

Submission status: Wrong Answer

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

WtoE=0
EtoW=S[1:N+1].count('E')
min_ans = WtoE + EtoW

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

    if min_ans > ans:
        min_ans = ans

print(min_ans)

Submission status: Wrong Answer

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

WtoE=0
EtoW=S[1:N+1].count('E')
min_ans = WtoE + EtoW

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

    if min_ans > ans:
        min_ans = ans

print(min_ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = str(input())
min_ans = 100000000000000000
for i in range(N):
    WtoE = S[0:i].count('W')
    EtoW = S[i+1:N].count('E')
    ans = (WtoE + EtoW)
    if min_ans > ans:
        min_ans = ans

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
right_w = 0
right_E = 0
left_E = 0
left_W = 0
ans = len(s)
for i in s:
    if i == "W":
        right_w += 1
    else:
        right_E += 1
for i in range(len(s)):
    tmp = s[i]
    tmp_ans = left_W+ right_E
    if tmp_ans < ans:
        ans = tmp_ans
    if tmp == 'W':
        right_w -= 1
        left_W += 1
    else:
        right_E -= 1
        left_E += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

S = input()
l = S
r = S[::-1]
w = [0]*(N+1)
e = [0]*(N+1) 

for i in range(N):
    if l[i] == "W":
        w[i+1] = 1
    
    if r[i] == "E":
        e[i+1] = 1

for i in range(N-1):
    w[i+1] += w[i]
    e[i+1] += e[i]

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
E = S.count("E")
cnt_E = [E]
res = 10**9
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    if res > cnt_E[i]+cnt_W[i]-1:
        res = cnt_E[i]+cnt_W[i]-1
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
E = S.count("E")
cnt_E = [E]
res = 10**9
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    if res > cnt_E[i]+cnt_W[i]:
        res = cnt_E[i]+cnt_W[i]-1
print(res)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
E = S.count("E")
cnt_E = [E]
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    SUM.append(cnt_W[i]+cnt_E[i])
print(max(0,min(SUM)-1))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
E = S.count("E")
cnt_E = [E]
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    SUM.append(cnt_W[i]+cnt_E[i]-1)
print(max(0,min(SUM)))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
E = S.count("E")
cnt_E = [E]
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    SUM.append(cnt_W[i]+cnt_E[i]-1)
print(min(SUM))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
E = S.count("E")
cnt_E = [E]
flag = True
if S[0] == "W":
    cnt_W = [1]
else:
    cnt_W = [0]
SUM = [cnt_E[0]+cnt_W[0]]
for i in range(1,N):
    if S[i] == "W":
        cnt_W.append(cnt_W[i-1]+1)
        cnt_E.append(cnt_E[i-1])
    else:
        cnt_W.append(cnt_W[i-1])
        if flag:
            cnt_E.append(cnt_E[i-1])
            flag = False
        else:
            cnt_E.append(cnt_E[i-1]-1)
    SUM.append(cnt_W[i]+cnt_E[i])
print(min(SUM)-1)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

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

    ans = 10 * 6
    sum_count = S.count('E')
    for s in S:
        aft = 0
        if s == 'W':
            aft = 1
        else:
            sum_count -= 1
        ans = min(ans, sum_count)
        sum_count += aft
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	if n == 2:
		print(1 if len(Counter(list(s)).keys()) == 2 else 0)
		return
	e_w = sum_e[-1] - sum_e[1]
	e_e = sum_w[n-1]
	ans = min(e_w, e_e)
	for i in range(n):
		v = sum_w[i-1] + (sum_e[-1] - sum_e[i+1])
		ans = min(ans, v)
	print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	if n == 2:
		print(1 if len(Counter(list(s)).keys()) == 2 else 0)
		return
	for i in range(n):
		if i == 0:
			v = sum_e[-1] - sum_e[i]
			ans = min(ans, v)
		else:
			v = sum_w[i-1] + (sum_e[-1] - sum_e[i+1])
			ans = min(ans, v)
	print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	if n == 2:
		print(1 if len(Counter(list(s)).keys()) == 2 else 0)
		return
	for i in range(1, n):
		v = sum_w[i-1] + (sum_e[-1] - sum_e[i+1])
		ans = min(ans, v)
	print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	for i in range(1, n):
		v = sum_w[i-1] + (sum_e[-1] - sum_e[i])
		ans = min(ans, v)
	print(ans)
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	for i in range(1, n):
		v = sum_w[i-1] + (sum_e[-1] - sum_e[i+1])
		ans = min(ans, v)
	print(ans)
resolve()

Submission status: Wrong Answer

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

count_e = [0] * n
count_w = [0] * n
for i in range(n):
    if i == 0:
        if s[i] == 'E':
            count_e[i] = 1
        else:
            count_w[i] = 1
        continue
    if s[i] == 'E':
        count_e[i] = count_e[i-1] + 1
        count_w[i] = count_w[i-1]
    else:
        count_w[i] = count_e[i-1]
        count_e[i] = count_e[i-1] + 1

ans = 10 ** 9
for i in range(n):
    if s[i] == 'E':
        e = count_e[-1] - count_e[i]
        w = count_w[i]
    else:
        e = count_e[-1] - count_e[i]
        w = count_w[i] - 1
    ans = min(ans,e+w)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = str(input())
A=[0] * N

for i in range(0, N):
    for j in range(0, N):
        if i != j:
            if j<i:
                if S[j] == "W":
                    A[i]+=1
                
            if j>i:
                if S[j] == "E":
                    A[i]+=1

ans=0
for i in range(0, N-1):
    if A[i] > A[i+1]:
        A[i] = A[i+1]
        ans = i + 1

print(ans+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 3*(10**5)
to_east = 0
to_west = s.count('E', 1)

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

  
print(ans)

Submission status: Wrong Answer

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

ans = 3*(10**5)
to_east = 0
to_west = s.count('E', 1)
for i in range(1, n):
  if s[i]=='W':
    to_east += 1
  if s[n-i]=='E':
    to_west -= 1
  ans = to_east+to_west if ans>to_east+to_west else ans
  
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
ans = N
r = 0
l = S[1:].count('E')
for s in S:
    if ans > r + l:
        ans = r + l
    if s == 'E':
        l -= 1
    else:
        r += 1
print(ans)

Submission status: Wrong Answer

Solution code:
input()
S = input()

ans = ans_cur = S.count("E")
for c in S:
    if c == "E":
        ans_cur -= 1
    else:
        ans = ans if ans <= ans_cur else ans_cur
        ans_cur += 1

print(ans)

Submission status: Wrong Answer

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

ans_cur = S.count("E")
ans = N
for c in S:
    if c == "E":
        ans_cur -= 1
    else:
        ans = ans if ans <= ans_cur else ans_cur
        ans_cur += 1

print(ans)

Submission status: Wrong Answer

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

    all_east = 0
    all_west = 0

    for i in range(n):
        if s[i] == 'W':
            all_west += 1
        else:
            all_east += 1

    count = 10 ** 6

    now_west = 0
    for i in range(n):
        if s[i] == 'W':
            all_east -= 1
        count = min(count, all_east + now_west)
        if s[i] == 'W':
            now_west += 1

    print(count)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

w = [0] * n
e = [0] * n

for i in range(n):
    if i == 0:
        if s[i] == 'W':
            w[i] = 1
            e[i] = 0
        else:
            w[i] = 0
            e[i] = 1
    else:
        if s[i] == 'W':
            w[i] = w[i - 1] + 1
            e[i] = e[i - 1]
        else:
            w[i] = w[i - 1]
            e[i] = e[i - 1] + 1

ans = 10 ** 10
for i in range(n):
    if i == 0:
        ans = min(ans, e[-1] - e[0])
    elif i == n:
        ans = min(ans, w[-1])
    else:
        right = e[-1] - e[i]
        left = w[i - 2]
        ans = min(ans, left + right)

print(ans)

Submission status: Wrong Answer

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

ans = 10 ** 10
for i in range(1, n):
    left = s[:i]
    right = s[i + 1:]
    l = list(left).count('W')
    r = list(right).count('E')
    ans = min(ans, l + r)

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
east = [0]*N
west = [0]*N
for i in range(N):
    s = S[i]
    if i == 0:
        if s == "E":
            east[i] = 1
        else:
            west[i] = 1
    else:

        if s == "E":
            east[i] = east[i-1]+1
            west[i] = west[i-1]
        else:
            east[i] = east[i-1]
            west[i] = west[i-1]+1
Min = 10000
for i in range(N):
    if i == 0:
        num = east[N-1]-east[0]
    elif i == N-1:
        num = west[N-2]
    else:
        num = east[N-1]-east[i]+west[i-1]
    Min = min(Min,num)
print(Min)

Submission status: Wrong Answer

Solution code:
import sys
# input = sys.stdin.readline
from itertools import accumulate

def main():
    N = int(input())
    S = input()
    
    east = accumulate([0] + list(S), lambda total, c: total + (1 if c == "E" else 0))
    next(east)
    east = list(east)

    west = accumulate([0] + list(S), lambda total, c: total + (1 if c == "W" else 0))
    next(west)
    west = list(west)

    east_max = east[-1]
    for i in range(N-1):
        e = east_max - east[i]
        w = 0 if i == 0 else west[i-1]
        yield e + w



print(min(main()))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
s = input()
list_e = [0]*n
list_w = [0]*n
for i in range(n):
  if s[i]=="E":
    list_e[i]=1
for i in range(n):
  if s[i]=="W":
    list_w[i]=1
    
list_e = list(accumulate([0]+list_e))
list_w = list(accumulate([0]+list_w))
m = 10**7
for i in range(n):
  a = list_w[i]
  b = list_e[-1]-list_e[i]
  m = min(m,a+b)
print(m)

Submission status: Wrong Answer

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

cnt = S.count('E')
for i in S:
    if i == 'E':
        cnt += 1
    if i == 'W':
        cnt -= 1
print(cnt)

Submission status: Wrong Answer

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

def f(S):
  count = S.count('E')
  for s in S:
    yield count
    count += 1 if s == 'W' else -1

print(min(list(f(S))))

Submission status: Wrong Answer

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

a = S.count("E")
ans = [a]
for i in range(1,N):
  if S[i]=="E" and S[i-1]=="E":
    a -= 1
  elif S[i]=="E" and S[i-1]=="W":
    a += 0
  elif S[i]=="W" and S[i-1]=="E":
    a += 0
  elif S[i]=="W" and S[i-1]=="W":
    a += 1
  ans.append(a)

print(min(ans))

Submission status: Wrong Answer

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

l = [0]*n
l[n-1] = s[:n-1].count('W')
for i in range(n-1):
  w_side = s[:i].count('W')
  e_side = s[i+1:].count('E')
  l[i] = w_side + e_side
  
print(l,min(l))

Submission status: Wrong Answer

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

l = [0]*n
l[n-1] = s[:n-1].count('E')
for i in range(n-1):
  w_side = s[:i].count('E')
  e_side = s[i+1:].count('W')
  l[i] = w_side + e_side
  
print(min(l))

Submission status: Wrong Answer

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

res = N
num = [0] * (N+1)
for i in range(0, N):
    num[i + 1] = num[i]
    if A[i] == 'W':
        num[i + 1] += 1
for i in range(1, N + 1):
    res = min(res, num[i] + (N - i) - (num[N] - num[i]))

print(res)

Submission status: Wrong Answer

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

res = N
num = [0] * (N+1)
for i in range(0, N):
    num[i + 1] = num[i]
    if A[i] == 'W':
        num[i + 1] += 1
for i in range(0, N):
    res = min(res, num[i] + (N - i) - (num[N] - num[i]))

print(res)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
 
W_csum = [0] * (n+1)
E_csum = [0] * (n+1)
for i in range(n):
    if s[i] == 'W':
        W_csum[i+1] = W_csum[i] + 1
        E_csum[i+1] = E_csum[i]
    else:
        W_csum[i+1] = W_csum[i]
        E_csum[i+1] = E_csum[i] + 1
E_csum.append(E_csum[-1])

ans = float('inf')
for i in range(1,n):
    count = 0
    count += W_csum[i-1]
    count += E_csum[-1] - E_csum[i]
    if count < ans:
        ans = count
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(str,input()))
count = 0
for i in range(n):
  if s[i] == "E":
    count += 1
box = [0]*n
if s[0] == "E":
  box[0] = count - 1
else:
  box[0] = count
for j in range(1,n):
  if s[j] == "E":
    count -= 1
  if s[j - 1] == "W":
    count += 1
  box[j] = count
print(min(box))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    w_ruisekiwa = [0] * (n+1)
    e_ruisekiwa = [0] * (n+1)

    for i in range(n):
        w_ruisekiwa[i+1] = w_ruisekiwa[i] + (1 if s[i] == "W" else 0)
        e_ruisekiwa[i+1] = e_ruisekiwa[i] + (1 if s[i] == "E" else 0)

    mn = 10 * 8

    for i in range(n):
        l_total = w_ruisekiwa[i] - w_ruisekiwa[0]
        r_total = e_ruisekiwa[n] - e_ruisekiwa[i+1]
        mn = min(l_total + r_total,mn)

    print(mn)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    w_ruisekiwa = [0] * (n+1)
    e_ruisekiwa = [0] * (n+1)

    for i in range(n):
        w_ruisekiwa[i+1] = w_ruisekiwa[i] + (1 if s[i] == "W" else 0)
        e_ruisekiwa[i+1] = e_ruisekiwa[i] + (1 if s[i] == "E" else 0)

    mn = 10 * 8

    for i in range(1,n-1):
        l_total = w_ruisekiwa[i] - w_ruisekiwa[0]
        r_total = e_ruisekiwa[n] - e_ruisekiwa[i+1]
        mn = min(l_total + r_total,mn)

    print(mn)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    w_ruisekiwa = [0] * (n+1)
    e_ruisekiwa = [0] * (n+1)

    for i in range(n):
        w_ruisekiwa[i+1] = w_ruisekiwa[i] + (1 if s[i] == "W" else 0)
        e_ruisekiwa[i+1] = e_ruisekiwa[i] + (1 if s[i] == "E" else 0)

    mn = 10 * 8

    for i in range(1,n):
        l_total = w_ruisekiwa[i] - w_ruisekiwa[0]
        r_total = e_ruisekiwa[n] - e_ruisekiwa[i+1]
        mn = min(l_total + r_total,mn)

    print(mn)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#C - Attention
N = int(input())
S = input()
DP_w= []
DP_e = []
tmp = 0
tmpw = 0
tmpe = 0
ans = 0
# for i in range(N):
#     if S[i] == "W":
#         tmpw += 1
#         DP_w.append(tmpw)
#         DP_e.append(tmpe)
#     else:
#         tmpe += 1
#         DP_w.append(tmpw)
#         DP_e.append(tmpe)

for i in range(N):
    tmpw = S[:i].count('W')
    tmpe = S[i:].count('E')
    ans = max(ans,tmpw+tmpe)

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    w_ruisekiwa = [0] * (n+1)
    e_ruisekiwa = [0] * (n+1)

    for i in range(n):
        w_ruisekiwa[i+1] = w_ruisekiwa[i] + (1 if s[i] == "W" else 0)
        e_ruisekiwa[i+1] = e_ruisekiwa[i] + (1 if s[i] == "E" else 0)

    mn = 10 * 6

    for i in range(n):
        l_total = w_ruisekiwa[i] - w_ruisekiwa[0]
        r_total = e_ruisekiwa[n] - e_ruisekiwa[i+1]
        mn = min(l_total + r_total,mn)
    print(mn)






if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
list_S = list(input())
left_W = 0
left_E = 0
right_W = list_S.count("W")
right_E = list_S.count("E")
ans = n

for i, s in enumerate(list_S):
    if s == "W":
        left_W += 1
        right_W -= 1
    elif s == "E":
        left_E += 1
        right_E -= 1

    if i == 0 and s == "W":
        ans = min(ans, left_W + right_E-1)
    elif i == n-1 and s == "E":
        ans = min(ans, left_W + right_E-1)
    else:
        ans = min(ans, left_W+right_E)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e_cnt = 0
for i in range(1, n):
    if s[i] == "E":
        e_cnt += 1
ans = e_cnt
w_cnt = 0
for i in range(n):
    ans = min(w_cnt + e_cnt, ans)
    if s[i] == "W":
        w_cnt += 1
    elif i != 0 and s[i] == "E":
        e_cnt -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e_cnt = 0
for i in range(1, n):
    if s[i] == "E":
        e_cnt += 1
ans = e_cnt
w_cnt = 0
for i in range(n):
    ans = min(w_cnt + e_cnt, ans)
    if s[i] == "W":
        w_cnt += 1
    else:
        e_cnt -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e_cnt = 0
for i in range(n):
    if s[i] == "E":
        e_cnt += 1
ans = e_cnt
w_cnt = 0
for i in range(n):
    ans = min(w_cnt + e_cnt - 1, ans)
    if s[i] == "W":
        w_cnt += 1
    else:
        e_cnt -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
e_cnt = 0
for i in range(n):
    if s[i] == "E":
        e_cnt += 1
ans = e_cnt
w_cnt = 0
for i in range(n):
    ans = min(w_cnt + e_cnt, ans)
    if s[i] == "W":
        w_cnt += 1
    else:
        e_cnt -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [1 if i == "W" else 0 for i in input()]
print(min([sum(a[:i]) + n - i - sum(a[i+1:]) for i in range(n)]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

S = list(input())

ans = [0]*N
ans[0] = S.count("E")

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

print(min(ans))

Submission status: Wrong Answer

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

E = [0] * (n + 10)
W = [0] * (n + 10)

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

ans = float("inf")
for i in range(1,n+1):
    x = 0
    x += W[i-1]
    x += E[n]-E[i+1]
    ans = min(ans, x)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    array = [0,S.count('E')]
    ans = sum(array)

    for i in range(1,len(S)):
        if S[i-1]=='W':
            array[0]+=1
        if S[i]=='E':
            array[1]-=1
        ans = min(ans, sum(array))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def Attention(n , s):

    ans = 10**5
    west = [0] * (n + 1)
    east = [0] * (n + 1)

    for i in range(n):
        if s[i] == 'W':
            west[i + 1] = west[i] + 1
            east[i + 1] = east[i]
        else:
            west[i + 1] = west[i]
            east[i + 1] = east[i] + 1

    for i in range(n):
        count = west[i] + (east[-1] - east[i + 1])
        ans = min(ans , count)

    return ans

def main():
    n = int(input())
    s = list(str(input()))
    print(Attention(n , s))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
t=s.count('E')
ans=10**8
e=0
w=0
for i in range(n):
  if s[i]=='E':
    e+=1
    ans=min(ans,t-e+w-1)
  elif s[i]=='W':
    w+=1
    ans=min(ans,t-e+w-1)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
t=s.count('E')
ans=10**6
e=0
w=0
for i in range(n):
  if s[i]=='E':
    e+=1
    ans=min(ans,t-e+w-1)
  elif s[i]=='W':
    w+=1
    ans=min(ans,t-e+w-1)
print(ans)

Submission status: Wrong Answer

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

e=[0 for i in range(n)]
w=[0 for i in range(n)]

if s[0]=='E':
    e[0]+=1
else:
    w[0]+=1

#累積和
for i in range(1,n):
    if s[i]=='E':
        e[i]=e[i-1]+1
        w[i]=w[i-1]
    else:
        w[i]=w[i-1]+1
        e[i]=e[i-1]

mn = 10**10
for i in range(n):
    dire = s[i]
    if dire == "E":
        if i != 0 and i != n-1:
            ans = w[i-1] + (e[n-1] - e[i])
        elif i == 0:
            ans = w[n-1] - w[i]
        else:
            ans = e[n-1] - 1
    else:
        if i != 0 and i != n-1:
            ans = w[i-1] + (e[n-1] - e[i])
        elif i == 0:
            ans = e[n-1] - e[i]
        else:
            ans = w[n-1] - 1
    if ans < mn:
        mn = ans

print(mn)