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):

import sys

def main():
	N = int(input())
	tmp = input()
	lines = list(tmp)
	
	nisi = [0 for _ in range(N)]
	higasi = [0 for _ in range(N)]
	for i, val in enumerate(lines):
		if val == "W":
			if i != 0:
				nisi[i] = nisi[i-1] + 1
			else:
				nisi[i] = 1
		else:
			if i != 0:
				nisi[i] = nisi[i-1]
			else:
				nisi[i] = 0
		
	for i in range(N-1, -1, -1):
		if lines[i] == "E":
			if i < N-1:
				higasi[i] = higasi[i+1] + 1
			else:
				higasi[i] = 1
		else:
			if i < N-1:
				higasi[i] = higasi[i+1]
			else:
				higasi[i] = 0
	
	ans_min = -1
	for i, val in enumerate(lines):
		ans = 0
		ans1 = 0
		ans2 = 0
		if i < (N - 1):
			ans1 = higasi[i+1]
		if i > 0:
			ans2 = nisi[i-1]
		ans = ans1 + ans2
			
		if -1 == ans_min:
			ans_min = ans
		if ans_min > ans:
			ans_min = ans
	
	print(ans_min)
		
if __name__ == "__main__":
	main()

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

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

Solution (Python):
def attention(N: int, S: str) -> int:
    
    Se = [0] * (N + 1)
    for i, c in enumerate(S):
        Se[i + 1] = Se[i] + (1 if c == 'W' else 0)

    min_people = float('inf')
    for i, c in enumerate(S):
        w2e = Se[i]  
        e2w = (N - (i + 1)) - (Se[N] - Se[i + 1])
        min_people = min(min_people, w2e + e2w)

    return min_people


if __name__ == "__main__":
    N = int(input())
    S = input()
    ans = attention(N, S)
    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()
L=[[0,0]]*(N+2)
numW=0
numE=0
for i in range(0,N):
  if S[i]=='W':
    numW+=1
  else:
    numE+=1
  L[i+1]=[numW,numE]

L[N+1]=L[N]
  
INF=10**6
ans=INF

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

Solution (Python):
import numpy as np
def main():
    N = int(input())
    S = list(input())
    S_W = [1 if S[i] == "W" else 0 for i in range(N)]
    S_E = [1 if S[N-i-1] == "E" else 0 for i in range(N)]
    S_W_rui = np.cumsum(S_W)
    S_E_rui = np.cumsum(S_E)
    
    
    cnt = N
    cnt_new = N
    
    for i in range(N):
        if i == 0:
            cnt_new = S_E_rui[N-i-2]
        elif i == N-1:
            cnt_new = S_W_rui[i-1]
        else:
            cnt_new = S_W_rui[i-1] + S_E_rui[N-i-2]
        if cnt_new < cnt:
            cnt = cnt_new
            
    print(cnt)
    
    
main()

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

Solution (Python):
N = int(input())
S = input()
turn = S.count('E')
min_turn = 300000
for i in range(N):
    if S[i] == 'E':
        turn -= 1
    if (S[i-1] == 'W') and (1 <= i):
        turn += 1
    if min_turn > turn:
        min_turn = turn
print(min_turn)

Solution (Python):
n=int(input())
S=input()
a=[0]*n
if S[0]=='W':
  a[0]=1
for i in range(1,n):
  if S[i]=='W':
    a[i]+=a[i-1]+1
  else:
    a[i]+=a[i-1]

N=n-1-(a[-1]-a[0])
M=min(n,N)
for k in range(1,n):
  N=a[k-1]+n-(k+1)-(a[-1]-a[k])
  M=min(M,N)

print(M)

Solution (Python):
N = int(input())
S = input()
min_turn = 300000
wnum = 0
for i in range(N):
    if i == 0:
        enum = S[1:].count('E')
    if (1 <= i) and (S[i] == 'E'):
        enum -= 1
    if (1 <= i) and (S[i-1] == 'W'):
        wnum += 1
    turn = enum + wnum
    if min_turn > turn:
        min_turn = turn
print(min_turn)

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

if S[0] == "E":
    E[0] = 1
else:
    W[0] = 1

for i in range(1, N):
    if S[i] == "E":
        E[i] = E[i-1] + 1
        W[i] = W[i-1]
    else:
        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, W[i-1] + E[N-1] - E[i])
print(ans)

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

result = 10 ** 6
E = S.count('E')
W = S.count('W')
for i in range(N):
    temp = 0
    if 'E' == S[i]:
        temp = E-1
    else:
        temp = E
    if 'W' == S[i]:
        E += 1
    else:
        E -= 1
    result = min(temp, result)
print(result)

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

e = S.count("E")
ans = 1e9
for s in S:
  if(s == "E"):
    e -= 1
  else:
    ans = min(ans, e)
    e += 1
ans = min(ans, e)
print(ans)

Solution (Python):
import numpy as np

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

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

for i in range(1,N):
    if S[i]=="W" and S[i-1]=="W":
        d[i]=d[i-1]+1
    elif S[i]=="E" and S[i-1]=="W":
        d[i]=d[i-1]
    elif S[i]=="W" and S[i-1]=="E":
        d[i]=d[i-1]
    else:
        d[i]=d[i-1]-1
        
print(int(np.min(d)))

Solution (Python):
n = int(input())
s = list(input())
e = s.count("E")
w = s.count("W")
res = 10**7
for i in range(n):
    if s[i] == "E":
        tmp = e - 1
    else:
        tmp = e
    if s[i] == "W":
        e += 1
    else:
        e -= 1
    res = min(res, tmp)
print(res)

Solution (Python):
int_n = int(input())
str_s = input()

li_west = [0] * int_n
for i in range(int_n - 1):
    if str_s[i] == 'W':
        li_west[i + 1] = li_west[i] + 1
    else:
        li_west[i + 1] = li_west[i]
li_east = [0] * int_n

for i in range(int_n - 1, 0, -1):
    if str_s[i] == 'E':
        li_east[i - 1] = li_east[i] + 1
    else:
        li_east[i - 1] = li_east[i]

minimum = 999999
for i in range(int_n):
    int_w = li_west[i] + li_east[i]
    minimum = min(minimum, int_w)

print(minimum)

Solution (Python):
n = int(input())
s = input()
s = [s[i:i+1] for i in range(n)]
lcost = [0 for _ in range(n)]
rcost = [0 for _ in range(n)]
if s[0] == 'W':
  lcost[0] = 1
if s[-1] == 'E':
  rcost[-1] = 1
for i in range(1, n):
  if s[i] == 'W':
    lcost[i] = lcost[i - 1] + 1
  else:
    lcost[i] = lcost[i - 1]
for i in range(2, n + 1):
  if s[-i] == 'E':
    rcost[-i] = rcost[-i + 1] + 1
  else:
    rcost[-i] = rcost[-i + 1]
mincost = min(lcost[-1], rcost[0])


for i in range(1, n - 1):
  hoge = lcost[i - 1] + rcost[i + 1]
  if hoge < mincost:
    mincost = hoge
print(mincost)

Solution (Python):
def main():
    N = int(input())
    S = input()
    cumsum = [(0, 0)] * (N+1)
    for i in range(N):
        e, w = cumsum[i]
        if S[i] == 'E':
            e += 1
        else:
            w += 1
        cumsum[i+1] = (e, w)
    ans = float("inf")
    for i in range(N):
        cost = cumsum[i][1] + cumsum[N][0] - cumsum[i+1][0]
        ans = min(ans, cost)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
num=int(input())
n=input()
m=len(n)
cnt=[0 for i in range(m)]
n1=n[1:]
cnt[0]=n1.count('E')

for i in range(m-1):
    point=0
    if n[i]=='W':
        point+=1
    if n[i+1]=='E':
        point-=1
    cnt[i+1]=cnt[i]+point
    
print(min(cnt))

Solution (Python):
n = int(input())
pp = list(input())
cnt = 0
lis = [0]*n
w = [0]*n
e = [0]*n
if pp[0] =="W":
    w[0] =1
else:
    e[0] =1
for i in range(1,n):
    if pp[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]
for i in range(1,n):
    lis[i] = w[i-1]+e[n-1]-e[i]
lis[0] = e[n-1]-e[0]

print(min(lis))

Solution (Python):
n = int(input())
slist = input().strip()
e, w = 0, 0
for s in slist:
  if s == 'E':
    e += 1
  else:
    w += 1

le, lw, re, rw = 0, 0, e, w
minr = n + 1
for s in slist:
  if s == 'E':
    re -= 1
    minr = minr if minr <= re + lw else re + lw
    le += 1
  else:
    rw -= 1
    minr = minr if minr <= re + lw else re + lw
    lw += 1

print(minr)

Solution (Python):
n=int(input())
s=input()
W=[0]*n
E=[0]*n
W[0]=1 if s[0]=="W" else 0
for i in range(n-1):
  W[i+1]=W[i]+ (1 if s[i+1]=="W" else 0)
  E[i+1]=(i+2)-W[i+1]
ans=E[-1]-E[0]
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()
W=[0]*n
W[0]=1 if s[0]=="W" else 0
for i in range(n-1):
  W[i+1]=W[i]+ (1 if s[i+1]=="W" else 0)
ans=(n-1)-(W[n-1]-W[0])
for i in range(1,n-1):
  hidari=W[i-1]
  migi  =(n-(i+1))-(W[n-1]-W[i])
  ans=min(ans,(hidari+migi))
print(min(ans,W[n-2]))

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

listSeeWSum=[]
index=0
wSum=0
while index < n:
    if index==0:
        listSeeWSum.append(0)
    else:
        if listS[index-1] == "W":
            wSum+=1
        listSeeWSum.append(wSum)
    index+=1

listSeeESum=[]
listS.reverse()
index = 0
eSum = 0
while index < n:
    if index == 0:
        listSeeESum.append(0)
    else:
        if listS[index-1] == "E":
            eSum += 1
        listSeeESum.append(eSum)
    index += 1
listSeeESum.reverse()

listSum=[]
index=0
while index < n:
    listSum.append(listSeeESum[index]+listSeeWSum[index])
    index+=1

print(min(listSum))

Solution (Python):
n=int(input())
s=input()
r=len([i for i in range(1,n) if s[i]=='E'])
l=0
i=1
x=r+l
while i<=n-1:
    if s[i]=='E' and s[i-1]=='E':
        r=r-1
        l=l
    elif s[i]=='W' and s[i-1]=='W':
        r=r
        l=l+1
    elif s[i]=='W' and s[i-1]=='E':
        r=r
        l=l
    elif s[i]=='E' and s[i-1]=='W':
        r=r-1
        l=l+1
    x=min(l+r,x)
    i+=1
print(x)

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

Solution (Python):
import sys

def main():
  input = sys.stdin.readline
  N = int(input())
  S = input().strip()
  dp = [0 for _ in range(N)]
  nE = S.count('E')
  nW = 0
  for i in range(N):
    if S[i] == 'E':
      dp[i] = nE - 1 + nW
      nE -= 1
    else:
      dp[i] = nE + nW
      nW += 1
  print(min(dp))


if __name__ == '__main__':
  main()

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

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

l=[S.count('E')]
n=l[0]

for x in S:
    if x == 'W':
        n += 1
    else:
        n -= 1
    l.append(n)

print(min(l))

Solution (Python):
N = int(input())
S = input()
if S[0] == "E":
    e = 0
    w = S.count("E") - 1
    before = "E"
else:
    e = 0
    w = S.count("E")
    before = "W"
now = w
for s in S[1:]:
    if s == "E":
        w -= 1
    if before == "W":
        e += 1
    before = s
    now = min(e + w, now)
print(now)

Solution (Python):
N = int(input())
S = input()
W_count = 0
W_cumulative = []
for i in range(N):
    if S[i] == "W":
        W_count += 1
    W_cumulative.append(W_count)

ans = N
W_cumulative.insert(0,0)
for i in range(1,N+1):
    count = W_cumulative[i-1]
    count += N - W_cumulative[-1] - i + W_cumulative[i]
    ans = min(ans,count)

print(ans)

Solution (Python):
n = int(input())
s = input()
list = []
E = s.count("E")
L = 0
check = 0 

for i in s:
    if i == "E":
        E -= 1
        if check == 0:
            list.append(E+L) 
        elif check == 1:
            check = 0
            L += 1
            list.append(E+L)
    elif i == "W":
        if check == 0: 
            check = 1
            list.append(E+L)
        elif check == 1: 
            L += 1
            list.append(E+L)

print(min(list))

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

Solution (Python):
n = int(input())
s = list(input())
a = []
a.append(s[1:].count("E"))
for i in range(n-1):
    if s[i+1] == "E" and s[i] == "E":
        a.append(a[i]-1)
    elif s[i+1] == "W" and s[i] == "E":
        a.append(a[i])
    elif s[i+1] == "E" and s[i] == "W":
        a.append(a[i])
    elif s[i+1] == "W" and s[i] == "W":
        a.append(a[i]+1)
print(min(a))

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

w = 0
e = s.count('E')
ans = w + e
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 = str(input())
ans = [0] * n
ans[0] = s[1:].count('E')
for i in range(1, n):
    if s[i-1] == 'E':
        ans[i] = ans[i-1]
    else:
        ans[i] = ans[i-1] + 1
    if s[i] == 'E':
        ans[i] -= 1
print(min(ans))

Solution (Python):
n = int(input())
s = input()
cum = [0] * (n+1)
cum[0] = 0
for i in range(n):
    cum[i+1] = cum[i] + int(s[i] == 'W')
cost = [0] * n
for i in range(n):
    cost[i] = cum[i] + (n-i-1) - (cum[n] - cum[i+1])
print(min(cost))

Solution (Python):
N = int(input())
S = input()
answer = S.count('E')
if S[0] =='E':
    test = S.count('E') - 1
else:
    test = S.count('E')

for x in range(0,N-1):
    if S[x] == 'W':
        test += 1
    if S[x+1] =='E':
        test -= 1
    answer = min (answer,test)

print(answer)

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

print(min(a))

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

w = 0
e = S.count("E")
ans = e+w
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()


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

ans = n
for i in range(n):
    if i == 0:
        a = WE[n-1][1]-WE[0][1]
    elif i == n-1:
        a = WE[n-2][0]
    else:
        a = WE[i-1][0] + WE[n-1][1]-WE[i][1]
    ans = min(a,ans)

print(ans)

Solution (Python):
n=int(input())
s=input()
num_s=[]
value=[]
left=0
for f_s in s:
    if(f_s=="E"):
        num_s.append(0)
    else:
        num_s.append(1)
right=sum(num_s[1:])
len_s=n-1
value.append(len_s-right)
for p in range(1,n):
    left+=num_s[p-1]
    right-=num_s[p]
    len_s-=1
    value.append(left+len_s-right)
print(min(value))

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

Solution (Python):
import numpy as np
N = int(input())
S = np.array(list(input()))
n = []
for i in range(N):
    if S[i] == 'E':
        S[i] = 1
    else:
        S[i] = 0
S = np.array(S, dtype=int)
S = np.cumsum(S)
last = S[N-1]
n.append(last-S[0])
for i in range(1, N):
    ls = i - S[i-1]
    rs = last - S[i]
    n.append(ls+rs)
print(min(n))

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

a = [(0,0) for _ in range(n + 1)]
b = []
for i in range(n):
    if s[i] == "W":
        a[i+1] = (a[i][0] + 1,a[i][1])
    else:
        a[i+1] = (a[i][0],a[i][1] + 1)
    b.append(a[i+1][1]-a[i+1][0])
    
ans = s[0:b.index(max(b))].count('W') + s[b.index(max(b))+1:].count('E')
print(ans)

Solution (Python):
def solve(s):
    n = len(s)
    w = [0]*n
    e = [0]*n

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

    tot = [x+y for x, y in zip(w, e)]
    return min(tot)


input()
print(solve(input()))

Solution (Python):

import sys



def solve(lines):
    N = int(lines.pop(0))
    S = lines.pop(0)
    RS = S[::-1]
    S = 'x' + S
    RS = 'x' + RS
    slist = []
    rslist = []
    ss = 0
    rss = 0
    for i in range(N):
        if S[i] == 'W':
            ss += 1
        if RS[i] == 'E':
            rss += 1
        slist.append(ss)
        
        rslist.append(rss)
    
    
    
    rslist.reverse()
    print(min([s + r for s, r in zip(slist, rslist)]))






lines = [x.strip() for x in sys.stdin.readlines()]




solve(lines)

Solution (Python):
N=int(input())
stringth =input()
back = None
ans = len(stringth)
REcount = stringth.count("E")
LWcount = 0
for now in stringth:
    if back == "W":
        LWcount += 1
    if now == "E":
        REcount -=1
    if ans > LWcount+REcount:
        ans = LWcount+REcount
    back = now
print(ans)

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

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

for k in range(1, n):
    now = s[k-1]
    next = s[k]
    num += 1 if now == 'W' else 0
    num -= 1 if next == 'E' else 0
    ans = min(ans, num)

print(ans)

Solution (Python):
N = int(input())
S = input()
w = 0
e = S.count("E")
ans = e + w
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()
rev_counter = []
rev_num = s.count("E")
if s[0] == "E":
    rev_num -= 1
rev_counter.append(rev_num)
for i in range(1, n):
    if s[i] == "E" and s[i-1] == "E":
        rev_num -= 1
    elif s[i] == "W" and s[i-1] == "W":
         rev_num += 1
    rev_counter.append(rev_num)
print(min(rev_counter))

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

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

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

cnt=ans

for i in range(n):
    if s[i:i+2]=='WW':
        cnt=cnt+1
    elif s[i:i+2]=='EE':
        cnt=cnt-1
    else:
        0
        
    
    ans=min(ans,cnt)
print(ans)

Solution (Python):
def main():
    N = int(input())
    S = input()
    left = 0
    right = S[1:].count('E')
    ans = right
    for n in range(1, N):
        if S[n - 1] == 'W':
            left += 1
        if S[n] == 'E':
            right -= 1
        ans = min(ans, left + right)
    print(ans)


main()

Solution (Python):
import numpy as np
n = int(input())
s = np.array([True if i=='E' else False for i in input()])
t = np.array([False if i else True for i in s])
t = np.cumsum(t)
t = np.hstack(([0], t[1:]))
s = np.cumsum(s[::-1])[::-1]
s = np.hstack((s[1:], [0]))
print(np.min(s+t))

Solution (Python):
class EList(list):
    def accumulation_sum(self):
        if self.__len__() > 1:
            for i in range(self.__len__() - 1):
                self[i + 1] += self[i]
        return self

    def back_accumulation_sum(self):
        if self.__len__() > 1:
            for i in range(self.__len__() - 1, 0, -1):
                self[i - 1] += self[i]
        return self


if __name__ == '__main__':
    N = int(input())
    S = [i for i in input()]
    left = EList(1 if i == 'W' else 0 for i in S)
    right = EList(1 if i == 'E' else 0 for i in S)
    left.accumulation_sum()
    right.back_accumulation_sum()

    ans = min(map(lambda l, r:
                  l + r - 1
              , *(left, right)))
    print(ans)

Solution (Python):
N, S = int(input()), input()
countw, counte = 0, S.count("E")

ans = 3 * 10 ** 5
for s in S:
    if s == "W":
        ans = min(ans, counte + countw)
        countw += 1
    elif s == "E":
        counte -= 1
        ans = min(ans, counte + countw)
print(ans)

Solution (Python):

n = int(input())
s = input()


en = s.count('E')

ans = en if s[0]=='W' else en -1


wc = 0
for i in range(1,len(s)):
    if s[i-1] == 'W':
        wc += 1
    else:
        en -= 1


    ans = min(ans, en + wc - (1 if s[i] == 'E' else 0 ) )

print(ans)

Solution (Python):
N = int(input())
S = [1 if l == 'E' else 0 for l in list(input())]

for i in range(1,N):
    S[i] += S[i-1]
S.insert(0,0)

ans = N
for i in range(N):
    diff = i - S[i] + S[-1] - S[i+1]
    if ans > diff:
        ans = diff

print(ans)

Solution (Python):
import numpy

n = int(input())
s = input()

li = numpy.asarray([1 if i == 'W' else 0 for i in s])
w = numpy.cumsum(li)
e = numpy.cumsum(1 - li[::-1])[::-1]

su = w + e
ans = min(su) - 1

print(ans)

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

def integral(li, query):
    res = [0]
    for i in li:
        cnt = res[-1] + (1 if i == query else 0)
        res.append(cnt)
    return res[1:]

w = integral(s, 'W')
e = integral(s[::-1], 'E')[::-1]

su = [x + y for x, y in zip(w, e)]
ans = min(su) - 1


print(ans)

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

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

ans = 10**9
for i in range(N):
    ans = min(ans, left[i]+right[i])

print(ans)

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

e = [0]
w = [0]

te, tw = 0, 0
for i in range(N):
    if s[i] == 'E':
        te += 1
    else:
        tw += 1
    e.append(te)
    w.append(tw)

ans = N
for leader_idx in range(N):
    t = 0
    t += (e[N] - e[leader_idx + 1])
    t += (w[leader_idx] - w[0])
    ans = min(ans, t)
print(ans)

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

L = [1 if s == 'W' else 0 for s in S]
R = [1 if s == 'E' else 0 for s in S][::-1]

for i in range(1, N):
    L[i] += L[i-1]
    R[i] += R[i-1]

L = [0] + L[:-1]
R = R[:-1][::-1] + [0]

print(min([L[i] + R[i] for i in range(N)]))

Solution (Python):
N = int(input())
S = input()
tmpval = sum([1 if S[i]=='E' else 0 for i in range(1, N)])
tmpmin = tmpval
for i in range(1, N):
    if S[i-1] == 'W':
        tmpval += 1
    if S[i] == 'E':
        tmpval -= 1
    tmpmin = min(tmpmin, tmpval)
print(tmpmin)

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

for i in range(N):
    if S[i] == "E":
        eastcount[i+1] = eastcount[i] + 1
    else:
        eastcount[i+1] = eastcount[i]
ans = 1000000
ans = min(ans, eastcount[N] - eastcount[1])
ans = min(ans, N - 1 - eastcount[N-1])
for i in range(1, N):
    ans = min(ans, (i-1 - eastcount[i-1]) + (eastcount[N] - eastcount[i]))
print(ans)

Solution (Python):
N = input()
S = input()
L = [0]+[]
for x in S:
    if x == 'W':
        L.append(0)
    else:
        L.append(1)
for i in range(len(L)-1):
    L[i+1] += L[i]
ans =[]
for i in range(len(L)-1):
    cnt = i - L[i]
    try :cnt += L[len(L)-1] - L[i+1]
    except : break
    ans.append(cnt)
print(min(ans))

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

ruiseki_count_w = [0]*n
ruiseki_count_w[0] = 1 if s[0]=="W" else 0

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


ans=300000

for i in range(n):
  
  if i ==0:
    ans = min(n-1-(ruiseki_count_w[n-1]-ruiseki_count_w[0]),ans)
  elif i ==n:
    ans = min(ruiseki_count_w[n-1],ans)
  else:
    left_side_w = ruiseki_count_w[i-1]
    rihgt_side_e = (n-1-i)-(ruiseki_count_w[n-1]-ruiseki_count_w[i])
    ans = min(left_side_w+rihgt_side_e,ans)

print(ans)

Solution (Python):
N = int(input())
S = list(input())
numS = [0 for i in range(N)]
sumS = [0 for i in range(N)]
for i in range(N):
    if S[i] == 'E':
        numS[i] = 0
    else:
        numS[i] = 1

for i in range(N):
    sumS[i] = sumS[i-1] + numS[i]

minS = (N-1) - (sumS[N-1]-sumS[0])
for i in range(1,N):
    minS = min(minS,sumS[i-1]+(N-i-1)-(sumS[N-1]-sumS[i]))
print(minS)

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):
import sys

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    S = ii(str)
    S = [1 if s == "W" else -1 for s in S]
    pivot = np.argmin(np.cumsum(S))
    E = S[:pivot].count(1)
    W = S[pivot+1:].count(-1)
    return E + W


if __name__ == "__main__":
    print(solve())

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


min = N


left = 0    
right = 0   
for i in S:
    if i =='E':
        right+=1

if S[0]=='E':
    right -=1

min = right

for i in range(1,N):
    if S[i-1]=='W':
        left += 1
    if S[i]=='E':
        right -= 1
    if min>left+right:
        min = left + right

print(min)

Solution (Python):
N = int(input())
S = input()
ww=0
ee=0
for i in S:
    if i=='E':
        ee+=1
res=[]
for i in S:
    if i=='E':
        ee-=1
    count=ww+ee
    res.append(count)
    if i=='W':
        ww+=1
print(min(res))

Solution (Python):


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

    m = r = S[1:].count('E')
    for i in range(1, N):
        s = S[i - 1: i + 1]
        if s == 'EE':
            r -= 1
        elif s == 'WW':
            r += 1
        m = min(m, r)

    print(m)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
S = input()
w_num = S.count('W')
e_num = S.count('E')
a = 0
b = e_num
amin = n+1
if(len(set(S)) == 1):
    print(0)
else:
    for i in S:
        if(i == 'E'):
            b -= 1
        else:
            a+=1
        if(a+b < amin):
            amin = a+b
    print(min(amin, w_num, e_num))

Solution (Python):
n = int(input())
a = input()
m = mi= a.count("E")
for i in range(n):
    if a[i] == "W" : m+=1
    else : m-=1
    mi = min(mi,m) 
print(mi)

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

ans = S[1:].count('E')
left = 0
right = ans

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

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

r = [0 for i in range(n)]
l = [0 for i in range(n)]

for i in range(1,n):
    l[i]+=l[i-1]
    r[n-i-1]+=r[n-i]

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

print(ans)

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

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

Solution (Python):
N = int(input())
s = list(input())
sum = [0]*(N+1)

for i in range(1,N+1):
    if s[i-1] == 'W':
        sum[i] = sum[i-1]+1
    else:
        sum[i] = sum[i-1]
    
for i in range(1,N+1):
    temp = sum[i-1] + (N-i - (sum[N]-sum[i]))
    if i == 1:
        ans = temp
    elif temp < ans:
        ans = temp
    

print(ans)

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

w = 0
e = s[1:].count('E')

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

print(ans)

Solution (Python):
from collections import deque

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

count = deque()

left = {'W':0, 'E':0}
w = S.count('W')
right = {'W':w, 'E':N-w}

for i in range(N):
    base = S[i]
    right[base] -= 1
    count.append(left['W']+right['E'])
    left[base] += 1
print(min(count))

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

min_count = N

left = {'W':0, 'E':0}
w = S.count('W')
right = {'W':w, 'E':N-w}

for i in range(N):
    base = S[i]
    right[base] -= 1
    min_count = min(left['W']+right['E'], min_count)
    left[base] += 1
print(min_count)

Solution (Python):
def we():
    input()
    s=input()
    cnt=s.count('E')
    ans=s.count('E')
    for i in s:
        if i =='W':
            cnt+=1
        else:
            cnt-=1
            ans=min(ans,cnt)
    return (min(cnt,ans))
print(we())

Solution (Python):
def we(s):
    cnt=s.count('E')
    ans=s.count('E')
    for i in s:
        if i =='W':
            cnt+=1
        else:
            cnt-=1
            ans=min(ans,cnt)
    return (min(cnt,ans))
input()
print(we(input()))

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

cnt = [[0, 0] for _ in range(N)]

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

if S[-1] == 'E':
    cnt[-1][1] = 1

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

count = float('inf')
for i in range(N):
    count = min(count, cnt[i][0] + cnt[i][1])

print(count - 1)

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

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

print(ans)

Solution (Python):
n=int(input())
s=input()
lw=0
re=s.count('E')
l=[]
for i in range(n):
  if s[i]=='W':
    x=lw+re
    lw+=1
  else:
    x=lw+(re-1)
    re-=1
  l.append(x)
print(min(l))

Solution (Python):
N = int(input())
S = [i for i in input()]
ans = S.count("E")
cnt = S.count("E")

for i in S:
  if i == "W":
    cnt += 1
  elif i == "E":
    cnt -= 1
    ans = min(ans,cnt)
  
print(ans)

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

C = 0
for i in range(1, N):
  if S[i] == "E":
    C += 1
    
A = C
for i in range(1, N):
  if S[i-1] == "W":
    C += 1
  if S[i] == "E":
    C -= 1
  if A > C:
    A = C
    
print(A)

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



ruiseki=[[0 for i in range(2)] for j in range(N)]

for i in range(0,N):
    if i!=0:
        if S[i]=='W':
            ruiseki[i][0]=1
        else:
            ruiseki[i][1]=1
    else:
        if S[i]=='W':
            ruiseki[i][0]=1
        else:
            ruiseki[i][1]=1

    ruiseki[i][0]+=ruiseki[i-1][0]
    ruiseki[i][1]+=ruiseki[i-1][1]







mn = 10**6

for i in range(0,N):
    if i>=1 and i<=N-2:
        m = ruiseki[i-1][0]+ruiseki[N-1][1]-ruiseki[i][1]
    elif i==0:
        m = ruiseki[N-1][1]-ruiseki[0][1]
    else:
        m = ruiseki[N-2][0]

    if mn>m:
        mn = m


print(mn)