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:


def main():
    N = int(input())
    S = input()
    min_count = 30000
    for i in range(N):
        count = 0
        for loc, j in enumerate(S):
            if i == loc: 
                continue

            if loc > i:
                if j == "E":
                    count += 1
            if loc < i:
                if j == "W":
                    count += 1
        if count < min_count:
            min_count = count
    print(min_count)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

# 累積和を求めておく
cumsum_e = 0
cumsum_w = 0
for i in range(n):
    if s[i] == 'E':
        cumsum_e += 1
    else:
        cumsum_w += 1

min_change = n + 1
for i in range(n):
    temp_change = 0
    if s[i] == 'E':
        temp_cumsum_e = cumsum_e - 1
        temp_cumsum_w = cumsum_w
    else:
        temp_cumsum_e = cumsum_e
        temp_cumsum_w = cumsum_w - 1

    current_w = 0
    current_e = 0
    for j in range(i):
        if s[j] == 'W':
            current_w += 1

    current_e = i - current_w
    migi_e = temp_cumsum_e - current_e
    temp_change = current_w + migi_e

    if temp_change < min_change:
        min_change = temp_change

print(min_change)

Submission status: Time Limit Exceeded

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

min_change = n + 1
for i in range(n):
    temp_change = 0
    for j in range(i - 1):
        if s[j] == 'W':
            temp_change += 1
    for k in range(i + 1, n, 1):
        if s[k] == 'E':
            temp_change += 1
    if temp_change < min_change:
        min_change = temp_change

print(min_change)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
left_w_count=0
right_e_count=0

for i in s:
    if(i=='E'):
        right_e_count+=1

ans=100
for i in range(0,n):
    left=s[:i]
    rigth=s[i+1:]
    if(s[i-1]=="W" and i>0):
        left_w_count+=1
    if(s[i]=="E"):
        right_e_count-=1
    if(ans > left_w_count + right_e_count):
        ans = left_w_count + right_e_count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
left_w_count=0
right_e_count=0

ans=100
for i in range(n):
    left=s[:i]
    rigth=s[i+1:]
    for j in left:
        if(j=="W"):
            left_w_count+=1
    for j in rigth:
        if(j=="E"):
            right_e_count+=1
    if(ans > left_w_count + right_e_count):
        ans = left_w_count + right_e_count
    left_w_count=0
    right_e_count=0
print(ans)

Submission status: Time Limit Exceeded

Solution code:

N=int(input())
l = input()
mi = 300000

for i in range(N):
    count=0
    for j in range(N):
        if i>j:
            if l[j]=="W":
                count+=1
        elif i<j:
            if l[j]=="E":
                count+=1
        else:
            continue
    mi=min(mi,count)

print(mi)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


for i in range(n):
    lis1[i] += lis[0:i+1].count("W")
    if lis[i] == "W":
        lis1[i] -= 1

for i in range(n):
    lis1[i] += lis[i:n].count("E")
    if lis[i] == "E":
        lis1[i] -= 1

print(min(lis1))

Submission status: Time Limit Exceeded

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

#index 0
ans = lis[1:n].count("E")

#index 1~n-2
for i in range(n):
    tmp = 0
    tmp = lis[0:i+1].count("W") + lis[i+1:n].count("E")
    ans = min(ans,tmp)

#index n-1
ans = min(ans,lis[0:n].count("W"))

print(ans)

Submission status: Time Limit Exceeded

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

ans = 10 ** 6
for i in range(1, n+1):
    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()
result = N
for i in range(N):
    r = S[:i].count("W") + S[i+1:].count("E")
    if r < result:
        result = r
print(result)

Submission status: Time Limit Exceeded

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

ans = 100000000

for i in range(N):
    A = S[0:i]
    B = S[i+1:]
    X = A.count('W')
    Y = B.count('E')
    ans = min(X + Y,ans)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans = 100000000

for i in range(N):
    A = S[0:i]
    B = S[i+1:]
    if S[i] == 'E':
        #向く方向を変える必要がある人数
        X = A.count('W')
        Y = B.count('E')
        ans = min(X + Y,ans)
    if S[i] == 'W':
        #向く方向を変える必要がある人数
        X = A.count('W')
        Y = B.count('E')
        ans = min(X + Y,ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
m=int(input())
n=input()
mm=len(n)
ans=-1
for i in range(mm):
    x=n[0:i].count('W')
    y=n[i+1:].count('E')
    if mm>x+y:
        mm=x+y
        ans=i
print(mm)

Submission status: Time Limit Exceeded

Solution code:
s=input()
li=list(input())

min = 100000000

w = 0

for index, i in enumerate(li):
  a = 0
  
  s = li[index + 1:].count('E')

  a = w + s
    
  if min > a:
    min = a
    
  if i == 'W':
    w += 1

print(min)

Submission status: Time Limit Exceeded

Solution code:
s=input()
li=list(input())

min = 100000000

for index, i in enumerate(li):
  a = 0
  
  w1 = li[0:index].count('W')
  s1 = li[index + 1:].count('E')

  a = w1 + s1
    
  if min > a:
    min = a
    
print(min)

Submission status: Time Limit Exceeded

Solution code:
s=input()
li=list(input())

min = 1000000000

for index, i in enumerate(li):
  a = 0
  
  left = li[0:index]
  right = li[index + 1:]
  
  w1 = left.count('W')
  s1 = right.count('E')

  a = w1 + s1
    
  if min > a:
    min = a
    
print(min)

Submission status: Time Limit Exceeded

Solution code:

def Attention():
    N = int(input())
    S = input()
    ans = 300000

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

        ans = min(ans, count)
    print(ans)

Attention()

Submission status: Time Limit Exceeded

Solution code:

def Attention():
    N = int(input())
    S = input()
    ans = 300000

    for i in range(N):
        count = len(S[:i-1].replace('E', '')) + len(S[i+1:].replace('W', ''))

        """
        for a in S[:i-1]:
            if a == 'W':count+=1
        for a in S[i+1:]:
            if a == 'E':count+=1'
        ans = min(ans, count)
        """
        ans = min(ans, count)
    print(ans)

Attention()

Submission status: Time Limit Exceeded

Solution code:

def Attention():
    N = int(input())
    S = list(input())
    ans = 300000

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

Attention()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
num = [0 for _ in range(N)]

for i in range(N):
  for j in range(i):
    if S[j] == 'W':
      num[i] += 1
  for j in range(i+1, N):
    if S[j] == 'E':
      num[i] += 1

print(min(num))

Submission status: Time Limit Exceeded

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

ret = N
for i in range(N):
    a = S[:i]
    b = S[i + 1 :]
    n = a.count("W") + b.count("E")
    if n == 0:
        continue
    ret = min(ret, n)
print(ret)

Submission status: Time Limit Exceeded

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

for i in range(N):
    leftWestNum = S[:i].count('W')
    rightEstNum = S[i+1:N].count('E')
    tmpMin = leftWestNum + rightEstNum
    if tmpMin < minimum:
        minimum = tmpMin
    
    
print(minimum)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
ans=s[1:].count('E')

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=input()
tmp=[]
for i in range(len(S)):
    tmp.append(S[:i].count('W')+S[i+1:].count('E'))

print(min(tmp))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=list(input())
result=[]
for i in range(n):
  total=0
  for j in range(n):
    if j == i: continue
    if i < j and s[j] == 'E': total+=1
    if i > j and s[j] == 'W': total+=1
  result.append(total)
print(min(result))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = list(input())
numbers = []
for i in range(N):
    S_left = S[:i]
    S_right = S[i+1:]
    number = S_left.count("W")+S_right.count("E")
    numbers.append(number)
print(min(numbers))

Submission status: Time Limit Exceeded

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

ans = float("inf")
for i in range(N):
    flont = S[:i].count("W")
    back = S[i+1:].count("E")

    ans = min(ans, flont+back)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
ans = [0] * n
for i in range(n):
    count = 0
    front = s[:i]
    back = s[i:]
    count += front.count('W')
    count += back.count('E')
    ans[i] = count

print(min(ans))

Submission status: Time Limit Exceeded

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

print(min(ans))

Submission status: Time Limit Exceeded

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

print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=[i for i in input()]
ans=float('inf')
for i in range(N):
    l=S[:i]
    r=S[i+1:]
    E_change=l.count('W')
    W_change=r.count('E')
    ans=min(E_change+W_change,ans)
print(ans)

Submission status: Time Limit Exceeded

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

n = len(s)

ans = 10*6

for i in range(n):
	a = s[:i]
	b = s[i+1:]
	#print(a,b)

	if a.count('W') + b.count('E') < ans:
		ans = a.count('W') + b.count('E')

print(ans)

Submission status: Time Limit Exceeded

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

n = len(s)

ans = 10**8

for i in range(n):
	a = s[:i]
	b = s[i+1:]
	#print(a,b)

	if a.count('W') + b.count('E') < ans:
		ans = a.count('W') + b.count('E')

print(ans)

Submission status: Time Limit Exceeded

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

n = len(s)

ans = 10**8

for i in range(n):
	a = s[:i]
	b = s[i+1:]
	#print(a,b)
	
	check_a = a.count('W')
	check_b = b.count('E')
	
	check = check_a + check_b
	
	if check < ans:
		ans = check

print(ans)

Submission status: Time Limit Exceeded

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

min_count = float('inf')
for i in range(n):
    count = 0
    for j in range(n):
        if i == j:
            continue
        elif j < i and s[j] == 'W':
            count += 1
        elif j > i and s[j] == 'E':
            count += 1
    min_count = min(min_count, count)
print(min_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
while s.count("WE"):
    s = s.replace("WE", "")
ans = (n - len(s)) // 2
print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
str_to_int = {"W":0, "E":1}
input_ = input()
row = [str_to_int[input_[n]] for n in range(N)]

min_change_count = N+1
for n in range(N):
    west_side = row[:n]
    east_side = row[n:]
    count = len(west_side) - sum(west_side)
    count += sum(east_side)
    
    if count < min_change_count:
        min_change_count = count
        
print(min_change_count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = input()
min_num = 3 * 10**5
for i in range(0, n):
  if i == 0:
    temp = s[1:].count("E")
  elif i == n-1:
    temp = s[:-1].count("W")
  else:
    s_1 = s[:i-1].count("W")
    s_2 = s[i+1:].count("E")
    temp = s_1 + s_2
  if min_num > temp:
    min_num = temp
print(min_num)

Submission status: Time Limit Exceeded

Solution code:
import time
n = int(input())
s = input()
r = [0]*n
r2 = [0]*n
starttime = time.time()

Min = 1000000
for i in range(n) :
  s1 = s[:i]
  s2 = s[i+1:]
  c1 = s1.count("W")
  c2 = s2.count("E")
  #print(s1,s2,c1,c2)
  Min = min(Min,c1+c2)

print(Min)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
people=list(input())
List=[]

for i in range(N):
    #向きを変える人数
    List.append(people[:i].count('W')+people[i:].count('E'))

print(min(List))

Submission status: Time Limit Exceeded

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

for i in range(n):
  l=s[0:i]
  r=s[i+1:n]
  v=l.count("W")+r.count("E")
  if ans>v:
    ans=v
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import bisect

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
s = [str(i) for i in s]

mi = 10**10
s.append("W")

for i in range(len(s)-1):
    fi = s[:i]
    la = s[i+1:]
    fi.sort()
    la.sort()
    fil = bisect.bisect(fi,"W")-bisect.bisect(fi,"E")
    mil = bisect.bisect(la,"E")
    mi = min(mi,fil+mil)
print(mi)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip()
s = [str(i) for i in s]

mi = 10**10
s.append("W")

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

Submission status: Time Limit Exceeded

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

S = list(map(lambda x: 0 if x == 'E' else 1, S))

count = N
for i in range(N):
    c = N - i - sum(S[i:]) + sum(S[:i])
    count = c if c < count else count
    if count == 1:
        break

print(count)

Submission status: Time Limit Exceeded

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

count = N
for i in range(N):
    c = 0
    for s1 in S[i:]:
        c += 0 if s1 == 'W' else 1
    for s2 in S[:i]:
        c += 0 if s2 == 'E' else 1
    count = c if c < count else count
    if count == 1:
        break

print(count)

Submission status: Time Limit Exceeded

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

opposite = [(members[:i].count("W")) + (members[i:].count("E")) for i in range(n)]
print(min(opposite))

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
members = input()

counts = []
for i in range(len(members)):
    count=0
    for j in range(len(members)):
        if i == j:
            continue
        else:
            if (members[j] == 'W' and i<j) or (members[j] == 'E' and i>j):
                continue
            else:
                count+=1
        if counts:
            if count > min(counts):
                break
    counts.append(count)
print(min(counts))

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
members = input()

counts = []
for i in range(len(members)):
    count = sum([1 for j in range(len(members)) if (members[j] == 'W' and i>j) or (members[j] == 'E' and i<j)])
    counts.append(count)
print(min(counts))

Submission status: Time Limit Exceeded

Solution code:
_ = int(input())
members = input()

counts = []
for i in range(len(members)):
    count=0
    for j in range(len(members)):
        if i == j:
            continue
        else:
            if (members[j] == 'W' and i<j) or (members[j] == 'E' and i>j):
                continue
            else:
                count+=1
    counts.append(count)
print(min(counts))

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(input())
mini=float('inf')
for i in range(n):
    x=a[:i].count("W")
    y=a[i+1:].count("E")
    mini=min(mini,x+y)
print(mini)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(input())
left_W=[]
right_E=[]
for i in range(n):
    x=a[:i].count("W")
    left_W.append(x)
    y=a[i+1:].count("E")
    right_E.append(y)
mini=float('inf')
for i in range(n):
    tmp=left_W[i]+right_E[i]
    mini=min(mini,tmp)
print(mini)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:

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

print(min([len([x for x in s[:i] if x == 'W']) + \
           len([x for x in s[i+1:] if x == 'E']) \
           for i in range(n)]))

Submission status: Time Limit Exceeded

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

mem_left_len = [len([x for x in s[:i] if x == 'W']) for i in range(n)]
mem_right_len = [len([x for x in s[i+1:] if x == 'E']) for i in range(n)]
  
numpeople = 3 * 10 ** 5 + 1
for leader in range(n):
  numpeople = min(numpeople, mem_left_len[leader] + mem_right_len[leader])

print(numpeople)

Submission status: Time Limit Exceeded

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

mem_left_len = [-1 for i in range(n)]
mem_right_len = [-1 for i in range(n)]

def len_left(leader):
  length = mem_left_len[leader]
  if length > 0:
    return length
  else:
    length = len([x for x in s[:leader] if x == 'W'])
    mem_left_len[leader] = length
    return length

def len_right(leader):
  length = mem_right_len[leader]
  if length > 0:
    return length
  else:
    length = len([x for x in s[leader+1:] if x == 'E'])
    mem_right_len[leader] = length
    return length
  
  
numpeople = 3 * 10 ** 5 + 1
for leader in range(n):
  left = len_left(leader)
  right = len_right(leader)
  numpeople = min(numpeople, left + right)

print(numpeople)

Submission status: Time Limit Exceeded

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

numpeople = 3 * 10 ** 5 + 1
for leader in range(n):
  left = len([x for x in s[:leader] if x == 'W'])
  right = len([x for x in s[leader+1:] if x == 'E'])
  numpeople = min(numpeople, left + right)

print(numpeople)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
result = float('inf')
for i in range(N):
  S_1 = S[:i].count('W')
  S_2 = S[i+1:].count('E')
  result = min(result,S_1+S_2)
print(result)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=str(input())
result = float('inf')
for i in range(N):
  S_1=S[:i]
  S_2=S[i+1:]
  result = min(result,S_1.count('W')+S_2.count('E'))
print(result)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count = 0
min = n
for i in range(n):
    if i != n - 1:
        count = s[:i].count('W') + s[i + 1:].count('E')
    else:
        count = s[:i].count('W')
    if count < min:
        min = count
print(min)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

H=list(str(input()))


m = 0 
for i in range(N):
    c = 0
    for s in range(N):
        if i > s and H[s] == 'W':
            c += 1
        
        if i < s and H[s] == 'E':
            c += 1
    
    if m == 0 or m >c:
        m = c
        


print(m)

Submission status: Time Limit Exceeded

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

ans = N
for i in range(N):
    cnt = 0
    for j in range(N):
        if i < j and S[j] == 'E':
            cnt += 1
        if i > j and S[j] == 'W':
            cnt += 1
    if cnt < ans:
        ans = cnt

print(ans)

Submission status: Time Limit Exceeded

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

opposite = [0 for _ in range(N)]

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

Submission status: Time Limit Exceeded

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

opposite = [0 for _ in range(N)]

for i in range(N):
	if S[i] == "E":
		for j in range(0, i):
			opposite[j] += 1
	else:
		for j in range(i + 1, N):
			opposite[j] += 1
print(min(opposite))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

S = input()
S_list = list(S)

lis = []
for i in range(N):
    sum = 0
    for k in range(0,i):
        if S[k] == 'W':
            sum += 1
    for j in range(i+1,N):
        if S[j] == 'E':
            sum += 1
    lis.append(sum)

print(min(lis))

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(input())
S=list(input())
#print(N,S)
num=1000000
for i in range(N):
	tmp=0
	for j in range(N):
		if i is not j:
			if i > j:
				if S[j] is 'W':
					tmp += 1
			else:
				if S[j] is 'E':
					tmp += 1
		#print(i,j,num,tmp)
	if num > tmp:
		num=tmp
print(num)

Submission status: Time Limit Exceeded

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

N=int(input())
S=input()
S=list(S)
number=0
ans=[]
for i in range(N):
  Left=S[:i]
  Right=S[i+1:]
  number+=Left.count('W')
  number+=Right.count('E')
  ans.append(number) 
  number=0
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
INF = 1e9
n = int(input())
s = list(input())
ew = [0]*n
ans = INF

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = str(input())
ew = [0]*n

for i in range(n):
    if s[i]=="E":
        ew[i]+=1
    else:
        continue

ans = [0]*n
for i in range(n):
    a = sum(ew[0:i])
    b = sum(ew[i+1:n])
    ans[i] = i-a+b
print(min(ans))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = list(input())
answer = n
for i in range(n):
  left_count = s[:i].count('W')
  right_count = s[i:].count('E')
  total = left_count + right_count
  answer = min(answer, total)
print(answer)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter
n = int(input())
s = list(input())
output = 300000

for i in range(n):
    forward = Counter(s[0:i:])
    backward = Counter(s[i::])
    count = forward["W"] + backward["E"]
    if output > count:
        output = count

print(output)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
l=[0+(s[0]=="W")]
for i in range(1,n):
  l.append(l[i-1]+(s[i]=="W"))
r=[0+(s[-1]=="E")]
for i in range(1,n):
  r.append(r[i-1]+(s[::-1][i]=="E"))
print(min([x+y for (x,y) in zip(l,r[::-1])])-1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(input())
count = N
for i in range(N):
  x=[i for i in S[:i] if i == 'W']
  y=[i for i in S[(i+1):] if i == 'E']
  if count > (len(x)+len(y)):
    count = (len(x)+len(y))

print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
"""
    リーダーに向いてない人の最小
    (W in 0~i) + (E in i+2~n-1)
"""
from collections import Counter as count
n = int(input())
s = input()
least = n

for i in range(n):
    turn = count(s[:i])['W'] + count(s[i+2:])['E']
    if turn < least:
        least = turn

print(least)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

li = []
nisi = []
higasi = []
for i in range(N):
    nisi.append(S[0:i].count('W'))
    higasi.append(S[i+1:N].count('E'))

mi = 10000000

for i in range(N-2):
    if nisi[i]+higasi[i] < mi: mi = nisi[i]+higasi[i]



print(mi)

Submission status: Time Limit Exceeded

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

li = []
nisi = []
higasi = []
for i in range(N):
    nisi.append(S[0:i].count('W'))
    higasi.append(S[i+1:N].count('E'))

for i in range(N-2):
    li.append(nisi[i]+higasi[i])



print(min(li))

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
b=input()
ans=[]
for i in range(a):
  ans2=0
  for j in range(a):
    if (i<j and b[j]=="E") or (i>j and b[j]=="W"):
      ans2+=1
  ans+=[ans2]
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
#https://atcoder.jp/contests/abc098/tasks/arc098_a

n = int(input())
def process(x):
    if x=='E':
        return 0
    else:
        return 1
data = list(map(process, list(input())))

cumul = [0 for _ in range(n)]
for i in range(n):
    cumul[i] = data[:i+1].count(0)
cumul.insert(0, 0)

costs = [0 for _ in range(n)]
for i in range(n):
    cost = 0
    cost += i-cumul[i]
    cost += cumul[n]-cumul[i+1]
    costs[i] = cost

print(min(costs))

Submission status: Time Limit Exceeded

Solution code:
#https://atcoder.jp/contests/abc098/tasks/arc098_a

n = int(input())
def process(x):
    if x=='E':
        return 0
    else:
        return 1
data = list(map(process, list(input())))

l = [0 for _ in range(n-1)]
for i in range(n-1):
    t = 0
    if data[i]==1:
        t += 1
    if data[i+1]==0:
        t -= 1
    l[i] = t

def default():
    cost = 0
    for value in data[1:]:
        if value == 0:
            cost += 1
    return cost
d = default()

if min([sum(l[:j+1]) for j in range(n-1)]) < 0:
    print(d + min([sum(l[:j+1]) for j in range(n-1)]))
else:
    print(d)

Submission status: Time Limit Exceeded

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

ans = float('inf')

for i in range(N):
    cnt_w = S[:i].count('W')
    cnt_e = S[i:].count('E')
    temp = cnt_w + cnt_e
    ans = min(ans, temp)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def main():
    n = int(input())
    s = input()
    ans = float('inf')
    for i in range(n):
        tmp = s[:i].count('W') + s[i:].count('E')
        if ans > tmp:
            ans = tmp

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
s=input()
w=list(map(int,list(s.replace('W','1').replace('E','0'))))
e=list(map(int,list(s.replace('E','1').replace('W','0'))))
ans=10**6
for i in range(n):
  mukihenko=sum(w[:i])+sum(e[i+1:])
  ans=min(mukihenko,ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
S = input()
sum_i = [n - 1] * n
for i in range(n):
    sum_i[i] = S[:i].count('W') + S[i + 1:].count('E')

print(min(sum_i))

Submission status: Time Limit Exceeded

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

print(answer)

Submission status: Time Limit Exceeded

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

# リーダーより西にいる人のうち西を向いている人数+リーダーより東にいる人のうち東を向いている人数 を調べる
n_turn_list = []
for i in range(N):
    n_turn_list.append(S[:i].count("W") + S[i+1:].count("E"))

print(min(n_turn_list))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input()
ss = [0]*N

ss[0] += S[1:].count('E')
ss[N-1] += S[:N-1].count('W')

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = "a" + input() + "a"
ma = n

for i in range(1,n+1):
  c = s[:i].count("W") + s[i+1:].count("E")
  ma = min(c,ma)
  if ma == 0:
    print(0)
    exit()
    
print(ma)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
s = "a" + input() + "a"
ma = n

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