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

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

w_fwd, e_fwd, w_bwd, e_bwd = 0, 0, 0, 0
res = len(S)
for c in S:
    if c == 'W':
        w_fwd += 1
    else:
        e_fwd += 1

for c in S:
    if c == 'W':
        w_fwd -= 1
        res = min(res, w_bwd + e_fwd)
        w_bwd += 1
    else:
        e_fwd -= 1
        res = min(res, w_bwd + e_fwd)
        e_bwd += 1

print(res)

Submission status: Accepted

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


count_list = []

prevous_count = s[1:].count("E")
current_min = prevous_count

for index in range(len(s) - 1):
    add_man = s[index]
    leader = s[index+1]

    if add_man == "W":
        prevous_count += 1
    if leader == "E":
        prevous_count -= 1
    if current_min > prevous_count:
        current_min = prevous_count


print(current_min)

Submission status: Accepted

Solution code:
def main():
    n, s = int(input()), input()

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

    p_lst = []
    for i in range(n):
        p_lst.append(w[i]+e[n]-e[i+1])

    print(min(p_lst))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    east = [0] * (N + 1)
    west = east[:]
    for i, s in enumerate(S, start=1):
        if s == 'E':
            east[i] += east[i-1] + 1
            west[i] += west[i-1]
        else:
            east[i] += east[i-1]
            west[i] += west[i-1] + 1

    people = float('inf')
    for i in range(1, N + 1):
        people = min(people, west[i-1] + east[-1] - east[i])
    print(people)

main()

Submission status: Accepted

Solution code:
N = int(input())
S = input()
res = 1000000
prev_W = 0
prev_E = S.count('E')
for i in range(N):
    if i >= 1: prev_W += S[i-1].count('W')
    prev_E -= S[i].count('E')
    res = min(res,prev_W + prev_E)
print(res)

Submission status: Accepted

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

forward_cost = []

cost = 0
for i in range(len(S)):
  if S[i] == "W":
    cost = cost + 1
  forward_cost.append(cost)

cost = 0
SR = S[::-1]
for i in range(len(S)):
  if SR[i] == "E":
    cost = cost + 1
  forward_cost[len(S)-i-1] = forward_cost[len(S)-i-1] + cost

#print(forward_cost)
print(min(forward_cost)-1)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
a = s.count("E") #1はs[0]の分
if s[0] == "E":
    a -= 1
ans = a
for i in range(n-1):
    if s[i] == "E":
        pass
    else:
        a += 1
    if s[i+1] == "E":
        a -= 1
    else:
        pass
    ans = min(ans,a)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = S[1:]
hazime = a.count("E")
tei = a.count("E")
for i in range(N-1):
  a = S[i]
  b = S[i+1]
  if a == "E" and b == "E":
    hazime += -1
    tei = min(tei,hazime)
  elif a == "E" and b == "W":
        continue
        tei = min(tei,hazime)
  elif a == "W" and b == "E":
     continue
     tei = min(tei,hazime)
  elif a == "W" and b == "W":
       hazime += 1
       tei = min(tei,hazime)
b = S[:N-1]
go = b.count("W")
tei = min(tei,go)
print(tei)

Submission status: Accepted

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

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

# print(e_count)
# print(w_count)


left_w = 0
right_e = e_count
min_cost = len(S)-1

for i in range(len(S)):
    if i > 0:
        if S[i-1] == 'W':
            left_w += 1
    if S[i] == 'E':
        right_e -= 1
    cost = left_w + right_e
    if cost < min_cost:
        min_cost = cost

print(min_cost)

Submission status: Accepted

Solution code:
N = int(input())
S = list(input())
w = 0
e = 0

lw = 0
le = 0

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

min = 100000000
for i in range(len(S)):
    temp = 0
    if S[i] == "W":
        temp = lw+e
        lw+=1
        w-=1
    else:
        temp = lw+e
        le +=1
        e -= 1
        temp-=1
    if temp<min:
        min = temp

print(min)

Submission status: Accepted

Solution code:
n=int(input())
s = input().rstrip()
numw,nume=0,0
if 'W' in s:
  numw = s.count('W')
  nume = n - numw
else:
  print(0)
  exit()
donew,donee=0,0
result = pow(10,6)
for i in range(n):
  if s[i] == 'W':
    numw -= 1
  else:
    nume -= 1
  result = min(result,(nume+donew))
  if s[i] == 'W':
    donew += 1
  else:
    donee += 1
print(result)

Submission status: Accepted

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

W = []
w_count = 0
for s in S:
    W.append(w_count)
    if s == "W":
        w_count += 1

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

mi = N
for i in range(N):
    mi = min(mi, E[i] + W[i])

print(mi)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    S = input()
    n_E = [0 for _ in range(N+1)]
    n_W = [0 for _ in range(N+1)]
    for i in range(N):
        if S[i] == "E":
            n_E[i+1] = n_E[i] + 1
            n_W[i+1] = n_W[i]
        elif S[i] == "W":
            n_E[i+1] = n_E[i]
            n_W[i+1] = n_W[i] + 1

    count = N
    for i in range(N):
        count = min(count,n_W[i] + (n_E[-1]-n_E[i+1]))

    return count

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

Submission status: Accepted

Solution code:
n=int(input())
s=list(input())
cost=s[1:].count("E")
min_cost=cost
#print(cost)
for i in range(n-1):
    if s[i]=="W":
        cost+=1
    if s[i+1]=="E":
        cost-=1
    if min_cost>cost:
        min_cost=cost
    #print(cost)
print(min_cost)

Submission status: Accepted

Solution code:
from collections import Counter
N = int(input())
S = input()

west_side = Counter()
east_side = Counter(S)


min_num = 1000000
for k in range(N):
    tmp = S[k]

    if tmp == "E":
        east_side["E"] -= 1
    else:
        east_side["W"] -= 1
    num_of_change_vector = east_side["E"] + west_side["W"]
    if min_num > num_of_change_vector:
        min_num = num_of_change_vector

    if tmp == "E":
        west_side["E"] += 1
    else:
        west_side["W"] += 1

print(min_num)

Submission status: Accepted

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

count = s[1:].count("E")
e = s[1:].count("E")
w = s[1:].count("W")
temp = []
temp.append(count)

for i in range(1,len(s)):
    count = temp[-1]
    if s[i-1] == "W":
        count += 1
    if s[i]=="E":
        count -= 1
    temp.append(count)

print(min(temp))

"""
n = input()
s = input()
l = int(n)
count = 0
for i in range(1,l):
    s1 = set(s[:i])
    s2 = set(s[i:])
    count = max(count,len(s1 & s2))
print(count)




"""

Submission status: Accepted

Solution code:
from itertools import accumulate as AC
n = int(input())
s = input()
    

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

for i in range(n):
    if s[i] == 'W':
        w[i] = 1
    else:
        e[i] = 1
        
aw = list(AC(w))
ae = list(AC(e))

ans = 1000000

for i in range(n):
    if i == 0:
        current = ae[n-1]-ae[0]
    elif 0 < i < n-1:
        current = aw[i-1]+ae[n-1]-ae[i]
    else:
        current = aw[i-1]
        
    if ans > current:
        ans = current
        

     
print(ans)

Submission status: Accepted

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

# i番目の人の左にある E, W の数
count_E = [0]
count_W = [0]

for s in S:
    if s == 'E':
        count_E.append(count_E[-1] + 1)
        count_W.append(count_W[-1])
    else:
        count_E.append(count_E[-1])
        count_W.append(count_W[-1] + 1)

ans = 10**6

for i in range(N):
    rotate_left = count_W[i]
    rotate_right = count_E[-1] - count_E[i+1]
    total = rotate_left + rotate_right
    if total < ans:
        ans = total

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
l = []
count = s[1:].count('E')
r = count
for i in range(n-1):
  if s[i] == 'W':
    count += 1
  if s[i + 1] == 'E':
    count -= 1
    if count < r:
      r = count
print(r)

Submission status: Accepted

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

e, w = 0, 0
for si in s:
    if si == 'E':
        e += 1
    else:
        w += 1

pri_e, pri_w= 0, 0
cnt_max = 0
for i in range(n):
    cnt = 0
    if s[i] == 'W':
        w -= 1
    
    cnt += pri_e
    cnt += w
    cnt_max = max(cnt, cnt_max)
    if s[i] == 'E':
        e -= 1
        pri_e += 1

print(n - (cnt_max+1))

Submission status: Accepted

Solution code:
from sys import stdin


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

Solution code:
N = int(input())
S = input()
# N = 8
# S = 'WWWWWEEE'
right = 0
left = 0
# N = 3
# for j in range(N - 1, 0, -1):
#     print(j)

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

        sum = right + left
        if sum < min: min = sum

print(min)

Submission status: Accepted

Solution code:
n = int(input())
s = input()
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":
        a = 1
    else:
        a = 0
    E[i] = E[i - 1] + a
    W[i] = W[i - 1] + 1 - a

num = float("inf")
for i in range(n):
    if i == 0:
        num = min(num, E[n - 1] - E[i])
    elif i == n - 1:
        num = min(num, W[i - 1])
    else:
        num = min(num, E[n - 1] - E[i] + W[i - 1])

print(num)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
result = S[1:].count("E")
cost = result
l0 = S[0]
for n in range(1, N):
    l1 = S[n]
    if l0 != l1:
        l0 = l1
        continue
    if l0 == "W":
        cost += 1
        continue
    cost -= 1
    if cost < result:
        result = cost

print(result)

Submission status: Accepted

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

ans = 10**6
e_list, w_list = [0], [0]
for i in range(n):
    if s[i] == 'E':
        e_list.append(e_list[i]+1)
        w_list.append(w_list[i])
    elif s[i] == 'W':
        w_list.append(w_list[i]+1)
        e_list.append(e_list[i])
e_list.append(e_list[n])
w_list.append((w_list[n]))

for leader in range(1, n+1):
    num = w_list[leader-1] + e_list[n+1]-e_list[leader]
    if num < ans:
        ans = num

print(ans)

Submission status: Accepted

Solution code:
# your code goes here
N = int(input())

direction = input()

ruiseki_W = [0]
ruiseki_E = [0]
num_W = 0
num_E = 0
ans = 1145141919
for i in range(N):
	if direction[i] == 'W':
		num_W += 1
	else:
		num_E += 1
	ruiseki_W.append(num_W)
	ruiseki_E.append(num_E)
	
for i in range(N):
	ans = min(ans,ruiseki_W[i]+(ruiseki_E[N] - ruiseki_E[i+1]))
	# print(ruiseki_W[i]+(ruiseki_E[N] - ruiseki_E[i+1]))
# print(ruiseki_W,ruiseki_E)
print(ans)

Submission status: Accepted

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

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

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


    mincount=min(count,mincount)

print(mincount)

Submission status: Accepted

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

n = S[1:].count("E")
ans = n
for i, s in enumerate(S):
    if i == 0:
        continue
    if s == "E":
        n -= 1
    if S[i-1] == "W":
        n += 1
    ans = min(ans, n)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
S = input()
a = [0 for i in range(N)]
for i in S[1:]:
    if i == "E":
        a[0] += 1
for i,s in enumerate(S[:-1]):
    if S[i] == "E" and S[i+1] == "E":
        a[i+1] = a[i] - 1
    if S[i] == "E" and S[i+1] == "W":
        a[i+1] = a[i]
    if S[i] == "W" and S[i+1] == "E":
        a[i+1] = a[i]
    if S[i] == "W" and S[i+1] == "W":
        a[i+1] = a[i] + 1

print(min(a))

Submission status: Accepted

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

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

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

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


ans=float("inf")
for i,j in zip(l,r):
	ans=min(ans,i+j)
print ans

Submission status: Accepted

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

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

print(min_ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
s=input()
w=s.count('W')
a=[0]*n
for i in range(n):
    p=1 if s[i]=='W' else 0
    if i==0:a[0]=p
    else:a[i] = a[i-1]+p
r=min(min(n-i-1-w+2*a[i]for i in range(n)),n-w)
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
s = input()
ans = []
for i in range(n):
    if i == 0:
        counter = 0
        for k in range(1,n):
            if s[k] == 'E':
                counter += 1
        ans.append(counter)
    else:
        counter = ans[i-1]
        if s[i-1] == 'W' and s[i] == 'W':
            counter += 1
            ans.append(counter)
        elif s[i-1] == 'E' and s[i] == "E":
            counter += -1
            ans.append(counter)
        else:
            ans.append(counter)
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
from collections import Counter
from pprint import pprint as pp

YN = {True: 'Yes', False: 'No'}
ri = lambda: int(input())
ria = lambda: list(map(int, input().split()))
rian = lambda n: [ria() for _ in range(n)]
rs = lambda: input()
rsa = lambda: input().split()
rsn = lambda n: [rs() for _ in range(n)]

N = ri()
S = input()
cnt = {'E': [0, S.count('E')],
       'W': [0, S.count('W')]}

ret = N
for c in S:
  cnt[c][1] -= 1
  ret = min(ret, cnt['W'][0] + cnt['E'][1])
  cnt[c][0] += 1
print(ret)

Submission status: Accepted

Solution code:
N = int(input())
S = input()
pos = 0
minpos = 0
mini = 0
for i in range(N):
    if S[i]=="W":
        pos += 1
    else:
        pos -= 1
    if pos < minpos:
        minpos = pos
        mini = i

s1 = S[0:mini].count("W")
s2 = S[mini+1:N].count("E")
print(s1+s2)

Submission status: Accepted

Solution code:
INF = 10 ** 20
n = int(input())
s = input()

w_cum = [0]
e_cum = [0]

w_acc = 0
e_acc = 0
for i in range(n):
  if s[i] == "W":
    w_acc += 1
  else:
    e_acc += 1
  w_cum.append(w_acc)
  e_cum.append(e_acc)

ans = INF
for i in range(1, n + 1):
  ans = min(ans, w_cum[i - 1] + e_cum[-1] - e_cum[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = list(map(lambda x: 1 if x=='E' else 0, list(input())))

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

S.insert(0, 0)
ans = n
for i in range(1, n+1):
    c = i-S[i-1]-1
    # print(c)
    c += S[-1]-S[i]
    ans = min(ans, c)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())

s = list(map(str,input().split()))

e = l.count("E")
w = n - e

if e >= w:
    print(w)
else:
    print(e)

Submission status: Runtime Error

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
S = [_ for _ in input()]

left_w = 0
right_e = S[1:].count('E')
min_sum = left_w + right_e

for i in range(N-1):
    if S[i] == 'W':
        left_w -= 1
    if S[i+1] == 'E':
        right_e += 1
    if min_sum < left_w + right_e:
        min_sum = left_w + right_e
        ans = i
print(ans)

Submission status: Runtime Error

Solution code:
e = S.count("E")
cnt = e
for i in S:
    if i == "E":
        cnt -= 1
    else:
        cnt += 1
    e = min(e, cnt)
print(e)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
a = s.count("E")
c = a 

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

print(a)

Submission status: Runtime Error

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

for i in xrange(N):
    count = min(S[0:i].count('W') + S[i+1:N].count('E'), count)
print(count)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
	N=int(input())
	S=input()
	ans=N
	cnt=0
	for i in range(N):
		cnt+=S[:i].count("W")
		cnt+=S[i:].count("E")
		print(S[:i],S[i:],cnt)
		ans=min(ans,cnt)
		cnt=0
	print(ans)
if __name__=='__main__':
	main()

Submission status: Runtime Error

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

sum_W = [0] * (N+1)
sum_E = [0] * (N+1)

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

answers = []
for i in range(N):
    answer = sum_W[i] + (sum_E[N+1] - sum_E[i+1])
    answers.append(answer)

print(min(answers))

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
S = list(input())
cnt = 0
cntW = 0
cntE = 0
n = N-1

index = 0
for i in range(N-1):
    if S[i] != S[i+1]:
        cnt += 1
        index = i
i = 0
if cnt == 1:
    m = min(index,n-index)
    print(m)
    exit()
else:
    while i != n-1:
        if S[n] == 'E':
            cntE += 1
        if S[i] == 'W':
            i += 1
            n -= 1
            cntW += 1
        else:
            i += 1
print(cntW+cntE)

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
S = list(input())
cnt = 0
cntW = 0
cntE = 0
n = N-1
i = 0

index = 0
for i in range(N):
    if S[i] == 'W' and S[i+1] == 'E':
        cnt += 1
        index = i
if cnt == 1:
    m = min(index,n-index)
    print(m)
    exit()
else:
    while i != n-1:
        if S[n] == 'E':
            cntE += 1
        if S[i] == 'W':
            i += 1
            n -= 1
            cntW += 1
        else:
            i += 1
print(cntW+cntE)

Submission status: Runtime Error

Solution code:
from collections import Counter

def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	if n == 2:
		print(1 if len(Counter(list(s)).keys()) == 2 else 0)
		return
	for i in range(n+1):
		if i == 0:
			v = sum_e[-1] - sum_e[i]
			ans = min(ans, v)
		else:
			v = sum_w[i-1] + (sum_e[-1] - sum_e[i+1])
			ans = min(ans, v)
	print(ans)
resolve()

Submission status: Runtime Error

Solution code:
def resolve():
	n = int(input())
	s = input()
	sum_e = [0]*(n+1)
	sum_w = [0]*(n+1)
	for i in range(n):
		ev = 1 if s[i] == 'E' else 0
		wv = 1 if s[i] == 'W' else 0
		sum_e[i+1] = sum_e[i] + ev
		sum_w[i+1] = sum_w[i] + wv
	ans = float('inf')
	if n == 2:
		print(1 if len(Counter(list(s)).keys()) == 2 else 0)
		return
	for i in range(n+1):
		if i == 0:
			v = sum_e[-1] - sum_e[i]
			ans = min(ans, v)
		else:
			v = sum_w[i-1] + (sum_e[-1] - sum_e[i+1])
			ans = min(ans, v)
	print(ans)
resolve()

Submission status: Runtime Error

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

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

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

for i in range(n):
    ans.append(w_e[i] + e_w[i])

print(min(ans))

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
t1 = [0]*n
t2 = [0]*n
for i in range(1, n):
  t1[i] = t1[i-1] + s[i-1] == 'W'
  t2[n-i-1] = t2[n-i] + s[n-i] == 'E'
ans = n
for i in range(n):
  ans = min(ans, t1[i] + t2[i])
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())
max = N - 1
for i in range(N-1):
  cnt = S[:i].count('W') + S[i+1:].count('E')
  if cnt < min:
    min = cnt
print(min)

Submission status: Runtime Error

Solution code:
N = int(input())
S = list(input())
max = N - 1
for i in range(N):
  cnt = S[:i].count('W') + S[i+1:].count('E')
  if cnt < min:
    min = cnt
print(min)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;

    vector<int> count_e(n, 0);
    vector<int> count_e_sum(n + 1, 0);
    vector<int> count_w(n, 0);
    vector<int> count_w_sum(n + 1, 0);

    for (int i = 0; i < n; i++)
    {
        if (s[i] == 'E')
        {
            count_e[i]++;
        }
        else if (s[i] == 'W')
        {
            count_w[i]++;
        }
        count_e_sum[i + 1] = count_e_sum[i] + count_e[i];
        count_w_sum[i + 1] = count_w_sum[i] + count_w[i];
    }

    vector<int> ans(n);
    for (int i = 0; i < n; i++)
    {
        ans[i] = count_w_sum[i] + count_e_sum[n] - count_e_sum[i + 1];
    }

    int answer = *min_element(begin(ans), end(ans));

    cout << answer;
}

Submission status: Runtime Error

Solution code:
# 累積和の手法をとる
# 簡単にいうと要素が ある区間の和 であるようなリストを先に作成しておき
# 必要な時に取り出すという手法である
# 同じ計算過程を何回も繰り返す（上の解法）ことなく結果がでる
N = int(input())
S = input()
w = [0 for _ in range(N)]       # Wを向いている人の番号に１、逆の方角の人に０を割り当てる
sw = [0 for _ in range(N+1)]    # リストwの累積和のリスト、つまり sw[i] は 0~i-1 でWを向いている人の累積和
e = [0 for _ in range(N)]       # Eを向いている人の番号に１、逆の方角の人に０を割り当てる
se = [0 for _ in range(N+1)]    # リストeの累積和のリスト
l = []

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

# 累積和を格納したリストを求める
for i in range(N):
    sw[i+1] = sw[i] + w[i]
    se[i+1] = se[i] + e[i]

# 本計算
for i in range(N):      # i 番目がリーダーだとする
    counter = sw[i] + (se[N+1] - se[i+1])
    l.append(counter)

print(min(l))

Submission status: Runtime Error

Solution code:
# 累積和の手法をとる
# 簡単にいうと要素が ある区間の和 であるようなリストを先に作成しておき
# 必要な時に取り出すという手法である
# 同じ計算過程を何回も繰り返す（上の解法）ことなく結果がでる
N = int(input())
S = input()
w = [0 for _ in range(N)]       # Wを向いている人の番号に１、逆の方角の人に０を割り当てる
sw = [0 for _ in range(N)]      # リストwの累積和のリスト
e = [0 for _ in range(N)]       # Eを向いている人の番号に１、逆の方角の人に０を割り当てる
se = [0 for _ in range(N)]      # リストeの累積和のリスト
l = []

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

# 累積和を格納したリストを求める
for i in range(N-1):
    sw[i+1] = sw[i] + w[i]
    se[i+1] = se[i] + e[i]

# 本計算
for i in range(N):
    if i != 0:
        counter = sw[i-1] + (se[N] - se[i])
    else:
        counter = se[N] - se[i]
    l.append(counter)

print(min(l))

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
l = []
for i in range(N):  # iはリーダーのインデックス番号
    counter = 0     # counterは向きを変える人数
    for j in range(i):
        if S[j] == "W":
            counter += 1
    for j in range(j+1,N):
        if S[j] == "E":
            counter += 1
    l.append(counter)
print(min(l))

Submission status: Runtime Error

Solution code:
N = int(input())
A1 = list(map(int, input().split()))
A2 = list(map(int, input().split()))
A = [A1,A2]
# ここまでが入力

l = []
# 経路の選び方は i に関して
# i回右に進む
# 1回下に進む
# N-i-1回右に進む
# つまり、上の行の[0]~[i]までを加算
# 下の行の[i]~[N]を加算すればいいだけである
for i in range(N):
    counter = 0
    for j in range(i+1):
        counter += A1[j]
    for j in range(i, N):
        counter += A2[j]
    l.append(counter)

print(max(l))

Submission status: Runtime Error

Solution code:
import sys

n, s = sys.stdin.read().split()

def main():
    cnt = s.count('E')
    cur = 'E'
    res = cnt
    for c in s:
        if cur == 'W': cnt += 1
        if c == 'E': cnt -= 1
        res = min(res, cnt)
        cur = c
    print(min(res))
    
if __name__ ==  '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
wc=[0]
ec=[0]
for i in range(n):
    if s[i]=="W":
        wc.append(wc[i]+1)
        ec.append(ec[i])
    else:
        wc.append(wc[i])
        ec.append(ec[i]+1)
ans=10**6
for i in range(n+1):
    tmp=0
    
    tmp+=wc[i]
    tmp+=ec[n]-ec[i+1]
    ans=min(tmp,ans)
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = input()
w = [0]*(n+1)
e = [0]*(n+1)
for i in range(1,n+1):
    if s.at(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
for i in range(n):
    ans = min(ans,w[i]+e[n]-e[i+1])
print(ans)

Submission status: Runtime Error

Solution code:
a = []
cnt = s.count("E")
s.insert(0, 0)
s.append(0)
for i in range(1, n + 1):
    if s[i - 1] == "W":
        cnt += 1
    if s[i] == "E" and cnt != 0:
        cnt -= 1
    a.append(cnt)
print(min(a))

Submission status: Runtime Error

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

#毎ステップ毎にカウントする処理→TLE
# ans=10**6
# for i in range(1,N):
#   ans=min(ans,s[:i-1].count("W")+s[i+1:].count("E"))
# print(ans)
ans=[]
ans.append(s[1:].count("E"))
tmp=ans[0]
for i in range(1:N):
  if s[i]=="E":
    tmp-=1
  if s[i-1]=="W":
    tmp+=1
  ans.append(tmp)
print(min(ans))

Submission status: Runtime Error

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

#毎ステップ毎にカウントする処理→TLE
# ans=10**6
# for i in range(1,N):
#   ans=min(ans,s[:i-1].count("W")+s[i+1:].count("E"))
# print(ans)
ans=[]
ans[0]=s[1:].count("E")
tmp=ans[0]
for i in range(1:N):
  if s[i]=="E":
    tmp-=1
  if s[i-1]=="W":
    tmp+=1
  ans.append(tmp)
print(min(ans))

Submission status: Runtime Error

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

def calculate(S):
    minVal = len(S)
    for index,value in enumerate(S):
        s1 = S[:index]
        s2 = S[index+1:]
        if value == "W":
            count1 = s1.count("W")
            count2 = s2.count("E")

        minVal = min(count1+count2,minVal)


    return minVal


value = calculate(S)

print(value)

Submission status: Runtime Error

Solution code:
N = int(input())
l = list(map(int,input().split()))
 
num_odd = 0
num_even = 0
num_four = 0
 
for i in l:
    if i % 4 == 0:
        num_four += 1
    elif i % 2 == 0:
        num_even += 1
    else:
        num_odd += 1
 
if num_four < num_odd:
    ans = "No"
else:
    ans = "Yes"
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter
N = int(input())
S = list(input())

counter = Counter(S)
E = counter.get('E')
W = counter.get('W')
# print('E W', E, W)

ans = 10**6
e, w = 0, 0
for i in range(N):
    v = w + E - e
    if S[i] == 'E':
        v -= 1
        e += 1
    else:
        w += 1
    # print('v, e, w', v, e, w)
    ans = min(ans, v)
print(ans)

Submission status: Runtime Error

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


def ejudge(arr):
    print(arr)
    print(arr.count('E'))
    return arr.count('E')


def wjudge(arr):
    print(arr)
    print(arr.count('W'))
    return arr.count('W')


min_men = len(s)
for i in range(n):
    east = s[:i]
    west = s[i+1:]
    count = wjudge(east) + ejudge(west)
    if min_men > count:
        min_men = count

print(min_men)

Submission status: Runtime Error

Solution code:
8
WWWWWEEE

Submission status: Runtime Error

Solution code:
v

Submission status: Runtime Error

Solution code:
import collections
n=int(input())
a = list(collections.Counter(input()).items())
if a[1][1]==0:
    print(0)
else:
    print(a[1][1])

Submission status: Runtime Error

Solution code:
import collections
n=int(input())
a = list(collections.Counter(input()).items())
if a[1][1]==0:
    print(0)
else:
    print(a[1][1]-1)

Submission status: Runtime Error

Solution code:
import collections
n=int(input())
a = collections.Counter(input()).items()
if a[1][1]==0:
    print(0)
else:
    print(a[1][1]-1)

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
s = input()
dic = dict(collections.Counter(s))
left_w = 0
right_e = dic["E"]
cnt = [[] for _ in range(n)]
#(左あるWの数+右にあるEの数)の最小値が答え
ans = n
for i in range(n):
  if s[i] == "W":
    #cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    left_w += 1
  elif s[i] == "E":
    #cnt[i] = [left_w,right_e]
    ans = min(ans,left_w+right_e)
    right_e -= 1
  
print(ans)