Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

def main():
  (n,q),c,*z=[[*map(int,t.split())]for t in open(0)]
  a=[0]*q
  j,*b=t=[0]*9**6
  for i in sorted(range(q),key=lambda x:z[x][1]):
    l,r=z[i]
    while j<r:
      k=c[j]
      j+=1
      x,y,b[k]=b[k]+1,j+1,j
      while x<=n:
        t[x]+=1
        x=x+(x&-x)
      while y<=n:
        t[y]-=1
        y=y+(y&-y)
    s=0
    while l:
      s+=t[l]
      l=l-(l&-l)
    a[i]=s
  print(' '.join(map(str,a)))
main()

### FIXED CODE ###

import sys
from operator import itemgetter
buf = sys.stdin.buffer
input = buf.readline
class BIT:
    def __init__(self, n):
        self.n = n
        self.tree = [0] * (n + 1)
    def add(self, i, x):
        n, tree = self.n, self.tree
        while i <= n:
            tree[i] += x
            i += i & -i
    def sum(self, i):
        tree = self.tree
        s = 0
        while i:
            s += tree[i]
            i -= i & -i
        return s
def main():
    n, q = map(int, input().split())
    res = [0] * q
    *c, = map(int, input().split())
    q = [(i, l, r) for i, (l, r) in enumerate(zip(*[map(int, buf.read().split())] * 2))]
    q.sort(key=itemgetter(2))
    bit = BIT(n + 2)
    add, sum = bit.add, bit.sum
    b = [1] * (n + 1)
    j = 1
    for i, l, r in q:
        while j <= r:
            d = c[j - 1]
            j += 1
            k = b[d]
            b[d] = j
            add(k + 1, 1)
            add(j + 1, -1)
        res[i] = sum(l + 1)
    print(' '.join(map(str, res)))
main()
### BUGGY CODE ###

n, k = map(int, input().split())
arr = list(map(int, input().split()))
dp = [float("inf")] * len(arr)
dp[0] = 0
for j in range(1, len(arr)):
    for i in range(1, k + 1):
        if j >= i:
            dp[j] = min(dp[j], dp[j - i] + abs(arr[j] - arr[j - i]))
        else:
            break
print(dp[-1])


### FIXED CODE ###

n, k = map(int, input().split())
arr = list(map(int, input().split()))
dp = [float("inf")] * len(arr)
dp[0] = 0
for j in range(1, len(arr)):
    for i in range(1, k + 1):
        if j >= i:
            dp[j] = min(dp[j], dp[j - i] + abs(arr[j] - arr[j - i]))
        else:
            break
print(dp[-1])
### BUGGY CODE ###

input()
s = input()

def find_cost(s):
    counter, i, j = 0, 0, len(s)-1
    while i < j:
        while i < len(s) and s[i] != 'W':
                i += 1
        while j >= 0 and s[j] != 'R':
                j -= 1
        if i < j:
            s = list(s)
            s[i], s[j] = s[j], s[i]
            s = "".join(s)
            counter += 1
        i += 1
        j -= 1

    # print(counter)

    for i in range(0, len(s) - 2):
        if s[i] == 'W' and s[i + 1] == 'R':
            counter += 1

    print(counter)


find_cost(s)

### FIXED CODE ###

input()
s = input()

def find_cost(s):
    counter, i, j = 0, 0, len(s)-1
    while i < j:
        while i < len(s) and s[i] != 'W':
                i += 1
        while j >= 0 and s[j] != 'R':
                j -= 1
        if i < j:
            # s = list(s)
            s[i], s[j] = s[j], s[i]
            # s = "".join(s)
            counter += 1
        i += 1
        j -= 1

    # print(counter)

    for i in range(0, len(s) - 2):
        if s[i] == 'W' and s[i + 1] == 'R':
            counter += 1

    print(counter)

s = list(s)
find_cost(s)
### BUGGY CODE ###

import sys
input = sys.stdin.readline


class RollingHash:
    def __init__(self, string):
        self.n = len(string)
        self.BASE = 1234
        self.MASK30 = (1 << 30) - 1
        self.MASK31 = (1 << 31) - 1
        self.MASK61 = (1 << 61) - 1
        self.MOD = self.MASK61
        self.hash = [0] * (self.n + 1)
        self.pow = [1] * (self.n + 1)

        for i, char in enumerate(string):
            self.hash[i + 1] = self.calc_mod(self.mul(self.hash[i], self.BASE) + ord(char))
            self.pow[i + 1] = self.calc_mod(self.mul(self.pow[i], self.BASE))

    def calc_mod(self, x):
        """x mod 2^61-1 を返す。"""
        xu = x >> 61
        xd = x & self.MASK61
        x = xu + xd
        if x >= self.MOD:
            x -= MASK61
        return x

    def mul(self, a, b):
        """a*b mod 2^61-1 を返す。"""
        au = a >> 31
        ad = a & self.MASK31
        bu = b >> 31
        bd = b & self.MASK31
        mid = ad * bu + au * bd
        midu = mid >> 30
        midd = mid & self.MASK30
        return self.calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd)

    def get_hash(self, l, r):
        """string[l:r]のハッシュ値を返す。O(1)"""
        res = (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.MOD
        return res


n = int(input())
s = [input()[:-1] for i in range(n)]


s.sort(key=len)
str_set = set()
cnt = 0

for string in s:
    string = string[::-1]
    n = len(string)
    rh = RollingHash(string)

    for r in range(1, n + 1):
        if rh.get_hash(0, r) in str_set:
            cnt += 1

    set_char = set()
    for r in range(1, n + 1)[::-1]:
        set_char.add(string[r - 1])
        for char in set_char:
            if char == string[r - 1]:
                continue
            h = (rh.hash[r - 1] * rh.BASE + ord(char)) % rh.MOD
            if h in str_set:
                cnt += 1
    str_set.add(rh.get_hash(0, n))

print(cnt)

### FIXED CODE ###

import sys
input = sys.stdin.readline


class RollingHash:
    def __init__(self, string):
        self.n = len(string)
        self.BASE = 1234
        self.MOD = (1 << 45) - 1
        self.hash = [0] * (self.n + 1)
        self.pow = [1] * (self.n + 1)

        for i, char in enumerate(string):
            self.hash[i + 1] = (self.hash[i] + ord(char)) * self.BASE % self.MOD
            self.pow[i + 1] = (self.pow[i] * self.BASE) % self.MOD

    def get_hash(self, l, r):
        """string[l:r]のハッシュ値を返す。O(1)"""
        res = (self.hash[r] - self.hash[l] * self.pow[r - l]) % self.MOD
        return res


n = int(input())
s = [input()[:-1] for i in range(n)]


s.sort(key=len)
str_set = set()
cnt = 0

for string in s:
    string = string[::-1]
    n = len(string)
    rh = RollingHash(string)

    for r in range(1, n + 1):
        if rh.get_hash(0, r) in str_set:
            cnt += 1

    set_char = set()
    for r in range(1, n + 1)[::-1]:
        set_char.add(string[r - 1])
        for char in set_char:
            if char == string[r - 1]:
                continue
            h = (rh.hash[r - 1] + ord(char)) * rh.BASE % rh.MOD
            if h in str_set:
                cnt += 1
    str_set.add(rh.get_hash(0, n))

print(cnt)
### BUGGY CODE ###

import sys
n = int(input())
a = [int(i) for i in input().split()]
if 0 in a:
    print(0)
    sys.exit()
else:
    acc = 1
    for i in a:
        acc *= i
    if acc > 10**18:
        print(-1)
        sys.exit()
print(acc)


### FIXED CODE ###

import sys
n = int(input())
a = [int(i) for i in input().split()]
tmp = 10**18
if 0 in a:
    print(0)
    sys.exit()
else:
    acc = 1
    for i in a:
        acc *= i
        if acc > tmp:
            print(-1)
            sys.exit()
print(acc)
### BUGGY CODE ###

import numpy as np
def min_mod2019(L, R):
    min_mod = 2019
    imin=0
    jmin=0
    for i in range(L, R):
        for j in range(i+1, R+1):
            #min_mod = min(min_mod, (i%2019)*(j%2019)%2019)
            if min_mod > (i%2019)*(j%2019)%2019:
                min_mod = (i%2019)*(j%2019)%2019
                imin=i
                jmin=j
    return min_mod

if __name__ == '__main__':
    L, R =map(int,input().split(' '))
    print(min_mod2019(L, R))

### FIXED CODE ###

import numpy as np
def min_mod2019(L, R):

    mod_list = [i for i in range(2019)]

    imin=0
    jmin=0
    
    if L%2019 < R%2019 and R < L+2019:
        mod_list = mod_list[L%2019:R%2019 + 1]
        min_mod = 2019
        for i in range(len(mod_list)-1):
            for j in range(i+1, len(mod_list)):
                if min_mod > (mod_list[i]*mod_list[j])%2019:
                    #imin=mod_list[i]
                    #jmin=mod_list[j]
                    min_mod=(mod_list[i]*mod_list[j])%2019
        return min_mod
    return 0

if __name__ == '__main__':
    L, R =map(int,input().split(' '))
    print(min_mod2019(L, R))
### BUGGY CODE ###

import sys
import itertools
import numpy as np

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
n = int(readline())
A = list(map(lambda x: x.split(), read().decode().split('\n')))

ids = [[0 for j in range(n)] for i in range(n)]

v = 0
for i in range(n):
    for j in range(n):
        ids[i][j] = v
        v += 1


# convert match to index-node num
for i in range(n):
    for j in range(n - 1):
        a = i
        b = int(A[i][j]) - 1
        if a > b:
            a, b = b, a
        A[i][j] = ids[a][b]
        
to = [[] for i in range(v)]
for i in range(n):
    for j in range(n - 2):
        to[A[i][j]].append(A[i][j + 1])


visited = [False for i in range(v)]
calculated = [False for i in range(v)]
dp = [0 for i in range(v)]

def dfs(node):
    if visited[node]:
        if not calculated[node]:
            print(-1)
            exit()
        return dp[node]

    visited[node] = True
    dp[node] = 1
    for u in to[node]:
        dp[node] = max(dfs(u) + 1, dp[node])

    calculated[node] = True
    return dp[node]

ans = 0
for i in range(v):
    ans = max(ans, dfs(i))
print(ans)



### FIXED CODE ###

import sys
import itertools
import numpy as np

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
N = int(readline())
A = [[-1] + [int(x) - 1 for x in row.split()][::-1] for row in readlines()]

# A.append([-1])

def f(cand):
    cand1 = set()
    can_use = []
    for n in cand:
        if A[A[n][-1]][-1] == n:
            can_use.append(n)
    for n in can_use:
        A[n].pop()
        cand1.add(n)
        if A[n][-1] != -1:
            cand1.add(A[n][-1])
    return len(can_use), cand1

answer = 0
rest = N*(N-1)
cand = set(range(N))
while True:
    answer += 1
    x, cand = f(cand)
    if x == 0:
        answer = -1
        break
    rest -= x
    if rest == 0:
        break

print(answer)
### BUGGY CODE ###

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

E = [(p+1)/2 for p in P]
M = [sum(E[i:i+K]) for i in range(N-K+1)]
print(max(M))


### FIXED CODE ###

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

M = [sum(P[:K]) / 2 + K / 2]

for i in range(1, N-K+1):
    M.append(M[i-1] - P[i-1]/2 + P[i-1+K]/2)


print(max(M))
### BUGGY CODE ###

M = 10 ** 9 + 7
s = input().strip()

num = 0
q_list = []
d = 1
l = len(s)
for i in range(l - 1, -1, -1):
	c = s[i]
	if c != "?":
		num = (num + int(c) * d) % 13
	else:
		q_list.append(d)
	d = (d * 10) % 13

counts = [0] * 13
counts[0] = 1
for d in q_list:
	new_counts = [0] * 13
	for n in range(10):
		r = (d * n) % 13
		for m in range(13):
			k = (r + m) % 13
			new_counts[k] = (new_counts[k] + counts[m]) % M
	counts = new_counts
print(counts[(13 + 5 - num) % 13])


### FIXED CODE ###


M = 10 ** 9 + 7
s = raw_input().strip()

num = 0
q_list = []
d = 1
for i in range(len(s) - 1, -1, -1):
	c = s[i]
	if c != "?":
		num = (num + int(c) * d) % 13
	else:
		q_list.append(d)
	d = (d * 10) % 13

counts = [0] * 13
counts[0] = 1
counts_save = [0] * 13
for d in q_list:
	for i in range(13):
		counts_save[i] = counts[i] % M
	for n in range(1, 10):
		r = (d * n) % 13
		for m in range(13):
			counts[(r + m) % 13] += counts_save[m]

print(counts[(13 + 5 - num) % 13] % M)
### BUGGY CODE ###

N = int(input())

import numpy as np

for i in range(1,N+1):

    right = 2 * i


    er = int(np.ceil(np.sqrt(right)))

    count = 0

    for x in range(1,er):
        for y in range(1,er):
    
            if x + y > np.sqrt(right):
                continue
      
            else:
                for z in range(1,er):
            
                    if (x + y > np.sqrt(right)) | (y + z > np.sqrt(right)) | (x + z > np.sqrt(right)):
                        continue
                    
                    else:
                        if (x + y) ** 2 + (y + z) ** 2 + (z + x) ** 2 == 2*i:
                            count += 1
            
    print(count)     

### FIXED CODE ###

n = int(input())
ans = [0 for a in range(10050)]


for i in range(1,105):
  for j in range(1,105):
    for k in range(1,105):
      v = i*i+j*j+k*k+i*j+j*k+k*i;
      
      if v<10050:
        ans[v]+=1

for i in range(n):
  print(ans[i+1])
### BUGGY CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
m = 0

for i in range(n-k+1):
    t = sum(p[i:i+k])
    if t > m:
        m = t

print((m+k)/2)

### FIXED CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))

a= sum(p[0:k])
ans = a

for i in range(n-k):
    a -= p[i]
    a += p[i+k]
    ans = max(ans,a)

print((ans+k)/2)
### BUGGY CODE ###

import numpy as np

N,K,Q = [int(a) for a in input().split()]

score = np.full(N, K, dtype=int)

for i in range(Q):
    p = int(input())
    score -= 1
    score[p-1] += 1 

is_win = score > 0

for res in is_win:
    if res:
        print("Yes")
    else:
        print("No")

### FIXED CODE ###

import numpy as np
import sys

input = sys.stdin.readline

N,K,Q = [int(a) for a in input().split()]

score = np.full(N, K, dtype=int)

for i in range(Q):
    p = int(input())
    score[p-1] += 1 

score -= Q

is_win = score > 0

res = ["Yes" if a else "No" for a in is_win]

print("\n".join(res) + "\n")
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
# print(A)
# print(len(A))

ans = 0
for i in range(len(A)-1):
    tmp = 0
    for j in range(1,len(A)-i):
        # print(A[i],'A[i]',A[i+j],'A[i+j]')
        tmp_ai = A[i] % (10**9 + 7)
        tmp_aij = A[i+j] % (10**9 + 7)
        tmp += tmp_ai * tmp_aij
        # print(tmp,'tmp')
    tmp %= (10**9 + 7)
    ans += tmp

ans %= (10**9 + 7)
print(ans)

### FIXED CODE ###

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

sumA = sum(A)
ans = 0
for i in range(len(A)-1):
    sumA -= A[i]
    tmp1 = A[i] % (10**9 + 7)
    tmp2 = sumA % (10**9 + 7)
    ans += tmp1 * tmp2
    ans %= (10**9 + 7)
    
print(ans)
### BUGGY CODE ###

from collections import defaultdict
debug = print
def debug(*va): pass

N, M = map(int, input().split())
dummynode = N
N += 1

nextnodes = [defaultdict(list), defaultdict(list)]
for i in range(M):
    x, y = map(int, input().split())
    nextnodes[0][x - 1].append(y - 1)
    nextnodes[1][y - 1].append(x - 1)

# connect all nodes:
# dummynode -> all nodes
for i in range(N - 1):
    nextnodes[0][dummynode].append(i)
    nextnodes[1][i].append(dummynode)

def longest_by_topological_sort(i, nexts, prevs):
    visited = [-1] * N
    visited[i] = 0
    max_dist = 0
    que = []
    for n in nexts[i]:
        que.append(n)
    while que:
        p = que.pop(0)
        if visited[p] == -1:
            if all(visited[q] != -1 for q in prevs[p]):
                dist = max(visited[q] for q in prevs[p])
                visited[p] = dist + 1
                max_dist = max(max_dist, dist + 1)
                for n in nexts[p]:
                    que.append(n)
    return max(visited)

max_dist = longest_by_topological_sort(dummynode, nextnodes[0], nextnodes[1])
max_dist -= 1 # dummy

print(max_dist)


### FIXED CODE ###

from collections import defaultdict
debug = print
def debug(*va): pass

def longest_by_topological_sort(N, nexts, prevs):
    visited = [-1] * N
    max_dist = 0
    que = list(range(N))
    while len(que):
        p = que.pop()
        if visited[p] == -1:
            if all(visited[q] != -1 for q in prevs[p]):
                dist = 0
                for q in prevs[p]:
                    dist = max(dist, visited[q] + 1)
                visited[p] = dist
                max_dist = max(max_dist, dist)
                for n in nexts[p]:
                    que.append(n)
    return max_dist

def test_separate():
    import time
    N = 10**5
    t0 = time.time()
    print(longest_by_topological_sort(N, defaultdict(list), defaultdict(list)))
    print('elapsed', time.time() - t0)

def test_fanout():
    import time
    N = 10**5
    nn, pp = defaultdict(list), defaultdict(list)
    for i in range(1, N):
        nn[0].append(i)
        pp[i].append(0)
    t0 = time.time()
    print(longest_by_topological_sort(N, nn, pp))
    print('elapsed', time.time() - t0)

def test_middle():
    import time
    N = 10**5
    nn, pp = defaultdict(list), defaultdict(list)
    for i in range(1, N):
        # fanout
        nn[0].append(i)
        pp[i].append(0)
        # fanin
        nn[i].append(1)
        pp[1].append(i)
    t0 = time.time()
    print(longest_by_topological_sort(N, nn, pp))
    print('elapsed', time.time() - t0)

if __name__ == '__main__':
    N, M = map(int, input().split())

    nextnodes = [defaultdict(list), defaultdict(list)]
    for i in range(M):
        x, y = map(int, input().split())
        nextnodes[0][x - 1].append(y - 1)
        nextnodes[1][y - 1].append(x - 1)

    max_dist = longest_by_topological_sort(N, nextnodes[0], nextnodes[1])
    print(max_dist)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
 
N = int(input())
data = [''.join(sorted(input().rstrip())) for n in range(N)]
count = 0
while data:
    number = data.count(data[0])
    if number > 1:
        count += number - 1
    del data[0]
    
print(count)

### FIXED CODE ###

import sys
input = sys.stdin.readline
 
N = int(input())
words = {}
while N > 0:
    word = ''.join(sorted(input().rstrip()))
    if word in words:
        words[word] += 1
    else:
        words[word] = 1
    N -= 1

count = 0
for n in words.values():
    if n > 1:
        count += n*(n-1) // 2

print(count)
### BUGGY CODE ###

n,m = map(int,input().split())
s = list(map(int,input().split()))
t = list(map(int,input().split()))
dp = [[0 for _ in range(m+1)] for _ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
for j in range(m+1):
    dp[0][j] = 1
mod = 10**9 + 7
for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] = (dp[i][j+1]+dp[i+1][j])%mod
        else:
            dp[i + 1][j + 1] = (dp[i][j+1] + dp[i+1][j] - dp[i][j]+mod) % mod
print(dp[n][m])

### FIXED CODE ###

n,m = map(int,input().split())
s = list(map(int,input().split()))
t = list(map(int,input().split()))
dp = [[0 for _ in range(m+1)] for _ in range(n+1)]
for i in range(n+1):
    dp[i][0] = 1
for j in range(m+1):
    dp[0][j] = 1
mod = 10**9 + 7
for i in range(n):
    for j in range(m):
        if s[i] == t[j]:
            dp[i+1][j+1] = (dp[i][j+1]+dp[i+1][j])%mod
        else:
            dp[i + 1][j + 1] = (dp[i][j+1] + dp[i+1][j] - dp[i][j]+mod) % mod
print(dp[n][m])
### BUGGY CODE ###

cnt = 0
def merge(A, left, mid, right):
    global cnt
    n1 = mid - left
    n2 = right - mid
    L, R = list(range(n1 + 1)), list(range(n2 + 1))
    for i in range(n1):
        L[i] = A[left + i]
    for i in range(n2):
        R[i] = A[mid + i]
    L[n1] = 10 ** 10
    R[n2] = 10 ** 10
    i = 0
    j = 0
    for k in range(left, right):
        cnt += 1
        if L[i] <= R[j]:
            A[k] = L[i]
            i = i + 1
        else:
            A[k] = R[j]
            j = j + 1
def merge_sort(A, left, right):
    if left + 1 < right:  # 1 < right - left
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)
    return A

n = int(input())
A = list(map(int, input().split()))
print(*merge_sort(A, 0, n))
print(cnt)


### FIXED CODE ###

cnt = 0
def merge(A, left, mid, right):
    global cnt
    L = A[left:mid] + [10 ** 10]
    R = A[mid:right] + [10 ** 10]
    i = j = 0
    for k in range(left, right):
        cnt += 1
        if L[i] <= R[j]:
            A[k] = L[i]
            i += 1
        else:
            A[k] = R[j]
            j += 1
def merge_sort(A, left, right):
    if left + 1 < right:  # 1 < right - left
        mid = (left + right) // 2
        merge_sort(A, left, mid)
        merge_sort(A, mid, right)
        merge(A, left, mid, right)
    return A

n = int(input())
A = list(map(int, input().split()))
print(*merge_sort(A, 0, n))
print(cnt)
### BUGGY CODE ###

n,q = map(int,input().split())
s = input()
l = [0]*q
r = [0]*q
for i in range(q) :
  l[i], r[i] = map(int,input().split())
  t = s[l[i]-1:r[i]]
  u = r[i] - l[i] + 1
  c = 0
  for j in range(u-1) :
    if t[j:j+2] == 'AC' :
      c += 1
  print(c)

### FIXED CODE ###

n,q = map(int,input().split())
s = input()
l = [0]*q
r = [0]*q

c = [0]
a = 0
for j in range(n) :
  if s[j:j+2] == 'AC' :
    a += 1
  c.append(a)

for i in range(q) :
  l[i], r[i] = map(int,input().split())
  print(c[r[i]-1] - c[l[i]-1])
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
B = 1
for i in range(N):
    B = B*A[i]
if B <= 10**18:
    print(B)
else:
    print(-1)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
A = sorted(A,reverse=True)
B = 1
if 0 in A:
    print(0)
    quit()
else:
    for i in A:
        B *= i
        if B> 10**18:  
            print(-1)
            quit()
print(B)
### BUGGY CODE ###

N = int(input())
A = [int(a) for a in input().split()]
Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    A = [c if a == b else a for a in A]
    print(sum(A))

### FIXED CODE ###

data = {}
total = 0
N = int(input())

for a in input().split():
    a = int(a)
    data[a] = data.get(a, 0) + 1
    total += a

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    diff = c - b
    count = data.get(b)
    if count:
        data[c] = data.get(c, 0) + data.pop(b)
        total += diff * count
    print(total)
### BUGGY CODE ###

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

counter = len(S)
 
for i in range(len(S)):
  cost = S[:i].count('W') + S[i+1:].count('E')
  if cost < counter:
    counter = cost
print(counter)

### FIXED 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:
    if cost < counter:
      counter = cost
    cost +=1
print(counter)
### BUGGY CODE ###

N, M = list(map(int, input().split()))
p = list(map(int, input().split()))

set_list = []
edge = [[] for _ in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    x, y = x-1, y-1

    s1 = None
    s2 = None
    for i, se in enumerate(set_list):
        if x in se:
            s1 = se
            xi = i
        if y in se:
            s2 = se
            yi = i

    if s1 == None:
        if s2 == None:
            set_list.append({x, y})
        else:
            s2.add(x)
    else:
        if s2 == None:
            s1.add(y)
        elif s1 == s2:
            continue
        else:
            s = s1 | s2
            set_list.append(s)
            l = [xi, yi]
            l.sort()
            xi, yi = l
            del set_list[yi]
            del set_list[xi]

count = 0
for i in range(N):
    if p[i] == i+1:
        count += 1
    else:
        for se in set_list:
            if {i, p[i]-1} <= se:
                count += 1
                continue

print(count)


### FIXED CODE ###

def root(R, a):
    if R[a] != a:
        R[a] = root(R, R[a])
    return R[a]

def union(R, x, y):
    a = root(R, x)
    b = root(R, y)
    if a != b:
        R[a] = b


N, M = list(map(int, input().split()))
p = list(map(int, input().split()))

R = [i for i in range(N)]
for i in range(M):
    x, y = list(map(int, input().split()))
    x, y = x-1, y-1
    union(R, x, y)

count = 0
for i in range(N):
    if root(R, i) == root(R, p[i] - 1):
        count += 1

print(count)