Harryxun/stf_run huggingface dataset:
n,k = map(int, input().split())
a =list(map(int, input().split()))
ans =1

if n == k:
   for i in a:
       ans *= i
       if abs(ans) >10**9+7:
           ans = ans%(10**9+7)
elif all(i <= 0 for i in a) and k%2 ==1:
    a.sort(reverse = True)
    for i in range(k):
        ans *= a[i]
        if abs(ans) > 10 ** 9 + 7:
            ans = ans % (10 ** 9 + 7)
else:
    am =[]
    ap =[]

    a.sort(key = abs,reverse = True)
    amax = a[:k]
    amin = a[k+1:]
    c=1
    for i in amax:
        if i<0:
            am.append(i)
            c *=-1
        else:
            ap.append(i)
    if c ==1:
        for i in amax:
            ans *= i
            if abs(ans) > 10 ** 9 + 7:
                ans = ans % (10 ** 9 + 7)
    else:
        if min(ap)/max(amin) > max(am)/min(amin):
            amax.remove(max(am))
            amax.append(min(amin))
        else:
            amax.remove(min(ap))
            amax.append(max(amin))
        for i in amax:
            ans *= i
            if abs(ans) > 10 ** 9 + 7:
                ans = ans % (10 ** 9 + 7)

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


### FIXED CODE ###

n,k = map(int, input().split())
a =list(map(int, input().split()))
ans =1

if n == k:
   for i in a:
       ans *= i
       if abs(ans) >10**9+7:
           ans = ans%(10**9+7)

elif all(i <= 0 for i in a) and k%2 ==1:
    a.sort(reverse = True)
    for i in range(k):
        ans *= a[i]
        if abs(ans) > 10 ** 9 + 7:
            ans = ans % (10 ** 9 + 7)

else:
    am =[]
    ap =[]

    a.sort(key = abs,reverse = True)
    amax = a[:k]
    amin = a[k:]
    c=1
    if 0 in amax:
        ans = 0
    else:
        for i in amax:
            if i<0:
                am.append(i)
                c *=-1
            elif i>0:
                ap.append(i)
        if c ==1:
            for i in amax:
                ans *= i
                if abs(ans) > 10 ** 9 + 7:
                    ans = ans % (10 ** 9 + 7)
        else:
            if ap == []:
                amax.remove(max(am))
                amax.append(max(amin))

            elif max(am)*min(amin) < min(ap)*max(amin):
                amax.remove(max(am))
                amax.append(max(amin))
            else:
                amax.remove(min(ap))
                amax.append(min(amin))
            for i in amax:
                ans *= i
                if abs(ans) > 10 ** 9 + 7:
                    ans = ans % (10 ** 9 + 7)

print(ans % (10 ** 9 + 7))
n, m = map(int, input().split(" "))
n, m = n, m if n>m else m, n
M=1000000007

def newfact(n):
    return (n * newfact(n-1)) % M

if n > m+1:
    print(0)
elif n == m:
    print((2 * newfact(n) * newfact(m)) % M)
else:
    print((newfact(n) * newfact(m)) % M)



### FIXED CODE ###

n, m = map(int, input().split(" "))
(n, m) = (n, m) if n>m else (m, n)
M=1000000007

def newfact(n):
    ans=1
    for i in range(1, n+1):
        ans = (ans*i)%M
    return ans

if n > m+1:
    print(0)
elif n == m:
    print((2 * newfact(n) * newfact(m)) % M)
else:
    print((newfact(n) * newfact(m)) % M)
from collections import deque
H, W = map(int, input().split())
A = tuple(tuple(input()) for _ in range(H))
next_q=deque([(i, j) for i in range(H) for j in range(W) if A[i][j] == '#'])
L = [[0] * W for _ in range(H)]
q=deque()

res = 0
while len(next_q) != 0:
    q=next_q.copy()
    next_q.clear()
    while q:
        y, x = q.popleft()
        if y > 0 and L[y - 1][x] == 0:
            L[y - 1][x] = 1
            next_q.append((y - 1, x))
        if y < H - 1 and L[y + 1][x] == 0:
            L[y + 1][x] = 1
            next_q.append((y + 1, x))
        if x > 0 and L[y][x - 1] == 0:
            L[y][x - 1] = 1
            next_q.append((y, x - 1))
        if x < W - 1 and L[y][x + 1] == 0:
            L[y][x + 1] = 1
            next_q.append((y, x + 1))
    if len(next_q)==0: break
    res += 1
print(res)


### FIXED CODE ###

from collections import deque
import copy
H, W = map(int, input().split())
A = tuple(tuple(input()) for _ in range(H))
L=[[0]*W for _ in range(H)]
next_q=deque()

for i in range(H):
    for j in range(W):
        if A[i][j]=='#':
            L[i][j]=1
            next_q.append((i,j))

q=deque()

res = 0
while len(next_q) != 0:
    q=copy.copy(next_q)
    next_q.clear()
    while q:
        y, x = q.popleft()
        if y > 0 and L[y - 1][x] == 0:
            L[y - 1][x] = 1
            next_q.append((y - 1, x))
        if y < H - 1 and L[y + 1][x] == 0:
            L[y + 1][x] = 1
            next_q.append((y + 1, x))
        if x > 0 and L[y][x - 1] == 0:
            L[y][x - 1] = 1
            next_q.append((y, x - 1))
        if x < W - 1 and L[y][x + 1] == 0:
            L[y][x + 1] = 1
            next_q.append((y, x + 1))
    if len(next_q)==0: break
    res += 1
print(res)
n = int(input())
max_n = 10000
dp = [10000 for i in range(max_n)]
dp[0] = 0

for i in range(n+1):
  k = 1
  while i + k <= n:
    dp[i+k] = min(dp[i+k],dp[i]+1)
    k *=6
  k = 1
  while i + k <= n:
    dp[i+k] = min(dp[i+k],dp[i]+1)
    k*=9

print(dp[n])

### FIXED CODE ###

n = int(input())
max_n = 100000
dp = [10000 for i in range(max_n+1)]
dp[0] = 0

for i in range(n+1):
  k = 1
  while k <= n:
    dp[i] = min(dp[i],dp[i-k]+1)
    k *=6
  k = 1
  while k <= n:
    dp[i] = min(dp[i],dp[i-k]+1)
    k*=9
print(dp[n])
s = input()
print(f"{s[0]}{len(s) - 2}{s[-1]}")

### FIXED CODE ###

s = input()
print(s[0], len(s) - 2, s[-1], sep="")
mn = input().split()
mn = [int(x) for x in mn]
n = mn[0]
m = mn[1]
a = input().split()
a  = [int(x) for x in a]


total = sum(a)
if n-total <0:
    print(-1)
else:
    prtin(n-total)

### FIXED CODE ###

mn = input().split()
mn = [int(x) for x in mn]
n = mn[0]
m = mn[1]
a = input().split()
a  = [int(x) for x in a]


total = sum(a)
if n-total <0:
    print('-1')
else:
    print(n-total)
from heapq import heappop, heappush
N, M = map(int, input().split())
route_list = [[] for _ in range(N)]
for i in range(M):
    L, R, C = map(int, input().split())
    route_list[L-1].append((R-1, C))
for i in range(N-1):
    route_list[i+1].append((i, 0))

distance = [float("inf")] * node_num
distance[0] = 0  # 初期のノードの距離は0とする
q = [(0, 0)]

while q:
    _, u = heappop(q)
    for des, cost in route_list[u]:
        if distance[des] > distance[u] + cost:
            distance[des] = distance[u] + cost
            heappush(q, (distance[des], des))

print(distance[-1] if distance[-1] != float("inf") else -1)

### FIXED CODE ###

from heapq import heappop, heappush
N, M = map(int, input().split())
route_list = [[] for _ in range(N)]
for i in range(M):
    L, R, C = map(int, input().split())
    route_list[L-1].append((R-1, C))
for i in range(N-1):
    route_list[i+1].append((i, 0))

distance = [float("inf")] * N
distance[0] = 0  # 初期のノードの距離は0とする
q = [(0, 0)]

while q:
    _, u = heappop(q)
    for des, cost in route_list[u]:
        if distance[des] > distance[u] + cost:
            distance[des] = distance[u] + cost
            heappush(q, (distance[des], des))

print(distance[-1] if distance[-1] != float("inf") else -1)
o = list(input())
e = list(input())

for i in range(len(o)):
    print(o[i],e[i],sep="",end="")

print("")


### FIXED CODE ###

o = list(input())
e = list(input())

for i in range(len(e)):
        print(o[i],e[i],sep="",end="")

if len(e)<len(o):
        print(o[len(o)-1])

print("")
n, k = map(int, input().split())
list1, list2, k = [], [], k*2
for x in range(k):
    list1.append(input().split())
while i < k:
    for j in list1[i]:
        list2.append(j)
    i += 2
print(n - len(set(list2)))

### FIXED CODE ###

n, k = map(int, input().split())
li = []
for x in range(k*2):
    li.append(input().split())
li2 = []
i = 1
while i < k*2:
    for j in li[i]:
        li2.append(j)
    i += 2
li2 = set(li2)
print(n - len(li2))
n = int(raw_input())
bricks = raw_input().split()
bricks = [int(i) for i in bricks]
smash = 0
result = []
pos = 1
if 1 not in bricks:
  smash = -1
else:
  for i in range(1,n+1):
    if i >= len(bricks): break
    while bricks[i-1] != i:
      bricks.pop(i-1)
      smash = smash+1
print(smash)

### FIXED CODE ###

n = int(raw_input())
bricks = raw_input().split()
bricks = [int(i) for i in bricks]
smash = 0
result = []
pos = 1
if 1 not in bricks:
  smash = -1
else:
  for i in bricks:
    if i == pos:
      pos = pos+1
    else:
      smash = smash + 1
print smash
import sys
sys.setrecursionlimit(20000)

[n], *e = [[*map(int, t.split())] for t in open(0)]
viewers = set(range(1, n**2 + 1))
mem = {x: min((x - 1) % n, (n - x) % n, (x - 1)//n, n - 1 - (x - 1)//n)
       for x in range(1, n**2 + 1)}


def directions(x):
    tmp = []
    if x % n != 1:
        tmp.append(x - 1)
    if x % n > 0:
        tmp.append(x + 1)
    if (x - 1)//n > 0:
        tmp.append(x - n)
    if (x - 1)//n < n - 1:
        tmp.append(x + n)
    return tmp


ans = 0

for x in e[0]:

    def fill(x):
        z = mem[x] + (x in viewers)
        for k in directions(x):
            if mem[k] > z:
                mem[k] = z
                fill(k)

    ans += mem[x]
    viewers.remove(x)
    fill(x)

print(ans)


### FIXED CODE ###

_, *e = [[*map(int, t.split())] for t in open(0)]

for n, a, b, c, d in e:
    mem = {0: 0, 1: d}
    mult_cost = {2: a, 3: b, 5: c}

    def solve(n):
        if n not in mem:
            tmp = n*d
            for k in mult_cost:
                if n % k == 0:
                    tmp = min(tmp, solve(n//k) + mult_cost[k])
                else:
                    tmp = min(tmp, solve(
                        n//k) + mult_cost[k] + (n % k)*d, solve(n//k + 1) + mult_cost[k] + k*d - (n % k)*d)
            mem[n] = tmp
        return mem[n]

    print(solve(n))
n, k = map(int, input().split())
p = list(map(int, input().split()))
A = [0]
for i in range(1, 1000):
    A.append(A[i-1]+i)
S = [0]*n
for j in range(n):
    S[j] = A[p[j]]/p[j]
ANS = [sum(S[a] for a in range(k))]
for i in range(n-k):
    ANS.append(ANS[i]-S[i]+S[k+i])
print(max(ANS))
        

### FIXED CODE ###

n, k = map(int, input().split())
p = list(map(int, input().split()))
A = [0]
for i in range(1, 1001):
    A.append(A[i-1]+i)
S = [0]*n
for j in range(n):
    S[j] = A[p[j]]/p[j]
ANS = [sum(S[a] for a in range(k))]
for i in range(n-k):
    ANS.append(ANS[i]-S[i]+S[k+i])
print(max(ANS))
N,M=map(int,input().split())
LRC=sorted([list(map(int,input().split())) for i in range(M)])
d=[10**15]*N
d[0]=0
import sys
sys.setrecursionlimit(10**6)
class SegTree:
    'min'
    def __init__(self,size,default=10**15):
        self.d=default
        self.N=size
        self.v=[self.d]*(2*self.N-1)
    def update(self,i,x):
        i+=self.N-1
        self.v[i]=x
        while i>0:
            i=(i-1)//2
            self.v[i]=min(self.v[i*2+1],self.v[i*2+2])
    def min(self,a,b,i=0,l=0,r=1e9):
        if r==1e9:
            r=self.N
        if r<=a or b<=l:
            return self.d
        if a<=l and r<=b:
            return self.v[i]
        return min(self.min(a,b,i*2+1,l,(l+r)//2),self.min(a,b,i*2+2,(l+r)//2,r))
s=SegTree(2**14)
s.update(0,0)
for l,r,c in LRC:
    d2=s.min(l-1,r-1)+c
    if d2<d[r-1]:
        d[r-1]=d2
        s.update(r-1,d2)
print(-1 if d[-1]==10**15 else d[-1])

### FIXED CODE ###

N,M=map(int,input().split())
LRC=sorted([list(map(int,input().split())) for i in range(M)],key=lambda x:x[0])
class rmq:
    def __init__(self,N):
        self.N0=2**(N-1).bit_length()
        self.INF=2**63-1
        self.data=[self.INF]*(2*self.N0)
        self.lazy=[None]*(2*self.N0)
    def gindex(self,l, r):
        L = l + self.N0; R = r + self.N0
        lm = (L // (L & -L)) >> 1
        rm = (R // (R & -R)) >> 1
        while L < R:
            if R <= rm:
                yield R
            if L <= lm:
                yield L
            L >>= 1; R >>= 1
        while L:
            yield L
            L >>= 1
    def propagates(self,*ids):
        for i in reversed(ids):
            v = self.lazy[i-1]
            if v is None:
                continue
            self.lazy[2*i-1] = self.data[2*i-1] = self.lazy[2*i] = self.data[2*i] = v
            self.lazy[i-1] = None
    def update(self,l, r, x):
        *ids, = self.gindex(l, r)
        self.propagates(*ids)
        L = self.N0 + l; R = self.N0 + r
        while L < R:
            if R & 1:
                R -= 1
                self.lazy[R-1] = self.data[R-1] = x
            if L & 1:
                self.lazy[L-1] = self.data[L-1] = x
                L += 1
            L >>= 1; R >>= 1
        for i in ids:
            self.data[i-1] = min(self.data[2*i-1], self.data[2*i])
    def rmin(self,l, r):
        self.propagates(*self.gindex(l, r))
        L = self.N0 + l; R = self.N0 + r
        s = self.INF
        while L < R:
            if R & 1:
                R -= 1
                s = min(s, self.data[R-1])
            if L & 1:
                s = min(s, self.data[L-1])
                L += 1
            L >>= 1; R >>= 1
        return s
d=rmq(N)
d.update(0,1,0)
for l,r,c in LRC:
    d.update(r-1,r,min(d.data[d.N0+r-2],d.rmin(l-1,r-1)+c))
a=d.rmin(N-1,N)
print(a if a<d.INF else -1)
import sys

sys.setrecursionlimit(10**7)

dx = (1, 0, -1, 0,)
dy = (0, 1, 0, -1,)

inf = 1<<60

def solve():
    def dfs(i, j, depth, a, visited):
        if depth > k:
            return

        visited[i][j] = True

        for l in range(len(dx)):
            ni = i + dy[l]
            nj = j + dx[l]

            if 0 <= ni < h and 0 <= nj < w:
                if (not visited[ni][nj]) and a[ni][nj]:
                    dfs(ni, nj, depth + 1, a, visited)

    h, w, k = map(int, sys.stdin.readline().split())

    si = -1
    sj = -1

    a = [[False]*w for i in range(h)]

    for i in range(h):
        s = sys.stdin.readline().rstrip()

        for j, ch in enumerate(s):
            if ch == '.':
                a[i][j] = True
            elif ch == 'S':
                a[i][j] = True
                si, sj = i, j

    visited = [[False]*w for i in range(h)]

    dfs(si, sj, 0, a, visited)

    # print(visited)

    ans = inf

    for i in range(h):
        for j in range(w):
            if visited[i][j]:
                tmp = min([h - 1 - i, i, w - 1 - j, j])
                tmp = (tmp + k - 1) // k + 1
                ans = min(ans, tmp)

    print(ans)

if __name__ == '__main__':
    solve()

### FIXED CODE ###

import sys
from collections import deque

dx = (1, 0, -1, 0,)
dy = (0, 1, 0, -1,)

inf = 1<<60

def solve():
    h, w, k = map(int, sys.stdin.readline().split())

    si = -1
    sj = -1

    a = [[False]*w for i in range(h)]

    for i in range(h):
        s = sys.stdin.readline().rstrip()

        for j, ch in enumerate(s):
            if ch == '.':
                a[i][j] = True
            elif ch == 'S':
                a[i][j] = True
                si, sj = i, j

    visited = [[False]*w for i in range(h)]
    visited[si][sj] = True

    nxts = deque([(si, sj, 0)])

    ans = inf

    while nxts:
        i, j, d = nxts.popleft()

        ans = min(ans, i, h - 1 - i, j, w - 1 - j)

        for l in range(len(dx)):
            ni = i + dy[l]
            nj = j + dx[l]

            if 0 <= ni < h and 0 <= nj < w and not visited[ni][nj] and a[ni][nj] and d + 1 <= k:
                visited[ni][nj] = True
                nxts.append((ni, nj, d + 1))

    ans = (ans + k - 1) // k + 1

    print(ans)

if __name__ == '__main__':
    solve()
a,b,c = map(int, input().split())
print(a+b+c)

### FIXED CODE ###

a = list(input())
for i in range(3):
    a[i] = int(a[i])
print(a[0]+a[1]+a[2])
# -*- coding: utf-8

k = input()
a = map(int,raw_input().split())

b = sorted(a)

min = b[0]
Max = b[k-1]

sum = 0
for i in b:
   sum += i

print('%i %i %i' % (min,Max,sum)



### FIXED CODE ###

# -*- coding: utf-8

k = input()
a = map(int,raw_input().split())

b = sorted(a)

min = b[0]
Max = b[k-1]

sum = 0
for i in b:
   sum += i

print('%i %i %i' % (min,Max,sum))
from operator import itemgetter

def dfs(n,team):
    li[n-1] = team
    for i in grp[n-1]:
        if(li[i[0]-1]==-1):
            dfs(i[0],(team+i[1])%2)



N = int(input())
li = [-1]*N
uvw = [[int(x) for x in input().split()]for _ in range(N-1)]
uvw.sort(key=itemgetter(0))
grp = [[]for _ in range(N)]
for i in uvw:
    grp[i[0]-1].append([i[1],i[2]])
    grp[i[1]-1].append([i[0],i[2]])
dfs(1,0)
for i in li:
    print(i)


### FIXED CODE ###

from operator import itemgetter
import sys
sys.setrecursionlimit(1000000)

def dfs(n,team):
    li[n-1] = team
    for i in grp[n-1]:
        if(li[i[0]-1]==-1):
            dfs(i[0],(team+i[1])%2)



N = int(input())
li = [-1]*N
uvw = [[int(x) for x in input().split()]for _ in range(N-1)]
uvw.sort(key=itemgetter(0))
grp = [[]for _ in range(N)]
for i in uvw:
    grp[i[0]-1].append([i[1],i[2]])
    grp[i[1]-1].append([i[0],i[2]])
dfs(1,0)
for i in li:
    print(i)
a = input().split(" ")
N = int(a[0])
M = int(a[1])
ans1 = 10
ans2 = 10
ans3 = 10
x = 0
for i in range(M):
    b = input().split(" ")
    c = int(b[0])
    d = int(b[1])
    if c == 1:
        if d == 0:
            if N == 1:
                ans1 = 0
            else:
                x = 1
        elif ans1 == 10:
            ans1 = d
        elif ans1 != d:
            x = 1
    elif c == 2:
        if ans2 == 10:
            ans2 = d
        elif ans2 != d:
            x = 1
    else:
        if ans3 == 10:
            ans3 = d
        elif ans3 != d:
            x = 1
if ans1 == 10:
    ans1 = 0
if ans2 == 10:
    ans2 = 0
if ans3 == 10:
    ans3 = 0

if x = 1:
    print(-1)
else:
    if N == 1:
        print(ans1)
    elif N == 2:
        if ans1 == 0:
            print(-1)
        else:
            print(str(ans1) + str(ans2))
    else:
        if ans1 == 0:
            print(-1)
        else:
            print(str(ans1) + str(ans2) + str(ans3))

### FIXED CODE ###

a = input().split(" ")
N = int(a[0])
M = int(a[1])
ans1 = 10
ans2 = 10
ans3 = 10
x = 0
for i in range(M):
    b = input().split(" ")
    c = int(b[0])
    d = int(b[1])
    if c == 1:
        if ans1 == 10:
            if d == 0:
                if N == 1:
                    ans1 = 0
                else:
                    x = 1
            else:
                ans1 = d
        elif ans1 != d:
            x = 1
    elif c == 2:
        if ans2 == 10:
            ans2 = d
        elif ans2 != d:
            x = 1
    else:
        if ans3 == 10:
            ans3 = d
        elif ans3 != d:
            x = 1
if ans1 == 10:
    if N == 1:
        ans1 = 0
    else:
        ans1 = 1
if ans2 == 10:
    ans2 = 0
if ans3 == 10:
    ans3 = 0

if x == 1:
    print(-1)
else:
    if N == 1:
        print(ans1)
    elif N == 2:
        if ans1 == 0:
            print(-1)
        else:
            print(str(ans1) + str(ans2))
    else:
        if ans1 == 0:
            print(-1)
        else:
            print(str(ans1) + str(ans2) + str(ans3))
#!/usr/bin python3
# -*- coding: utf-8 -*-

h, n = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(n)]

#DP[i] = i までの魔法でモンスターの体力を減らすため消耗する魔力の最小値
dp = [0] * h

for i in range(h):
    dp[i] = min(dp[i-a] + b for a, b in ab)
print(dp[h-1])


### FIXED CODE ###

import numpy as np
H,N=map(int, input().split())
A = [None]*N
B = [None]*N
for i in range(N):
  A[i],B[i]=map(int,input().split())
dp=[10**8]*(H+1)
A = np.array(A)
B = np.array(B)
dp = np.array(dp)
dp[0]=0
for i in range(1,H+1):
  x=np.amin((dp[np.maximum(0,i-A)]+B))
  dp[i]=min(x,dp[i])
print(dp[-1])
A, B, C = map(int,input().split())

K = int(input())


for i in range(0, K):
    for j in range(0, K):
        for k in range(0, K):
            x = A * 2**i
            y = B * 2**j
            z = C * 2**k

            if x < y < z:
                flag = True
                

if flag == True:
    print('Yes')
else:
    print('No')

### FIXED CODE ###

A, B, C = map(int,input().split())

K = int(input())

flag = False

for i in range(0, K+1):
    for j in range(0, K+1):
        for k in range(0, K+1):
            x = A * 2**i
            y = B * 2**j
            z = C * 2**k
            # print(x, y, z)

            if x < y < z and i + j + k <= K:
                flag = True


if flag == True:
    print('Yes')
else:
    print('No')
from collections import deque

n,m,k=map(int,input().split())
fre=[[]for _ in range(n)]
blo=[[] for _ in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    fre[a-1].append(b-1)
    fre[b-1].append(a-1)
for i in range(k):
    c,d=map(int,input().split())
    blo[c-1].append(d-1)
    blo[d-1].append(c-1)

cnt=[0]*n
l=deque()
x=1
dic={}
for i in range(n):
    if cnt[i]==0:
        l.append(i)
    while l:
        now=l.popleft()
        for to in fre[now]:
            if cnt[to]==0:
                cnt[to]=x
                if x in dic:
                    dic[x]+=1
                else:
                    dic[x]=1
                l.append(to)
    x+=1

for i in range(n):
    k=len(fre[i])
    for b in blo[i]:
        if cnt[b]==cnt[i]:
            k+=1
    print(dic[cnt[i]]-k-1)

### FIXED CODE ###

from collections import deque

n,m,k=map(int,input().split())
fre=[[]for _ in range(n)]
blo=[[] for _ in range(n)]
for i in range(m):
    a,b=map(int,input().split())
    fre[a-1].append(b-1)
    fre[b-1].append(a-1)
for i in range(k):
    c,d=map(int,input().split())
    blo[c-1].append(d-1)
    blo[d-1].append(c-1)

cnt=[0]*n
l=deque()
x=1
dic={}
for i in range(n):
    if cnt[i]==0:
        l.append(i)
    while l:
        now=l.popleft()
        for to in fre[now]:
            if cnt[to]==0:
                cnt[to]=x
                if x in dic:
                    dic[x]+=1
                else:
                    dic[x]=1
                l.append(to)
    x+=1

for i in range(n):
    if dic=={}:
        print(0)
    else:
        k=len(fre[i])
        for b in blo[i]:
            if cnt[b]==cnt[i]:
                k+=1
        if cnt[i] in dic:
            print(dic[cnt[i]]-k-1)
        else:
            print(0)
from collections import deque
A = deque(input())
while A != [] and A != [0]:
    if A[:7] == list("dreamer") and A[5:10] != list("erase"):
        A = A[7:]
    elif A[:5] == list("dream"):
        A = A[5:]
    elif A[:6] == list("eraser"):
        A = A[6:]
    elif A[:5] == list("erase"):
        A = A[5:]
    else:
        A = [0]
if A == []:
    print("YES")
else:
    print("NO")


### FIXED CODE ###

s = input().replace("eraser", "").replace("erase", "")
s = s.replace("dreamer", "").replace("dream", "")
if s:
    print("NO")
else:
    print("YES")
import math
N, X = map(int, input().split())
s = 0
cake =[]
for i in range (N):
  s = int(input())
  cake.append(s)
ans = N + (X - sum(cake)) / min(cake)
print(math.floor(ans))


### FIXED CODE ###

s = input()
count = 700
for i in range (len(s)):
  if s[i] == 'o':
    count += 100
  else:
    pass
print(count)
N = input()
A = [None] + [int(x) for x in input().split()]

answer = 0
for i in range(1,N+1):
  if A[A[i]] == i:
    answer += 1
answer //= 2
print(answer)

### FIXED CODE ###

N = int(input())
A = [None] + [int(x) for x in input().split()]

answer = 0
for i in range(1,N+1):
  if A[A[i]] == i:
    answer += 1
answer //= 2
print(answer)