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: Runtime Error

Solution code:
N = int(input())
S = input()
stock = []
#INF = 10**9
for i in range(1,len(S)-1):
  if (S[i-1] == "E") & (S[i+1] == "W"): stock.append(i)

dic = {k:0 for k in stock}
dic[0]＝ S.count("E")
dic[len(S)-1] = S.count("W")

for i in stock:
  for j in range(i-1,0,-1):
    if j < 0: break
    else:
      if S[j] == "E" : dic[i] += 1
      else: break

  for j in range(i+1, len(S)):
    if j > len(S)-1: break
    else:
      if S[j] == "W" : dic[i] += 1
      else: break

max_k = max(dic, key=dic.get)


count = 0
for i in range(0,max_k):
  if S[i] == "W": count += 1

for i in range(max_k+1, len(S)):
  if S[i] == "E": count += 1




print(count)

Submission status: Runtime Error

Solution code:
A, B, C, X, Y = map( int, input().split() )

if A + B > 2 * C:
  m = min( X, Y )
  print( min( 2 * C * m + ( X - m ) * A + ( Y - m ) * B, 
              2 * C * m + ( X - m ) * 2 * C + ( Y - m ) * 2 * C ) )
else:
  print( X * A + Y * B )

Submission status: Runtime Error

Solution code:
from itertools import*
_, s = open(0)
b, c = [([0]+list(accumulate(map(int, s[::a].translate(str.maketrans("EW","01"[::a])))))[:-1])[::a] for a in [-1,1]]
print(min(x+y for x, y in zip(b, c)))

Submission status: Runtime Error

Solution code:
from itertools import*
_, s = open(0)
print(min(b+c for b, c in zip(*[([0]+list(accumulate(map(int, s[::a].translate(str.maketrans("EW","01"[::a])))))[:-1])[::a] for a in [-1,1]])))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
n  = int(input())
#  = map(int, input().split())
l  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

l = sorted(l)[::-1]

ans = 0
for i1 in range(len(l)):
    for i2 in range(i1+1, len(l)):
        for i3 in range(i2+1, len(l)):
            l_ = sorted([l[i1], l[i2], l[i3]])[::-1]
            if l_[0] < l_[1] + l_[2]:
                ans += 1
            else:
                break

print(ans)

Submission status: Runtime Error

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

t = [0] * n
if s[0] == 'E':
    s[0] = 1

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

ans = 10000000000

for i in range(n):
    if i == 0:
        a = 0
    else:
        a = i - t[i-1]
    b = t[n-1] - t[i]
    c = a + b

    ans = min(ans, c)

print(ans)

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
DP=[0]*N
l=0
for i,x in enumerate(S):
  DP[i]+=l
    if S[i]=="W":
      l+=1
r=0
for j,y in enumerate(S[::-1]):
  DP[N-1-j]+=r
    if S[N-1-j]=="E":
      r+=1
print(min(DP))

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
DP=[0]**N
DP[0]=S[1:].count("E")
for i in range(1,N):
  if S[i]=="E":
    DP[i]=DP[i-1]-1+S[:i].count("W")
  else:
    DP[i]=DP[i-1]+S[:i].count("W")
print(min(DP))

Submission status: Runtime Error

Solution code:
ARC098 - Attention
 
Shoji Taisaku(FJK 庄司 大作)
2020/03/11 (水) 11:33
受信トレイ; 送信済みトレイ
宛先:
Shoji Taisaku(FJK 庄司 大作);
N=int(input())
S=input()

left=[0]*N
right=[0]*N
to_change=[0]*N

if S[0]=="E":
    right[0]=1
else:
    left[0]=1

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

to_change[0]=right[-1]-right[0]
to_change[-1]=left[-2]

for j in range(1,N-1):
    to_change[j]=(right[-1]-right[j])+(left[j-1])
#print(to_change)
print(min(to_change))

Submission status: Runtime Error

Solution code:
from collections import Counter
n = int(input())
s = input()
c = Counter(s)
tmp = c['E'] - (1 if s[0] == 'E' else 0)
best = tmp
for i in range(1, n):
    if s[i - 1] == 'W'
        tmp += 1
    else:
        tmp -= 1
    if tmp < best:
        best = tmp
print(best)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
CS = [0]*N
for i in range(1,N):
  if S[i] == 'W':
    CS[i] = CS[i-1]+1
  else:
    CS[i] = CS[i-1]-1
min_value = min(CS)
Leader = CS.index(min_value) + 1
counter = 0
for i in range(1,Leader):
  if CS[i]<CS[i+1]:
    counter += 1
for i in range(Leader,N-1):
  if CS[i]>CS[i+1]:
    counter +=1
print(counter)

Submission status: Runtime Error

Solution code:
import sys

n = int(sys.stdin.readline().strip())
a_list = [int(v) for v in sys.stdin.readline().strip().split()]


left_sums = []
left_xors = []
prev_sum = 0
prev_xor = 0

for a in a_list:
  nsum = a + prev_sum
  left_sums.append(nsum)
  prev_sum = nsum
    
  nxor = prev_xor ^ a
  left_xors.append(nxor)
  prev_xor = nxor

    
def sum_subarr(l, r):
  if l == 0:
    return left_sums[r - 1]
  else:
    return left_sums[r - 1] - left_sums[l - 1]


def xor_subarr(l, r):
  if l == 0:
    return left_xors[r - 1]
  else:
    return left_xors[r - 1] ^ left_xors[l - 1]

result = 0
for i in range(len(a_list)):
  for j in range(1, len(a_list) - i + 1):
    if sum_subarr(i, i + j) == xor_subarr(i, i + j):
      result += 1
      
print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
W = S.count('W')
E = S.count('E')
counter = len(S)
cost = E
for i in range(len(S)):
  if S[i] = 'E':
    cost -= 1
    if cost < counter:
      counter = cost
  else:
    cost +=1
print(counter)

Submission status: Runtime Error

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

min_n = s[1:].count('E')
for i in range(1, n):
    tmp_n += s[i-1].count('W') - s[i].count('E')
    min_n = min(tmp_n, min_n)

print(min_n)

Submission status: Runtime Error

Solution code:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;

  vector<int> ruiseki_E(N, 0);
  vector<int> ruiseki_W(N, 0);
  for (int i = 1; i < N; ++i) {
    if (S[i - 1] == 'W') {
      ruiseki_E[i] = ruiseki_E[i - 1] + 1;
    }
  }
  for (int i = N - 2; i != -1; --i) {
    if (S[i - 1] == 'E') {
      ruiseki_E[i] = ruiseki_E[i + 1] + 1;
    }
  }
  int ans = ruiseki_E[0] + ruiseki_W[0];
  for (int i = 1; i < N; ++i) {
    ans = min(ans, ruiseki_E[0] + ruiseki_W[0]);
  }
  cout << ans << endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
import math
N = int(input())
inList = [int(s) for s in input().split(" ")]

ans = 0

for l in range(N):
    sum = 0
    xor = 0
    for r in range(l,N):
        sum += inList[r]
        xor = xor ^ inList[r]
        if sum == xor:
            ans += 1
        else:
            break
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
import sys
sys.setrecursionlimit(100000+1)
s = input()

ce = {}
#iより右に居て，東を向いている人の数
def counte(s,i):
    if i in ce:
        return ce[i]
    elif i == n-1:
        return 0
    else:
        ce[i] = counte(s,i+1) + s[i+1:i+2].count("E")
        return ce[i]

cw = {}
#iより←に居て，西を向いている人の数
def countw(s,i):
    if i in cw:
        return cw[i]
    elif i == 0:
        return 0
    else:
        cw[i] = countw(s,i-1) + s[i-1:i].count("W")
        return cw[i]

minew = n
for i in range(0,n):
    ew = counte(s,i)+countw(s,i)
    if ew < minew:
        minew = ew
print(minew)

Submission status: Runtime Error

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

ce = {}
#iより右に居て，東を向いている人の数
def counte(s,i):
    if i in ce:
        return ce[i]
    elif i == n-1:
        return 0
    else:
        ce[i] = counte(s,i+1) + s[i+1:i+2].count("E")
        return ce[i]

cw = {}
#iより←に居て，西を向いている人の数
def countw(s,i):
    if i in cw:
        return cw[i]
    elif i == 0:
        return 0
    else:
        cw[i] = countw(s,i-1) + s[i-1:i].count("W")
        return cw[i]

minew = n
for i in range(0,n):
    ew = counte(s,i)+countw(s,i)
    if ew < minew:
        minew = ew
print(minew)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef string str;
const long long INF = INT64_MAX;

#define REP(i, n) for(ll i=0;i<n;i++)
#define ASC(v) sort((v).begin(), (v).end())
#define DESC(v) sort((v).rbegin(), (v).rend())
#define INV(v) reverse((v).rbegin(), (v).rend())
#define FOLDL(src, dst, lambda) partial_sum((src).begin(), (src).end(), (dst).begin(), lambda)
#define FOLDR(src, dst, lambda) INV(src);partial_sum((src).begin(), (src).end(), (dst).begin(), lambda);INV(dst)
#define CUMSUML(src, dst) FOLDL(src, dst, [](auto a, auto b) {return a+b;})
#define CUMSUMR(src, dst) FOLDR(src, dst, [](auto a, auto b) {return a+b;})
#define DESC(v) sort((v).rbegin(), (v).rend())
#define ERASE(v, i) v.erase(v.begin() + i)
#define UNIQ(v) ASC(v);erase(unique(v.begin(), v.end()), v.end())
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);

void solve() {
    ll N;
    str S;
    cin >> N >> S;
    vll D(N) ,E(N), Es(N), W(N), Ws(N);
    for(int i=0;i<N;i++) (S[i] == 'E' ? E[i] : W[i]) = 1;
    CUMSUML(W, Ws);
    CUMSUMR(E, Es);
    ll ans = INF;
    for(int i = 0; i < N; i++) {
        ll w = (i == 0 ? 0 : Ws[i - 1]);
        ll e = (i == N - 1 ? 0 : Es[i + 1]);
        ans = min(ans, e+w);
    }
    cout << ans << endl;
}

int main() {
    FIO;
    solve();
    return 0;
}

Submission status: Runtime Error

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

East = [0] * N
West = [0] * N

for i,s in enumerate(list(S)):
    if s == 'W' : 
        if i==0:
            East[i] = 1
        else:
            East[i] = East[i-1] + 1
    else:
        East[i] = East[i-1]
i = N-1
for s in reversed((list(S))):
    if s == 'E' : 
        if i==N-1:
            West[i] = 1
        else:
            West[i] = West[i+1] + 1
    else:
        West[i] = West[i+1]
    i -= 1
            
ans = 2**31-1

for i in range(N):
    if i == 1:
        ans2 = West[1]
    elif i == N-1:
        ans2 = East[N-2]
    else:
        ans2 = East[i-1]+West[i+1]
    ans = min(ans,ans2)
    
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A_lst = input().split()
cp_A_lst = []
for i in range(N):
    A_lst[i] = int(A_lst[i])
    cp_A_lst.append(A_lst[i])


A_binary_lst = []


for i in range(N):
    tmp_lst = []
    for j in range(20):
        if cp_A_lst[i] % 2 == 0:
            tmp_lst.insert(0, 0)
        else:
            tmp_lst.insert(0, 1)
        cp_A_lst[i] = int(cp_A_lst[i]/2)
    A_binary_lst.append(tmp_lst)

A_arr = np.array(A_lst)
A_binary_arr = np.array(A_binary_lst)
count = 0

for i in range(N):
    for j in range(i, N):
        binary_xor_arr = np.sum(A_binary_arr[i:j+1], axis=0)
        binary_xor_arr = binary_xor_arr % 2
        xor_num = 0
        for k in range(20):
            xor_num += binary_xor_arr[k] * 2**(19 - k) #  10進法に書き直す
        A_sum = sum(A_arr[i:j+1])
        if A_sum >= 2**20:
            break
        if A_sum == xor_num:
            count += 1

print(count)

Submission status: Runtime Error

Solution code:
def f(i):
    if i > len(S)-2:
        return 10**6
    ans = S[:i].count('W')+S[i+1:].count('E')
    return min(ans,f(i+1))

n=input()
S = input()+'_'

print(f(0))

Submission status: Runtime Error

Solution code:
def f(i):
    if i > len(S)-2:
        return 10**6
    ans = S[:i].count('W')+S[i:].count('E')
    return min(ans,f(i+1))

input()
S = input()+'_'

print(f(0))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N=int(input())
s=input()
import collections
a=collections.Counter(s)
a=sorted(a.items(),key=lambda x:x[0])
e=a[0][1]
w=a[1][1]

ans=[0,0]
dp=[-1 for _ in range(N)]

for i in range(N):
    dp[i]=ans[1]+(e-ans[0])
    if(s[i]=='W'):
        ans[1]+=1
    else:
        ans[0]+=1


print(min(dp))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [format(int(a), "b").zfill(20) for a in input().split()]

Sum = [0] * 20
start = 0
end = 1
Ans = N
for i in range(20):
    Sum[i] += int(A[0][i])
while end < N:
    for i in range(20):
        Sum[i] += int(A[end][i])
    if 2 in Sum:
        while 2 in Sum:
            Ans += end - start - 1
            for j in range(20):
                Sum[j] -= int(A[start][j])
            start += 1
    end += 1
while start < N and 2 in Sum:
    for j in range(20):
        Sum[j] -= int(A[start][j])
    start += 1
Ans += (N - start - 1) * (N - start) // 2
print(Ans)

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
a = 0
b = 0
num = 0
while a + b < N-2:
	if S[a] != 'W':
		while S[a] != 'W':
			a += 1
		a += 1
		num += 1
	else:
		num += 1
		a += 1
	if S[N - b - 1] != 'E':
		while S[N - b - 1] != 'E':
			b +=1
		b += 1
		num += 1
	else:
		b += 1
if S[a] == 'W' and S[N-b-1] == 'E':
	num += 1
print(num)

Submission status: Runtime Error

Solution code:
N=int(input())
S=input()
nummin = 3e5
a = 0
b = 0
num = 0
while a + b < N-2:
	if S[a] != 'W':
		while S[a] != 'W':
			a += 1
		a += 1
		num += 1
	else:
		num += 1
		a += 1
	if S[N - b - 1] != 'E':
		while S[N - b - 1] != 'E':
			b +=1
		b += 1
		num += 1
	else:
		b += 1
if S[a+1] == 'W' and S[N-b-2] == 'E':
	num += 1
print(num)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def main():
	N = int(input())
	a = input()
    
	W = [0]
	E = [0]
	for i in range(N):
		if a[i] == 'W':
			W[i+1] = W[i] + 1
			E[i+1] = E[i]
		else:
			W[i+1] = W[i]
			E[i+1] = E[i] + 1
	E_max = E[-1]
	n_min = N
	for i in range(N):
		t = W[i] + E_max - E[i+1]
		if t < n_min:
			n_min = t
	print (n_min)
main()

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
 
lE = [0] * N 
lW = [0] * N 
le = lw = 0 
for i in range(N):
    if S[i] == "E":
        le += 1
    else:
        lw += 1
    lE[i] = le
    lW[i] = lw
 
rtn = lE[-1] - lE[0] 
 
for i in range(1, N - 1): 
    tmp = lW[i-1] + lE[-1] - lE[i]
    if tmp <= rtn:
        rtn = tmp 
 
most_east = lW[-2] 
if most_east <= rtn:
    rtn = count
 
print(rtn)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = input()
a = input()
S = [a[i:i+1] for i in range(len(a))]
left = []
right = []

answer = n

for (i, x) in enumerate(S):
    a = 0
    left = S[:i]
    right = S[i+1:]
    a += left.count("W")
    a += right.count("E")
    
    if answer > a : answer = a
 print(answer)

Submission status: Runtime Error

Solution code:
n=int(input())
s=input()
k=n
for i in range(n):
    a=s[:i]
    b=s[i+1:]
    l=a.count("W")+b.count("E")
    if l<k:
        k=l
print(k

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, K, Q = [int(i) for i in input().split(" ")]
A = [int(a) for a in input().split(" ")]

_min = min(A)
A.sort()
for k in range(K):
    _max = A.pop(0)
print(_max - _min)

Submission status: Runtime Error

Solution code:
input()
s = input()

def f(i):
  return s[0:i].count('W') + s[i:].count('E')

x = len(s)

for i in range(len(s)):
  x = min(x, f(i))

print(min(arr))

Submission status: Runtime Error

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


min = 10000000

N = int(N)

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


print(min)

Submission status: Runtime Error

Solution code:
N=int(input())
A=input().split(' ')

for i in range(N):
	A[i]=int(A[i])

num=0
for i in range(N):
	plus=0
	xor=0
	for j in range(N-i):
		plus=plus+A[i+j]
		xor=xor^A[i+j]
		if plus==xor:
			num=num+1
		else:
			break

print(num)

Submission status: Runtime Error

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

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

for i in range(length):

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

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

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

print(min_num)

Submission status: Runtime Error

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

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

for i in range(len(S)):

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

  right -= int(S[i + 1] == "E")

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

print(min_num)

Submission status: Runtime Error

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

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

print(min(turns_num_list))

Submission status: Runtime Error

Solution code:
# https://beta.atcoder.jp/contests/arc098/tasks/arc098_a
length = int(input())
str = input()
 
# 人数を計算する
def count(s, lp, min):
	cnt = 0
	for i in range(len(s)):
		if i < lp and s[i] == 'W':
			cnt += 1
		elif i > lp and s[i] == 'E':
			cnt += 1
 		if cnt >= min:
 			return cnt # 最小超えたら評価をやめる
	return cnt
 
min = length
for lp in range(length):
	cnt = count(str, lp, min)
	if min > cnt:
		min = cnt
 
print(min)

Submission status: Runtime Error

Solution code:
for i in range(N): 
	w=0
	e=0
	for j in range(i+1,N):
		if S[j]=="E":
			e+=1
	for j in range(0,i):
		if S[j]=="W":
			w+=1
	West.append(w)
	East.append(e)
print(West,East)
min=West[0]+East[0]
min_i=0
for i in range(1,N):
	if min>West[i]+East[i]:
		min=West[i]+East[i]
		min_i=i
print(min)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))

res = 0
asum = [0]
axor = [0]

for i in A:
    asum.append(asum[-1] + i)
    axor.append(axor[-1] ^ i)

for l in range(N):
    ok = l+1
    ng = N+1
    while(ng - ok > 1):
        r = (ok + ng) // 2
        s = (axor[r] ^ axor[l])
        t = (asum[r] - asum[l])
        if s == t:
            ok = r
        else:
            ng = r
    
    res += ok - l
         
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
A =list(map(int, input().split(" ")))

res = 0
asum = [0]
axor = [0]

for i in A:
    asum.append(asum[-1] + i)
    axor.append(axor[-1] ^ i)

for l in range(N):
    ok = l+1
    ng = N+1
    while(ng - ok > 1):
        r = (ok + ng) // 2
        s = (axor[r] ^ axor[l])
        t = (asum[r] - asum[l])
        if s == t:
            ok = r
        else:
            ng = r
    
    res += ok - l
         
print(res)

Submission status: Runtime Error

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

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

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

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

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

Submission status: Runtime Error

Solution code:
def foo (s):
    ans = 0
    count = 0
    for i in s:
        if i == 'W':
            count += 1
    elif count :
        count -= 1
        ans += 1
    else:
        pass
    return ans
  
n = input()
s = input()
 
print(foo(s))

Submission status: Runtime Error

Solution code:
def foo (s):
    ans = 0
    count = 0
    for i in s:
        if i == 'W':
            count += 1
    elif count :
        count -= 1
        ans += 1
    else:
        pass
    return ans
  
  n = input()
s = input()
 
print(foo(s))

Submission status: Runtime Error

Solution code:
def foo (s):
    ans = 0
    count = 0
    for i in s:
        if i == 'W':
        count += 1
    elif count :
        count -= 1
        ans += 1
    else:
        pass
    return ans
 
n = input()
s = input()

print(foo(s))

Submission status: Runtime Error

Solution code:
def foo (s):
    ans = 0
    count = 0
    for i in s:
        if i == 'W':
        count += 1
    elif count :
        count -= 1
        ans += 1
    else:
        pass
    return ans

n = input()
s = input()
 
print(foo(s))

Submission status: Runtime Error

Solution code:
def foo (s):
	ans = 0
	count = 0
	for i in s:
		if   (not count) and (i == 'E'):pass
		elif not count: count += 1
		elif i == 'E': count -= 1; ans += 1
		else: count += 1
	return ans
  
  n = int(input())
  s = input()
  print(foo(s))

Submission status: Runtime Error

Solution code:
n, k, q = map(int, input().split())
a = [int(x) for x in input().split()]
m = None
for x in a:
    b = []
    c = []
    for y in a:
        if y < x:
            if len(c) >= k:
                c.sort()
                b.extend(c[:len(c) - k + 1])
            c = []
        else:
            c.append(y)
    if len(c) >= k:
        c.sort()
        b.extend(c[:len(c) - k + 1])
    if len(b) >= q:
        b.sort()
        if m is None or m > b[q - 1] - x: m = b[q - 1] - x
print(m)

Submission status: Runtime Error

Solution code:
8
WWWWWEEE

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))

answer = N
mark = 0
for i in range(N-1):
    #print('%d, %d, %d' % (i, mark, answer))
    if i < mark:
        answer += mark - i
        v = A[i]
        for x in range(i+1, mark+1):
            v += A[x]
        start_j = mark + 1
    else:
        v = A[i]
        start_j = i + 1
    for j in range(start_j, N):
        vx = v ^ A[j]
        vp = v + A[j]
        if vx == vp:
            answer += 1
            v = vx
            mark = j
        else:
            break
print(answer)

Submission status: Runtime Error

Solution code:
# E
from heapq import heappush, heappop
import numpy as np

N, K, Q = map(int, input().split())
A = list(map(int, input().split()))

A_args = np.argsort(A)

nodeid = np.ones(N).astype("int32")
canput = np.ones(N)
nodeid_dict = dict()
nodeid_dict[1] = [0, N]

# initial output
res = A[A_args[Q-1]] - A[A_args[0]]

ind_del = 0

for i in range(N):
    
    nid = A_args[i]
    
    # update nodeid
    ndid = nodeid[nid]
    nodeid[nid] = 0
    left, right = nodeid_dict[ndid]
    nodeid[(nid+1):right] = i+2
    nodeid_dict[nid] = [left, nid]
    nodeid_dict[i+2] = [nid+1, right]

    # update canput
    canput[nid] = 0
    
    canput[left:nid] = 0
    h = []
    for j in range(left, nid):
        heappush(h, (-A[j], j))
    while len(h) >= K:
        _, j = heappop(h)
        canput[j] = 1
    
    canput[(nid+1):right] = 0
    h = []
    for j in range(nid+1, right):
        heappush(h, (-A[j], j))
    while len(h) >= K:
        _, j = heappop(h)
        canput[j] = 1
    
    # break
    if canput.sum() <= Q:
        break
        
    # calculate
    q = 0
    qmin = 10**9
    qmax = 1
    for i in range(N):
        if canput[A_args[i]] == 1:
            q += 1
            qmin = min(A[A_args[i]], qmin)
            qmax = max(A[A_args[i]], qmax)
            if q == Q:
                r = qmax - qmin
                break
    res = min(res, r)
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))

answer = N
for i in range(N-1):
    v = A[i]
    for j in range(i+1, N):
        vx = v ^ A[j]
        vp = v + A[j]
        if vx == vp:
            answer += 1
            v = vx
        else:
            break
print(answer)

Submission status: Runtime Error

Solution code:
a = int(input())
b= [int(x) for x in input().split()]
c = a
for s in range(a):
  d = b[s]
  e = b[s]
  for t in b[s+1::]:
    d = d ^ t
    e = e + t
    if d == e:
      c += 1
    else:
      break
print(c)

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
_min = -1
for i in range(N):
    left_str = S[:i]
    right_str = S[i+1:]
    left = count('W', left_str)
    right = count('E', right_str)
    total = left + right
    if _min == -1:
        _min = total
    elif total < _min:
        _min = total
print(_min)
def count(item, str1):
    counter = 0
    for c in str1:
        if c == item:
            counter += 1
    return counter

Submission status: Runtime Error

Solution code:
N = int(input())
S = input()
_min = -1
for i in range(N):
    left_str = S[:i]
    right_str = S[i+1:]
    left = count('W', left_str)
    right = count('E', right_str)
    total = left + right
    if _min == -1:
        _min = total
    elif total < _min:
        _min = total
print(_min)
def count(item, str):
    counter = 0
    for c in str:
        if c == item:
            counter += 1
    return counter

Submission status: Runtime Error

Solution code:
def a(n, s):
    score = n
    for i in range(n):
        tmp = 0
        for j, c in enumerate(s):
            if j is i:
                continue
            if j < i:
                if c is 'W':
                    tmp += 1
            else:
                if c is 'E':
                    tmp += 1
        if tmp < score:
            score = tmp
    print(score)


n = input()
s = input()

a(n, s)

Submission status: Runtime Error

Solution code:
N=int(input())
A=input()
for i in range (len(A)):
    if A[i]=='W':
        A[i]=-1#westside
    else:
        A[i]=1#eastside
print(A)
min_output=len(A)
for j in range (len(A)):
    if min_output>A[0:j].count(-1)+A[j+1:].count(1):
        min_output=A[0:j].count(-1)+A[j+1:].count(1)
print(min_output)

Submission status: Runtime Error

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

    AW = [0]*(N+1)
    AE = [0]*(N)

    wcount = 0
    ecount = 0
    for i in range(N):
        if S[i] == "W":
            wcount += 1
        else:
            ecount += 1
        AW[i] = wcount
        AE[i] = ecount

    MIN = 1000000

    for i in range(N):
        tmp = 0
        tmp += AW[i-1]
        tmp += AE[-1] - AE[i]
        MIN = min(MIN, tmp)
    print(MIN)

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