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()
e=[]
w=[]
cnt_e=0
cnt_w=0
e.append(0)
w.append(0)
for x in s:
    if x=="E":
        cnt_e+=1
    else:
        cnt_w+=1
    e.append(cnt_e)
    w.append(cnt_w)

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

print(ans)

Solution (Python):
n=int(input())
s=str(input())
list1=[0]*(n+2)
list2=[0]*(n+2)
count1=0
count2=0
min=n
for i in range(1,n+1):
  if s[i-1]=='E':
    count1 += 1
  if s[i-1]=='W':
    count2 += 1
  list1[i] = count1
  list2[i] = count2
for i in range(1,n+1):
  sum=list2[i-1]+(list1[n]-list1[i])
  if sum < min:
    min=sum
print(min)

Solution (Python):
N=int(input())
s=input()
a=[0]*N
for i in range(1,N):
    if s[i]=="E": a[0]+=1

for j in range(1,N):
    tmp=a[j-1]
    if s[j-1]=="W": tmp+=1
    if s[j]=="E": tmp-=1
    a[j]=tmp

print(min(a))

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

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

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

cnt = S[1:].count('E')

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

print(res)

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

subS = [0]
for s in (S):
    subS.append(subS[-1]+int(s=="W"))
subS.append(subS[-1])

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

Solution (Python):



n = int(input())
s = input()
wl = [0]*(n+1)
el = [0]*(n+1)

for i in range(n):
    if s[i] == "W":
        wl[i+1] = wl[i] + 1
        el[i+1] = el[i]
    else:
        el[i+1] = el[i] + 1
        wl[i+1] = wl[i]

minp = 10**6
for i in range(n+1):
    chp = wl[i] + (el[n]-el[i])
    if chp < minp:
        minp = chp

print(minp)

Solution (Python):
N = int(input())
S = input()
Lw = [0]*N
Le = [0]*N

cw = 0
ce = 0
for i in range(N):
  if S[i] == "W":
    cw += 1
  Lw[i] = cw
  if S[N - 1 - i] == "E":
    ce += 1
  Le[N-1-i] = ce

min = min(Le[1], Lw[N-2])
for i in range(1, N-1):
  p = Le[i + 1] + Lw[i - 1]
  if p < min:
    min = p

print(min)

Solution (Python):
N =int(input())
S = input()
i = 1
W = [0 for i in range(N)] 
E = [0 for i in range(N)] 

if S[0] == "W":
    W[0] = 1
else:
    E[0] = 1
while i < N:
    if S[i] == "W":
        W[i] = W[i-1]+1
        E[i] = E[i-1]
    else:
        E[i] = E[i-1]+1
        W[i] = W[i-1]
    i += 1
ans = 300001
for i in range(N):
    
    if i != 0:
        temp = W[i-1]+E[N-1]-E[i]
    else:
        temp = E[N-1]-E[0]
    if temp < ans:
        ans = temp
print(str(ans))

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

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

cnt_w = 0
cnt_e = 0
for i in range(n):
  if s[i] == "W":
    cnt_w += 1
    w[i] = cnt_w
    e[i] = cnt_e
  else:
    w[i] = cnt_w
    cnt_e += 1
    e[i] = cnt_e
    
ch = n - 1
for i in range(n):
  if i >= 1:
    ch = min(ch, w[i - 1] + e[-1] - e[i])
  else:
    ch = min(ch, e[-1] - e[0])
print(ch)

Solution (Python):
from bisect import bisect_right, bisect_left

N = int(input())
S = input()
W = [i for i, s in enumerate(S) if s == 'W']
E = [i for i, s in enumerate(S) if s == 'E']
m = 10**8
for i in range(N):
    turn_E = bisect_left(W, i)
    turn_W = len(E) - bisect_right(E, i)
    m = min(m, turn_E + turn_W)
print(m)

Solution (Python):


n = int(input())
s = list(input())
w = [0] * n
e = [0] * n

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

ans = float("inf")

for i in range(n):
    num = 0
    if i-1 >= 0:
        num += w[i-1]
    num += e[-1] - e[i]
    ans = min(num,ans)

print(ans)

Solution (Python):
n=int(input())
s=list(input())
l=[]
sh=s[0]
s.remove(sh)
c=0
for i in s:
  if i=='E':
    c+=1
l.append(c)
for i in range(len(s)):
  tsu=s[i]
  if sh=='W':
    c+=1
  if tsu=='E':
    c-=1
  l.append(c)
  sh=tsu
print(min(l))

Solution (Python):





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

X = []
count = 0
for s in S:
    if s == 'W':
        X.append(count)
    else:
        count += 1
        X.append(count)
     
min_person = int(1e10) 
for i, x in enumerate(X):
    num_person = 0
    if i > 0:
        num_person += i - X[i - 1]
    if i < N:
        num_person += X[-1] - X[i]
        
    min_person = min(min_person, num_person)
    
print(min_person)

Solution (Python):
n=int(input())
s=input()
cnt=s[1:].count('E')
res=cnt
for i in range(len(s)-1):
    if s[i]=='W' and s[i+1]=='W':
        cnt+=1
    if s[i]=='E' and s[i+1]=='E':
        cnt-=1
    
    if res>cnt:
        res=cnt
print(res)

Solution (Python):
n = int(input())
s = list(input())
a = s.count("E")
b1 = 0
t = 10000000000000
for i in range(n):
    if s[i] == "E":
        a -= 1
        c = b1 + a
    else:
        c = b1 + a
        b1 += 1
    if t > c:
        t = c
print(t)

Solution (Python):
n = int(input())
s = list(input())
a = s.count("E")
b1 = 0
t = 10000000000000
for i in range(n):
    if s[i] == "E":
        a -= 1
        c = b1 + a
    else:
        c = b1 + a
        b1 += 1
    if t > c:
        t = c
print(t)

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

num_W = 0
num_E = S.count("E")
if S[0] == 'W':
    num_W = 1
else:
    num_E -= 1
minv = num_E
for i in range(1, N):
    if S[i] == 'W':
        num_W += 1
    else:
        num_E -= 1
    minv = min(minv, num_W+num_E)
print(minv)

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

es = [0] * N 
ws = [0] * N 

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

print(min(es[i] + ws[i] for i in range(N)))

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')



N = 0
S = ""


def set_inputs():
    global N, S
    N = get_int()
    S = get_str()
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    
    turn_e = [0] + list(itertools.accumulate([s == "W" for s in S[:-1]]))
    turn_w = list(reversed(list(itertools.accumulate(
        [s == "E" for s in reversed(S[1:])]
    )))) + [0]
    print(min(e + w for e, w in zip(turn_e, turn_w)))
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq
import itertools
import operator


inf = float('inf')



N = 0
S = ""


def set_inputs():
    global N, S
    N = get_int()
    S = get_str()
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    
    turn_e = [0] + list(itertools.accumulate([s == "W" for s in S[:-1]]))
    turn_w = list(reversed(list(itertools.accumulate(
        [s == "E" for s in reversed(S[1:])]
    )))) + [0]
    print(min(itertools.starmap(operator.add, zip(turn_e, turn_w))))
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types):
    if len(types) == 1:
        return [types[0](input()) for _ in range(n)]
    return zip(*(
        [t(x) for t, x in zip(types, input().split())]
        for _ in range(n)
    ))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
S = input()
a = S.count("E")
c = 0
A = N
for i in range(N):
    a += c
    c = 0
    if S[i] == "E":
        a -= 1
    else:
        c = 1
    A = min(A, a)
print(A)

Solution (Python):
n = int(input())
s = list(input())
w = [0] * (n+1)
e = [0] * (n+1)
ans = n
for i in range(n):
    if s[i] == "W":
        w[i+1] = w[i] + 1
    else:
        w[i+1] = w[i]

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

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

Solution (Python):
N = int(input())
S = list(input())
sumE = [0]
sumW = [0]
for i, s in enumerate(S):
    if S[len(S)-1-i] == 'E':
        e = 1
    else:
        e = 0
    if S[i] == 'W':
        w = 1
    else:
        w = 0
    
    sumE.append(sumE[i]+e)
    sumW.append(sumW[i]+w)
min = len(S)
for i in range(N):
    sum = sumE[len(S)-1-i] + sumW[i]
    if sum < min:
        min = sum
print(min)

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

e_cnt, w_cnt = 0, 0
e_arr, w_arr = [0]*(N+1), [0]*(N+1)
for i,s in enumerate(S):
    if s=='E':
        e_cnt += 1
    else:
        w_cnt += 1
    e_arr[i+1] = e_cnt
    w_arr[i+1] = w_cnt

max_ew = -1e6
for i in range(N):
    max_ew =  max(e_arr[i] - w_arr[i+1], max_ew)
print(N - 1 - (max_ew+w_cnt))

Solution (Python):
N=int(input())
S=input()
L=[0 if S[i]=="E" else 1 for i in range(N)]
Lmk=0
Rmk=N-1-sum(L[1:])
ans=Rmk
for i in range(1,N):
  Lmk+=1 if L[i-1]==1 else 0
  Rmk-=1 if L[i]==0 else 0
  ans=min(ans,Lmk+Rmk)
print(ans)

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

Solution (Python):
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
    else:
        w[i + 1] = w[i]
for i in range(N - 1, -1, -1):
    if s[i] == 'E':
        e[i] = e[i + 1] + 1
    else:
        e[i] = e[i + 1]
ans = float('inf')
for i in range(1, N + 1):
    ans = min(ans, w[i - 1] + e[i])
print(ans)

Solution (Python):
n = int(input())
s = input()
a = [0]*(n+2)
b = [0]*(n+2)
for i in range(n):
    a[i+1] = a[i]
    b[-i-2] = b[-i-1]
    if s[i] == "W":
        a[i+1] += 1
    if s[-i-1] == "E":
        b[-i-2] += 1
b = b[2:n+2]
c = [a[i] + b[i] for i in range((n))]
print(min(c))

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

w_sum, e_sum = [0] * n, [0] * n

for i, item in enumerate(s):
    if i == 0:
        continue
    if s[i-1] == "W":
        w_sum[i] = w_sum[i-1] + 1
    else:
        w_sum[i] = w_sum[i-1]

s = s[::-1]

for i, item in enumerate(s):
    if i == 0:
        continue
    if s[i-1] == "E":
        e_sum[i] = e_sum[i-1] + 1
    else:
        e_sum[i] = e_sum[i-1]

e_sum = e_sum[::-1]
ans = float("inf")

for i, item in enumerate(s):
    ans = min(ans, e_sum[i] + w_sum[i])

print(ans)

Solution (Python):
import numpy as np

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

W=[1 if s=='W' else 0 for s in S]
E=[1 if s=='E' else 0 for s in S]

print(
min((np.cumsum(W)[:-1] + np.cumsum(E[::-1])[::-1][1:]).min(),
    np.cumsum(W)[-1],
    np.cumsum(E[::-1])[::-1][0])
)

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

ecum = [0] * n

for i, item in enumerate(s):
  if i == 0:
    if item == "E":
      ecum[i] = 1
  else:
    if item == "E":
      ecum[i] = ecum[i-1] + 1
    else:
      ecum[i] = ecum[i-1]

ans = n
for i, item in enumerate(s):
  if i == 0:
    cost = ecum[-1]
  else:
    cost = i - ecum[i-1] + ecum[-1] - ecum[i]
  ans = min(ans, cost)
  
print(ans)

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

cost = S.count("E")
ans = cost

for s in S:
    if s == "E":
        cost -= 1
        ans = min(ans, cost)
    else:
        cost += 1

print(ans)

Solution (Python):
N = int(input())
S = input()
ws = [0]*N
w_c = 0
for i in range(0, N):
    if S[i] == "W":
        w_c += 1
    ws[i] += w_c
es = [0]*N
e_c = 0
for i in reversed(range(0, N)):
    if S[i] == "E":
        e_c += 1
    es[i] += e_c
res = 3*10**5
for i in range(0, N):
    if i - 1 == 0:
        if es[0] < res:
            res = es[1]
        continue
    if i + 1 == N:
        if ws[i] < res:
            res = ws[i-1]
        continue
    t = ws[i-1] + es[i+1]
    if t < res:
        res = t
print(res)

Solution (Python):

N = int(input())
S = list(input())
 
numE = S.count('E')
numTurn = numE
numW = 0
 
for dir in S:
    if dir == 'E':
        numE -= 1
    else:
        numW += 1
    if numTurn > numE + numW:
        numTurn = numE + numW
 
print(numTurn)

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

numE = S.count('E')
numTurn = numE
numW = 0

for dir in S:
    if dir == 'E':
        numE -= 1
    else:
        numW += 1
    if numTurn > numE + numW:
        numTurn = numE + numW

print(numTurn)

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

numE = S.count('E')
numTurn = numE
numW = 0

for i in range(N):
    if S[i] == 'E':
        numE -= 1
    else:
        numW += 1
    if numTurn > numE + numW:
        numTurn = numE + numW

print(numTurn)

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

left_W = [0] * N
right_E = [0] * N
for i in range(N-1):
    if S[i] == "W":
        left_W[i+1] = left_W[i] + 1
    else:
        left_W[i+1] = left_W[i]

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


min_count = 100000000

for i in range(N):
    change_count = left_W[i] + right_E[i]
    if change_count < min_count:
        min_count = change_count
print(min_count)

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

left_W = [0] * N
for i in range(N-1):
    if S[i] == "W":
        left_W[i+1] = left_W[i] + 1
    else:
        left_W[i+1] = left_W[i]

right_E = [0] * N
for i in range(N-1):
    if S[N-i-1] == "E":
        right_E[N-i-2] = right_E[N-i-1] + 1
    else:
        right_E[N-i-2] = right_E[N-i-1]


min_count = 100000000



for i in range(N):
    change_count = left_W[i] + right_E[i]
    if change_count < min_count:
        min_count = change_count
print(min_count)

Solution (Python):
import numpy as np

n = int(input())
string = input()

lst_w = []
lst_e = []

count = 0
for i in string:
    if i == 'W':
        lst_w.append(count)
        count += 1
    else:
        lst_w.append(count)
        
count = 0
for i in string[::-1]:
    if i == 'E':
        lst_e.append(count)
        count += 1
    else:
        lst_e.append(count)
lst_e = lst_e[::-1]

print(min(np.array(lst_w) + np.array(lst_e)))

Solution (Python):
N=int(input())
S=input()
l=list(S)
E=[0]*N
W=[0]*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:
        E[i]=E[i-1]
        W[i]=W[i-1]+1


ans=N
ans=min(ans,E[N-1]-E[0])


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

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

N = I()
S = input()

w_count = S.count('W')
e_count = S.count('E')

result = min(w_count, e_count)
left_w = 0
right_e = e_count
for s in S:
    
    result = min(result, left_w + right_e)
    if s == 'W':
        left_w += 1
    else:
        right_e -= 1

print(result)

Solution (Python):
N = int(input())
s = input()
goukei_W = []
goukei_E = []
goukei = []
sum = []

count = 0
for i in range(N):
    if s[i] == 'W':
        count += 1
    goukei_W.append(count)

count = 0
for i in range(N):
    if s[N-i-1] == 'E':
        count += 1
    goukei_E.append(count)

for i in range(N):
    if i-1 < 0:
        goukei.append(goukei_E[N-i-2])
    elif N-i-2 < 0:
        goukei.append(goukei_W[i-1])
    else:
        goukei.append(goukei_W[i-1] + goukei_E[N-i-2])

print(min(goukei))

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

cntW = 0
cntE = 0
sumW =[]
sumE = []
num_of_turn = []

for i in range(N):
    if S[i] == 'W':
        cntW += 1
    elif S[i] == 'E':
        cntE += 1
    sumW.append(cntW)
    sumE.append(cntE)

num_of_turn.append(sumE[N - 1] - sumE[0])

for i in range(1, N - 1):
    num_of_turn.append(sumW[i - 1] + sumE[N - 1] - sumE[i])

num_of_turn.append(sumW[N - 1])

print(min(num_of_turn))

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

Solution (Python):
n = int(input())
s = input()
count = s.count('E')
m = count
for i in s:
    if i == 'E':
        m -= 1
    else:
        m += 1
    count = min(count, m)
print(count)

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

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

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

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

ans = 300000
for L in range(1,N+1):
    num = E[N] - E[L] + W[L-1] -W[0]
    if ans > num:
        ans = num

print(ans)

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

W = 0
E = 0

for c in s:
    if c == 'W':
        W += 1
    if c == 'E':
        E += 1

arr = []

e_left = 0
w_left = 0
for c in s:
    if c == 'W':
        e_right = E - e_left
        arr.append(w_left + e_right)
        w_left += 1
    if c == 'E':
        e_right = E - e_left - 1
        arr.append(w_left + e_right)
        e_left += 1

print(min(arr))

Solution (Python):
n=int(input())
s=input()
w=[0]*n
e=[0]*n
for i in range(n):
    if s[i]=="W":
        if i==0:
           w[i]+=1
        else:
            w[i]+=w[i-1]+1
            e[i]+=e[i-1]
    else:
        if i==0:
            e[i]+=1
        else:
            e[i]+=e[i-1]+1
            w[i]+=w[i-1]
min=9999999
for i in range(n):
    if i==0:
        num=e[n-1]-e[i]

    else:
        num=w[i-1]+e[n-1]-e[i]
    if min>num:
        min=num
print(min)

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

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

W = [0 for _ in range(N)]
T = [0 for _ in range(N)]
count = 0
for i in range(N):
    if S[i] == 'W':
        count += 1
    W[i] = count

min_value = 1000000000000
for i in range(0, N):
    t = 0
    if i > 0:
        t += W[i - 1]
    if i < N - 1:
        t += E[i + 1]
    if min_value > t:
        min_value = t
print(min_value)

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

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

R = [0]*N
if S[-1]=="E":
  R[-1] = 1
for i in range(N-2, -1, -1):
  R[i] = R[i+1] + (S[i]=="E")

ans = N
for i in range(N):
  t = 0
  if i>0:
    t += L[i-1]
  if i<N-1:
    t += R[i+1]
  ans = min(ans, t)
print ans

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

w_accumulate = [0]
e_accumulate = [0]

for i in range(1, N):
    w_accumulate.append(w_accumulate[-1] + (S[i - 1] == "W"))
    e_accumulate.append(e_accumulate[-1] + (S[-i] == "E"))

e_accumulate.reverse()

print(min([c1 + c2 for c1, c2 in zip(w_accumulate, e_accumulate)]))

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

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

count_min = E[-1]
for i in range(1, n):
    count = W[i-1] + (E[-1] - E[i]) 
    count_min = min(count_min, count)

print(count_min)

Solution (Python):
n = int(input())
s = input()
c = s.count("E")
d = c
for i in s:
  if i == "E":
    d -= 1
  else:
    d += 1
  c = min(c,d)
print(c)

Solution (Python):

 
def main(N, S):
    num_e = S.count("E")
    num_w = 0
    res = num_e
    for s in S:
        if(s=="E"):
            num_e -= 1
        else:
            num_w += 1
        res = min(res, num_e + num_w)
    print(res)
    
if(__name__ == "__main__"):
    N = int(input())
    S = input()
    
    main(N, S)

Solution (Python):


"""
Created on 2019/3/16
Solved on 2019/3/
@author: shinjisu
"""






def getInt(): return int(input())

def getIntList(): return [int(x) for x in input().split()]


def zeros(n): return [0]*n


def getIntLines(n): return [int(input()) for i in range(n)]
"""
def getIntLines(n):
    data = zeros(n)
    for i in range(n):
        data[i] = getInt()
    return data
"""

def zeros2(n, m): return [zeros(m)]*n 

def getIntMat(n, m):  
    
    mat = zeros2(n, m)
    for i in range(n):
        mat[i] = getIntList()
    return mat

ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**18

class Debug():
    def __init__(self):
        self.debug = True
 
    def off(self):
        self.debug = False
 
    def dmp(self, x, cmt=''):
        if self.debug:
            if cmt != '':
                print(cmt, ':  ', end='')
            print(x)
        return x


def prob():
    d = Debug()
    d.off()
    N = getInt()
    d.dmp((N))
    A = input()
    d.dmp(A)
    west, east = 0, 0
    for i in range(N):
        if A[i] == 'W':
            west += 1
        else:
            east += 1
    minTurn = east
    turn = east
    for i in range(N):
        if A[i] == 'W':
            pass
        else:
            turn -= 1
        d.dmp(turn,'turn')
        minTurn = min(minTurn, turn)
        
        if A[i] == 'W':
            turn += 1
        else:
            turn += 1-1
        d.dmp(minTurn,'minTurn')
    return minTurn


ans = prob()
if ans is None:
    pass
elif type(ans) == tuple and ans[0] == 1:  
    for elm in ans[1]:
        print(elm)
else:
    print(ans)

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

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

Solution (Python):
N = int(input())
S = input()
E_cost = S.count("E")
W_cost = 0
ans = N
for s in S:
    if s == "E":
        E_cost -= 1
    ans = min(ans, E_cost + W_cost)
    if s == "W":
        W_cost += 1
print(ans)

Solution (Python):
n = int(input())
s = str(input())
answer = n
dpa = [-1]*(n+1)
dpa[0] = 0
dpb = [-1]*(n+1)
dpb[0] = s.count('E')

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

for i in range(n+1):
	answer = min(answer, dpa[i]+dpb[i])
	
print(answer)

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

le = [[0,0]]
ri = [[0,0] for i in range(n)]
for i in range(n-1):
    if s[i] == 'W':
        le.append([le[-1][0],le[-1][1]+1])
    else:
        le.append([le[-1][0]+1,le[-1][1]])
for i in range(n-1,0,-1):
    if s[i] == 'W':
        ri[i-1] = [ri[i][0],ri[i][1]+1]
    else:
        ri[i-1] = [ri[i][0]+1,ri[i][1]]

res = 10**9
for i in range(n):
    res = min(res,le[i][1]+ri[i][0])
print(res)

Solution (Python):
def solve(string):
    n, s = string.split()
    num_e = s.count("E")
    count_e = count_w = 0
    ans = int(n)
    for _s in s:
        tmp = count_w
        if _s == "E":
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, s = string.split()
    num_e = s.count("E")
    count_e = count_w = 0
    ans = int(n)
    for _s in s:
        tmp = count_w
        if _s == "E":
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, s = string.split()
    num_e = s.count("E")
    count_e = 0
    count_w = 0
    ans = int(n)
    for _s in s:
        tmp = count_w
        if _s == "E":
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, s = string.split()
    n = int(n)
    es = {i for i, _s in enumerate(s) if _s == "E"}
    num_e = len(es)
    count_e = 0
    count_w = 0

    ans = n
    for i, _s in enumerate(s):
        tmp = count_w
        if i in es:
            count_e += 1
        else:
            count_w += 1
        tmp += num_e - count_e
        ans = min(ans, tmp)
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, s = string.split()
    n = int(n)
    es = {i for i, _s in enumerate(s) if _s == "E"}
    num_e = len(es)
    num_w = n - num_e
    count_e = 0
    count_w = 0

    ans = n
    for i, _s in enumerate(s):
        tmp = [count_e, count_w]
        if i in es:
            count_e += 1
        else:
            count_w += 1
        tmp += [num_e - count_e, num_w - count_w]
        ans = min(ans, tmp[1] + tmp[2])
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

Solution (Python):
def solve(string):
    n, s = string.split()
    n = int(n)
    es = {i for i, _s in enumerate(s) if _s == "E"}
    num_e = len(es)
    num_w = n - num_e
    count_e = 0
    count_w = 0

    ans = n
    for i, _s in enumerate(s):
        tmp = [count_e, count_w]
        if i in es:
            count_e += 1
        else:
            count_w += 1
        tmp += [num_e - count_e, num_w - count_w]
        ans = min(ans, tmp[1] + tmp[2])
    return str(ans)


if __name__ == '__main__':
    print(solve('\n'.join([input(), input()])))

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

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

original_e = []
prev = 0
for i in range(N - 1, -1, -1):
    if S[i] == 'E':
        prev += 1
    original_e.append(prev)

ans = min(original_e[N - 1], original_w[N - 2])
for i in range(1, N - 1):
    ans = min(ans, original_w[i - 1] + original_e[N - (i + 1)])
print(ans)

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

e = S.count('E')
w = 0
ans = e
for s in S:
    if s == 'E':
        e -= 1
    else:
        w += 1
    ans = min(ans,e+w)

print(ans)

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

score = []
score.append(s[1:].count("E"))

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

print(min(score))

Solution (Python):




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

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

Solution (Python):
from sys import stdin

input = stdin.readline

MAX_N = 300001

N = int(input())

S = input()


def main():
    
    E = [0] * N

    if S[0] == 'E':
        E[0] += 1

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

    ans = MAX_N

    for i in range(N):
        
        num = E[-1] - E[i]

        if (0 < i):
            
            num += i - E[i - 1]

        ans = min(ans, num)

    print(ans)
    return


main()

Solution (Python):
N = int(input())
S = input()
E = 0
for i in range(N):
    if S[i] == 'E':
        E += 1
ne = 0
nw = 0
ansl = []
for i in range(N):
    if S[i] == 'E':
        ansl.append(nw+E-ne-1)
        ne += 1
    else:
        ansl.append(nw+E-ne)
        nw += 1
print(min(ansl))

Solution (Python):
N = int(input())
S = input()
e, w, ans = [0], [0], 10**6
for i in range(1, N + 1):
    if S[i-1] == 'E':
        e.append(e[i - 1] + 1)
        w.append(w[i - 1])
    else:
        e.append(e[i - 1])
        w.append(w[i - 1] + 1)
for i in range(1, N + 1):
    ans = min(ans, w[i - 1] + e[N] - e[i])
print(ans)

Solution (Python):
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
        continue
    if S[i] == 'W':
        W[i] = W[i - 1] + 1
    else:
        W[i] = W[i - 1]
for i in range(N - 1, -1, -1):
    if i == N - 1:
        if S[i] == 'E':
            E[i] = 1
        continue
    if S[i] == 'E':
        E[i] = E[i + 1] + 1
    else:
        E[i] = E[i + 1]
A = []
for i in range(N):
    A.append(W[i] + E[i] - 1)
print(min(A))

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