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: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
m = 3 * 10 ** 5
for i in range(n):
    count = 0
    for j in range(0, i):
        if s[j] == 'W':
            count += 1
    for k in range(i + 1, n):
        if s[k] == 'E':
            count += 1
    m = min(m, count)

print(m)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
m = 3 * 10 ** 5

for i in range(n):
    w = s[:i]
    e = s[i + 1:]
    count = w.count('W') + e.count('E')
    m = min(m, count)

print(m)

Submission status: Time Limit Exceeded

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

for i in range(n):
    w = s[:i]
    e = s[i + 1:]
    count = w.count('W') + e.count('E')
    m = min(m, count)

print(m)

Submission status: Time Limit Exceeded

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

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

print(m)

Submission status: Time Limit Exceeded

Solution code:
n, s = [input() for i in range(2)]
n = int(n)

count = n
for i in range(n):
    ldr, bef, aft = s[i], s[:i], s[i + 1:]
    bef_c = len(bef.replace('E', ''))
    aft_c = len(aft.replace('W', ''))
    count = min(bef_c + aft_c, count)

print(count)

Submission status: Time Limit Exceeded

Solution code:
n, s = [input() for i in range(2)]
n = int(n)

count = n
for i in range(n):
    ldr, bef, aft = s[i], s[:i], s[i + 1:]
    bef_c = len([x for x in bef if x != 'E'])
    aft_c = len([x for x in aft if x != 'W'])
    count = bef_c + aft_c if count > bef_c + aft_c else count

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(map(lambda x: 1 if x == "W" else 0, input()))

N -= 1

min_turn = min(N - sum(S[1:]), sum(S[:N - 2]))
for i in range(1, N - 1):
    to_east = sum(S[:i])
    to_west = N - i - sum(S[i + 1:])
    min_turn = min(to_east + to_west, min_turn)

print(min_turn)

Submission status: Time Limit Exceeded

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

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

print(min_turn)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt_l = []
for i in range(n):
  cnt = 0
  cnt += s[:i].count("W")
  cnt += s[i+1:].count("E")
  cnt_l.append(cnt) 
print(min(cnt_l))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
cnt_l = []
for i in range(n):
  cnt = 0
  if i != 0:
    for j in s[:i]:
      if j == "W":
        cnt += 1  
  if i != n - 1:
    for k in s[i+1:]:
      if k == "E":
        cnt += 1
  cnt_l.append(cnt) 
print(min(cnt_l))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
l = []
for i in range(N):
    cnt = 0
    cnt += S[i+1:N].count("E")
    cnt +=S[0:i].count("W")
    l.append(cnt)
print(min(l))

Submission status: Time Limit Exceeded

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

s = []

for i in range(N):
    b = S[:i].count("W") + S[i+1:].count("E")
    s.append(b)

print(min(s))

Submission status: Time Limit Exceeded

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

min = 3*10**5
cont = 0


for i in range(1,N):
  cont = 0
  left = S[:i-1]
  right = S[i+1:]
  cont = left.count("W") + right.count("E")
  if min >= cont:
    min = cont

print(min)

Submission status: Time Limit Exceeded

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

print(min([S[:i].count('W')+S[(i+1):].count('E') for i in range(N)]))

Submission status: Time Limit Exceeded

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

look = []
for i in range(N):
    count = 0
    for j in range(0,i):
        if(S[j] == "W"):
            count = count + 1
    for k in range(i+1,N):
        if(S[k] == "E"):
            count = count + 1
    look.append(count)

print(min(look))

Submission status: Time Limit Exceeded

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

leaderArrayNum = 0
minDireChangeNum = 4*10**5

while leaderArrayNum < N:
 direChangeNum = 0
 compareArrayNum = 0

 while compareArrayNum < N:
  compareMan = S[ compareArrayNum ]
  if compareArrayNum < leaderArrayNum:
   direChangeNum += 1 if compareMan == "W" else 0  
  elif leaderArrayNum < compareArrayNum:
   direChangeNum += 1 if compareMan == "E" else 0
  elif compareArrayNum == leaderArrayNum:
   compareArrayNum += 1
   continue
  else:
   compareArrayNum += 1
   continue

  compareArrayNum += 1		
 minDireChangeNum = direChangeNum if direChangeNum < minDireChangeNum else minDireChangeNum 
 leaderArrayNum += 1
print( minDireChangeNum )

Submission status: Time Limit Exceeded

Solution code:
def attention(N, S):
    counter = []
    x = []
    y = []
    for i in range(N):
        if i != 0:
            x = ''.join(list(S[:i]))
        if i != N:
            y = ''.join(list(S[i+1:]))
        if x == []:
            counter.append(y.count('E'))
        elif y == []:
            counter.append(x.count('W'))
        else:
            tmp1 = x.count('W')
            tmp2 = y.count('E')
            counter.append(tmp1 + tmp2)
    return min(counter)

def main():
    N = int(input())
    S = list(input())
    print(attention(N, S))

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = str(raw_input())
opt_cnt = N
for i in range(N):
    first = S[:i]
    second = S[i+1:]
    cnt = first.count('W') + second.count('E')
    if opt_cnt > cnt:
        opt_cnt = cnt
print(opt_cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = str(raw_input())
opt_cnt = N
for i in range(N):
    first = S[:i]
    second = S[i+1:]
    cnt = 0
    for f in first:
        if f is not 'E':
            cnt = cnt + 1
        if cnt >= opt_cnt:
            break
    for s in second:
        if s is not 'W':
            cnt = cnt + 1
        if cnt >= opt_cnt:
            break
    if opt_cnt > cnt:
        opt_cnt = cnt
print(opt_cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
S = str(raw_input())
opt_cnt = N
for i in range(N):
    first = S[:i]
    second = S[i+1:]
    cnt = 0
    for f in first:
        if f is not 'E':
            cnt = cnt + 1
    for s in second:
        if s is not 'W':
            cnt = cnt + 1
    if opt_cnt > cnt:
        opt_cnt = cnt
print(opt_cnt)

Submission status: Time Limit Exceeded

Solution code:
n = input()
l = list(raw_input())
ans = n

for i in range(0, n):
  tmp = l[:i].count("W") + l[i+1:].count("E")
  if tmp < ans:
    ans = tmp

print ans

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(str,input()))
A=[]
for i in range(N):
    a=S[0:i].count('W')+S[i+1:N+1].count('E')
    A.append(a)
print(min(A))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(str,input()))
A=[]
for i in range(N):
    a=S[0:i].count('W')+S[i+1:N+1].count('E')

    A.append(a)

print(min(A))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
min_turn = n
for i in range(n):
  turn = 0
  for j in range(n):
    if j < i:
      if s[j] == 'W':
        turn += 1
    elif j > i:
      if s[j] == 'E':
        turn += 1
    if turn == min_turn:
      break
  if turn < min_turn:
    min_turn = turn
print(min_turn)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
c=0
d=1000000

for i in range(a):
 c=0
 for j in range(a):
  if ((i<j and b[j]=="E")or(i>j and b[j]=="W")):
   c+=1
 d=min(d,c)


print(d)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    min = N
    mc = 0
    for i in range(0, N):
        m = S[i]
        l = S[:i]
        r = S[i:]
        c = 0
        c = mc
        for h in r:
            if h != 'W': c += 1
        if min > c: min = c
        if m != 'E': mc += 1
    print(min)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    min = N
    for i in range(0, N):
        m = S[i]
        l = S[:i]
        r = S[i:]
        c = 0
        for h in l:
            if h != 'E': c += 1
        if c > min: continue
        for h in r:
            if h != 'W': c += 1
        if min > c: min = c
    print(min)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    N = int(input())
    S = input()
    min = N
    for i in range(0, N):
        m = S[i]
        l = S[:i]
        r = S[i:]
        c = 0
        for h in l:
            if h != 'E': c += 1
        for h in r:
            if h != 'W': c += 1
        if min > c: min = c
    print(min)
        
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
m=list(input())
ans=10**5
for i in range(n):
    p=0
    x=m[:i]
    y=m[i+1:]
    p=x.count("W")+y.count("E")
    ans=min(ans,p) 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = input()
str = raw_input()

mini = N

for i in range(0, N):
    forward = str[0:i].count("W")
    if forward < mini:
        backward = str[i+1:N].count("E")
        if mini > forward + backward:
            mini = forward + backward
print mini

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = input()
str = raw_input()

nums = []

for i in range(0, N):
    nums.append(str[0:i].count("W")+str[i+1:N].count("E"))

print min(nums)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
li=[]

for i in range(n):
    cnt=0
    for j in range(n):
        if j>i and s[j]=="E":
            cnt+=1
        if j<i and s[j]=="W":
            cnt+=1
    li.append(cnt)
print(min(li))

Submission status: Time Limit Exceeded

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

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

m = n
for i in range(0, n):
    left = s[:i].count('W')
    right = s[i:].count('E')
    if left + right < m:
        m = left + right
print(m)

Submission status: Time Limit Exceeded

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

min = N
for i in range(N):
    left_parts = S[:i]
    right_parts = S[i:]
    left_count = left_parts.count('W')
    right_count = right_parts.count('E')
    count = left_count + right_count

    if count < min:
        min = count

print(min)

Submission status: Time Limit Exceeded

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

min = N
for border in range(N):
    # 入れ替える回数
    count = 0
    for i in range(N):
        if i < border:
            if S[i] == 'W':
                count += 1
        elif i > border:
            if S[i] == 'E':
                count += 1

        if count > min:
            continue

    if count < min:
        min = count

print(min)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

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

for reader in range(N):
    changer = 0
    for i in range(N):
        if (reader < i and S[i] == "E")\
        or (reader > i and S[i] == "W"):
            changer += 1
    if changer < ans:
        ans = changer

print(ans)

Submission status: Time Limit Exceeded

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

counts = []
for border in range(N):
    # 入れ替える回数
    count = 0
    for i in range(N):
        if i < border:
            if S[i] == 'W':
                count += 1
        elif i > border:
            if S[i] == 'E':
                count += 1

    counts.append(count)

print(min(counts))

Submission status: Time Limit Exceeded

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

min_count = len(s)
for i in range(len(s)):
  count = 0
  s1 = s[:i]
  s2 = s[i+1:]
  for e in s1:
    if e!= 'E':
      count += 1
  for w in s2:
    if w != 'W':
      count += 1
  if count < min_count:
    min_count = count
print(min_count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min_num = 10**6
west_num = [0]
east_num = [0]

for i in range(0, N): #累積和を求める.
    if S[i] == 'W':
        west_num.append(1 + max(west_num))
        east_num.append(max(east_num))
    else:
        west_num.append(max(west_num))
        east_num.append(1 + max(east_num))

for i in range(0, N):
    num = 0
    num = west_num[i] + east_num[-1] - east_num[i]
    if num < min_num:
        min_num = num
        
print(min_num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
min_num = 10**6

for i in range(0, N):
    num = 0
    num = sum(x == 'W' for x in S[:i]) + sum(x == 'E' for x in S[i+1:])    
    if num < min_num:
        min_num = num
        
print(min_num)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
m = 10000
for i in range(n):
    count = 0
    for j in range(n):
        if i > j:
            if s[j] == "W":
                count += 1
        if i < j:
            if s[j] == "E":
                count += 1
    m = min(m,count)
print(m)

Submission status: Time Limit Exceeded

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

ans = N
for n in range(0, N):
  west_side = S[:n].count('W')
  east_side = S[n+1:].count('E')
  
  ans = min(ans, west_side+east_side)
  
print(ans)

Submission status: Time Limit Exceeded

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

ans = N
for n in range(0, N):
  leader = S[n]
  west_side = [s for s in S[:n] if s == 'W']
  east_side = [s for s in S[n+1:] if s == "E"]
  
  ans = min(ans, (len(west_side) + len(east_side)))
  
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:


N = int(input())
S = input()
#N = 8
#S = 'WWWWWEEE'


min = 1000000
for i in range(1,N-1):
    if S[i:i+1] == 'EE':
        continue
    if S[i-1:i] == 'WW':
        continue
    l = S[i]
    if l == 'W':
        left = S[0:i-1].count('W')
        right = S[i+1:].count('E')
    else:
        left = S[0:i-1].count('W')
        right = S[i+1:].count('E')
    sum = right + left
    #print(l, sum, left, right)
    if min > sum:
        min = sum
print(min)

Submission status: Time Limit Exceeded

Solution code:


N = int(input())
S = input()
#N = 8
#S = 'WWWWWEEE'


min = 1000000
for i in range(1,N-1):
    l = S[i]
    if l == 'W':
        left = S[0:i-1].count('W')
        right = S[i+1:].count('E')
    else:
        left = S[0:i-1].count('W')
        right = S[i+1:].count('E')
    sum = right + left
    #print(l, sum, left, right)
    if min > sum:
        min = sum
print(min)

Submission status: Time Limit Exceeded

Solution code:


N = int(input())
S = input()
#N = 8
#S = 'WWWWWEEE'


min = 1000000
for i in range(1,N):
    l = S[i]
    if l == 'W':
        left = S[0:i-1].count('W')
        right = S[i+1:].count('E')
    else:
        left = S[0:i-1].count('W')
        right = S[i+1:].count('E')
    sum = right + left
    #print(l, sum, left, right)
    if min > sum:
        min = sum
print(min)

Submission status: Time Limit Exceeded

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

print(min([text[:i].count('W') + text[i+1:].count('E') for i in range(n)]))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    a = int(input())
    b = list(input())
    counts = []

    for x in range(0, len(b)):
        b_1 = b[0:x]
        b_2 = b[x:len(b)]
        counts.append(b_1.count('W') + b_2.count('E'))

    print(min(counts))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    a = int(input())
    b = list(input())
    counts = []

    for x in range(0, len(b)):
        count = 0
        for y in range(0, len(b)):
            if y < x and b[y] != 'E':
                count = count + 1
            elif y > x and b[y] != 'W':
                count = count + 1
        counts.append(count)

    print(min(counts))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = list(input())
min_people = int(1e+6)

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=[]
ecnt=list(s).count("E")
for i in range(n):
    left=list(s[:i]).count("W")
    right=ecnt-(i-left)
    ans.append(left+right)
print(min(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

if __name__ == '__main__':
    N = int(input())
    S = input()

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
n_we = []

for i in range(0, n):
    s1 = s[:i]
    s2 = s[i+1:]
    n_we.append(s1.count("W") + s2.count("E"))
print(min(n_we))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#abc098c

n=int(raw_input())
s=raw_input()
res=float('inf')
for i in xrange(len(s)):
 s1=s[:i]
 s2=s[i+1:]
 res=min(s1.count('W')+s2.count('E'),res)
print res

Submission status: Time Limit Exceeded

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

result = n
for i in range(n):
    temp = s[:i].count("W") + s[i:].count("E")
    if temp < result:
        result = temp

print(result)

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
word=str(input())
n_th = 3*10^5
for i in range(num):
	n=word[:i].count('W')+word[i+1:].count('E')
	if n_th > n:
		n_th = n
print ( n_th)

Submission status: Time Limit Exceeded

Solution code:
num=int(input())
word=str(input())
n_th = 3*10^5
for i in range(num):
	n=0
	for a in word[:i]:
		if a == 'W':
			n += 1
	for a in word[i+1:]:
		if a == 'E':
			n +=1
	if n_th > n:
		n_th = n
print ( n_th)

Submission status: Time Limit Exceeded

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

for i in range(N):
    pre_reader = S[:i]
    post_reader = S[i+1:]
    pre_count = len([k for k in range(i) if S[k] == 'W'])
    post_count = len([k for k in range(i+1, N) if S[k] == 'E'])
    if (count > pre_count + post_count) and (i >= 1):
        count = pre_count + post_count
    elif i == 0:
        count = pre_count + post_count

print(count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
mi=n
for i in range(n):
  l=list(s[:i])
  r=list(s[i+1:])
  mi=min(l.count("W")+r.count("E"),mi)
print(mi)

Submission status: Time Limit Exceeded

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

total = []

for i in range(N):
    _sum = S[0:i].count("W")+S[i+1:N].count("E")
    total.append(_sum)

print(min(total))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
anslist=[]
for i in range(n):
    reader_w = s[:i]
    reader_e = s[i+1:]
    wcnt = len([i for i in reader_w if i == 'W'])

    ecnt = len([i for i in reader_e if i == 'E'])
    #print(i,wcnt,ecnt)
    #print(ecnt)
    #print("")
    anslist.append(wcnt+ecnt)

print(min(anslist))

Submission status: Time Limit Exceeded

Solution code:
import collections

n = int(input())
s = input()
min_people = n - 1

def we_counter(pos):
  front = s[:pos]
  back = s[pos+1:]
  people = front.count('W') + back.count('E')
  return people


for i in range(n):
  people = we_counter(i)

  if people < min_people:
    min_people = people

print(min_people)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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])
  c = (s[0:i-1])
  
  count += a.count('E')
  count += c.count('W')
  
  if (max > count):
    max = count
print (max)

Submission status: Time Limit Exceeded

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

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

print(res)

Submission status: Time Limit Exceeded

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

count=0
i=0
j=N-1

while i<=j:
    if s[i]=="E":
        i+=1
        continue
    if s[j]=="W":
        j-=1
        continue
    sub_i=i
    sub_j=j
    j_i=0
    j_j=0
    while s[sub_i]=="W":
        sub_i+=1
        j_i+=1
    while s[sub_j]=="E":
        sub_j-=1
        j_j+=1
    if j_i<j_j:
        i=sub_i-1
        count+=1
    else:
        j=sub_j+1
        count+=1
    
print(count)

Submission status: Time Limit Exceeded

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

# 桁数 2 < N < 300000
N = int(input())
# 文字列'E','W'
S = input()

min_v = 300000
for i in range(0,N):
    # リーダーより西
    X = ''.join(list(S[:i]))
    # リーダーより東
    Y = ''.join(list(S[i+1:]))
    # 西向きの人をカウント 東向きの人をカウント
    # print(str(i) + ':' + X + ':' + str(X.count('W')) + ',' + Y + ':' + str(Y.count('E')))
    v = X.count('W') + Y.count('E')
    if min_v > v:
        min_v = v
print (min_v)

Submission status: Time Limit Exceeded

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

cnt = N
for i in range(1, N + 1):
    tmp = S[:i-1].count('W') + S[i:].count('E')#changeDirection(S, i, N)
    if tmp < cnt:
        cnt = tmp
        
print(cnt)

Submission status: Time Limit Exceeded

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

count=0
i=0
j=N-1

while i<=j:
    if s[i]=="E":
        i+=1
        continue
    if s[j]=="W":
        j-=1
        continue
    sub_i=i
    sub_j=j
    j_i=0
    j_j=0
    while s[sub_i]=="W":
        sub_i+=1
        j_i+=1
    while s[sub_j]=="E":
        sub_j-=1
        j_j+=1
    if j_i<j_j:
        i=sub_i-1
        count+=1
    else:
        j=sub_j+1
        count+=1
    
print(count)

Submission status: Time Limit Exceeded

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

for i in range(N):
    a = S[:i]
    b = S[i+1:]
    k = a.count("W")+b.count("E")
    if l>k:
        l = k
print(l)

Submission status: Time Limit Exceeded

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])
  
  count += a.count('E')
  count += c.count('W')
  
  if (max > count):
    max = count
print (max)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = raw_input()
d = []
for i in range(0,n):
    c = s[0:i].count('W')+s[i+1:n].count('E')
    d.append(c)
print(min(d))

Submission status: Time Limit Exceeded

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

import os
import sys

def main():
    list_length = int(input())
    result = list_length
    global west_all
    data = list(input())
    west_all = data.count("W")
    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: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
min_change = n-1
for x in range(n):
    change = 0
    for y in range(n):
        if x > y:
            if s[y] == 'W': change += 1
        elif x < y:
            if s[y] == 'E': change += 1
    if min_change > change: min_change = change
print(min_change)

Submission status: Time Limit Exceeded

Solution code:
# Q3

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

def changeDirection(S, L, N):
    Sb = S[:L-1].count('W')
    Sa = S[L:].count('E')
    return Sb + Sa

cnt = N
for i in range(1, N + 1):
    tmp = changeDirection(S, i, N)
    if tmp < cnt:
        cnt = tmp
        
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 300001
for i in range(N):
  tmp_count = 0
  s1 = S[0:i]
  s2 = S[i+1:]
  if i<N/2:
    if s2.count('E')>count:
      continue
  else:
    if s1.count('W')>count:
      continue
  tmp = s1.count('W')+s2.count('E')
  if count>tmp:
    count = tmp
print(count)

Submission status: Time Limit Exceeded

Solution code:

def abc098_c():
    n = int(input())
    s = input()
    count_min = n
    w_num = s.count("W")
    for i in range(n):
        w_left = s[:i].count("W")
        e_right = n - w_num - (i - w_left) - (s[i] == "E")
        count = w_left + e_right
        if count == 0:
            return 0
        count_min = min(count, count_min)

    return count_min


print(abc098_c())

Submission status: Time Limit Exceeded

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

c = set()
for i in range(N):
    west = S[:i]
    east = S[i+1:]
    cnt = west.count('W') + east.count('E')
    c.add(cnt)
print(min(c))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
d = []
for i in range(0,n):
    c = s[0:i].count('W')+s[i+1:n].count('E')
    d.append(c)
print(min(d))

Submission status: Time Limit Exceeded

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

w_cnt = s.count("W")
e_cnt = s.count("E")

if w_cnt > e_cnt:
   for i in range(int(n/2)+1):
      if i == 0:
         cnt = s.count("E")
         if s[0] == "E":
            cnt = cnt - 1
      elif i == n -1:
         cnt = s.count("W")
         if s[n-1] == "W":
            cnt -= 1
      else: 
         a = s[:i]
         b = s[i+1:]
         a_cnt = a.count("W")
         if a_cnt > min_c: continue
         b_cnt = b.count("E")
         if b_cnt > min_c: continue
         cnt = a_cnt + b_cnt
         if cnt > min_c: continue
      min_c = min(min_c, cnt)
else:
   for i in range(int(n/2)+2, n):
      if i == 0:
         cnt = s.count("E")
         if s[0] == "E":
            cnt = cnt - 1
      elif i == n -1:
         cnt = s.count("W")
         if s[n-1] == "W":
            cnt -= 1
      else: 
         a = s[:i]
         b = s[i+1:]
         a_cnt = a.count("W")
         if a_cnt > min_c: continue
         b_cnt = b.count("E")
         if b_cnt > min_c: continue
         cnt = a_cnt + b_cnt
         if cnt > min_c: continue
      min_c = min(min_c, cnt)

print(min_c)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
count = 300001
for i in range(N):
  tmp_count = 0
  s1 = S[0:i]
  s2 = S[i+1:]
  tmp = s1.count('W')+s2.count('E')
  if count>tmp:
    count = tmp
print(count)

Submission status: Time Limit Exceeded

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

nW = 0
nE = 0

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

count = N

for i in range(N):
    if S[i] == 'E':
        temp = 1
    else: 
        temp = 0
    tempW = 0
    tempE = 0
    for j in range(i):
        if S[j] == 'W':
            tempW += 1
        else:
            tempE += 1
    if tempW + nE - tempE - temp < count:
        count = tempW + nE - tempE - temp

print(count)

Submission status: Time Limit Exceeded

Solution code:
def abc098_c():
    n = int(input())
    s = input()
    count_min = n
    w_num = s.count("W")
    for i in range(n):
        w_left = s[:i].count("W")
        e_right = n - w_num - (i - w_left) - (s[i] == "E")
        count = w_left + e_right
        if count == 0:
            return 0
        if count_min > count:
            count_min = count

    return count_min


print(abc098_c())

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
a =[0 for i in range(n)]
for i in range(n):
    if s[i]=="E":
        for j in range(i):
            a[j]+=1
    else:
        for j in range(i+1,n):
            a[j]+=1
print(min(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
d = []
for i in range(0,n):
    list_1 = s[0:i]
    list_2 = s[i+1:n]
    c = list_1.count('W') + list_2.count('E')
    d.append(c)
print(min(d))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
 
N = int(input())
S = input()
 
c=[]
 
for i in range(0,N):
    if i==0:
        c.append(S[1:].count("E"))
        if 0 in c:
            break
    elif i == N-1:
        c.append(S[0:N-1].count("W"))
        if 0 in c:
            break
    else:
        if S[i-1] == S[i] and S[i] == S[i+1]:
            pass    
        else:
            c.append(S[0:i].count("W") + S[i+1:].count("E"))
            if 0 in c:
                break

print(min(c))

Submission status: Time Limit Exceeded

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

count_list = []
for i in range(N):
    x = S[:i]
    y = S[i+1:]
    count_list.append(x.count('W') + y.count('E'))

print(min(count_list))

Submission status: Time Limit Exceeded

Solution code:
import re
n = int(input())
s = input()
a=300000
for i in range(n):
    l = re.sub("E", "", s[0:i])
    r = re.sub("W", "", s[i + 1:n])
    b = len(l) + len(r)
    if a > b:
        a = b
print(a)