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

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

west = [0] * (n + 2)
east = [0] * (n + 2)

for i,v in enumerate(s):
    west[i + 1] = west[i]
    if v == 'W':
        west[i + 1] += 1

for i,v in enumerate(reversed(s)):
    east[n - i] = east[n + 1 - i]
    if v == 'E':
        east[n - i] += 1

min_count = None
for i in range(n):
    if min_count is None:
        min_count = west[i] + east[i + 2]
    else:
        min_count = min(min_count, west[i] + east[i + 2])

print(min_count)

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

turns = [s[1:].count('E')]

for i in range(1, n):
  if s[i] != s[i-1]:
    turns.append(turns[i-1])
  elif s[i] == 'E':
    turns.append(turns[i-1] - 1)
  else:
    turns.append(turns[i-1] + 1)
  
print(min(turns))

Solution (Python):
n = int(input())
s = input()
sum_w = s.count("W")
ans = n
lw = [0]*n
count = 0
for i in range(n):
    lw[i] = count
    if s[i] == "W":
        count += 1
for i in range(n):
    left_w = lw[i]
    right_w = sum_w-left_w
    if s[i] == "W":
        right_w -= 1
    right_e = (n-i-1)-right_w
    sum = left_w+right_e
    if sum < ans:
        ans = sum
print(ans)

Solution (Python):
n = int(input())
s = input()
num = s[1:].count('E')
ans = [num]

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

Solution (Python):
N = int(input())
S = input()
num = S[1:].count('E')
ans = [num]
for i in range(1, N):
    if S[i] == 'E':
        num -= 1
    if S[i-1] == 'W':
        num += 1
    ans.append(num)
print(min(ans))

Solution (Python):
def main():
    N = int(input())
    S = input()
    max_count = 0
    totE, totW = 0, 0
    for c in S:
        if c == "E": totE += 1
        else: totW += 1

    countE, countW = 0, 0
    min_flip = float('inf')
    for i in range(N):
        f = countW  + totE-(S[i]=="E")- countE
        min_flip = min(min_flip, f)
        if S[i] == "E": countE += 1
        else: countW += 1

    return min_flip

if __name__ == '__main__':
    print(main())

Solution (Python):
n=int(input())
s=input()
ce=s.count('E')
cw=0
ans=n
for i in range(n):
    if s[i]=='E':
        ce-=1
        ans=min(ans,ce+cw)
    else:
        ans=min(ans,ce+cw)
        cw+=1
print(ans)

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

t_w = [0]*N
t_e = [0]*N

ans = 3*10**6
west = 0
east = 0

for i in range(N):
    if i == 0:
        if S[i] == "W":
            t_w[0] = 1 
            t_e[0] = 0
        elif S[i] == "E":
            t_w[0] = 0 
            t_e[0] = 1 
    else:
        if S[i] == "W":
            t_w[i] = t_w[i-1] + 1
            t_e[i] = t_e[i-1]
        elif S[i] == "E":
            t_w[i] = t_w[i-1]
            t_e[i] = t_e[i-1] + 1

for i in range(N):
    west = 0 if i == 0 else t_w[i-1]
    east = t_e[N-1]-t_e[i]
    t = west + east
    ans = min(ans,t)
print(ans)

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

ans = N

d = [0] * N

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

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

print(min(d))

Solution (Python):

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n=getint()
s=input()

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

east_count = [0]*n
east_count[-1] = 1 if s[-1] == 'E' else 0
for i in range(n-2,-1,-1):
    east_count[i] = east_count[i + 1] + (1 if s[i] == 'E' else 0)

res = n
for i in range(n):
    tmp = west_count[i - 1] if i > 0 else 0
    tmp += east_count[i + 1] if i < n - 1 else 0
    res = min(res, tmp)

print(res)

Solution (Python):
def resolve():
    n = int(input())
    s = input()
    left = [0] * n
    right = [s[1:].count("E")] + [0] * (n - 1)
    ans = [s[1:].count("E")] + [0] * (n - 1)
    for i in range(1, n):
        left[i] = left[i - 1] + (1 if s[i - 1] == "W" else 0)
        right[i] = right[i - 1] + (-1 if s[i] == "E" else 0)
        ans[i] = left[i] + right[i]
    print(min(ans))


if __name__ == "__main__":
    resolve()

Solution (Python):
import sys
import math

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

wl = [0] * (N + 2)
er = [0] * (N + 2)

for i in range(N):
    wl[i + 1] = int(S[i] == 'W') + wl[i]
    er[N - i] = int(S[N - 1 - i] == 'E') + er[N - i + 1]

ans = N

S = ' ' + S
for i in range(1, N + 1):
    ans = min(wl[i - 1] + er[i + 1], ans)
print(ans)

Solution (Python):
N = int(input())
S = str(input())
E = [0]*N
W = [0]*N
min = 10**9
for i in range(N):
    if S[i]=="E":
        E[i] += 1+E[i-1]
        W[i] = W[i-1]
    elif S[i]=="W":
        W[i] += 1+W[i-1]
        E[i] = E[i-1]
if S[0]=="W":
    W.insert(0,0)
    W.pop(-1)
for j in range(N):
    tmp = E[-1]-E[j]+W[j]
    if min>tmp:
        min = tmp
print(min)

Solution (Python):

N = int(input())
A = list(input())

L = [0] * N
R = [0] * N
L[0] = 0
R[N-1] = 0
for i in range(1, N):
    count = 1 if A[i-1] == 'W' else 0
    L[i] = L[i-1] + count

    count = 1 if A[N-i] == 'E' else 0
    R[N-i-1] = R[N-i] + count

print(min(list(map(lambda a, b: (a+b), L, R))))

Solution (Python):
n=int(input())
s=input()
e=s[1:].count("E")
x=e
ans=[]
for i in range(1,n):
	ans.append(x)
	if s[i-1]=="W":
		x+=1
	if s[i]=="E":
		x-=1
ans.append(x)
print(min(ans))

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

c_e=0

for i in range(1,n):
	if s[i]=='E':
		c_e+=1

cnt=[]
c_w=0
cnt.append(c_e)

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

print(min(cnt))

Solution (Python):
N=int(input())
S=input()
t=0
for i in range(1,N):
    if S[i]=="E":
        t+=1
a=t
for i in range(1,N):
    if S[i-1]=="W":
        t+=1
    if S[i]=="E":
        t-=1
        a=min(a,t)
print(a)

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

score = [0] * N

counter = 0
for i in range(N):
    score[i] += counter
    if S[i] == 'W':
        counter += 1


counter = 0
for i in range(N):
    i = N - i - 1
    score[i] += counter
    if S[i] == 'E':
        counter += 1

print(min(score))

Solution (Python):
N = int(input())
s = list(input())
e_count =s.count("E")
w_count =s.count("W")
temp = s[1:].count("E")
ans = 10**6
if temp <= ans:
    ans = temp
for i in range(N-1):
    if s[i] == "E" and s[i+1] == "E":
        temp -= 1
    elif s[i] == "W" and s[i+1] == "E":
        pass
    elif s[i] == "E" and s[i+1] == "W":
        pass
    else:
        temp += 1

    if temp <= ans:
        ans = temp
print(ans)

Solution (Python):
N = int(input())
A = input()
B = []
count = 0
for i in range(1, len(A)):
  if A[i] != 'W':
    count += 1
B.append(count)
for i in range(1, len(A)):
  count = B[i - 1]
  if A[i - 1] != 'E':
    count += 1
  if A[i] != 'W':
    count -= 1
  B.append(count)
print(min(B))

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

ws=sum([1 for i in s if i=="E"])
s="E"+s
count=[0]*n

for i in range(n):
    now=s[i+1]
    before=s[i]
    
    if now=="E":
        ws-=1
    if before=="W":
        ws+=1
    count[i]=ws
print(min(count))

Solution (Python):
n = int(input())
s = input()
en = s.count("E")
es = n - en
cnt = ans = en - (s[0] == "E") * 1
for i in range(1, n):
    if s[i] == "E": cnt -= 1
    if s[i - 1] == "W": cnt += 1
    if cnt < ans:
        ans = cnt
print(ans)

Solution (Python):
n = int(input())
s = list(input())
count_w = 0
count_e = 0
count_list = [[0] * 2 for i in range(n)]
min_num = 100000000

for i in range(n-1):
    if s[i] == 'W':
        count_w += 1
    count_list[i+1][0] = count_w

for i in range(n-1, 0, -1):
    if s[i] == 'E':
        count_e += 1
    count_list[i-1][1] = count_e

for i in range(n):
    num = count_list[i][0] + count_list[i][1]
    if num <= min_num:
        min_num = num

print(min_num)

Solution (Python):
n = int(input())
s1 = list(str(input()))

s2 = list(reversed(s1))

s1s = [0]*(n+1)
s2s = [0]*(n+1)

for i in range(n):
  if s1[i] == 'W':
    s1s[i+1] = s1s[i]+1
  else:
    s1s[i+1] = s1s[i]
  
  if s2[i] == 'E':
    s2s[i+1] = s2s[i]+1
  else:
    s2s[i+1] = s2s[i]

min_s = 3*10**5+1
for j in range(n):
  min_s = min(min_s, s1s[j]+s2s[n-1-j])
print(min_s)

Solution (Python):
N = int(input())
S = input().rstrip()

L = [0] * (N + 1)  
R = [0] * (N + 1)  

cnt = 0
for i in range(N):
    if S[i] == 'W':
        cnt += 1
    L[i] = cnt

cnt = 0
for i in reversed(range(N)):
    if S[i] == 'E':
        cnt += 1
    R[i] = cnt

ans = 300000
for i in range(N):
    if i == 0:
        ans = R[i + 1]
    else:
        ans = min(ans, L[i-1] + R[i+1])

print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    W = [0] * (N + 1)
    E = [0] * (N + 1)

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

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

main()

Solution (Python):
n = int(input())
s = input()
left_attention = [0 for i in range(n)]
right_attention = [0 for i in range(n)]

for i in range(1,n):
    left_attention[i]=left_attention[i-1]
    if(s[i-1]=='W'):
        left_attention[i]+=1


for i in range(n-2,-1,-1):
    right_attention[i]=right_attention[i+1]
    if(s[i+1]=='E'):
        right_attention[i]+=1


ans=n
for i in range(n):
    ans = min(ans, left_attention[i]+right_attention[i])


print(ans)

Solution (Python):
import copy

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

lst = []
lst.append({"W": 0, "E": 0})
for i in range(1,len(S)+1):
  res = {"W": 0, "E": 0}
  if S[i-1] == "W":
    res["W"] += lst[i-1]["W"] + 1
    res["E"] += lst[i-1]["E"]
  else:
    res["W"] += lst[i-1]["W"]
    res["E"] += lst[i-1]["E"] + 1
  lst.append(res)
ans = N
for i in range(1,len(lst)):
  n = 0
  if (i != len(lst)-1):
    n = lst[i-1]["W"] + (lst[-1]["E"] - lst[i]["E"])
  else:
    n = lst[i-1]["W"]
  if (n < ans):
    ans = n
print(ans)

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

cntE = [0] * N
cntW = [0] * N

if S[0] == 'E' :
    cntE[0] = 1
if S[N-1] == 'W' :
    cntW[N-1] = 1

for i in range(1, N) :
    cntE[i] = cntE[i-1]
    if S[i] == 'E' :
        cntE[i] += 1

for i in range(N-1)[::-1] :
    cntW[i] = cntW[i+1]
    if S[i] == 'W' :
        cntW[i] += 1



cntSum = list(map(sum, zip(cntE, cntW)))

print(N - max(cntSum))

Solution (Python):
N = int(input())
ss = list(input())

def counter(ss,direction):
    l = []
    cnt = 0
    l.append(cnt)
    for s in ss[:-1]:
        if s == direction:
            cnt += 1
        l.append(cnt)
        
    return l

wtoe_cnt = counter(ss,"W")
etow_cnt = counter(ss[::-1],"E")[::-1]

cnt = 10**9
for wtoe,etow in zip(wtoe_cnt,etow_cnt):
    tmp_cnt = wtoe+etow
    if tmp_cnt < cnt:
        cnt = tmp_cnt
        
print(cnt)

Solution (Python):
n = int(input())
s = input()
w = [0] * n
e = [0] * n
for i in range(n):
    if i == 0:
        w[i] = 1 if s[i] == "W" else 0
        e[-i-1] = 1 if s[-i-1] == "E" else 0
    else:
        if s[i] == "W":
            w[i] = w[i-1] + 1
        else:
            w[i] = w[i-1]
        if s[-i-1] == "E":
            e[-i-1] = e[-i] + 1
        else:
            e[-i-1] = e[-i]
mp = 10 ** 9
for i in range(n):
    mp = min(mp, w[i] + e[i] - 1)
print(mp)

Solution (Python):
N = int(input())

S = input()

re = [0] * N 
le = [0] * N 
INF = float("inf")
for i in range(1,N):
    
    if S[i-1] == "E":
        le[i] = le[i-1] + 1
    else:
        le[i] = le[i-1]

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

ans = [INF] * N

for i in range(N):
    ans[i] = (i - le[i]) + (re[i])

print(min(ans))

Solution (Python):
n, s = int(input()), input()
w = [0] * n
e = [0] * n

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

tmp = []
for i in range(n):
  tmp.append(w[i]+e[i])
print(min(tmp))

Solution (Python):
N=int(input())
S=input()
A=[0]*N
B=[0]*N
count=0
for i in range(N):
    if S[i]=='W':
        count+=1
    A[i]=count
cnt=0

for i in range(N)[::-1]:
    if S[i]=='E':
        cnt+=1
    B[i]=cnt
mini=10000000
if mini>B[1]:
    mini=B[1]
if mini>A[N-2]:
    mini=A[N-2]

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

print(mini)

Solution (Python):
N = int(input())
S = input()
left_attention = [0 for i in range(N)]
right_attention = [0 for i in range(N)]

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

for i in range(N - 2, -1, -1):
    right_attention[i] = right_attention[i + 1]
    if S[i + 1] == "E":
        right_attention[i] += 1
        
ans = N
for i in range(N):
    ans = min(ans, left_attention[i] + right_attention[i])

print(ans)

Solution (Python):
n = int(input())
S = input()
ans = n
num_list = []
W_count = 0

for i in range(n):
    if S[i] == 'W':
        W_count += 1
    num_list.append(W_count)

for i in range(1, n-1):
    left_w = num_list[i-1]
    right_e = n - num_list[n-1] - (1+i - num_list[i])
    pre_ans = left_w + right_e
    if ans > pre_ans:
        ans = pre_ans

ans_0 = n-num_list[n-1] - (1-num_list[0])
ans_n = num_list[n-2]
print(min(ans, ans_0, ans_n))

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

sumW = [0 for _ in range(N + 1)]
sumE = [0 for _ in range(N + 1)]

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

ans = float('inf')
for i, s in enumerate(S) :
    count = sumW[i] + (sumE[N] - sumE[i + 1])
    ans = min(ans, count)

print(ans)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
S = (stdin.readline().rstrip())
w = [0] * N
e = [0] * N

W_c = 0
E_c = 0
for ind,i in enumerate(S):
    if i == "E":
        E_c += 1
    
    e[ind] = E_c

for ind,i in enumerate(reversed(S)):
    if i == "W":
        W_c += 1
   
    w[ind] = W_c

w = list(reversed(w))
ans = [0] * N
w_end = w[0]
e_end = e[-1]

for j in range(N):
    ans[j] = (e_end - e[j]) + (w_end - w[j])
print(min(ans))

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

W = [0 for k in range(N)]
E = [0 for k in range(N)]

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

for k in range(1,N):
    if S[N-k] == "E":
        E[N-k-1] = E[N-k] + 1
    else:
        E[N-k-1] = E[N-k]

ans = 10**9
for k in range(N):
    ans = min(W[k]+E[k],ans)

print(ans)

Solution (Python):
n = int(input())
s = [x for x in input()]
start = len([1 for i in s[1:] if i=='E'])
dp = [0]*n
ans =start
for i,j in enumerate(s):
	if i ==0:
		now=start
		continue
	if j =='E':
		now-=1
	if s[i-1]=='W':
		now+=1
	ans=min(ans,now)
print(ans)

Solution (Python):
N = int(input())
S = list(input())
mi =[0]*N

LE = 0
LW = 0
RE = S[1:N].count("E")
RW = N-1-RE
mi[0] = RE

if S[0] == "E":
    LE +=1
else:
    LW +=1

for i in range(1,N-1):
    if S[i]=="E":
        RE -=1
        mi[i]= LW + RE 
        LE +=1
    elif S[i]=="W":
        RW -=1
        mi[i]=LW +RE
        LW +=1

mi[N-1] = S[0:N-1].count("W")
print(min(mi))

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

LE = 0
LW = 0
RE = S[1:N].count("E")
RW = N-1-RE
ans = RE

if S[0] == "E":
    LE +=1
else:
    LW +=1

for i in range(1,N-1):
    if S[i]=="E":
        RE -=1
        if LW + RE < ans:
            ans = LW +RE
        LE +=1
    elif S[i]=="W":
        RW -=1
        if LW +RE < ans:
            ans = LW+RE
        LW +=1

if S[0:N-1].count("W")<ans:
    ans = S[0:N-1].count("W")
print(ans)

Solution (Python):
n=int(input());s=input()
w=[0]*(n+1);e=[0]*(n+1)

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

for i in range(n,0,-1):
  e[i-1]=e[i]
  if (s[i-1]=='E'):
    e[i-1]+=1
    
a=2*n
for i in range(n+1):
  a=min(a,w[i]+e[i])
print(a)

Solution (Python):
n = int(input())
s = input()
can = []
for lead in range(n):
  now = 0
  try:
    if s[lead-1] == 'W' and s[lead] == 'W':
      now = can[-1] + 1
    elif s[lead-1] == 'W' and s[lead] == 'E':
      now = can[-1]
    elif s[lead-1] == 'E' and s[lead] == 'W':
      now = can[-1]
    else:
      now = can[-1] - 1
  except:
    
    for i in range(1,n):
      if s[i] == 'E':
        now += 1
  can.append(now)
print(min(can))

Solution (Python):
n=int(input())
s=input()
e=s.count("E")
w=0
ans=e+w
for si in s:
    if si=="E":
        e-=1
    else:
        w+=1
    ans=min(ans,w+e)
print(ans)

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

cnt = [0] * N
if S[0] == "W":
    cnt[0] = 1

for i in range(1, N):
    if S[i] == "W":
        cnt[i] = cnt[i-1] + 1
    else:
        cnt[i] = cnt[i-1]

ans = 3*10**5
for i in range(N):
    temp = 0
    
    if i != 0:
        temp += cnt[i-1]
    
    if i != N-1:
        temp = temp + ((N-1) - i) - (cnt[N-1] - cnt[i])
    if ans > temp:
        ans = temp
print(ans)

Solution (Python):
"""
WEEWW
<>><<
01100
10100
01000
10000
10010

下記の和
1. リーダーより左にいるWの数
2. リーダーより右にいるEの数
"""

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

cntW = 0
W_num = [0]*N
for i,s in enumerate(S):
    if s == "W":
        cntW += 1
    W_num[i] = cntW
        

cntE = 0
E_num = [0]*N
for i,s in enumerate(S[::-1]):
    if s == "E":
        cntE +=1 
    E_num[i] = cntE
E_num = E_num[::-1]

ans = 10**10
for e,w in zip(E_num, W_num):
    cnt = e + w - 1
    ans = min(cnt, ans)
print(ans)

Solution (Python):
N = int(input())
S = input()
ans = N+1
W_cnt = S.count('W')
left_W_cnt = 0
right_W_cnt = W_cnt
E_cnt = S.count('E')
left_E_cnt = 0
right_E_cnt = E_cnt

for i in range(0, N):
    if S[i] == 'W':
        left_W_cnt += 1
        ans = min(ans,(left_W_cnt-1 + right_E_cnt))
    else:
        right_E_cnt -= 1
        ans = min(ans,(left_W_cnt + right_E_cnt))
    
print(ans)

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


num = 0
w2e = [0] * len(s)
prev = s[0]
for ix in range(1, len(s)):
    if prev == 'W':
        num += 1
        w2e[ix] = num
    else:
        w2e[ix] = num
    
    prev = s[ix]




num = 0
e2w = [0] * len(s)
prev = s[len(s)-1]

for ix in range(len(s)-2, -1, -1):
    if prev == 'E':
        num += 1
        e2w[ix] = num
    else:
        e2w[ix] = num

    prev = s[ix]





minv = 2**32
for ix in range(0, n):
    cand = w2e[ix] + e2w[ix]
    if minv > cand:
        minv = cand

print(minv)

Solution (Python):

import sys, math, fractions, itertools


def solve(N: int, S: str):
    E = []
    W = []
    e, w = 0, 0
    for i in range(N):
        if S[i] == 'W':
            w += 1
        W.append(w)
        if S[N-1-i] == 'E':
            e += 1
        E.append(e)
    mn = 1e6
    for i in range(N):
        mn = min(mn, E[i] + W[N-1-i] - 1)
    print(mn)
    return



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)  
    solve(N, S)

if __name__ == '__main__':
    main()

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

w_num = [0] * (n+1)
e_num = [0] * (n+1)
e = s.count('E')

for i in range(n):
    if s[i] == 'W':
        w_num[i+1] = w_num[i] + 1
    else:
        w_num[i+1] = w_num[i]
        
    if s[i] == 'E':
        e_num[i+1] = e_num[i] + 1
    else:
        e_num[i+1] = e_num[i]


ans = n

for i in range(n):
    cnt = w_num[i] + (e - e_num[i+1])
    if cnt < ans:
        ans = cnt

print(ans)

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

sumsW = [0]
sumsE = [0]

cnt = 0
for i in range(N):
    if S[i] == 'W':
        cnt += 1
    sumsW.append(cnt)

cnt = 0
for j in range(N-1, -1, -1):
    if S[j] == 'E':
        cnt += 1
    sumsE.append(cnt)
sumsE.reverse()

ans = 3 * 10**5
for i in range(N+1):
    ans = min(sumsW[i] + sumsE[i], ans)
    
print(ans)

Solution (Python):
_=input()
q=input()
ans=min(q[1:].count("E"),q[:-1].count("W"))
left=0
right = q.count("E")
for i in q:
	if i=="E":
		right-=1
	else:
		left+=1
	ans=min(ans,right+(left!=0)*left)
print(ans)

Solution (Python):
import sys

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

count = sys.maxsize
ans = S[1:].count("E")

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

count = min(ans, count)
print(count)

Solution (Python):
N = int(input())
S = input()
left = [0]
for i in range(N):
    if S[i] == "W":
        left.append(left[-1]+1)
    else:
        left.append(left[-1])
right = [0]
for i in range(N):
    if S[-i-1] == "E":
        right.append(right[-1]+1)
    else:
        right.append(right[-1])
ans = N-1
for i in range(N):
    ans = min(ans,left[i]+right[N-i-1])
print(ans)

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

Solution (Python):
n=int(input())
s=str(input())
a=s.count('W')
b=s.count('E')
count=0
if s[0]=='W':
    count=b
else:
    count=b-1
ans=[count]
for i in range(1,n):
    if s[i-1]=='W':
        count+=1
    if s[i]=='E':
        count-=1
    ans.append(count)
print(min(ans))

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


ans = N

f = S[0]
s = S[1:]

nw = 0
ne = 0

for w in s:
    if w != 'W':
        ne += 1

ans = nw+ne
for i in range(1, N):
    
    
    
    
    
    
    
    

    if S[i-1] != 'E':
        nw += 1

    if S[i] != 'W':
        ne -= 1
    
    ans = min(ans, nw+ne)

print(ans)

Solution (Python):
def solve():
    s = read()
    result = think(s)
    write(result)


def read():
    n = read_int(1)[0]
    return read_line(n=n)


def read_int(n):
    return list(map(lambda x: int(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(s):
    accumulated_count_facing_east, accumulated_count_facing_west = preprocess(s)
    member_count_needed_to_change_direction = len(s)  

    n = len(s)

    for i in range(len(s)):
        if i == 0:
            change_to_west = accumulated_count_facing_east[n - 1] - accumulated_count_facing_east[0]
            member_count_needed_to_change_direction = min(change_to_west, member_count_needed_to_change_direction)
        elif i == len(s) - 1:
            change_to_east = accumulated_count_facing_west[n - 2]
            member_count_needed_to_change_direction = min(change_to_east, member_count_needed_to_change_direction)
        else:
            change_to_east = accumulated_count_facing_west[i - 1]
            change_to_west = accumulated_count_facing_east[n - 1] - accumulated_count_facing_east[i]
            member_count_needed_to_change_direction = min(change_to_east + change_to_west, member_count_needed_to_change_direction)
    return member_count_needed_to_change_direction


def preprocess(s):
    east_symbol = 'E'
    west_symbol = 'W'
    accumulated_count_facing_east = [0 for x in range(len(s))]
    accumulated_count_facing_west = [0 for x in range(len(s))]
    for i, elem in enumerate(s):
        if i == 0:
            if elem == east_symbol:
                accumulated_count_facing_east[i] = 1
            else:
                accumulated_count_facing_west[i] = 1
        else:
            if elem == east_symbol:
                accumulated_count_facing_east[i] = accumulated_count_facing_east[i - 1] + 1
                accumulated_count_facing_west[i] = accumulated_count_facing_west[i - 1]
            else:
                accumulated_count_facing_east[i] = accumulated_count_facing_east[i - 1]
                accumulated_count_facing_west[i] = accumulated_count_facing_west[i - 1] + 1
    return accumulated_count_facing_east, accumulated_count_facing_west

def write(result):
    print(result)


if __name__ == '__main__':
    solve()

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

l = [0]
r = [0]

for i in s:
    if i == "W":
        l.append(l[-1] + 1)
    else:
        l.append(l[-1])

for i in s[::-1]:
    if i == "E":
        r.append(r[-1] + 1)
    else:
        r.append(r[-1])

l = l[1:]
r = r[1:][::-1]

ans = 10 ** 9

for i in zip(l, r):
    ans = min(sum(i), ans)

print(ans-1)

Solution (Python):
N = int(input())
S = str(input())
x = S[1:].count('E')
ans = x
for i in range(1, N):
    if S[i] == 'E':
        x -= 1
    if S[i - 1] == 'W':
        x += 1
    ans = min(ans, x)
print(ans)

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

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

Solution (Python):

import sys


def solve(N: int, S: str):
    lw = [0] * N
    re = [0] * N

    if(S[0] == 'W'):
        lw[0] = 1
    if(S[N-1] == 'E'):
        re[N-1] = 1

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

    for i in reversed(range(0, N-1)):
        if(S[i] == 'E'):
            re[i] = re[i+1] + 1
        else:
            re[i] = re[i+1]

    
    

    ans = 1000000000
    for i in range(N):
        if(i == 0):
            ans = min(ans, re[i+1])
        elif(i == N-1):
            ans = min(ans, lw[i-1])
        else:
            ans = min(ans,lw[i-1]+re[i+1])

    print(ans)

    return



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)  
    solve(N, S)

if __name__ == '__main__':
    main()

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

west = [0]*(n+2)
east = [0]*(n+2)
ans = 10**6
for i in range(1,n+1):
	if S[i-1] == "W":
		west[i] = west[i-1]+1
	else:
		west[i] = west[i-1]
	if S[-i] == "E":
		east[-i-1] = east[-i]+1
	else:
		east[-i-1] = east[-i]

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

print(ans)

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

num = 0
ans = N
for i in range(1, N):
    if S[i] == "E":
        num += 1
ans = num
for j in range(1, N):
    if S[j - 1] == "W":
        num += 1
    if S[j] == "E":
        num -= 1
    if num < ans:
        ans = num
print(ans)

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

Solution (Python):
n = int(input())
s = input()
ans_min = s[1:].count('E')
ans = s[1:].count('E')
for i in range(1, len(s)):
    if(s[i] == 'E'):
        ans -= 1   
    if(s[i-1] == 'W'):
        ans += 1
    ans_min = min(ans_min, ans)
print(ans_min)

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

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

Solution (Python):
n=int(input())
s=list(input())
minn=5
ban=n
count=0
for i in range(n):
    if s[i]=='W':
        count=count+1
    else:
        count=count-1
    
    if count<minn:
        minn=count
        ban=i
ans=0

for j in range(n):
    if j<ban and s[j]=='W':
        ans=ans+1
    if j>ban and s[j]=='E':
        ans=ans+1


print(ans)

Solution (Python):
N = int(input())
S = list(input())
e_ = [0 for i in range(N)]
w_ = [0 for i in range(N)]

e_c = S.count('E')
w_c = 0
for i in range(N):
    if S[i] == 'E':
        e_c -= 1
    e_[i] = e_c

    w_[i] = w_c
    if S[i] == 'W':
        w_c += 1




ans = 10**9
for i in range(N):
    ans = min(ans, w_[i]+e_[i])
print(ans)

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

ans = 99999999

cnt_w = 0
cnt_e = 0
i = 0
for j in range(i):
    if S[j] == 'W':
        cnt_w += 1
for j in range(i,N):
    if S[j] == 'E':
        cnt_e += 1
ans = cnt_w + cnt_e

for i in range(N):
    if S[i] == 'W':
        cnt_w += 1
    else:
        cnt_e -= 1
    ans = min(cnt_w + cnt_e, ans)
print(ans)

Solution (Python):
N = int(input())
S = input()
leader = [0] * (N)
sum_e = 0
sum_w = 0
for i in range(N):
  leader[i] += sum_e
  leader[N-i-1] += sum_w
  if S[i] == 'E':
    sum_e += 1
  if S[N-i-1] == 'W':
    sum_w += 1
print(N-max(leader)-1)

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

lst_ans=[]
num_e= s.count("E")
ans = num_e
for i in range(n):
    if s[i] == "E":ans -= 1
    if i != 0:
        if s[i-1]=="W":ans += 1
    lst_ans.append(ans)
print(min(lst_ans))

Solution (Python):
n=int(input())
s=list(input())
n_W,n_E=0,0
l_W,l_E=[0],[0]
for i in s:
    if i=="W":n_W+=1
    else:n_E+=1
    l_W.append(n_W)
    l_E.append(n_E)
ans=10**9+7
for i in range(n):
    tmp=l_W[i]+l_E[-1]-l_E[i+1]
    if tmp<ans:ans=tmp
print(ans)

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

current_and_left_w=0
ans=current_and_right_e=S.count("E")

for i in S:
    if i=="E":
        current_and_right_e-=1
    else: 
        current_and_left_w+=1
        
    ans=min(ans,current_and_right_e+current_and_left_w)
print(ans)

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

wcnt = [(S[0]=='W')*1]+[0]*(N-1)
for i in range(1, N):
    wcnt[i] = wcnt[i-1]+(S[i]=='W')*1

w = wcnt[N-1]
e = N-w
ans = 10**9
for j in range(N):
    tmp = wcnt[j]
    ans = min(e+2*wcnt[j]-(S[j]=='W')*1-1-j, ans)
    
print(ans)

Solution (Python):
N = int(input())
S = [0 if a == 'E' else 1 for a in input()]
Sp = [S[0]]
for i in range(1, N):
    Sp.append(Sp[-1]+S[i])

max = Sp[-1]
ans = N - max
for i in range(1, N):
    ans = min(abs(Sp[i-1]) + abs(N-(i+1)-max+Sp[i]), ans)

print(ans)

Solution (Python):
n = int(input())
s = input()
e = [0]
w = [0]
a = []

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

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

Solution (Python):
n = int(input())
st = input()

c = [0] * (n+1)
ans = []

for i in range(len(st)):
  c[i+1] = c[i]
  if st[i] == "E":
    c[i+1] += 1
for i in range(n):
  e_left = c[i]
  e_right = c[n] - c[i+1]
  ans.append(i-e_left + e_right)
print(min(ans))