Problem:


Score : 300 points

Problem Statement

There are N people standing in a row from west to east.
Each person is facing east or west.
The directions of the people is given as a string S of length N.
The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader.
Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized.
Find the minimum number of people who have to change their directions.

Constraints

2 \leq N \leq 3 \times 10^5

|S| = N

S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.

Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader.
Then, the first person from the west needs to face east and has to turn around.
The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case.
It is not possible to have 0 people who have to change their directions, so the answer is 1.

Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3



Sample input:
5
WEEWW


Sample output:
1

Submission status: Runtime Error

Solution code:
s = input().split()

WtoE = []
wtoec = 0
EtoW = []
etowc = 0

for c in s[1]:
    if c == "W":
        wtoec = wtoec + 1
    WtoE.append(wtoec)

for c in s[1][::-1]:
    if c == "E":
        etowc = etowc + 1
    EtoW.append(etowc)
EtoW.reverse()
print(WtoE)
print(EtoW)

a = []
for i in range(0, int(s[0])):
    ans = WtoE[i] + EtoW[i]
    if WtoE[i] != 0 and s[1][i] == "W" or EtoW[i] != 0 and s[1][i] == "E":
        ans = ans - 1

    a.append(ans)

print(min(a))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
t = []
x = 0
y = s.count('E')
for ch in s:
    if ch == 'E':
        y -= 1
    t.append(x+y)
    if ch == 'W:
        x += 1
print(min(t))

Submission status: Runtime Error

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

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

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

res = [0 for _ in range(n)]

for i in range(n):
    res = w[i] + e[i]

print(min(res))

Submission status: Runtime Error

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

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

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

res = [0 for _ in range(n)]

for i in range(n):
    res = w[i] + e[i]

print(min(res))

Submission status: Runtime Error

Solution code:
N = int(raw_input())
S = list(raw_input())
D = [[0, 0]]
W = [0] * (N+2)
E = [0] * (N+2)
for i in range(N):
 if S[i] == 'W':
  W[i+1] = W[i]+1
for i in range(N)[::-1]:
 if S[i] == 'E':
  E[i] = E[i+1]+1
ans = N+1
for w, e in zip(W, E):
  if w+e < ans:
  ans = w+e
print ans

Submission status: Runtime Error

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

res = 0
for i in range(n):
    if len(s) < 2:
        break
    elif arr.pop(0) != arr.pop(-1):
        res += 1

print(res)

Submission status: Runtime Error

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

# preparation
max = 400000
count = 0

# main
for i in range(1, len(s)):
  count = 0
  a = (s[i:n])
  b = (s[i])
  c = (s[0:i-1])
  print (c, end=" ")
  print (b, end=" ")
  print (a)
  
  count += a.count('E')
  count += c.count('W')
  
  if (max > count):
    max = count
print (max)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
lst = []
if N%2 == 0:
    n = N/2
else:
    n = (N-1)/2
w = S.count("W")
e = S.count("E")
if w>e:
    for i in range(1,n):
        W = S[0:i-1]
        E = S[i:n]
        Wchange = W.count("W")
        Echange = E.count("E")
        total = Wchange + Echange
        lst.append(total)
        i = i+1
    print(min(lst))
else:
    for i in range(n,N):
        W = S[0:i-1]
        E = S[i:N]
        Wchange = W.count("W")
        Echange = E.count("E")
        total = Wchange + Echange
        lst.append(total)
        i = i+1
    print(min(lst))

Submission status: Runtime Error

Solution code:
"""


N = input()
S = raw_input()

count_l = [0]
count_r = [0]


for i in range(N):
    if S[i] == 'W':
        tmp = count_l[i] + 1
    else:
        tmp = count_l[i]
    count_l.append(tmp)
    if S[N-1-i] == 'E':
        tmp = count_r[i] + 1
    else:
        tmp = count_r[i]
    count_r.append(tmp)

count_r.reverse()

Ans = count_l

for i in range(N+1):
    Ans[i] = count_r[i] + count_l[i]

print(min(Ans))

Submission status: Runtime Error

Solution code:
N = int(input())
str = input()
str_list=list(str)
pre_list=[]
length_list=[]
answer_list=[]
answer_first=0
answer_second=0
for i in range (N-1):
  if str_list[i] != str_list[i+1]:
    pre_list.append(i)
    if pre_list == []:
      print("0")
      sys.exit()
    lenlen=len(pre_list)
for i in range (lenlen-1):
  length_list.append(pre_list[i+1]-pre_list[i])
  if lenlen=1:
    for i in range (N):
      if str_list[i] == "E":
        seki = -1
        else:
          seki = 1
          point = (i-pre_list[0])*seki
          if point<0:
            answer_first = answer_first+1
    print(answer_first)
    sys.exit()
basyo = length_list.index(max(length_list))
basyo2_kouho1 = pre_list[basyo]
basyo2_kouho2 = pre_list[basyo+1]
for i in range (N):
  if str_list[i] == "E":
    seki = -1
  else:
    seki = 1
  point = (i-basyo2_kouho1)*seki
  if point<0:
    answer_first = answer_first+1
for i in range (N):
  if str_list[i] == "E":
    seki = -1
  else:
    seki = 1
  point = (i-basyo2_kouho2)*seki
  if point<0:
    answer_second = answer_second+1
if answer_first>=answer_second:
  print(answer_second)
else:
  print(answer_first)

Submission status: Runtime Error

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

# numはstartからendまで
def mozisu(text, num):
    ans = 0
    ans += text[:num].count("W")
    ans += text[num+1:].count("E")
    return ans

li = []
start = S.find("W")
end = S.rfind("E")
if end < start:
    print(0)
else:
    for i in range(end - start + 1):
        if S[i+start] != S[i+start-1]:
            li.append(mozisu(S, i+start))
        if 0 in li:
            break
    print(min(li))

Submission status: Runtime Error

Solution code:
def aa(arr, num):
    if len(arr) < 2:
        return num
    ge = num
    if arr[0] != arr[-1]:
            ge += 1
    return aa(arr[1:-1], ge)


input()

print(aa(list(input()), 0))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
lst = []
if N%2 == 0:
    n = N/2
else:
    n = (N-1)/2
w = S.count("W")
e = S.count("E")
if w>e:
    for i in range(1,n):
        W = S[0:i-1]
        E = S[i:n]
        Wchange = W.count("W")
        Echange = E.count("E")
        total = Wchange + Echange
        lst.append(total)
        i = i+1
    print(min(lst))
else:
    for i in range(n,N):
        W = S[0:i-1]
        E = S[1:n]
        Wchange = W.count("W")
        Echange = E.count("E")
        total = Wchange + Echange
        lst.append(total)
        i = i+1
    print(min(lst))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import os
import sys

def main():
    list_length = int(input())
    result = list_length
    global west_all = data.count("W")
    data = list(input())
    for i in range(list_length):
        c = l_count(i, data)
        if c is None:
            continue
        elif result > c:
            result = c
    print(result)

def l_count(i, data):
    if i > 1 and data[i - 1] != "E":
        return None
    west = data[:i].count("W")
    east = len(data[i:]) - (west_all - west)
    return west + east

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

SUM = 10000000
#1番目は終わり
W_sum = 0
E_sum = S[-(N-1):].count('E')

#両サイドからカウントする
#文字が変化したらカウント
for i in range(1, N):#i番目を選ぶ
    left = S[:i]
    right = S[-(N-(i))+1:]
    print(left)
    print(right)
    if left[i-1] == 'W':
        W_sum += 1
    if right[0] == 'E':
        E_sum -= 1
    
    sumsum = W_sum + E_sum
    if sumsum < SUM:#更新
        SUM = sumsum

print(SUM)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
import numpy
ans = 1000000
for i in range(n):
    s1 = np.array([0 if x=="E" else 1 for x in s[:i]])
    t1 = np.zeros(i)
    s2 = np.array([0 if x=="W" else 1 for x in s[i+1:]])
    t2 = np.zeros(n-i-1)
    c = sum(s1-t1)+ sum(s2-t2)
    ans = min(ans, c)
print(int(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
str = input()
str_list=list(str)
pre_list=[]
length_list=[]
answer_list=[]
answer_first=0
answer_second=0
for i in range (N-1):
  if str_list[i] != str_list[i+1]:
    pre_list.append(i)
    lenlen=len(pre_list)
for i in range (lenlen-1):
  length_list.append(pre_list[i+1]-pre_list[i])
basyo = length_list.index(max(length_list))
basyo2_kouho1 = pre_list[basyo]
basyo2_kouho2 = pre_list[basyo+1]
for i in range (N):
  if str_list[i] == "E":
    seki = -1
  else:
    seki = 1
  point = (i-basyo2_kouho1)*seki
  if point<0:
    answer_first = answer_first+1
for i in range (N):
  if str_list[i] == "E":
    seki = -1
  else:
    seki = 1
  point = (i-basyo2_kouho2)*seki
  if point<0:
    answer_second = answer_second+1
if answer_first>=answer_second:
  print(answer_second)
else:
  print(answer_first)

Submission status: Runtime Error

Solution code:
def aa(arr, num):
    if len(arr) < 2:
        return num
    ge = num
    if arr.pop(0) != arr.pop(-1):
            ge += 1
    return aa(arr, ge)


input()
s = list(input())

print(aa(s, 0))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
ma = max(A)
xma = 1
for i in range(20):
    xma = xma  * 2
    if xma >= ma:
        break
ans = 0
a = 0
max_flag = False
for l in range(N):
    a = A[l]
    x = A[l]
    if max_flag:
       ma = max(A[l:N])
       xma = 1
       for i in range(20):
           xma = xma  * 2
           if xma >= ma:
               break
    if ma == A[l]:
        max_flag =True
    for j in range(l+1,N):
        a += A[j]
        x = x ^ A[j]
        if a == x:
            ans += 1
        elif a >= xma:
            break
print(ans + N)

Submission status: Runtime Error

Solution code:
def aa(arr, num):
    if len(arr) < 2:
        return num
    ge = num
    if arr.pop(0) != arr.pop(-1):
            ge += 1
    return aa(arr, ge)


n = int(input())
s = list(input())
 
print(aa(s, 0))

Submission status: Runtime Error

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

ms = n
for i in range(n):
	c = s[0:i].count('W')
	c += s[i+1:n].count('E')
	print(s[0:i], s[i+1:n], c)
	if(ms >= c):
		ms = c
	else:
		break

print(ms)

Submission status: Runtime Error

Solution code:
N=int(input())
S=str(input())
Slist=list(S)
c=[]
for i in xrange(0,N-1):
    slice=S[0:i]
    slice=S[i+1:N]
    a=S[0:i].count('W')
    b=S[i+1:N].count('E')
    c.append(a+b)
print(min(c))

Submission status: Runtime Error

Solution code:
import sys

size = input()
str = input()

str = str.replace("W", "0")
str = str.replace("E", "1")

des = list(map(int, str))
ans = size

for i in range(size):
    num = 0
    for j in range(0, i):
        if des[j] == 0:
            num = num + 1
    for j in range(i+1, size):
        if des[j] == 1:
            num = num + 1
    if num < ans:
        ans = num
    
print(ans)

Submission status: Runtime Error

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

list=[]
for i in range(n):
    a=0
    for j in range(i):
        if s[j]=="W":
            a+=1
    for k in range(n-i):
        if s[k+i+1]=="E":
            a+=1
    list.append(a)

f=max(list)

print(f)

Submission status: Runtime Error

Solution code:
rom collections import Counter
N = int(input())
s= input()

D = N
for i in range(N):
    L,R = s[:i],s[i+1:]
    n1,n2 = Counter(L),Counter(R)
    num = n1["W"] + n2["E"]
    if num < D:
        D = num
print(D)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
S = input()
S_01 = []
for c in _S:
    num = 0
    if (c == "E"):
        num = 1
    S_01.append(num)

n_list = []
S_01 = np.array(S_01)

num += np.sum(S_01[1:] == 1)
n_list.append(num)
for i in range(1, N-2):
    num = 0
    num += np.sum(S_01[:i-1] == 0) +  np.sum(S_01[i+1:] == 1)
    n_list.append(num)
num += np.sum(S_01[:-1] == 0)
n_list.append(num)

if (len(n_list) > 0):
    print(min(n_list))
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys

size = input()
str =　input()

des = list(str)
ans = size

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

Submission status: Runtime Error

Solution code:
import math
import numpy as np

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

nW = [0 for i in range(N)]
nE = [0 for i in range(N)]

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

if S[N-1] == 'E':
    nE[N-1] = 1

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

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

if nE[0] > nW[N-1]:
    count = nW[n-1]
else:
    count = nE[0]

for i in range(1,N-1):
    if nW[i-1] + nE[i+1] < count:
        count = nW[i-1] + nE[i+1]


print(count)

Submission status: Runtime Error

Solution code:
import sys

size = input()
str =　input()

des = list(str)
ans = 12

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

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
temp_count = S.count("W")
ans_min = 10**10
count_E = N-temp_count
count_W = 0 
for i in range(N):
    if S[i] == "W":
        count_E -= 1
    else:
        count_W +=1
    if ans_min > coun_E+count_W:
        ans_min =  coun_E+count_W
print(ans_min)

Submission status: Runtime Error

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

ans = N

if S.count("W")>0:
    Wbegin = S.index("W")

if S.count("E")>0:
    Ebegin = S[::-1].index("E")


for i in range(max(0,Wbegin-1), N-max(1, Ebegin)+1):
    ans = min(S[0:i].count("W")+S[i+1:].count("E"), ans)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
str = s[1:]
a = 0
for i in str:
  if i == "E":
    a += 1
b = a

for i in range(len(n)):
  if str[i] == "w":
  	if str[i+1] == "E":
      continue
    else:
      a += 1 
  else:
    if str[i+1] == "E"
      a -= 1
      if b > a:
        a = b
    else:
      continue
      
print(b)

Submission status: Runtime Error

Solution code:
from collections import Counter
n = int(input())
s = input()
r = dict(Counter(s))
l = {'W': 0, 'E': 0}
if s[0] == 'W':
    a = r['E']
else:
    a = r['E'] - 1
for c in s:
    if c == 'W' and r['W'] > 0:
        l['W'] += 1
        r['W'] -= 1
    if c == 'E' and r['E'] > 0:
        l['E'] += 1
        r['E'] -= 1
    if c == 'W':
        a = max(1, min(a, l['W'] + r['E'] - 1))
    else:
        a = max(1, min(a, l['W'] + r['E']))
print(a)

Submission status: Runtime Error

Solution code:
import collections
n, s = int(input()), input()
cost = 2*N
for i in range(N):
    if i == N-1:
        temp = collections.Counter(s[:i])['W']
    else:
        temp = collections.Counter(s[:i])['W']+collections.Counter(s[i+1:])['E']
    if temp<cost:
        cost = temp
print(cost)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
print(min(map(lambda x: x[0].count(
    "W") + x[1].count("E"),  [(s[:x], s[x+1:]) for x in range(n - 1)])))

Submission status: Runtime Error

Solution code:
from sys import stdin
stdin = open('sample.txt')


def main():
    N = int(stdin.readline().rstrip())
    S = stdin.readline()
    count = 0
    s1 = []
    for i, x in enumerate(S):
        if x == 'W':
            count += 1
        s1.append(count)
    count = 0
    s2 = []
    for i, x in enumerate(S[::-1]):
        if x == 'E':
            count += 1
        s2.append(count)
    s2.reverse()

    result = N
    for i in range(N):
        count = 0
        idx = i - 1
        if idx >= 0:
            count += s1[idx]
        idx = i + 1
        if idx < N:
            count += s2[idx]
        result = min(result, count)

    print(result)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(raw_input())

S = raw_input()

num = S.count('E')
score_list = []
score_list.append(num)

for i in range(N-1)
	if S[i]=='W':
		num+=1
	else:
		num-=1
	score_list.append(num)
print min(score_list)