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: Wrong Answer

Solution code:
N = int(input())
S = input()
count_E = 0
min_change = 0
change = 0
W_flag = True
if S[0] == 'E':
    count_E += 1
    W_flag = False
for i in range(1, len(S)):
    if W_flag:
        change += 1
    if S[i] == 'E':
        count_E += 1
        W_flag = False
        change -= 1
        min_change = min(change, min_change)
    elif S[i] == 'W':
        W_flag = True
ans = count_E + min_change
print(ans)

Submission status: Wrong Answer

Solution code:
S=int(input())
N=input()
a=S
b=0
for i in range(S):
    c=N.count("E")
    if N[i]=="W":
        b+=1
    if N[i]=="E":
        c-=1
    if b-1+c<a:
        a=b+c-1
print(a)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
count_list = []
camu_west = []
for i in range(1,N+1):
  look_west = len([S[j] for j in range(i) if S[j] == "W"])
  camu_west.append(look_west)
for i in range(N):
  count_l = camu_west[i-1]
  if i == 0:
    count_l = 0
  count_r = N-1-i-(camu_west[N-1]-camu_west[i])
  if i == N:
    count_r = 0
  count = count_l + count_r 
  count_list.append(count)
ans = min(count_list)
print("test")

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
count_E = S.count("E")
count_W = 0
count = count_E + count_W
ans = count
for i in range(N):
    if i == 0 and S[i] == "E":
        count_E -= 1
    else:
        if S[i-1] == "E" and i != 1:
            count_E -= 1
        elif S[i-1] == "W":
            count_W += 1
    if count < ans :
        ans = count
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
count_E = S.count("E")
count_W = 0
count = count_E + count_W
ans = count + 1
for e in S:
    if e == "E":
        count_E -= 1
    elif e == "W":
        count_W += 1
    if count < ans :
        ans = count
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = list(input())
count_E = S.count("E")
count_W = 0
count = count_E + count_W
ans = count + 1
for e in S:
    if e == "E":
        count_E -= 1
    elif e == "w":
        count_w += 1
    if count < ans :
        ans = count
print(ans)

Submission status: Wrong Answer

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

W = [0 for _ in range(N + 1)]
E = [0 for _ in range(N + 1)]

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

ret = 10 ** 6
for i in range(1, N):
    w = W[i - 1]
    e = E[N] - E[i]
    ret = min(ret, w + e)

print(ret)

Submission status: Wrong Answer

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

W = [0 for _ in range(N + 2)]
E = [0 for _ in range(N + 2)]

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

ret = 10 ** 6
for i in range(1, N):
    w = W[i - 1] - W[0]
    e = W[N + 1] - W[i + 1]
    ret = min(ret, w + e)

print(ret)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
t_min = 0
for i in range(N):
  t = 0
  for j in range(N):
    if (j < i and S[j] == "W") or (i < j and S[j] == "E"):
      t += 1
  t_min = min(t_min, t)
print(t_min)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
for i in range(N):
  t_min = 0
  t = 0
  for j in range(N):
    if (j < i and S[j] == "W") or (i < j and S[j] == "E"):
      t += 1
  t_min = min(t_min, t)
print(t_min)

Submission status: Wrong Answer

Solution code:
debug = True

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

    tmpCnt=S.count('E')
    if S[0]=='E':
        minCnt=tmpCnt-1
    else:
        minCnt=tmpCnt

    for i in range(1,N):
        if S[i]=='E':
            tmpCnt-=1
        if S[i-1]=='W':
            tmpCnt+=1
        minCnt=min(minCnt,tmpCnt)
        
    print(minCnt)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

ans = line[1:N].count('E')
tmp = ans
for i in range(1,N):
 if line[i] == 'E':
  tmp -= 1
 if line[i-1] == 'W':
  tmp += 1
 if ans>tmp:
  ans = tmp
 print(i)


print(ans)

Submission status: Wrong Answer

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

ans = line.count('E')
tmp = ans
for i in range(1,N):
 if line[i] == 'E':
  tmp -= 1
 if line[i-1] == 'W':
  tmp += 1
 if ans>tmp:
  ans = tmp


print(ans)

Submission status: Wrong Answer

Solution code:
n = input()
a = input()
S = [a[i:i+1] for i in range(len(a))]
left = []
right = []
 
answer = n
 
for i in range(len(a)):
    an = 0
    left = S[:i]
    right = S[i+1:]
    an += left.count("W")
    an += right.count("E")
    
    if answer > a : answer = an
print(answer)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python

E = "E"  # ->
W = "W"  # <-


class Line:

    def __init__(self, people):
        self.people = people

    def get_change_count(self, leader):
        leader_i = leader - 1
        count = 0
        for i in range(len(self.people)):
            if i == leader_i:
                continue
            elif i < leader_i:
                if self.people[i] == W:
                    count += 1
            elif leader_i < i:
                if self.people[i] == E:
                    count += 1
            else:
                assert ()
        return count


def main():
    people_count = int(input().rstrip())

    people = input().rstrip()
    line = Line(people)

    min_leader_no = None
    min_change_count = float('inf')

    for i in range(1, people_count + 1):
        change_count = line.get_change_count(i)
        if change_count < min_change_count:
            min_leader_no = i
            min_change_count = change_count

        if min_leader_no <= 1:
            break

    print(min_change_count)


main()

Submission status: Wrong Answer

Solution code:
input()
ss = input()
loss = ss[1:].count("E")
min_loss = loss
ss_before = ss[:-1]
ss_after = ss[1:]
for b, a in zip(ss_before, ss_after):
    loss += b == "W"
    if a == "W":
        loss -=1
    else:
        loss +=1
    if loss < min_loss:
        min_loss = loss
print("{}".format(min_loss))

Submission status: Wrong Answer

Solution code:
input()
ss = input()
loss = ss[1:].count("E")
min_loss = loss
ss_before = ss[:-1]
ss_after = ss[1:]
for b, a in zip(ss_before, ss_after):
    if b == "E":
        loss -=1
    else:
        loss +=1
    if a == "W":
        loss -=1
    else:
        loss +=1
    if loss < min_loss:
        min_loss = loss
print("{}".format(min_loss))

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
m = len(s)
for i in range(len(s)):
    c = s[:i].count('W') + s[i + 1:].count('E')
    if c < m:
        m = c
    elif c > m:
      break
print(m)

Submission status: Wrong Answer

Solution code:
import sys

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

position_L = []
position_R = []
for s in S:
    if s == 'W':
        position_R.append(0)
    else:
        position_R.append(1)
        
flips = sum(position_R[0:])
flips_temp = flips
i = 1
while i < N:
    flips_temp = flips_temp - position_R[i] + ((-1) * position_R[i-1] + 1)
    #print(i,flips_temp)
    if flips_temp < flips:
        flips = flips_temp
    i = i + 1
print(flips)

Submission status: Wrong Answer

Solution code:
import sys

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

position = []
for s in S:
    if s == 'W':
        position.append(-1)
    else:
        position.append(1)
i = 0
flips = N + 1
while i < N:
    flips_temp = 0
    j  = 0
    while j < N:
        if j < i:
            flips_temp += (1 - position[j]) / 2.
        if j > i:
            flips_temp += (1 + position[j]) / 2.
        j = j + 1
    if flips_temp < flips:
        flips = flips_temp
    i = i + 1
print(flips)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
l_w = 0
r_e = N
result = l_w + r_e
for i in range(1, N):
  if S[i - 1] == "W":
    l_w -= 1
  if S[i] == "E":
    r_e += 1
  result = min(result, l_w + r_e)
print(result)

Submission status: Wrong Answer

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

for i in range(1, N+1):
    X = S[:i]
    Y = S[i+1:N]
    
    n = X.count("W") + Y.count("E")
    if min > n:
        min = n


print(min)

Submission status: Wrong Answer

Solution code:
from sys import stdin

N = stdin.readline().rstrip()
S = stdin.readline().rstrip()


min = 10000000

N = int(N)

for i in range(1, N+1):
    X = S[:i]
    Y = S[i+1:N]
    
    n = X.count("W") + Y.count("E")
    if min > n:
        min = n


print(min)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
 
left = 0
right = S[0:].count("E")
ans = left + right

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)

Submission status: Wrong Answer

Solution code:
N = int(input())
S = input()
 
ans = len(S) - 1
left = 0
right = S[0:].count("E")

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)

Submission status: Wrong Answer

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

length = len(S)
min_num = 300000
left = 0
right = S.count("E")

for i in range(length):

  if i > 0:
    left += int(S[i - 1] == "W")

  if i < length - 1:
    right -= int(S[i + 1] == "E")

  if left + right < min_num:
    min_num = left + right

print(min_num)

Submission status: Wrong Answer

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

min_num = 300000
left = 0

for i in range(len(S)):

  if i > 0:
    left += int(S[i] == "W")
    
  right = S[i+1: ].count("E")

  if left + right < min_num:
    min_num = left + right

print(min_num)

Submission status: Wrong Answer

Solution code:
N = input()
S = input().split()

turns_num_list = list()
for i, s in enumerate(S):
  left = [s == "E" for s in S[0:i]].count(True)
  right = [s == "W" for s in S[i+1:]].count(True)
  turns_num_list.append(left+right)

print(min(turns_num_list))

Submission status: Wrong Answer

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
length = int(input())
str = input()

# 人数を計算する
def count(s, lp, min):
	cnt = 0
	for i in range(len(s)):
		if i < lp and s[i] == 'W':
			cnt += 1
		elif i > lp and s[i] == 'E':
			cnt += 1
		if cnt >= min:
			return cnt # 最小超えたら評価をやめる
	return cnt

min = int(length/2)+1
for lp in range(length):
	min = count(str, lp, min)
#	if min > cnt:
#		min = cnt

print(min)

Submission status: Wrong Answer

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
length = int(input())
str = input()

# 人数を計算する
def count(s, lp, min):
	cnt = 0
	for i in range(len(s)):
		if i < lp and s[i] == 'W':
			cnt += 1
		elif i > lp and s[i] == 'E':
			cnt += 1
		if cnt >= min:
			return cnt # 最小超えたら評価をやめる
	return cnt

min = int(length/2)
for lp in range(length):
	min = count(str, lp, min)
#	if min > cnt:
#		min = cnt

print(min)

Submission status: Wrong Answer

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
length = int(input())
str = input()

# 人数を計算する
def count(s, lp, min):
	cnt = 0
	for i in range(len(s)):
		if i < lp and s[i] == 'W':
			cnt += 1
		elif i > lp and s[i] == 'E':
			cnt += 1
		if cnt >= min:
			return cnt # 最小超えたら評価をやめる
	return cnt

min = length
for lp in range(length):
	min = count(str, lp, min)
#	if min > cnt:
#		min = cnt

print(min)

Submission status: Wrong Answer

Solution code:
length = int(raw_input())
directions = raw_input()

from_west = [None] * length
from_east = [None] * length

# west count
west = 0
east = 0
west_reverse = 0
east_reverse = 0

for n in range(length):
    if directions[n] == 'W':
        west += 1
    else:
        east += 1

    if directions[-(n + 1)] == 'W':
        west_reverse += 1
    else:
        east_reverse += 1

    from_west[n] = (west, east)
    from_east[-(n + 1)] = (west_reverse, east_reverse)


min_length = length + 1
for i in range(length):
    left = None
    right = None
    if i != 0:
        left = from_west[i - 1]
    if i != length - 1:
        right = from_east[i + 1]

    left_turns = 0
    if left:
        left_turns = left[1] if directions[i] == 'E' else left[0]
    right_turns = 0
    if right:
        right_turns = right[1] if directions[i] == 'E' else right[0]

    if left_turns + right_turns < min_length:
        min_length = left_turns + right_turns

print min_length

Submission status: Wrong Answer

Solution code:
n = raw_input()
directions = raw_input()
east = 0
west = 0
for c in directions:
    if c == 'W':
        west += 1
    else:
        east += 1
print min(east, west)

Submission status: Wrong Answer

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

tmp = [int(p.replace('W', '-1')) if p == 'W' else int(p.replace('E', '1')) for p in list(S)]

result = []
cnt = 0
for CTO in range(N):
    for MEM in range(N):
        print(CTO)
        if MEM == CTO:
            continue
        if CTO < MEM:
            if tmp[CTO] == -1:
                if (tmp[MEM] + tmp[CTO]) == 0:
                    cnt += 1
            if tmp[CTO] == 1:
                if (tmp[MEM] + tmp[CTO]) != 0:
                    cnt += 1

        if MEM < CTO:
            if tmp[CTO] == -1:
                if (tmp[MEM] + tmp[CTO]) != 0:
                    cnt += 1
            if tmp[CTO] == 1:
                if  (tmp[MEM] + tmp[CTO]) == 0:
                    cnt += 1
        print(cnt)
    result.append(cnt)
    cnt = 0

result_x = [ result[i] for i in range(N) if result[i] != 0]
print(min(result_x))

Submission status: Wrong Answer

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

print(S)
X=[]
for i in range(N):
    a=0
    y=S[i]
 
    x='E'*i+y+'W'*(N-i-1)
    for j in range(len(x)):
        if x[j]!=S[j]:
            a+=1
    X.append(a)
print(min(X))

Submission status: Wrong Answer

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

print(S)
X=[]
for i in range(N):
    a=0
    y=S[i]

    x='E'*i+y+'W'*(N-i-1)
    for j in range(len(x)):
        if x[j]!=S[j]:
            a+=1
    X.append(a)
print(min(X))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

IN = 'sample-1.in'  # sample case for local test
def local_test():
    try:
        from pcm.utils import set_stdin
        import sys
        if len(sys.argv) == 1:
            set_stdin(os.path.dirname(__file__) + '/test/' + IN)
    except:
        pass


import sys
import os
import time
import re
from pydoc import help
import string
import math
import numpy as np
from operator import itemgetter
from collections import Counter
from collections import deque
from collections import defaultdict as dd
import fractions
from heapq import heappop, heappush, heapify
import array
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy as dcopy
import itertools
sys.setrecursionlimit(10**7)
INF = 10**20
GOSA = 1.0 / 10**10
MOD = 10**9+7
ALPHABETS = [chr(i) for i in range(ord('a'), ord('z')+1)]  # can also use string module
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 DP(N, M, first): return [[first] * M for n in range(N)]
def DP3(N, M, L, first): return [[[first] * L for n in range(M)] for _ in range(N)]


def solve():
    print(input())


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

Submission status: Wrong Answer

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

sumE=0
for s in range(n):
    if S[s] == 'E':
        sumE+=1
idx = 0
num = n

sumW=0
for s in range(n):
    nn = 0

    if s == 0:
        if  S[s]=='E':
            sumE -= 1
        nn = sumE
    else:
        if  S[s]=='E':
            sumE -= 1
        if  S[s-1]=='W':
           
            sumW += 1
        nn = sumE + sumW
    if nn < num:
        num = nn
    if num == 1:
        break
print(num)

Submission status: Wrong Answer

Solution code:
n =int(input())
S=input()
E = []
W=[]
for s in range(n):
    if S[s] == 'E':
        E.append(1)
        W.append(0)
    else:
        E.append(0)
        W.append(1)
idx = 0
num = n
sumE = sum(E)
sumW=0
for s in range(n):
    nn = 0

    if s == 0:
        sumE -= E[s]
        nn = sumE
    else:
        sumE -= E[s]
        sumW += W[s - 1]
        nn = sumE + sumW
    if nn < num:
        num = nn
    if num == 1:
        break
        
    
   
print(num)

Submission status: Wrong Answer

Solution code:
da  = int(input())

k = list(input())
p = [0]*da
for i in range(da):
  if k[i] == "E" and i < da -1:
    p[i] += 1
    p[da-1] -= 1
  elif k[i] == "W":
    p[0] += 1
    p[i] -= 1

for i in range(1,da):
  p[i] += p[i-1]

if (max(p) == p[0] and k[0] == "W") or (max(p) == p[da-1] and k[da-1] =="E"):
  p[0] += 1
  p[da-1] += 1
print(da - max(p))

Submission status: Wrong Answer

Solution code:
da  = int(input())

k = list(input())
p = [0]*da
for i in range(da):
  if k[i] == "E" and i < da -1:
    p[i] += 1
    p[da-1] -= 1
  elif k[i] == "W":
    p[0] += 1
    p[i] -= 1

for i in range(1,da):
  p[i] += p[i-1]

if max(p) == p[0] or max(p) == p[da-1]:
  p[0] += 1
  p[da-1] += 1
print(da - max(p))

Submission status: Wrong Answer

Solution code:
da  = int(input())

k = list(input())
p = [0]*da
for i in range(da):
  if k[i] == "E" and i < da -1:
    p[i+1] += 1
    p[da-1] -= 1
  else:
    p[0] += 1
    p[i] -= 1

for i in range(1,da):
  p[i] += p[i-1]

print(da - max(p))

Submission status: Wrong Answer

Solution code:
da  = int(input())

k = list(input())
p = [0]*da
for i in range(da):
  if k[i] == "E":
    p[i] += 1
    p[da-1] -= 1
  else:
    p[0] += 1
    p[i] -= 1

for i in range(1,da):
  p[i] += p[i-1]

print(da - max(p)-1)

Submission status: Wrong Answer

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

cost = 10000000
for i in range(0, n):
    cost = (cost, s[:i].count('W') + s[(i+1):].count('E'))
print(cost)

Submission status: Wrong Answer

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

n = 12
s = 'WEWEWEEEWWWE'

n = 5
s = 'WEEWE'
cost = list()
for i in range(0, n):
    cost.append(s[:i].count('W') + s[(i+1):].count('E'))

print(min(cost))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(input())
for i in range(n):
    if a[i] == 'E':
        a[i] = 1
    else:
        a[i] = 0

ans = 99999999999999999999
# print(a)
l = 0
r = sum(a)
for i in range(n):

    if i == 0:
        if a[i] == 1:
            r -= a[i]
            a[i] = 0
        else:
            a[i] = 1
        l = 0
    else:
        if a[i] == 1:
            a[i] = 0
            r -= 1
        else:
            a[i] = 1
        l += a[i-1]

    ans = min(l+r,ans)
    print(i,l,r,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(input())
for i in range(n):
    if a[i] == 'E':
        a[i] = 1
    else:
        a[i] = 0

ans = 99999999999999999999
# print(a)
l = 0
r = sum(a)
for i in range(n-1):

    if i == 0:
        if a[i] == 1:
            r -= a[i]
            a[i] = 0
        else:
            a[i] = 1
        l = 0
    else:
        if a[i] == 1:
            a[i] = 0
            r -= 1
        else:
            a[i] = 1
        l += a[i-1]

    ans = min(l+r,ans)
    # print(i,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
s=input()
a=[]
for i in s:
    if i=='W': a.append(1)
    else: a.append(-1)
a2=[-1*i for i in a]
suma=[-a[0]]
for i in range(len(a)-1):
    suma.append(-a[i+1]+suma[i])
suma2=[sum(a2)]
for i in range(len(a2)-1):
    suma2.append(suma2[i]+a2[i])
#print(a,a2)
count=0
for i in range(len(a)):
    d=suma[i]+suma2[i]
    if count<d:count=d
    #print(sum(a[i+1:]),sum(a2[:i]),d)
print((n-1-count)//2)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(input())
for i in range(n):
    if a[i] == 'E':
        a[i] = 1
    else:
        a[i] = 0

ans = 99999999999999999999
# print(a)
l = 0
r = sum(a)
for i in range(n-1):

    if i == 0:
        r -= a[i]
        l = 0
        a[i] = 1
    else:
        if a[i] == 1:
            a[i] = 0
            r -= 1
        else:
            a[i] = 1
        l += a[i-1]

    ans = min(l+r,ans)
    # print(i,ans)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stderr
from functools import reduce
from operator import add
def f(): [int(i) for i in input().split()]
def debug(*x): stderr.write(reduce(add,repr(x)).strip("()',")+'\n')

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

longest = {"E":(0,1),"W":(0,1)}
# index,num of people who is E

i = 0
state = ""
po = 0
while i < n:
    debug(i)
    if po == 0:
        debug("po==0")
        po += 1
        state = s[i]
        i += 1
    else:
        if s[i] == state:
            debug(state,"po increased")
            po += 1
            i += 1
        else:
            debug("po changed")
            if longest[state][1] < po:
                if state == "E":
                    longest[state] = (i-1, po)
                else:
                    longest[state] = (i-po-1,po)
            po = 0
            i += 1

debug(longest)
if longest["E"][1] < longest["W"][1]:
    leader = longest["W"][0]
else:
    leader = longest["E"][0]

ans = 0
debug(leader,s)
for i in range(leader):
    if s[i] == "W":
        debug(i)
        ans += 1
for i in range(leader+1,n):
    if s[i] == "E":
        debug(i)
        ans += 1

print(ans)

Submission status: Wrong Answer

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

left = 0
right = N - 1

DP = [0] * N

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

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

print(min(DP))

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy


def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [int(input_raw[0])]
    input_raw = read_func().strip().split()
    [S] = [(input_raw[0])]

    min_count = 0
    for i in range(1, N):
        if S[i] != "W":
            min_count += 1
    prev_count = min_count

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

        if S[i - 1] == "E":
            prev_count -= 1
        else:
            prev_count += 1
        if prev_count < min_count:
            min_count = prev_count

    print min_count


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

right_EW = Counter(S)
left_EW = {"E":0,"W":0}

ans = len(S)
for s in S:
    temp = left_EW["W"] + right_EW["E"]
    if temp < ans: ans = temp
    right_EW[s] -= 1
    left_EW[s] += 1
    
print(ans)

Submission status: Wrong Answer

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


count = 0
for e,i in enumerate(s):
    if e == 0:
        continue
    if i == 'E':
        count += 1

mincount = count
for e,i in enumerate(s):
    if e == len(s)-1:
        break

    if i == 'W':
        count+=1
    else:
        count-=1

    if s[e+1] == 'E':
        mincount = min(count-1,mincount)
    else:
        mincount = min(count,mincount)

print(mincount)

Submission status: Wrong Answer

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

e = s.count('E')
w = 0
c = 10**5
for i in range(n):
    if s[i] == 'E':
        e -= 1
        c = min(c, w + e)
    else:
        c = min(c, w + e)
        w += 1

print(c)

Submission status: Wrong Answer

Solution code:
import sys


n = int(sys.stdin.readline())
s = sys.stdin.readline()[0:n]

e = s.count("E")
w = len(s) - e

x = min(e, w)

print(x - 1)

Submission status: Wrong Answer

Solution code:
input()
S = input()
tmp = sum([1 for s in S[1:] if s == "E"])
ans = tmp
for i in range(1, len(S)):
    if S[i-1] == "W":
        tmp += 1
    else:
        tmp -= 1
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
a = input()
b = 0
for s in input():
  b += 1 if s=='W' else -1
print(abs(b))

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
line = raw_input()

west = [0] * n

for i in range(n - 1):
    west[i + 1] = west[i]
    if line[i] == 'W':
        west[i + 1] += 1

east = [0] * n

for i in range(n - 1, 0, -1):
    east[i - 1] = east[i]
    if line[i] == 'E':
        east[i - 1] += 1

min_change = 1e5

for i in range(n):
    if west[i] + east[i] < min_change:
        min_change = west[i] + east[i]

print min_change

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C
# CreatedDate:  2020-09-27 01:01:29 +0900
# LastModified: 2020-09-27 01:12:14 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from collections import Counter


def main():
    N = int(input())
    S = input()
    S = [s for s in S]
    ans = 0
    for i in range(N):
        if i == 0:
            obj = S[1:]
            ans = obj.count('E')
            continue
        elif i == N-1:
            obj = S[:i]
            cnt = obj.count('W')
        else:
            obj_1 = S[:i]
            obj_2 = S[i+1:]
            cnt = obj_1.count('W') + obj_2.count('E')
        if ans > cnt:
            ans = cnt
    print(ans)

    


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()
X = [0]*n

for i in range(n):
    S0 = S[0:i]
    S1 = S[i+1:n]
    for v in S0:
        if v == 'W':
            X[i] += 1
    for v in S1:
        if v == 'E':
            X[i] += 1
print(min(X))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

n = int(input())
s = input()
t01 = s.replace("E", "0")
t01 = t01.replace("W", "1")
t10 = str(sum((1 - int(t01[n - i - 1])) * 10 ** i for i in range(n))).zfill(n)
l1 = [int(a) for a in t01]
l2 = [int(a) for a in t10]
left = [0] + list(accumulate(l1))[:-1]
right = list(accumulate(l2[::-1]))[-2::-1] + [0]
print(min(w + e for (w, e) in zip(left, right)))

Submission status: Time Limit Exceeded

Solution code:
import sys
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def main():
    n = int(stdin.readline().rstrip())
    s = str(stdin.readline().rstrip())
    #n,m = map(int, stdin.readline().rstrip().split())
    #l = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    ans = n
    for i in range(n):
        left = s[:i]
        right = s[i+1:]
        l = left.count("W")
        r = right.count("E")
        ans = min(ans, r+l)
    print(ans)


main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = N
for i in range(N+1):
  S1 = S[:i]
  S2 = S[i:]
  a = S1.count('W') + S2.count('E')
  if a < ans:
    ans = a
print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

sum = 3000000
for i in range(0, N):
    # print("left", S[:i])
    # print("right", S[i+1:])
    left_side_reverse_num = S[:i].count('W')
    right_side_reverse_num = S[i+1:].count('E')

    # print(left_side_reverse_num)
    # print(right_side_reverse_num)

    if sum > left_side_reverse_num + right_side_reverse_num:
        sum = left_side_reverse_num + right_side_reverse_num

    #print(sum)
print(sum)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
S = S + "Z"
sum_w = []
sum_e = []

for i in range(N):
  sum_w.append(S[:i].count("W"))

for i in range(N):
  sum_e.append(S[i+1:].count("E"))

#print(sum_e, sum_w)
sumu = [x+y for x, y in zip(sum_w, sum_e)]

print(min(sumu))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = 10**9

for i in range(n):
  west = s[:i].count("W")
  east = s[i+1:].count("E")
  ans = min(ans,west+east)

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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