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 = input()
result = []
for i in range(n):
  if i == 0:
    w = s[1:]
    ans = w.count("E")
    result.append(ans)
  if i == n-1:
    e = s[:n-1]
    ans = e.count("W")
    result.append(ans)
  else:
    e = s[:i-1]
    ans_1 = e.count("W")
    w = s[i+1:]
    ans_2 = w.count("E")
    result.append(ans_1 + ans_2)
    
print(min(result))

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = list(input())
mn = 10**6
for i in range(n-1):
    left = dict(Counter(s[:i]))
    right = dict(Counter(s[i+1:]))
    count = 0
    if 'W' in left:
        count += left["W"]
    if 'E' in right:
        count += right['E']
    mn = min(mn,count)
    if mn==count:
        ans = i
print(mn)

Submission status: Time Limit Exceeded

Solution code:
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    s = input()
    min_ = float('inf')
    for i in range(n):
        min_ = min(min_, s[:i].count('W') + s[i:].count('E'))
    print(min_)


if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def cal(n,s):
	val = [-1]*n
	for i in range(n):
		val[i] = sum(s[:i] == 'W') + sum(s[i+1:] == 'E')
	return min(val)

import numpy as np
n = int(input())
s = np.array(list(input()))
print(cal(n,s))

Submission status: Time Limit Exceeded

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

count = [0 for i in range(N)]

for i in range(N):
    for j in range(N):
        if j < i and S[j] == "W":
            count[i] += 1
        elif j > i and S[j] == "E":
            count[i] += 1
        else:
            pass


print(min(count))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(input())
s=[0]*n

for i in range(n):
  if a[i]=="W":
    a[i]="0"
    a[i]=int(a[i])
  else:
    a[i]="1"
    a[i]=int(a[i])

for i in range(n):
  s[i]=(i-(sum(a[0:i])))+(sum(a[i+1:n]))
  
print(min(s))

Submission status: Time Limit Exceeded

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

cnt = []
for i in range(n):
    cnt.append(0)
    for j in range(i):
        if s[j] == 'W':
            cnt[i] += 1
    for j in range(i+1, n):
        if s[j] == 'E':
            cnt[i] += 1
print(min(cnt))

Submission status: Time Limit Exceeded

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

cnt = []
for i in range(n):
    cnt.append(0)
    for j in range(n):
        if i < j:
            if s[j] == 'E':
                cnt[i] += 1
        elif i > j:
            if s[j] == 'W':
                cnt[i] += 1

print(min(cnt))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(map(str,input()))
box = [0]*n
for i in range(n):
  count1 = 0
  count2 = 0
  for j in range(i):
    if s[j] == "W":
      count1 += 1
  for k in range(i+1,n):
    if s[k] == "E":
      count2 += 1
  total = count1 + count2
  box[i] = total
print(min(box))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(map(str,input()))
l = len(s)
box = []
for i in range(l):
  count1 = 0
  count2 = 0
  for j in range(i):
    if s[j] == "W":
      count1 += 1
  for k in range(i+1,l):
    if s[k] == "E":
      count2 += 1
  total = count1 + count2
  box.append(total)
print(min(box))

Submission status: Time Limit Exceeded

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

lst = []
for i in range(n):
  lst.append(s[:i].count('W') + s[i+1:].count('E'))
print(min(lst))

Submission status: Time Limit Exceeded

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

res = -1
for i in range(n):
  if res == -1:
    res = s[:i].count('W') + s[i+1:].count('E')
  else:
    if res > s[:i].count('W') + s[i+1:].count('E'):
      res = s[:i].count('W') + s[i+1:].count('E')
print(res)

Submission status: Time Limit Exceeded

Solution code:
#C - Attention
N = int(input())
S = input()
DP = []
tmpw = 0
tmpe = 0
ans = 10**9
for i in range(N):
    tmpw = S[:i].count('W')
    tmpe = S[i+1:].count('E')
    if ans >= tmpe+tmpw:
        ans = tmpe+tmpw
    else:
        pass

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#C - Attention
N = int(input())
S = input()
DP = []
tmpw = 0
tmpe = 0
for i in range(N):
    tmpw = S[:i].count('W')
    tmpe = S[i+1:].count('E')
    DP.append(tmpw+tmpe)

print(min(DP))

Submission status: Time Limit Exceeded

Solution code:
import sys

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

cnt_list = []
for i in range(n):
    cnt = 0
    for j in range(i+1, n):
        if s[j] == 'E':
            cnt += 1
    for j in range(i):
        if s[j] == 'W':
            cnt += 1
    cnt_list.append(cnt)

print("{}".format(min(cnt_list)))

Submission status: Time Limit Exceeded

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

ans=float('inf')
for i in range(N):
  cnt=0
  S1=S[:i]
  S2=S[i+1:]
  cnt+=S1.count('W')+S2.count('E')
  ans=min(ans,cnt)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [1 if i == "W" else 0 for i in input()]
print(min([sum(a[:i]) + n - i - 1 - sum(a[i+1:]) for i in range(n)]))

Submission status: Time Limit Exceeded

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

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 = input()
ans = n
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(map(str, input()))

a = []
for i in range(n):
    cnt = 0
    if "E" in s[i + 1 :]:
        cnt += s[i + 1 :].count("E")
    if "W" in s[:i]:
        cnt += s[:i].count("W")
    a.append(cnt)
print(min(a))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = str(input())
ans = 10**6
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:
def main():
    n = int(input())
    S = input()

    ans = 10**9
    for i in range(len(S)):
        ans = min(ans, S[:i].count('W')+S[i+1:].count('E'))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

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


resolve()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
L = [[] for _ in range(N)]
res = 10**9
for i in range(N):
    if S[i] == 'W':
        left = S[0:i].count('W')
        right = S[i:N].count('E')
        res = min(res,left+right)
    if S[i] == 'E':
        left = S[0:i].count('W')
        right = S[i:N].count('E')
        res = min(res,left+right)
print(res)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

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

cnt = []
cnt.append(Counter(s[1:])["E"])
cnt.append(Counter(s[:n])["W"])
for i in range(1, n-1):
    a = Counter(s[:i])["W"]
    b = Counter(s[i+1:])["E"]
    cnt.append(a+b)
print(min(cnt))

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
N = int(input())
S = input()
s = []
#print(N)
#print(S)

#まず右（E）を向いている人を1、左（W）を向いている人を0に変換する
for i in range(len(S)):
    if(S[i] == 'E'):
        s.append(1)
    else:
        s.append(0)
#print(s)

#その地点までで左(W)を向いている人数を累積和するリストを作ります。要は右に振り向かせる人数リスト
E_count = []
#その地点から先で右(E)を向いている人数を累積和するリストを作ります。要は左に振り向かせる人数リスト
#W_count = []
#ans = []
for i in range(len(s)):
    E_count.append(  len(s[:i]) - sum(s[:i])  +   sum(s[i+1:]))
    #W_count.append(  sum(s[i+1:])  )
    #ans.append( E_count[i] + W_count[i])
#print(min(ans))
#print(E_count)
#print(W_count)
#for i in range(len(s)):
    #ans.append(E_count[i] + W_count[i])
print(min(E_count))

'''
#これでもTLEします
def count_tern_new(s, leader):
    counter = 0
    #leaderより小さい人は右に向かせる。 左を向いている人数　＝　人数-右を向いている人数
    counter += len(s[:leader]) - sum(s[:leader])
    #leaderより大きい人には左を向かせる。 右を向いている人数
    counter += sum(s[leader+1:])
    return counter

answer_list_new = []
for i in range(len(s)):
    answer_list_new.append(count_tern_new(s, i))
print(min(answer_list_new))
'''


'''
#これだとTLEします。
def count_tern(s, leader): #文字列とリーダーのインデックスを引数にとる
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            #print("tern to E, i = {}, counter = {}".format(i,counter))
    #print("leader:{}".format(leader))
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            #print("tern to W, j = {}, counter = {}".format(j, counter))
    return counter

#answer = 1000
answer_list = []
for i in range(len(S)):
    answer_list.append(count_tern(S, i))

#for i in range(len(S)):
    #print(count_tern(S, i))
    #print()
    #if(count_tern(S, i) < answer):
        #answer = count_tern(S, i)

print(min(answer_list))
'''

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
N = int(input())
S = input()
s = []
#print(N)
#print(S)

#まず右（E）を向いている人を1、左（W）を向いている人を0に変換する
for i in range(len(S)):
    if(S[i] == 'E'):
        s.append(1)
    else:
        s.append(0)
#print(s)

#その地点までで左(W)を向いている人数を累積和するリストを作ります。要は右に振り向かせる人数リスト
E_count = []
#その地点から先で右(E)を向いている人数を累積和するリストを作ります。要は左に振り向かせる人数リスト
W_count = []
ans = []
for i in range(len(s)):
    E_count.append(  len(s[:i]) - sum(s[:i]))
    W_count.append(  sum(s[i+1:])  )
    #ans.append( E_count[i] + W_count[i])
#print(min(ans))
#print(E_count)
#print(W_count)
for i in range(len(s)):
    ans.append(E_count[i] + W_count[i])
print(min(ans))

'''
#これでもTLEします
def count_tern_new(s, leader):
    counter = 0
    #leaderより小さい人は右に向かせる。 左を向いている人数　＝　人数-右を向いている人数
    counter += len(s[:leader]) - sum(s[:leader])
    #leaderより大きい人には左を向かせる。 右を向いている人数
    counter += sum(s[leader+1:])
    return counter

answer_list_new = []
for i in range(len(s)):
    answer_list_new.append(count_tern_new(s, i))
print(min(answer_list_new))
'''


'''
#これだとTLEします。
def count_tern(s, leader): #文字列とリーダーのインデックスを引数にとる
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            #print("tern to E, i = {}, counter = {}".format(i,counter))
    #print("leader:{}".format(leader))
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            #print("tern to W, j = {}, counter = {}".format(j, counter))
    return counter

#answer = 1000
answer_list = []
for i in range(len(S)):
    answer_list.append(count_tern(S, i))

#for i in range(len(S)):
    #print(count_tern(S, i))
    #print()
    #if(count_tern(S, i) < answer):
        #answer = count_tern(S, i)

print(min(answer_list))
'''

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
N = int(input())
S = input()
s = []
#print(N)
#print(S)

#まず右（E）を向いている人を1、左（W）を向いている人を0に変換する
for i in range(len(S)):
    if(S[i] == 'E'):
        s.append(1)
    else:
        s.append(0)
#print(s)

#その地点までで左(W)を向いている人数を累積和するリストを作ります。要は右に振り向かせる人数リスト
E_count = []
#その地点から先で右(E)を向いている人数を累積和するリストを作ります。要は左に振り向かせる人数リスト
W_count = []
ans = []
for i in range(len(s)):
    E_count.append(  len(s[:i]) - sum(s[:i]))
    W_count.append(  sum(s[i+1:])  )
    ans.append( E_count[i] + W_count[i])
print(min(ans))
#print(E_count)
#print(W_count)

'''
#これでもTLEします
def count_tern_new(s, leader):
    counter = 0
    #leaderより小さい人は右に向かせる。 左を向いている人数　＝　人数-右を向いている人数
    counter += len(s[:leader]) - sum(s[:leader])
    #leaderより大きい人には左を向かせる。 右を向いている人数
    counter += sum(s[leader+1:])
    return counter

answer_list_new = []
for i in range(len(s)):
    answer_list_new.append(count_tern_new(s, i))
print(min(answer_list_new))
'''


'''
#これだとTLEします。
def count_tern(s, leader): #文字列とリーダーのインデックスを引数にとる
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            #print("tern to E, i = {}, counter = {}".format(i,counter))
    #print("leader:{}".format(leader))
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            #print("tern to W, j = {}, counter = {}".format(j, counter))
    return counter

#answer = 1000
answer_list = []
for i in range(len(S)):
    answer_list.append(count_tern(S, i))

#for i in range(len(S)):
    #print(count_tern(S, i))
    #print()
    #if(count_tern(S, i) < answer):
        #answer = count_tern(S, i)

print(min(answer_list))
'''

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
N = int(input())
S = input()
s = []
#print(N)
#print(S)

#まず右（E）を向いている人を1、左（W）を向いている人を0に変換する
for i in range(len(S)):
    if(S[i] == 'E'):
        s.append(1)
    else:
        s.append(0)
#print(s)

def count_tern_new(s, leader):
    counter = 0
    #leaderより小さい人は右に向かせる。 左を向いている人数　＝　人数-右を向いている人数
    counter += len(s[:leader]) - sum(s[:leader])
    #leaderより大きい人には左を向かせる。 右を向いている人数
    counter += sum(s[leader+1:])
    return counter

answer_list_new = []
for i in range(len(s)):
    answer_list_new.append(count_tern_new(s, i))
print(min(answer_list_new))

'''
#これだとTLEします。
def count_tern(s, leader): #文字列とリーダーのインデックスを引数にとる
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            #print("tern to E, i = {}, counter = {}".format(i,counter))
    #print("leader:{}".format(leader))
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            #print("tern to W, j = {}, counter = {}".format(j, counter))
    return counter

#answer = 1000
answer_list = []
for i in range(len(S)):
    answer_list.append(count_tern(S, i))

#for i in range(len(S)):
    #print(count_tern(S, i))
    #print()
    #if(count_tern(S, i) < answer):
        #answer = count_tern(S, i)

print(min(answer_list))
'''

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
N = int(input())
S = input()
#s = []
#print(N)
#print(S)
'''
#まず右（E）を向いている人を1、左（W）を向いている人を0に変換する
for i in range(len(S)):
    if(S[i] == 'E'):
        s.append(1)
    else:
        s.append(0)
print(s)
'''


#これだとTLEします。
def count_tern(s, leader): #文字列とリーダーのインデックスを引数にとる
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            #print("tern to E, i = {}, counter = {}".format(i,counter))
    #print("leader:{}".format(leader))
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            #print("tern to W, j = {}, counter = {}".format(j, counter))
    return counter

#answer = 1000
answer_list = []
for i in range(len(S)):
    answer_list.append(count_tern(S, i))

#for i in range(len(S)):
    #print(count_tern(S, i))
    #print()
    #if(count_tern(S, i) < answer):
        #answer = count_tern(S, i)

print(min(answer_list))

Submission status: Time Limit Exceeded

Solution code:
## coding: UTF-8
N = int(input())
S = input()

#print(N)
#print(S)

def count_tern(s, leader): #文字列とリーダーのインデックスを引数にとる
    counter = 0
    for i in range(leader):
        if(s[i] == 'W'):
            counter += 1            
            #print("tern to E, i = {}, counter = {}".format(i,counter))
    #print("leader:{}".format(leader))
    for j in range(leader+1, len(s)):
        if(s[j] == 'E'):
            counter += 1
            #print("tern to W, j = {}, counter = {}".format(j, counter))
    return counter

answer = 1000
for i in range(len(S)):
    #print(count_tern(S, i))
    #print()
    if(count_tern(S, i) < answer):
        answer = count_tern(S, i)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = s + "a"
ans = 3 * 10**5

for i in range(n):
    change = s.count('W',0,i) + s.count("E",i+1,n)
    ans = min(ans, change)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
s = s + "a"
ans = 3 * 10**5

for i in range(n):
    w_side = s[:i]
    e_side = s[i+1:]
    change = w_side.count('W') + e_side.count("E")
    ans = min(ans, change)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    arr = tuple(input())
    count = n
    for i in range(n):
        c = 0
        left = arr[:i]
        right = arr[i+1:]
        c += sum([1 for j in left if j == 'W'])
        c += sum([1 for k in right if k == 'E'])
        count = min(c, count)

    print(count)

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n = int(input())
    arr = list(input())
    count = n
    for i in range(n):
        c = 0
        left = arr[:i]
        right = arr[i+1:]
        c += left.count('W')
        c += right.count('E')
        count = min(c, count)

    print(count)

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
S = input()

left = [0]*N
right = [0]*N
for i in range(N):
    tmp1 = S[:i]
    tmp2 = S[i+1:]
    left[i] = tmp1.count('W')
    right[i] = tmp2.count('E')

left = np.array(left)
right = np.array(right)
ans = (left+right).min()
print(ans)

Submission status: Time Limit Exceeded

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

ans = float('inf')
for i in range(N):
    left = Counter(S[:i])
    right = Counter(S[i+1:])
    tmp = left['W'] + right['E']
    ans = min(ans,tmp)
print(ans)

Submission status: Time Limit Exceeded

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

ans = float('inf')
for i in range(N):
    left = S[:i]
    right = S[i+1:]
    tmp = left.count('W') + right.count('E')
    ans = min(ans,tmp)
print(ans)

Submission status: Time Limit Exceeded

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

l=[]
for i in range(n):
    x = s[:i].count('W') + s[i:].count('E')
    l.append(x)
    x=0

print(min(l))

Submission status: Time Limit Exceeded

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


A = []
for i in range(N):
  if S[i] == "W":
    A.append(1)
  elif S[i] == "E":
    A.append(-1)
    
saidai = 0
for i in range(N):
  if saidai < sum(A[i+1:]) - sum(A[:i]):
    
    saidai = sum(A[i+1:]) - sum(A[:i])
    flag = i
    
ans = A[:flag].count(1) + A[flag+1:].count(-1)

print(ans)

Submission status: Time Limit Exceeded

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

"""
まあまずは愚直にOrder（N*N）でやろう
"""

ans = INF

for i in range(N):
  count = 0
  for t in range(N):
    if i == t:
      continue
    if t < i and S[t] == "W" or t > i and S[t] == "E":
      count += 1

  ans = min(ans, count)

print(ans)

Submission status: Time Limit Exceeded

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

ans = s.count("E")

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

print(ans)

Submission status: Time Limit Exceeded

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

arr = []

arr.append(s.count("E"))

for i in range(n-1):
    j = i + 1
    l = s[:j].count("W")
    r = s[j+1:].count("E")
    arr.append(l + r)

print(min(arr))

Submission status: Time Limit Exceeded

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

ans = float('inf')
w_left = [0]
e_right = [0]

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

for i in range(n):
    ans = min(ans , w_left[i]+e_right[i])
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = float('inf')

for i in range(n):
  counter = 0
  for j in range(n):
    if j < i and s[j] == 'W':
      counter += 1
    if j > i and s[j] == 'E':
      counter += 1
  ans = min(ans,counter)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ans = N
for i in range(N):
    hoge = S[:i].count('W') + S[i:].count("E")
    if ans > hoge:
        ans = hoge
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

def cntchg(s,i):
  cnt = 0
  for j in range(0,i):
    if s[j] == 'W':
      cnt += 1
  for j in range(i+1,len(s)):
    if s[j] == 'E':
      cnt += 1
  return cnt
low = n
for i in range(n):
  if low > cntchg(s,i):
    low = cntchg(s,i)
print(low)

Submission status: Time Limit Exceeded

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

left = []
right = []
ans = 3*10**5
E = S.count("E")
W = len(S)-E
# print(E)
# print(W)

for i in range(N):
    left_count = 0
    right_count = 0
    left = S[0:i]
#     right = S[i+1:]

    left_count = left.count("W")
    if S[i]== "W":
        right_count = E - (i - left_count)
    else:
        right_count = E - 1 - (i - left_count)

#     print(left_count)
#     print(right_count)
    count = left_count + right_count
    ans = min(count, ans)
print(ans)

Submission status: Time Limit Exceeded

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

left = []
right = []
ans = 3*10**5
E = S.count("E")
W = S.count("W")
# print(E)
# print(W)

for i in range(N):
    left_count = 0
    right_count = 0
    left = S[0:i]
#     right = S[i+1:]

    left_count = left.count("W")
    if S[i]== "W":
        right_count = E - (i - left_count)
    else:
        right_count = E - 1 - (i - left_count)

#     print(left_count)
#     print(right_count)
    count = left_count + right_count
    ans = min(count, ans)
print(ans)

Submission status: Time Limit Exceeded

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

left = []
right = []
ans = 3*10**5

for i in range(N):
    left_count = 0
    right_count = 0
    left = S[0:i]
    right = S[i+1:]

    for l in left:
        if l == "W":
            left_count += 1

    for r in right:
        if r == "E":
            right_count += 1
            
    count = left_count + right_count
    ans = min(count, ans)
print(ans)

Submission status: Time Limit Exceeded

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

left = []
right = []
ans = 3*10**5

for i in range(N):
    left = S[0:i]
    right = S[i+1:]
#     print(list(left))
#     print(list(right))
    left_count = list(left).count("W")
#     print(left_count)
    right_count = list(right).count("E")
#     print(right_count)
    count = left_count + right_count
    ans = min(count, ans)
print(ans)

Submission status: Time Limit Exceeded

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

S = []
for i in s:
    S.append(i)

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

Submission status: Time Limit Exceeded

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

S = []
for i in s:
    S.append(i)

test = ['W' for _ in range(n)]

ans = 100100100
# i番目の人がリーダーの時
for i in range(n):
    # iより西の人は東を向き、iより東の人は西を向くべき
    for j in range(i):
        test[j] = 'E'
    test[i] = S[i]

    # コストを算出
    cost = 0
    for k in range(n):
        if S[k] != test[k]:
            cost += 1
    # ansを更新
    ans = min(ans, cost)

print(ans)

Submission status: Time Limit Exceeded

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

Wlist = [0] 
Elist = [0]

for i in range(N):
    Wlist.append(S.count('W',0,i+1))
    Elist.append(S.count('E',0,i+1))
    
ans = []

for i in range(1,N+1):
    ans.append((Elist[-1] - Elist[i]) + (Wlist[i-1]))
    
print(min(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    W = []
    wcum = 0
    for c in s:
        W.append(wcum)
        if c == 'W':
            wcum += 1

    E = []
    ecum = 0
    for c in s[::-1]:
        E.insert(0, ecum)
        if c == 'E':
            ecum += 1

    ans = min([W[i] + E[i] for i in range(n)])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

def main():
    N = int(input())
    S = list(input())
    vals = set([])
    for i in range(N):
        vals.add(collections.Counter(S[0:i])["W"] + collections.Counter(S[i+1:])["E"])
    print(min(vals))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for i in range(N):
    cnt = S.count('W',0,i)
    cnt += S.count('E',i+1,N)
    ans.append(cnt)
print(min(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

#----------
N = int(input().strip())
A_list = list(input().strip())
#----------

count = N
for i_leader in range(len(A_list)):
    ahead_list = A_list[:i_leader]
    behind_list = A_list[i_leader+1:]
    
    turn_W_to_E = len( [ i for i in ahead_list if i == "W"] )
    turn_E_to_W = len( [ i for i in behind_list if i == "E"] )
    turn_member = turn_W_to_E + turn_E_to_W
    
    if turn_member < count:
        count = turn_member

print(count)

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:
N = int(input())
S = input()
answer = float('inf')
for i in range(N):
    count = 0
    left = list(S[:i])
    right = list(S[i+1:])
    for j in range(len(left)):
        if 'W' in left[j]:
            count += 1
    for j in range(len(right)):
        if 'E' in right[j]:
            count += 1
    if answer >= count:
        answer = count

    # print(left, right)

print(answer)

Submission status: Time Limit Exceeded

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

for n in range(N):
  a = min(a,S[n:].count("E")+S[:n].count("W"))

print(a)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
L=[1 if s=='W' else 0 for s in S]
ans=set()

for i in range(N):
    W,E=sum(L[:i+1]),N-i-1-sum(L[i:])
    ans.add(W+E)

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
words=list(input())
res=[];
for i in range(N):
    tmp=0
    for j in range(N):
        if j<i:
            if words[j]=="W":
                tmp+=1
        elif i<j:
            if words[j]=="E":
                tmp+=1
    res.append(tmp)
print(min(res))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
count=N
for i in range(N):
  c=0
  for j in range(N):
    if j>i and S[j]=='E':
      c+=1
    elif j<i and S[j]=='W':
      c+=1
  if count>c:
    count=c
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


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


def wjudge(arr):
    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: Time Limit Exceeded

Solution code:


def main():
    N = int(input())
    S = [i for i in input()]
    min_count = 30000
    for i in range(N):
        left = S[:i-1]
        right = S[i:]
        tmp_c = left.count("W")+right.count("E")
        if tmp_c < min_count:
            min_count = tmp_c
    print(min_count)

if __name__ == "__main__":
    main()