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 = input()

# [West, East]
l = [0, 0]
L = []
m = [0, 0]
M = [0] * N
for i in range(N):
  if S[i] == 'W':
    l[0] += 1
    L.append(list(l))
    print(l)
  else:
    l[1] += 1
    L.append(list(l))
    print(l)

for i in range(N-1, -1, -1):
  if S[i] == 'W':
    m[0] += 1
    M[i] = list(m)
  else:
    m[1] += 1
    M[i] = list(m)
    
count = []
for i in range(N):
  if i == 0:
    count.append(M[1][0])
  elif i == N-1:
    count.append(L[N-2][1])
  else:
    count.append(L[i-1][0]+M[i+1][1])

print(min(count))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
l=0
r=S[1:].count("E")
print(r)
answer=N

for i in range(N-1):
    if l+r<answer:
        answer=l+r
    if S[i]=="W":
        l+=1
    if S[i+1]=="E":
        r-=1
if l+r<answer:
    answer=l+r
print(answer)

Submission status: Wrong Answer

Solution code:
N=int(input())
S=input()
l=0
r=S[1:].count("E")
answer=N

for i in range(N-1):
    if l+r<answer:
        answer=l+r
    if S[i]=="W":
        l+=1
    if S[i+1]=="E":
        r-=1
print(answer)

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [int(c == 'W') for c in list(input())]
L = [0]
for i in range(n):
    L.append(L[-1]+S[i])
R = [0]
for i in range(n-1, 0, -1):
    R.append(R[-1]+S[i])
R.reverse()
print(min([L[i]+R[i] for i in range(n)]))

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [int(c == 'W') for c in list(input())]
L = [0]
for i in range(n):
    L.append(L[-1]+S[i])
print(min([n-i+2*L[i]-L[n] for i in range(n)]))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    west = [0 for _ in range(n)]
    west[0] = 1 if s[0] == "W" else 0
    for i in range(1, n):
        if s[i] == "W":
            west[i] = west[i - 1] + 1
        else:
            west[i] = west[i - 1]
    answer = n - west[-1]
    if s[0] == "E":
        answer -= 1
    for i in range(1, n):
        answer = min(answer, west[i - 1] + n - i - west[-1] + west[i - 1])
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    s = input()
    west = [0 for _ in range(n)]
    west[0] = 1 if s[0] == "W" else 0
    for i in range(1, n):
        if s[i] == "W":
            west[i] = west[i - 1] + 1
        else:
            west[i] = west[i - 1]
    answer = n - west[-1]
    for i in range(1, n):
        answer = min(answer, west[i - 1] + n - i - west[-1] + west[i - 1])
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

s = S[::-1]
w = [0]*(N+1)
e = [0]*(N+1)
cnt_w = 0
cnt_e = 0

for i in range(N):
    if(S[i] == "W"):
        cnt_w += 1
        w[i+1] = cnt_w
    else:
        w[i+1] = cnt_w

for i in range(N):
    if(s[i] == "E"):
        cnt_e += 1
        e[i+1] = cnt_e
    else:
        e[i+1] = cnt_e

print(w)
print(e)
e = e[::-1]        
ans = 2*N

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

Submission status: Wrong Answer

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

s = S[::-1]
w = [0]*(N+1)
e = [0]*(N+1)
cnt_w = 0
cnt_e = 0

for i in range(N):
    if(S[i] == "W"):
        cnt_w += 1
        w[i+1] = cnt_w
    else:
        w[i+1] = cnt_w

for i in range(N):
    if(s[i] == "E"):
        cnt_e += 1
        e[i+1] = cnt_e
    else:
        e[i+1] = cnt_e
        
e = e[::-1]        
ans = 2*N

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

Submission status: Wrong Answer

Solution code:

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


def csum(lis):
      counter=[]
      countE=lis.count('E')
      countW=0

      for i,l in enumerate(lis):
          counter.append(countE+countW)
          if(l=='W'):
              countW+=1
          else:
              countE-=1

      return min(counter)

print(csum(S))

Submission status: Wrong Answer

Solution code:

N=int(input())
S=list(input())
ans=10**10
ans2=0
pre=''
for i,s in enumerate(S):
    if(pre=='E' and s == 'W'):
        tmp=S[:i-2].count('W')

        tmp+=S[i+2:].count("E")
        ans=min(ans,tmp)

    elif(pre=='W' and s=='E'):
        ans2=min(S[:i].count('W'),S[i:].count('E'))
    pre=s

if(ans==10**10):
    if(ans2!=0):
        print(ans2)
    else:
        print(0)
else:
    print(ans)

Submission status: Wrong Answer

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

cusum_e = [0] * (N+1)
cusum_w = [0] * (N+1)
for i in range(N):
  cusum_e[i+1] = cusum_e[i]
  cusum_w[i+1] = cusum_w[i]
  if S[i] == 'E':
    cusum_e[i+1] += 1
  else:
    cusum_w[i+1] += 1

ans = 1e9
for i in range(N-1):
  if S[i] == 'E' and S[i+1] == 'W':
    turn_w = cusum_w[i+1]
    turn_e = cusum_e[N] - cusum_e[i+1]
    ans = min(ans, turn_w + turn_e)
    #print(i, turn_w, turn_e)

if ans == 1e9:
  ans = min(cusum_e[N], cusum_w[N])
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
tmp = c['E']
best = tmp - (1 if s[0] == 'E' else 0)
for i in range(1, n):
    if s[i - 1] == 'W':
        tmp += 1
    else:
        tmp -= 1
    if tmp - (1 if s[0] == 'E' else 0) < best:
        best = tmp - (1 if s[0] == 'E' else 0)
print(best)

Submission status: Wrong Answer

Solution code:
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
tmp = c['E'] - (1 if s[0] == 'E' else 0)
best = tmp
for i in range(1, n):
    if s[i - 1] == 'W':
        tmp += 1
    else:
        tmp -= 1
    if tmp < best:
        best = tmp
print(best)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
L = list(input())
LE,LW = [0]*n,[0]*n
for i in range(n):
    if L[i] == "W":
        LW[i] = 1
    else:
        LE[i] = 1
LE = list(accumulate(LE))
LW = list(accumulate(LW))
ans = 3*(10**5)
for i in range(1,n-1):
    Ll = LW[:i]
    Lr = LE[i+1:]
    ans = min(ans,Ll[-1] + Lr[-1] - Lr[0])
    #print(Ll,Lr,Ll[-1] , Lr[-1] - Lr[0])
print(ans)

Submission status: Wrong Answer

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

west_lr=[]
east_rl=[]

w_cnt=0
e_cnt=0
for i in range(N):
    west_lr.append(w_cnt)
    if S[i]=="W":
        w_cnt+=1

for i in range(N-1,-1,-1):
    east_rl.append(e_cnt)
    if S[i]=="E":
        e_cnt+=1

#east_rl.reverse()

mn=10*9

for i in range(N):
    res = west_lr[i]+east_rl[N-i-1]
#    print(res)
    if mn>res:
        mn=res
print(mn)

Submission status: Wrong Answer

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

west_lr=[]
east_rl=[]

w_cnt=0
e_cnt=0
for i in range(N):
    west_lr.append(w_cnt)
    if S[i]=="W":
        w_cnt+=1

for i in range(N-1,-1,-1):
    east_rl.append(e_cnt)
    if S[i]=="E":
        e_cnt+=1
east_rl.reverse()

mn=10*9
for i in range(N):
    res = west_lr[i]+east_rl[i]
    if mn>res:
        mn=res
print(mn)

Submission status: Wrong Answer

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

west_lr=[]
east_rl=[]

w_cnt=0
e_cnt=0
for i in range(N):
    if S[i]=="W":
        w_cnt+=1
    west_lr.append(w_cnt)

for i in range(N-1,-1,-1):
    if S[i]=="E":
        e_cnt+=1
    east_rl.append(e_cnt)
east_rl.reverse()

mn=10*9
for i in range(N):
    res = west_lr[i]+east_rl[i]-1
    if mn>res:
        mn=res
print(mn)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = input()
lm = [0] *(n+1)
rm = [0] *(n+1)

def check_l(x):
    if x=='W':
        r = 1
    else:
        r = 0
    return r
def check_r(x):
    if x=='E':
        r = 1
    else:
        r = 0
    return r
    
for i in range(1,n):
    lm[i+1] = lm[i] + check_l(s[i-1])
    rm[i] = rm[i+1] + check_r(s[i])
ans = float('inf')
for i in range(1,n+1):
    ans = min(ans,lm[i]+rm[i])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
s = input().strip()
d = {"W":0,"E":0}
x=[]
for i, c in enumerate(s):
    x.append(i - d[c])
    d[c]+=1
d = {"W":0,"E":0}
r = N
for i, c in enumerate(s[::-1]):
    r = min(r, x[N-i-1]+d[c])
    d[c]+=1
print(r)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
S = list(map(lambda x: 0 if x == "W" else 1, S))

E = 0
W = sum(S[1:N])

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

    print(p)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
s = str(input())
tt = [1 if i  == "W" else 0 for i in s]
B = [0] + tt
B = list(accumulate(B))
#print(B)
ans = float('Inf')
for i in range(1,n):
    #w = s[:i].count("W")
    ##W = sum(tt[:i])
    w = B[i]
    #print(W,w)
    #e = s[i:].count("E")
    #E = len(tt[i:]) - sum(tt[i:])
    e = n - i - B[-1] + B[i]
    #print(E,e)
    #ans = min(ans,W+E)
    ans = min(ans,w+e)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
n = int(input())
s = str(input())
tt = [1 if i  == "W" else 0 for i in s]
B = [0] + tt
B = list(accumulate(B))
#print(B)
ans = float('Inf')
for i in range(0,n):
    #w = s[:i].count("W")
    ##W = sum(tt[:i])
    w = B[i]
    #print(W,w)
    #e = s[i:].count("E")
    #E = len(tt[i:]) - sum(tt[i:])
    e = n - i - B[-1] + B[i]
    #print(E,e)
    #ans = min(ans,W+E)
    ans = min(ans,w+e)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

A = [0]*(N+1) #N+1まで入れること忘れず

#リーダーの方向を向く，W->1の累積和，[0]start
for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j
#A =list(accumulate(A))
#print(A)

#iより左にいて西を向いている人の最小値
ans=N
for i in range(N+1):
    val = N-i +A[i] -A[N] +1
    ans = min(ans, A[i-1] +val)

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

A = [0]*(N+1) #N+1まで入れること忘れず

#リーダーの方向を向く，W->1の累積和，[0]start
for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j
#A =list(accumulate(A))
#print(A)

#とするとiより左にいて西を向いている人の最小値
ans=N
for i in range(N+1):
    val = N-i +A[i] -A[N]
    ans = min(ans, A[i-1] +val)

if ans==0: #剥く人いないとき1
    ans=1

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

A = [0]*(N+1) #N+1まで入れること忘れず

#リーダーの方向を向く，W->1の累積和
for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j
#A =list(accumulate(A))

#とするとiより左にいて西を向いている人の最小値
ans=N
for i in range(N):
    val = N-i +A[i] -A[N]
    ans = min(ans, A[i-1] +val)

if ans==0: #剥く人いないとき1
    ans=1

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

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

A = [0]*(N+1) #N+1まで入れること忘れず

#リーダーの方向を向く，W->1の累積和
for i in range(1, N+1):
    j = 0
    if S[i-1] == "W":
        j = 1
    A[i]=A[i-1] +j
#A =list(accumulate(A))
print(A)

#とするとiより左にいて西を向いている人の最小値
ans=N
for i in range(N):
    val = N-i +A[i] -A[N]
    ans = min(ans, A[i-1] +val)

if ans==0: #剥く人いないとき1
    ans=1

print(ans)

Submission status: Wrong Answer

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

A = [0]*(N+1) #N+1まで入れること忘れず

#リーダーより東は変えなくていい？W->1のりすと
for i in range(1, N+1):
    k = 0
    if S[i-1] == "W":
        k = 1
    A[i]=k
    #print(A)

#とするとiより左にいて西を向いている人の最小値
ans=0
for i in range(1, N+1):
    ans = min(ans, k+A[i-1])

if ans==0: #剥く人いないとき1
    ans=1
print(ans)

Submission status: Wrong Answer

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

A = [0]*(N+1) #N+1まで入れること忘れず

#リーダーより東は変えなくていい？W->1のりすと
for i in range(1, N+1):
    k = 0
    if S[i-1] == "W":
        k = 1
    A[i]=k
    #print(A)

#とするとiより左にいて西を向いている人の最小値
ans=0
for i in range(1, N+1):
    ans = min(ans, A[i-1])

if ans==0: #剥く人いないとき1
    ans=1
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


def solve(N: int, S: str):
    C = {'E': 0, 'W': 0}
    D = {'E': 0, 'W': 0}
    for c in S:
        C[c] += 1
    a = 1e20
    for c in S:
        a = min(a, C['E'] + D['W'])
        C[c] -= 1
        D[c] += 1
    return a


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))
    S = next(tokens)
    print(solve(N, S))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
S=list(input())
for i in range(len(S)):
    S[i]=-1 if S[i]=="W" else 1
data=[-1 for i in range(N)]
stidx,endidx,maidx=0,N-1,int((N-1)/2)
for i in [stidx,endidx]:
    data[i]=-1*sum([S[m] for m in range(0,i) if S[m]==-1])+sum([S[m] for m in range(i+1,N) if S[m]==1])
while endidx-stidx>2:

    t1=int(stidx+(endidx-stidx)*1/3)
    t2=int(stidx+(endidx-stidx)*2/3)
    for i in [t1,t2]:
        data[i]=-1*sum([S[m] for m in range(0,i) if S[m]==-1])+sum([S[m] for m in range(i+1,N) if S[m]==1])
    if data[t1]>data[t2]:
        stidx=t1
    else:
        endidx=t2

print(min([i for i in data if i>0]))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=list(input())
for i in range(len(S)):
    S[i]=-1 if S[i]=="W" else 1
data=[-1 for i in range(N)]
stidx,endidx,maidx=0,N-1,int((N-1)/2)
for i in [stidx,endidx]:
    data[i]=-1*sum([S[m] for m in range(0,i) if S[m]==-1])+sum([S[m] for m in range(i+1,N) if S[m]==1])
while endidx-stidx>3:

    t1=int(stidx+(endidx-stidx)*1/3)
    t2=int(stidx+(endidx-stidx)*2/3)
    for i in [t1,t2]:
        data[i]=-1*sum([S[m] for m in range(0,i) if S[m]==-1])+sum([S[m] for m in range(i+1,N) if S[m]==1])
    if data[t1]>data[t2]:
        stidx=t1
    else:
        endidx=t2

print(min([i for i in data if i>0]))

Submission status: Wrong Answer

Solution code:
N=int(input())
S=list(input())
for i in range(len(S)):
    S[i]=-1 if S[i]=="W" else 1
data=[-1 for i in range(N)]
stidx,endidx,maidx=0,N-1,int((N-1)/2)
for i in [stidx,endidx]:
    data[i]=-1*sum([S[m] for m in range(0,i) if S[m]==-1])+sum([S[m] for m in range(i+1,N) if S[m]==1])
while endidx-stidx>3:

    t1=int(stidx+(endidx-stidx)*1/3)
    t2=int(stidx+(endidx-stidx)*2/3)
    print(stidx,endidx,t1,t2)
    for i in [t1,t2]:
        data[i]=-1*sum([S[m] for m in range(0,i) if S[m]==-1])+sum([S[m] for m in range(i+1,N) if S[m]==1])
    if data[t1]>data[t2]:
        stidx=t1
    else:
        endidx=t2

print(min([i for i in data if i>0]))

Submission status: Wrong Answer

Solution code:
n = int(input())
s = str(input())
ans = float('Inf')
for i in range(1,n):
    w = s[:i].count("W")
    e = s[i:].count("E")
    ans = min(ans,w+e)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
count_e = [0] * (N + 1)
count_w = [0] * (N + 1)
for i in range(0, N):
    if S[i] == "W":
        count_w[i+1] = count_w[i] + 1
    else:
        count_w[i+1] = count_w[i]

    if S[N-i-1] == "E":
        count_e[i+1] = count_e[i] + 1
    else:
        count_e[i+1] = count_e[i]

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

Submission status: Wrong Answer

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

s = [0]*(N+1)
e = [0]*(N+1)
for i in range(1, N+1):
    k = 0
    if S[i-1] == "W":
        k = 1
    s[i] = s[i-1] + k

for i in reversed(range(0, N)):
    k = 0
    if S[i] == "E":
        k = 1
    e[i] = e[i+1] + k

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


print(ans)

Submission status: Wrong Answer

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

s = [0]*(N+1)

for i in range(1, N+1):
    k = 0
    if S[i-1] == "W":
        k = 1
    s[i] = s[i-1] + k

ans = N
for i in range(1, N):
    k = (N - i) - (s[N] - s[i])
    ans = min(ans, k + s[i-1])


print(ans)

Submission status: Wrong Answer

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

l=[]
c=s.count('E',1)
for i in range(1,n):
    if s[i-1]=='W':
        c+=1
    if s[i]=='E':
        c-=1
    l.append(c)
print(min(l))

Submission status: Wrong Answer

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


ans=10**100
c=s.count('E',1)
for i in range(1,n):
    if s[i-1]=='W':
        c+=1
    if s[i]=='E':
        c-=1
    ans=min(c,ans)
print(min(c,ans))

Submission status: Wrong Answer

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


ans=10**100
a=s.count('W',0,1)
b=s.count('E',0,n)
for i in range(n):
    if a+b-1<=ans:
        ans=a+b-1
    if s[i]=='W':
        a+=1
    if s[i]=='E':
        b-=1
a=s.count('W',0)
b=s.count('E',n-1)
print(min(ans,a+b-1))

Submission status: Wrong Answer

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


ans=10**100
a=s.count('W',0,1)
b=s.count('E',0,n)
for i in range(n):
    if a+b-1<=ans:
        ans=a+b-1
    if s[i]=='W':
        a+=1
    if s[i]=='E':
        b-=1
print(ans)

Submission status: Wrong Answer

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


ans=10**100
a=0
b=s.count('E',1,n)
for i in range(n):
    if a+b<=ans:
        ans=a+b
    if s[i]=='W':
        a+=1
    if s[i]=='E':
        b-=1
print(ans)

Submission status: Wrong Answer

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

a=0
b=0
e=0
w=0
for i in range(n):
    if s[i]=='E':
        a+=1
    else:
        e=max(a,e)
        if e==a:
            ans_1=i
        a=0
e=max(a,e)
if e==a:
    ans_1=n-1

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

#print(e,ans_1)
#print(w,ans_2) 
if e<=w:
    z=s.count('W',0,ans_2+1)
    z=z-w
    z+=s.count('E',ans_2)
else:
    z=s.count('E',ans_1+1,n)
    z+=s.count('W',0,ans_1)
print(z)

Submission status: Wrong Answer

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

a=0
b=0
e=0
w=0
for i in range(n):
    if s[i]=='E':
        a+=1
    else:
        e=max(a,e)
        if e==a:
            ans_1=i
        a=0
e=max(a,e)
if e==a:
    ans_1=n-1

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

#print(e,ans_1)
#print(w,ans_2) 
if e<=w:
    z=s.count('W',0,ans_2+1)
    z=z-w
    z+=s.count('E',ans_2)
else:
    z=s.count('E',ans_1,n)
    z+=s.count('W',0,ans_1)
print(z)

Submission status: Wrong Answer

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

a=0
b=0
e=0
w=0
for i in range(n):
    if s[i]=='E':
        a+=1
    else:
        e=max(a,e)
        if e==a:
            ans_1=i
        a=0
e=max(a,e)
if e==a:
    ans_1=n-1

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

#print(e,ans_1)
#print(w,ans_2) 
if e<=w:
    z=s.count('W',0,ans_2+1)
    z=z-w
    z+=s.count('E',ans_2)
else:
    z=s.count('E',ans_1,n)
    z-=1
    z+=s.count('W',0,ans_1)
print(z)

Submission status: Wrong Answer

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

a=0
b=0
e=0
w=0
for i in range(n):
    if s[i]=='E':
        a+=1
    else:
        e=max(a,e)
        if e==a:
            ans_1=i
        a=0
e=max(a,e)
if e==a:
    ans_1=n-1

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

#print(e,ans_1)
#print(w,ans_2) 
if e<=w:
    z=s.count('W',0,ans_2+1)
    z=z-w
    z+=s.count('E',ans_2)
else:
    z=s.count('E',e,n)
    z+=s.count('W',0,ans_1)
print(z)

Submission status: Wrong Answer

Solution code:
n = int(input())
import numpy as np
s = np.array([{"W":0,"E":1}[i] for i in input()])
cse = np.cumsum(s)
csw = np.cumsum(s==0)
s = s.tolist()
print(min([csw[max(0,i-1)]-csw[0] + cse[-1]-cse[min(n-1,i+1)] for i in range(n)]))

Submission status: Wrong Answer

Solution code:
n = int(input())
import numpy as np
s = np.array([{"W":0,"E":1}[i] for i in input()])
cse = np.cumsum(s)
csw = np.cumsum(s==0)
s = s.tolist()
print(min([csw[i]+cse[-1]-cse[i] for i in range(n)]))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#ARC098C
import sys
import math

n=int(input())
#tmp = input().split()
#hoge = list(map(lambda a: int(a), tmp))
s=input()

sumw=[]
sumw.append(0)
for i in range(1,n+1):
	if(s[i-1]=="W"):
		tmp = sumw[i-1]+1
	else:
		tmp = sumw[i-1]
	sumw.append(tmp)
sume=[]
sume.append(0)
for i in range(1,n+1):
	if(s[n-i]=="E"):
		tmp = sume[i-1]+1
	else:
		tmp = sume[i-1]
	sume.append(tmp)

mini=sume[n-1]
for i in range(2,n+1):
	tmp=sumw[i-1]+sume[n-i+1]
	mini=min(mini,tmp)

#print(sumw)
#print(sume)
#print(n)
#print(s)
print(mini)

Submission status: Wrong Answer

Solution code:
import os,re,sys,operator
from collections import Counter,deque
from operator import itemgetter
from itertools import accumulate,combinations,groupby
from sys import stdin,setrecursionlimit
from copy import deepcopy

setrecursionlimit(10**6)

n=int(stdin.readline().rstrip())
s=stdin.readline().rstrip()
cntw=[0]*(n+2);cnte=[0]*(n+2)
ans=10**6

for i in range(1,n+1):
    if s[i-1]=="W":
        cntw[i]=cntw[i-1]+1
        cnte[i]=cnte[i-1]
    elif s[i-1]=="E":
        cnte[i]=cnte[i-1]+1
        cntw[i]=cntw[i-1]
sumw,sume=cntw[n],cnte[n]
for i in range(n):
    now=cntw[i]+(sume-cnte[i])
    ans=min(ans,now)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

rcost = 0
lcost = S.count('E')
ans = lcost

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

Submission status: Wrong Answer

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

rcost = 0
lcost = S.count('E')
ans = lcost

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

Submission status: Wrong Answer

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

rcost = 0
lcost = S.count('E')
ans = lcost

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

Submission status: Wrong Answer

Solution code:
n = int(input())
S = [s for s in input()]
e,w = [0],[0]
for i in range(n):
    if S[i] is 'W':
        e.append(e[i]+1)
    else:
        e.append(e[i])
    if S[n-i-1] is 'E':
        w.append(w[i]+1)
    else:
        w.append(w[i])
w = w[::-1]
ans = float('inf')
for i in range(n):
    ans = min(ans,e[i]+w[i])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
e,w=s.count('E'),s.count('W')
le,lw=0,0
ans=10**6
for c in s:
	if c=='E':
		e-=1
		ans=min(ans,e+lw)		
	else:
		lw+=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

dic = {'E':1,'W':-1}

S = [dic.get(item) for item in list(map(str,input()))]

C = [sum([i for i in S[1:] if i == 1])]

for i in range(1,N):
  C.append(sum([a for a in S[i:] if a == 1])-sum([b for b in S[:i] if b == -1]))

print(C)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
left = 0
right = S[1:].count('E')
MIN = left + right
for i in range(0,N):
  if S[i] == 'W':
    MIN = min(MIN,left+right)
    left += 1
  else:
    MIN = min(MIN,left+right)
    right -= 1
  
  if i==N-1:
    MIN = min(MIN,S[:i].count('W'))

print(MIN)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
MIN = float('inf')
left = S[0].count('W')
right = S[1:].count('E')
for i in range(1,N):
  if S[i] == 'W':
    left += 1
  else:
    right -= 1
  
  if i==N-1:
    MIN = min(MIN,S[:i].count('W'))
  else:
    MIN = min(left+right,MIN)
print(MIN)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
MIN = float('inf')
left = 0
right = S.count('E')
for i in range(N):
  if S[i] == 'W':
    left += 1
  else:
    right -= 1
  if i==0 or i==N-1:
    MIN = min(max(left,right),MIN)
  else:
    MIN = min(left+right,MIN)
print(MIN)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
MIN = float('inf')
left = 0
right = S.count('E')
for i in range(N):
  if S[i] == 'W':
    left += 1
  else:
    right -= 1
  MIN = min(left+right,MIN)
print(MIN)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
dpe = [0] * (N+1)
dper = [0] * (N+1)

for i in range(N):
    dpe[i+1] = dpe[i]
    dper[N-1-i] = dper[N-i]
    if S[i] == "W":
        dpe[i+1] += 1
    if S[N-1-i] == "E":
        dper[N-1-i] += 1
ans = 10**6
for i in range(N):
    tmp = dpe[i] + dper[i]
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
dpe = [0] * (N+1)
dper = [0] * (N+1)

for i in range(N):
    dpe[i] = dpe[i-1]
    dper[N-1-i] = dper[N-i]
    if S[i] == "W":
        dpe[i] += 1
    if S[N-1-i] == "E":
        dper[N-1-i] += 1
ans = 10**6
for i in range(N):
    tmp = dpe[i-1] + dper[i]
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

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

east_count = [0]*(N+1)
west_count = [0]*(N+1)

for i in range(N):
    east_count[i+1] += east_count[i]
    west_count[i+1] += west_count[i]
    if S[i] == 'E':
        east_count[i+1] += 1
    else:
        west_count[i+1] += 1

ans = 2**64
for i in range(N):
    s = S[i]
    count = west_count[i-1]
    count += east_count[N] - east_count[i+1]
    ans = min(ans, count)

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
hoge=input()
mig=[0]
hid=[0]
for i in range(n-1):
  if(hoge[i]=="W"):
    hid.append(1+hid[-1])
  else:
    hid.append(hid[-1])

for i in range(n-1):
  if(hoge[-1-i]=="W"):
    mig.append(1+mig[-1])
  else:
    mig.append(mig[-1])
    
mig=mig[::-1]
ans=[]
for i in range(n):
  ans.append(max(0,mig[i]+hid[i]-1))
  
print(min(ans))

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
w = [0 for i in range(N)]
e = [0 for i in range(N)]

if S[0] == 'W':
    w[0] = 1

for i in range(1,N-1):
    if S[i] == 'W':
        w[i] = w[i-1] + 1
        e[i+1] = e[i]  
    else:
        w[i] = w[i-1]
        e[i+1] = e[i] - 1

if S[-1] == 'W':
    w[-1] = w[-2] + 1
else:
    w[-1] = w[-2] 

ans = w[0] + e[0]
scale = -e[-1]
for i in range(N):
    w[i] = w[i] + e[i]
    if ans > w[i]:
        ans = w[i]

print(ans + scale)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
s = list(stdin.readline().rstrip())
wl = [0]
el = [0]
ans=[]
for i in range(n):
    wl.append(wl[i]+1 if s[i] == 'W' else wl[i])
for i in range(n):
    el.append(el[i]+1 if s[i] == 'E' else el[i])
for i in range(n):
    ans.append(wl[i]+el[-1]-el[i])
print(min(ans))

Submission status: Wrong Answer

Solution code:
""" AtCoder """

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

e = S.count('E')
w = 0
ans = e

for s in S:
    ans = min(ans, w + e)
    if s == 'W':
        w += 1
    else:
        e -= 1

print(ans)

Submission status: Wrong Answer

Solution code:
from numpy import array
n=int(input())
s=input()
t=s.count("E")
r=int(s[0]=="W")
stack=0
dp =[0 for i in range(n)]
dp2=[t]+[0 for i in range(n-1)]
for i in range(1,n):
    if s[i]=="W":
        if s[i-1]=="W":
            stack=1
        dp[i]=dp[i-1]+stack
        r+=1
        dp2[i]=dp2[i-1]
    else:
        stack=0
        dp[i] =r
        dp2[i]=dp2[i-1]-1
print(min(array(dp)+array(dp2)))

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
mi=float('inf')
for i in range(n):
    if s[i]=='W':
        mi=min(mi,s[:i].count('W')+s[i:].count('E'))
    else:
        mi = min(mi, s[:i].count('E') + s[i:].count('W'))
print(mi)

Submission status: Wrong Answer

Solution code:
N=int(input())
s=input()
e=0
w=0
for i in s:
    if(i=='W'):
        w+=1
    elif(i=='E'):
        e+=1
ans=[0,0]
dp=[-1 for _ in range(N)]

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


print(min(dp))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
count = 0

for i in s:
    print(i)
    if i == 'W':
        count += 1
    elif count:
        count -= 1
        ans += 1
    else:
        pass


print(ans)

Submission status: Wrong Answer

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

ans = 0
count = 0

for i in s:
    if i == 'w':
        count += 1
    elif count:
        count -= 1
        ans += 1
    else:
        pass


print(ans)

Submission status: Wrong Answer

Solution code:
a = int(input())
ar = list(input())
count = ar.count("E")
br = []
for i in range(a):
    br.append(count)
    if ar[i] == "E":
        count -= 1
    else:
        count += 1
print(min(br))