Input:
import sys

from collections import defaultdict as dd

import heapq

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 6)

N = int(eval(input()))

e = dd(list)

inc = [0] * (N + 1)

for _ in range(N - 1):

  u, v = list(map(int, input().split()))

  e[u].append(v)

  e[v].append(u)

  inc[u] += 1

  inc[v] += 1

x = 0

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

  if inc[x] < inc[i]: x = i

c = list(map(int, input().split()))

c.sort()

ressum = sum(c[: -1])

vis = set()

res = [0] * (N + 1)

res[x] = c[-1]

def dfs(y):

  global vis

  global res

  global c

  res[y] = c.pop()

  for z in e[y]:

    if z in vis: continue

    vis.add(z)

    dfs(z)

vis.add(x)

dfs(x)

print(ressum)

print((*res[1: ]))

import sys

from collections import defaultdict as dd

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 6)

N = int(eval(input()))

e = dd(list)

for _ in range(N - 1):

  u, v = list(map(int, input().split()))

  e[u].append(v)

  e[v].append(u)

c = list(map(int, input().split()))

c.sort()

sm = sum(c) - c[-1]

res = [0] * (N + 1)

def dfs(x):

  global res

  res[x] = c.pop()

  for y in e[x]:

    if res[y]: continue

    dfs(y)

dfs(1)

print(sm)

print((*res[1: ]))

import sys
from collections import defaultdict as dd
import heapq

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(eval(input()))
e = dd(list)
inc = [0] * (N + 1)
for _ in range(N - 1):
    u, v = list(map(int, input().split()))
    e[u].append(v)
    e[v].append(u)
    inc[u] += 1
    inc[v] += 1
x = 0
for i in range(1, N + 1):
    if inc[x] < inc[i]:
        x = i
c = list(map(int, input().split()))
c.sort()
ressum = sum(c[:-1])
vis = set()
res = [0] * (N + 1)
res[x] = c[-1]

def dfs(y):
    global vis
    global res
    global c
    res[y] = c.pop()
    for z in e[y]:
        if z in vis:
            continue
        vis.add(z)
        dfs(z)

vis.add(x)
dfs(x)
print(ressum)
print((*res[1:]))

import sys
from collections import defaultdict as dd

input = sys.stdin.readline
sys.setrecursionlimit(10**6)
N = int(eval(input()))
e = dd(list)
for _ in range(N - 1):
    u, v = list(map(int, input().split()))
    e[u].append(v)
    e[v].append(u)
c = list(map(int, input().split()))
c.sort()
sm = sum(c) - c[-1]
res = [0] * (N + 1)

def dfs(x):
    global res
    res[x] = c.pop()
    for y in e[x]:
        if res[y]:
            continue
        dfs(y)

dfs(1)
print(sm)
print((*res[1:]))

-import heapq
-inc = [0] * (N + 1)
-    inc[u] += 1
-    inc[v] += 1
-x = 0
-for i in range(1, N + 1):
-    if inc[x] < inc[i]:
-        x = i
-ressum = sum(c[:-1])
-vis = set()
+sm = sum(c) - c[-1]
-res[x] = c[-1]
-def dfs(y):
-    global vis
+def dfs(x):
-    global c
-    res[y] = c.pop()
-    for z in e[y]:
-        if z in vis:
+    res[x] = c.pop()
+    for y in e[x]:
+        if res[y]:
-        vis.add(z)
-        dfs(z)
+        dfs(y)
-vis.add(x)
-dfs(x)
-print(ressum)
+dfs(1)
+print(sm)


Input:
from collections import Counter

n=int(eval(input()))

s=[eval(input()) for i in range(n)]

c1=Counter(s.pop())

for ss in s:

    c2=Counter(ss)

    for k in list(c1.keys()):

        c1[k]=min(c1[k],c2[k])

ans=''

for k,v in sorted(c1.items()):

    ans+=k*v

print(ans)

n=int(eval(input()))

s=[eval(input()) for i in range(n)]

cnt=[0]*26

t=[0]*26

for i,ss in enumerate(s):

  t=[0]*26

  if i==0:

    for sss in ss:

      cnt[ord(sss)-97]+=1

    continue

  for sss in ss:

    t[ord(sss)-97]+=1

  for i in range(26):

    if cnt[i]>t[i]:

      cnt[i]=t[i]

ans=""

for i in range(26):

  ans+=chr(i+97)*cnt[i]

print(ans)

from collections import Counter

n = int(eval(input()))
s = [eval(input()) for i in range(n)]
c1 = Counter(s.pop())
for ss in s:
    c2 = Counter(ss)
    for k in list(c1.keys()):
        c1[k] = min(c1[k], c2[k])
ans = ""
for k, v in sorted(c1.items()):
    ans += k * v
print(ans)

n = int(eval(input()))
s = [eval(input()) for i in range(n)]
cnt = [0] * 26
t = [0] * 26
for i, ss in enumerate(s):
    t = [0] * 26
    if i == 0:
        for sss in ss:
            cnt[ord(sss) - 97] += 1
        continue
    for sss in ss:
        t[ord(sss) - 97] += 1
    for i in range(26):
        if cnt[i] > t[i]:
            cnt[i] = t[i]
ans = ""
for i in range(26):
    ans += chr(i + 97) * cnt[i]
print(ans)

-from collections import Counter
-c1 = Counter(s.pop())
-for ss in s:
-    c2 = Counter(ss)
-    for k in list(c1.keys()):
-        c1[k] = min(c1[k], c2[k])
+cnt = [0] * 26
+t = [0] * 26
+for i, ss in enumerate(s):
+    t = [0] * 26
+    if i == 0:
+        for sss in ss:
+            cnt[ord(sss) - 97] += 1
+        continue
+    for sss in ss:
+        t[ord(sss) - 97] += 1
+    for i in range(26):
+        if cnt[i] > t[i]:
+            cnt[i] = t[i]
-for k, v in sorted(c1.items()):
-    ans += k * v
+for i in range(26):
+    ans += chr(i + 97) * cnt[i]


Input:
primeset = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009}

def gcd(n,m):

    while n:

        m,n = n,m%n

    return m

N = int(eval(input()))

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

soinsu = set()

a,b = 0,0

def pariwise():

    global primeset,N,A,soinsu,a,b

    for i in range(N):

        a = A[i]

        b = set()

        for j in primeset:

            while a != 1 and a % j == 0:

                a //= j

                b.add(j)

        if a != 1:

            b.add(a)

        for j in b:

            if j in soinsu:

                return False

            soinsu.add(j)

    return True

def setwise():

    global primeset,N,A,soinsu,a,b

    a = A[0]

    for i in range(1,N):

        a = gcd(a,A[i])

    return (a == 1)

if pariwise():

    print('pairwise coprime')

elif setwise():

    print('setwise coprime')

else:

    print('not coprime')

def gcd(n,m):

    while n:

        m,n = n,m%n

    return m

N = int(eval(input()))

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

maxA = max(A)

B = [0]*(maxA+1)

for i in range(2,maxA+1):

    if B[i] == 0:

        B[i] = i

        for j in range(2*i,maxA+1,i):

            B[j] = i

soinsu = set()

a,b = 0,0

def pariwise():

    global N,A,soinsu,a,b

    for i in range(N):

        a = A[i]

        b = set()

        while a != 1:

            b.add(B[a])

            a //= B[a]

        for j in b:

            if j in soinsu:

                return False

            soinsu.add(j)

    return True

def setwise():

    global N,A,soinsu,a,b

    a = A[0]

    for i in range(1,N):

        a = gcd(a,A[i])

    return (a == 1)

if pariwise():

    print('pairwise coprime')

elif setwise():

    print('setwise coprime')

else:

    print('not coprime')

def gcd(n, m):
    while n:
        m, n = n, m % n
    return m

N = int(eval(input()))
A = list(map(int, input().split()))
maxA = max(A)
B = [0] * (maxA + 1)
for i in range(2, maxA + 1):
    if B[i] == 0:
        B[i] = i
        for j in range(2 * i, maxA + 1, i):
            B[j] = i
soinsu = set()
a, b = 0, 0

def pariwise():
    global N, A, soinsu, a, b
    for i in range(N):
        a = A[i]
        b = set()
        while a != 1:
            b.add(B[a])
            a //= B[a]
        for j in b:
            if j in soinsu:
                return False
            soinsu.add(j)
    return True

def setwise():
    global N, A, soinsu, a, b
    a = A[0]
    for i in range(1, N):
        a = gcd(a, A[i])
    return a == 1

if pariwise():
    print("pairwise coprime")
elif setwise():
    print("setwise coprime")
else:
    print("not coprime")

+        for j in range(2 * i, maxA + 1, i):

-    global primeset, N, A, soinsu, a, b

-            while a != 1 and a % j == 0:

-    global primeset, N, A, soinsu, a, b


Input:
c = 0

for i in [0]*int(eval(input())):

  c = max(c,int(eval(input()))%2)

print(("sfeicrosntd"[c::2]))

c = 0

for i in [0]*int(eval(input())):

  c += int(eval(input()))%2

print(("sfeicrosntd"[min(c,1)::2]))

c = 0
for i in [0] * int(eval(input())):
    c = max(c, int(eval(input())) % 2)
print(("sfeicrosntd"[c::2]))

c = 0
for i in [0] * int(eval(input())):
    c += int(eval(input())) % 2
print(("sfeicrosntd"[min(c, 1) :: 2]))

-    c = max(c, int(eval(input())) % 2)
-print(("sfeicrosntd"[c::2]))
+    c += int(eval(input())) % 2
+print(("sfeicrosntd"[min(c, 1) :: 2]))


Input:
a, b, c = sorted(map(int, input().split()))

print((c*10+a+b))

a, b, c = sorted(map(int, input().split()))
print((c * 10 + a + b))

(*x,) = list(map(int, input().split()))
print((sum(x) + max(x) * 9))

-a, b, c = sorted(map(int, input().split()))

+(*x,) = list(map(int, input().split()))


Input:
from itertools import groupby

def main():

    S = open(0).read().split()[1]

    print((len(list(groupby(S)))))

main()

from itertools import groupby

def main():

    S = open(0).read().split()[1]

    print((sum(1 for x in groupby(S))))

main()

+    print((sum(1 for x in groupby(S))))


Input:
N = int(eval(input()))

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

Q = int(eval(input()))

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

exhaustive_dict = {}

def create_exhastive_list(head, sum):

    for i in range(head, N):

        sum += A[i]

        exhaustive_dict[sum] = 1

        create_exhastive_list(i+1, sum)

        sum -= A[i]

head = 0

sum = 0

create_exhastive_list(head, sum)

for target in M:

    if target in list(exhaustive_dict.keys()):

        print("yes")

    else:

        print("no")

N = int(eval(input()))

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

Q = int(eval(input()))

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

combinations = {}

def create_combinations(idx, sum):

    combinations[sum] = 1

    if idx >= N:

        return

    create_combinations(idx+1, sum)

    create_combinations(idx+1, sum+A[idx])

    return

create_combinations(0, 0)

for target in M:

    if target in list(combinations.keys()):

        print("yes")

    else:

        print("no")

N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
M = list(map(int, input().split()))
exhaustive_dict = {}

def create_exhastive_list(head, sum):
    for i in range(head, N):
        sum += A[i]
        exhaustive_dict[sum] = 1
        create_exhastive_list(i + 1, sum)
        sum -= A[i]

head = 0
sum = 0
create_exhastive_list(head, sum)
for target in M:
    if target in list(exhaustive_dict.keys()):
        print("yes")
    else:
        print("no")

N = int(eval(input()))
A = list(map(int, input().split()))
Q = int(eval(input()))
M = list(map(int, input().split()))
combinations = {}

def create_combinations(idx, sum):
    combinations[sum] = 1
    if idx >= N:
        return
    create_combinations(idx + 1, sum)
    create_combinations(idx + 1, sum + A[idx])
    return

create_combinations(0, 0)
for target in M:
    if target in list(combinations.keys()):
        print("yes")
    else:
        print("no")

-        create_exhastive_list(i + 1, sum)

+    create_combinations(idx + 1, sum + A[idx])

-    if target in list(exhaustive_dict.keys()):

+    if target in list(combinations.keys()):


Input:
import numpy as np

n = eval(input())

w = list(map(int, input().split()))

w = np.array(w)

print((np.min(np.abs(2 * np.cumsum(w) -np.sum(w)))))

n = int(eval(input()))

w = list(map(int, input().split()))

ans = sum(w)

for i in range(1,n):

    ans = min(ans,abs(sum(w[:i + 1]) - sum(w[i +1:])))

print(ans)

import numpy as np

n = eval(input())
w = list(map(int, input().split()))
w = np.array(w)
print((np.min(np.abs(2 * np.cumsum(w) - np.sum(w)))))

n = int(eval(input()))
w = list(map(int, input().split()))
ans = sum(w)
for i in range(1, n):
    ans = min(ans, abs(sum(w[: i + 1]) - sum(w[i + 1 :])))
print(ans)

-print((np.min(np.abs(2 * np.cumsum(w) - np.sum(w)))))

+    ans = min(ans, abs(sum(w[: i + 1]) - sum(w[i + 1 :])))


Input:
def nap(n,cap):

    val = [0]*(cap+1)

    for _ in range(n):

        w,v = list(map(int,input().split()))

        for k in range(cap,w-1,-1):

            nv=val[k-w]+v

            if val[k]<nv:

                val[k]=nv

    return max(val)

n,cap=list(map(int,input().split()))

print((nap(n,cap)))

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

dp=[[0]*(m+1) for _ in range(n+1)]

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

    w,v=list(map(int,input().split()))

    dp[i][w]=max(dp[i-1][w],v)

    for j in range(m+1):

        if dp[i-1][j]==0:

            continue

        if j+w<=m:

            dp[i][j+w]=max(dp[i][j+w],dp[i-1][j]+v)

        dp[i][j]=max(dp[i-1][j],dp[i][j])

print((max(dp[n])))

def nap(n, cap):
    val = [0] * (cap + 1)
    for _ in range(n):
        w, v = list(map(int, input().split()))
        for k in range(cap, w - 1, -1):
            nv = val[k - w] + v
            if val[k] < nv:
                val[k] = nv
    return max(val)

n, cap = list(map(int, input().split()))
print((nap(n, cap)))

n, m = list(map(int, input().split()))
dp = [[0] * (m + 1) for _ in range(n + 1)]
for i in range(1, n + 1):
    w, v = list(map(int, input().split()))
    dp[i][w] = max(dp[i - 1][w], v)
    for j in range(m + 1):
        if dp[i - 1][j] == 0:
            continue
        if j + w <= m:
            dp[i][j + w] = max(dp[i][j + w], dp[i - 1][j] + v)
        dp[i][j] = max(dp[i - 1][j], dp[i][j])
print((max(dp[n])))

-        w, v = list(map(int, input().split()))

-        for k in range(cap, w - 1, -1):

-n, cap = list(map(int, input().split()))

+    w, v = list(map(int, input().split()))


Input:
import numpy as np

n = int(input())

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

border = list(range(400, 3201, 400))

cnt = [0] * (len(border) + 1)

for aa in a:

    for i, k in enumerate(border):

        if aa < k:

            cnt[i] += 1

            break

    else:

        cnt[i+1] += 1

k = np.count_nonzero(cnt[:-1])

print(k, k + cnt[-1]) if k != 0 else print(1, cnt[-1])

from bisect import bisect_left

import numpy as np

n = int(input())

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

border = np.arange(399, 3200, 400)

cnt = np.zeros(len(border) + 1, dtype=np.int)

for i in a:

    cnt[bisect_left(border, i)] += 1

k = np.count_nonzero(cnt[:-1])

print(k, k + cnt[-1]) if k != 0 else print(1, cnt[-1])

import numpy as np

n = int(input())
a = map(int, input().split())
border = list(range(400, 3201, 400))
cnt = [0] * (len(border) + 1)
for aa in a:
    for i, k in enumerate(border):
        if aa < k:
            cnt[i] += 1
            break
    else:
        cnt[i + 1] += 1
k = np.count_nonzero(cnt[:-1])
print(k, k + cnt[-1]) if k != 0 else print(1, cnt[-1])

+cnt = np.zeros(len(border) + 1, dtype=np.int)


Input:
from math import factorial

def combination(n, r):

    return factorial(n) // (factorial(n - r) * factorial(r))

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

t = 100*(n-m) + m*1900 # 1回あたり

rs = [0.5**m * ( 1- 0.5**m)**i for i in range(int(1e4))]

ans = 0.0

for i,r in enumerate(rs):

    ans += ((i+1)*t)*r

print((round(ans)))

from math import factorial

def combination(n, r):
    return factorial(n) // (factorial(n - r) * factorial(r))

n, m = list(map(int, input().split()))
t = 100 * (n - m) + m * 1900  # 1回あたり
rs = [0.5**m * (1 - 0.5**m) ** i for i in range(int(1e4))]
ans = 0.0
for i, r in enumerate(rs):
    ans += ((i + 1) * t) * r
print((round(ans)))

-    return factorial(n) // (factorial(n - r) * factorial(r))


Input:
from collections import defaultdict, deque

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

used = [True]*N

dic = defaultdict(list)

for i in range(M):

  l, r, d = list(map(int,input().split()))

  l -= 1

  r -= 1

  used[r] = False

  dic[l] += [(r,d)]

dist = [-1]*N

q = deque([])

for i in range(N):

  if used[i]:

    q += [i]

    dist[i] = 0

ans = True

while q:

  e = q.popleft()

  flag = False

  for p, d in dic[e]:

    if dist[p]==-1:

      dist[p] = d+dist[e]

      q += [p]

    elif dist[p]!=dist[e]+d:

      ans = False

      flag = True

      break

  if flag:

    break

if ans:

  if -1 not in dist:

    print('Yes')

  else:

    print('No')

else:

  print('No')

from collections import deque

N, M, *L = list(map(int, open(0).read().split()))

dic = [[] for i in range(N+1)]

llog = set()

rlog = set()

for l,r,d in zip(*[iter(L)]*3):

  dic[l].append((r,d))

  llog.add(l)

  rlog.add(r)

log = llog.difference(rlog)

q = deque(log)

dist = [-1]*(N+1)

for c in log:

  dist[c] = 0

while q:

  v = q.popleft()

  flag = False

  for u,d in dic[v]:

    if dist[u]==-1:

      dist[u] = dist[v]+d

      q.append(u)

    elif dist[u]!=dist[v]+d:

      print('No')

      flag = True

      break

  if flag:

    break

else:

  U = llog.union(rlog)

  for c in U:

    if dist[c]==-1:

      print('No')

      break

  else:

    print('Yes')

from collections import defaultdict, deque

N, M = list(map(int, input().split()))
used = [True] * N
dic = defaultdict(list)
for i in range(M):
    l, r, d = list(map(int, input().split()))
    l -= 1
    r -= 1
    used[r] = False
    dic[l] += [(r, d)]
dist = [-1] * N
q = deque([])
for i in range(N):
    if used[i]:
        q += [i]
        dist[i] = 0
ans = True
while q:
    e = q.popleft()
    flag = False
    for p, d in dic[e]:
        if dist[p] == -1:
            dist[p] = d + dist[e]
            q += [p]
        elif dist[p] != dist[e] + d:
            ans = False
            flag = True
            break
    if flag:
        break
if ans:
    if -1 not in dist:
        print("Yes")
    else:
        print("No")
else:
    print("No")

from collections import deque

N, M, *L = list(map(int, open(0).read().split()))
dic = [[] for i in range(N + 1)]
llog = set()
rlog = set()
for l, r, d in zip(*[iter(L)] * 3):
    dic[l].append((r, d))
    llog.add(l)
    rlog.add(r)
log = llog.difference(rlog)
q = deque(log)
dist = [-1] * (N + 1)
for c in log:
    dist[c] = 0
while q:
    v = q.popleft()
    flag = False
    for u, d in dic[v]:
        if dist[u] == -1:
            dist[u] = dist[v] + d
            q.append(u)
        elif dist[u] != dist[v] + d:
            print("No")
            flag = True
            break
    if flag:
        break
else:
    U = llog.union(rlog)
    for c in U:
        if dist[c] == -1:
            print("No")
            break
    else:
        print("Yes")

-from collections import defaultdict, deque

-    l, r, d = list(map(int, input().split()))

+N, M, *L = list(map(int, open(0).read().split()))


Input:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)

inf=10**20

mod=10**9+7

def LI(): return list(map(int,input().split()))

def I(): return int(eval(input()))

def LS(): return input().split()

def S(): return eval(input())

def main():

  s=S()

  k=I()

  l=[]

  for i in range(len(s)):

    l.append(s[i])

    l.append(s[i:i+2])

    l.append(s[i:i+3])

    l.append(s[i:i+4])

    l.append(s[i:i+5])

  l=list(set(l))

  l.sort()

  return l[k-1]

print((main()))

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)

inf=10**20

mod=10**9+7

def LI(): return list(map(int,input().split()))

def I(): return int(eval(input()))

def LS(): return input().split()

def S(): return eval(input())

def main():

  s=S()

  k=I()

  l=[]

  for i in range(len(s)):

    for j in range(1,6):

      l.append(s[i:i+j])

  l=list(set(l))

  l.sort()

  return l[k-1]

print((main()))

import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI():
    return list(map(int, input().split()))

def I():
    return int(eval(input()))

def LS():
    return input().split()

def S():
    return eval(input())

def main():
    s = S()
    k = I()
    l = []
    for i in range(len(s)):
        l.append(s[i])
        l.append(s[i : i + 2])
        l.append(s[i : i + 3])
        l.append(s[i : i + 4])
        l.append(s[i : i + 5])
    l = list(set(l))
    l.sort()
    return l[k - 1]

print((main()))

import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI():
    return list(map(int, input().split()))

def I():
    return int(eval(input()))

def LS():
    return input().split()

def S():
    return eval(input())

def main():
    s = S()
    k = I()
    l = []
    for i in range(len(s)):
        for j in range(1, 6):
            l.append(s[i : i + j])
    l = list(set(l))
    l.sort()
    return l[k - 1]

print((main()))

-        l.append(s[i])
-        l.append(s[i : i + 2])
-        l.append(s[i : i + 3])
-        l.append(s[i : i + 4])
-        l.append(s[i : i + 5])
+        for j in range(1, 6):
+            l.append(s[i : i + j])


Input:
# import 周り

from fractions import gcd

# 定数関係

mod = 10 ** 9 + 7

# 補助関数

def inv(a):

    b, u, v = mod, 1, 0

    while b != 0:

        t = a // b

        a, b = b, a - t * b

        u, v = v, u - t * v

    return u

if __name__ == '__main__':

    #入力関係

    n = int(eval(input()))

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

    U, V = 0, 1

    for ele in A:

      V = V * ele // gcd(V, ele) # LCMを計算

      U += inv(ele)

    V %= mod

    print((U * V % mod))

# import 周り

from fractions import gcd

# 定数関係

mod = 10 ** 9 + 7

# 補助関数

def inv(a):

    b, u, v = mod, 1, 0

    while b != 0:

        t = a // b

        a, b = b, a - t * b

        u, v = v, u - t * v

    return u

if __name__ == '__main__':

    #入力関係

    n = int(eval(input()))

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

    U, V = 0, 1

    for ele in A:

      V *= ele // gcd(V, ele)# LCMを計算

      U += inv(ele)

    V %= mod

    print((U * V % mod))

# import 周り
from fractions import gcd

# 定数関係
mod = 10**9 + 7
# 補助関数
def inv(a):
    b, u, v = mod, 1, 0
    while b != 0:
        t = a // b
        a, b = b, a - t * b
        u, v = v, u - t * v
    return u

if __name__ == "__main__":
    # 入力関係
    n = int(eval(input()))
    A = list(map(int, input().split()))
    U, V = 0, 1
    for ele in A:
        V = V * ele // gcd(V, ele)  # LCMを計算
        U += inv(ele)
    V %= mod
    print((U * V % mod))

# import 周り
from fractions import gcd

# 定数関係
mod = 10**9 + 7
# 補助関数
def inv(a):
    b, u, v = mod, 1, 0
    while b != 0:
        t = a // b
        a, b = b, a - t * b
        u, v = v, u - t * v
    return u

if __name__ == "__main__":
    # 入力関係
    n = int(eval(input()))
    A = list(map(int, input().split()))
    U, V = 0, 1
    for ele in A:
        V *= ele // gcd(V, ele)  # LCMを計算
        U += inv(ele)
    V %= mod
    print((U * V % mod))

-        V = V * ele // gcd(V, ele)  # LCMを計算

+        V *= ele // gcd(V, ele)  # LCMを計算


Input:
h,n = list(map(int,input().split()))

a = list(map(int,input().split()))

if sum(a) >= h:

  print("Yes")

else:

  print("No")

h,n = list(map(int,input().split()))

sp = list(map(int,input().split()))

if h > sum(sp):

  print('No')

else:

  print('Yes')

h, n = list(map(int, input().split()))
a = list(map(int, input().split()))
if sum(a) >= h:
    print("Yes")
else:
    print("No")

h, n = list(map(int, input().split()))
sp = list(map(int, input().split()))
if h > sum(sp):
    print("No")
else:
    print("Yes")

-a = list(map(int, input().split()))
-if sum(a) >= h:
+sp = list(map(int, input().split()))
+if h > sum(sp):
+    print("No")
+else:
-else:
-    print("No")


Input:
# E - Coprime

from math import gcd

N = int(eval(input()))

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

A = sorted(A)

MAX = 10**6

L = [0]*(MAX+1)

def prime(x):

    global L

    p = 2

    while x>1:

        if L[x]==1:

            return False

        if x%p==0:

            while x%p==0:

                x //= p

            tmp = p

            while tmp<=MAX:

                L[tmp] = 1

                tmp += p

        p = p+1 if p*p<x else x

    return True

G = A[0]

pairwise = True

_ = prime(A[0])

for i in range(1,N):

    a = A[i]

    if pairwise:

        pairwise = prime(a)

    G = gcd(G,a)

if pairwise:

    print('pairwise coprime')

elif G==1:

    print('setwise coprime')

else:

    print('not coprime')

# E - Coprime

from math import gcd

N = int(eval(input()))

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

MAX = 10**6

p = [0]*(MAX+1)

for i in range(2,MAX+1):

    if p[i]>0:

        continue

    tmp = i

    while tmp<=MAX:

        p[tmp] = i

        tmp += i

pairwise = True

g = -1

q = [-1]*(MAX+1)

for i in range(N):

    a = A[i]

    g = a if g<0 else gcd(g,a)

    while pairwise and a>1:

        pairwise = True if q[p[a]] in [-1,i] else False

        q[p[a]] = i

        a //= p[a]

if pairwise:

    print('pairwise coprime')

elif g==1:

    print('setwise coprime')

else:

    print('not coprime')

# E - Coprime
from math import gcd

N = int(eval(input()))
A = list(map(int, input().split()))
A = sorted(A)
MAX = 10**6
L = [0] * (MAX + 1)

def prime(x):
    global L
    p = 2
    while x > 1:
        if L[x] == 1:
            return False
        if x % p == 0:
            while x % p == 0:
                x //= p
            tmp = p
            while tmp <= MAX:
                L[tmp] = 1
                tmp += p
        p = p + 1 if p * p < x else x
    return True

G = A[0]
pairwise = True
_ = prime(A[0])
for i in range(1, N):
    a = A[i]
    if pairwise:
        pairwise = prime(a)
    G = gcd(G, a)
if pairwise:
    print("pairwise coprime")
elif G == 1:
    print("setwise coprime")
else:
    print("not coprime")

# E - Coprime
from math import gcd

N = int(eval(input()))
A = list(map(int, input().split()))
MAX = 10**6
p = [0] * (MAX + 1)
for i in range(2, MAX + 1):
    if p[i] > 0:
        continue
    tmp = i
    while tmp <= MAX:
        p[tmp] = i
        tmp += i
pairwise = True
g = -1
q = [-1] * (MAX + 1)
for i in range(N):
    a = A[i]
    g = a if g < 0 else gcd(g, a)
    while pairwise and a > 1:
        pairwise = True if q[p[a]] in [-1, i] else False
        q[p[a]] = i
        a //= p[a]
if pairwise:
    print("pairwise coprime")
elif g == 1:
    print("setwise coprime")
else:
    print("not coprime")

+        pairwise = True if q[p[a]] in [-1, i] else False


Input:
s = eval(input())

n = len(s)

ans = 0

for i in range(2**(n-1)):

  form = s

  bit = []

  for _ in range(n-1):

    bit.append(i%2)

    i =  i//2

  for j in range(n-1):

    if bit[j]:

      form = form[:n-j-1] + '+' + form[n-j-1:]

  ans += eval(form)

print(ans)

s = eval(input())

n = len(s)

cand = []

def func(ac,yet,sum):

  next = int(yet[0])

  if len(yet) ==1:

    cand.append(sum+ ac+ next)

    cand.append(sum+ ac*10+ next)

  else:

    func(next, yet[1:],ac+sum)

    func(ac*10+next, yet[1:],sum)

func(0,s,0)

print((sum(cand)//2))

s = eval(input())
n = len(s)
ans = 0
for i in range(2 ** (n - 1)):
    form = s
    bit = []
    for _ in range(n - 1):
        bit.append(i % 2)
        i = i // 2
    for j in range(n - 1):
        if bit[j]:
            form = form[: n - j - 1] + "+" + form[n - j - 1 :]
    ans += eval(form)
print(ans)

s = eval(input())
n = len(s)
cand = []

def func(ac, yet, sum):
    next = int(yet[0])
    if len(yet) == 1:
        cand.append(sum + ac + next)
        cand.append(sum + ac * 10 + next)
    else:
        func(next, yet[1:], ac + sum)
        func(ac * 10 + next, yet[1:], sum)

func(0, s, 0)
print((sum(cand) // 2))

+        cand.append(sum + ac * 10 + next)

+        func(ac * 10 + next, yet[1:], sum)


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

if K%2==1:

    n = N//K

    ans = n**3

else:

    n1 = N//K

    n2 = 1+(N-K//2)//K

    ans = n1**3+n2**3

print(ans)

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

L = N//K

if K%2==1:

    print((L**3))

else:

    M = N//(K//2)

    M -= L

    print((L**3+M**3))

N, K = list(map(int, input().split()))
if K % 2 == 1:
    n = N // K
    ans = n**3
else:
    n1 = N // K
    n2 = 1 + (N - K // 2) // K
    ans = n1**3 + n2**3
print(ans)

N, K = list(map(int, input().split()))
L = N // K
if K % 2 == 1:
    print((L**3))
else:
    M = N // (K // 2)
    M -= L
    print((L**3 + M**3))


Input:
from itertools import product

N = int(eval(input()))

ans = 0

for l in range(1, 11):

    for p in product("357", repeat=l):

        v = "".join(p)

        if "3" in v and "5" in v and "7" in v and int("".join(p)) <= N:

            ans += 1

print(ans)

N = int(eval(input()))

cnt = 0

def dfs(s):

    global cnt

    if '3' in s and '5' in s and '7' in s and int(s) <= N:

        cnt += 1

    if len(s) >= 10:

        return cnt

    dfs(s + '3')

    dfs(s + '5')

    dfs(s + '7')

    return cnt

print((dfs('')))

N = int(eval(input()))
cnt = 0

def dfs(s):
    global cnt
    if "3" in s and "5" in s and "7" in s and int(s) <= N:
        cnt += 1
    if len(s) >= 10:
        return cnt
    dfs(s + "3")
    dfs(s + "5")
    dfs(s + "7")
    return cnt

print((dfs("")))

-        if "3" in v and "5" in v and "7" in v and int("".join(p)) <= N:

+    if "3" in s and "5" in s and "7" in s and int(s) <= N:


Input:
X, Y, Z, K = list(map(int, input().split()))

A = sorted(list(map(int, input().split())),reverse=True)

B = sorted(list(map(int, input().split())),reverse=True)

C = sorted(list(map(int, input().split())),reverse=True)

ans = []

for x in range(X):

    if x+1 > K:

        break

    for y in range(Y):

        if (x+1) * (y+1) > K:

            break

        for z in range(Z):

            if (x+1) * (y+1) * (z+1) > K:

                break

            value = A[x] + B[y] + C[z]

            ans.append(value)

ans.sort(reverse=True)

for i in range(K):

    print((ans[i]))

import heapq

X, Y, Z, K = list(map(int, input().split()))

A = sorted(list(map(int, input().split())),reverse=True)

B = sorted(list(map(int, input().split())),reverse=True)

C = sorted(list(map(int, input().split())),reverse=True)

used = set()

candidate = []

heapq.heappush(candidate, (-A[0]-B[0]-C[0], 0, 0, 0))

for _ in range(K):

    value, x, y, z = heapq.heappop(candidate)

    print((-value))

    if x+1 < X and (x+1, y, z) not in used:

        heapq.heappush(candidate, (-A[x+1]-B[y]-C[z], x+1, y, z))

        used.add((x+1, y, z))

    if y+1 < Y and (x, y+1, z) not in used:

        heapq.heappush(candidate, (-A[x]-B[y+1]-C[z], x, y+1, z))

        used.add((x, y+1, z))

    if z+1 < Z and (x, y, z+1) not in used:

        heapq.heappush(candidate, (-A[x]-B[y]-C[z+1], x , y, z+1))

        used.add((x, y, z+1))

X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
ans = []
for x in range(X):
    if x + 1 > K:
        break
    for y in range(Y):
        if (x + 1) * (y + 1) > K:
            break
        for z in range(Z):
            if (x + 1) * (y + 1) * (z + 1) > K:
                break
            value = A[x] + B[y] + C[z]
            ans.append(value)
ans.sort(reverse=True)
for i in range(K):
    print((ans[i]))

import heapq

X, Y, Z, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())), reverse=True)
B = sorted(list(map(int, input().split())), reverse=True)
C = sorted(list(map(int, input().split())), reverse=True)
used = set()
candidate = []
heapq.heappush(candidate, (-A[0] - B[0] - C[0], 0, 0, 0))
for _ in range(K):
    value, x, y, z = heapq.heappop(candidate)
    print((-value))
    if x + 1 < X and (x + 1, y, z) not in used:
        heapq.heappush(candidate, (-A[x + 1] - B[y] - C[z], x + 1, y, z))
        used.add((x + 1, y, z))
    if y + 1 < Y and (x, y + 1, z) not in used:
        heapq.heappush(candidate, (-A[x] - B[y + 1] - C[z], x, y + 1, z))
        used.add((x, y + 1, z))
    if z + 1 < Z and (x, y, z + 1) not in used:
        heapq.heappush(candidate, (-A[x] - B[y] - C[z + 1], x, y, z + 1))
        used.add((x, y, z + 1))

+heapq.heappush(candidate, (-A[0] - B[0] - C[0], 0, 0, 0))

+    value, x, y, z = heapq.heappop(candidate)

+    if x + 1 < X and (x + 1, y, z) not in used:

+        heapq.heappush(candidate, (-A[x + 1] - B[y] - C[z], x + 1, y, z))

+    if y + 1 < Y and (x, y + 1, z) not in used:

+        heapq.heappush(candidate, (-A[x] - B[y + 1] - C[z], x, y + 1, z))

+    if z + 1 < Z and (x, y, z + 1) not in used:

+        heapq.heappush(candidate, (-A[x] - B[y] - C[z + 1], x, y, z + 1))


Input:
n = int(eval(input()))

a = list(map(int, input().split()))

# 貪欲法

# a[i] < a[i+1]なら全部買う

# a[i] > a[i+1]なら全部売る

money = 1000

stock = 0

for i in range(n):

    if i == n-1 or a[i] > a[i+1]:

        money += stock * a[i]

        stock = 0

    elif money > a[i]:

        stock = money // a[i]

        money = money % a[i]

print(money)

n = int(eval(input()))

a = list(map(int, input().split()))

# DP

# dp[i]: i日目の所持金の最大値（持ち株は売る）

dp = [0] * n

dp[0] = 1000

# dp[1] = 0日目に全部買って1日目に全部売る or 0日目から何もしない

# dp[2] = 0 or 1日目に全部買って2日目に全部売る or 1日目から何もしない

# dp[i+1] + 0~i日目のどこかで全部買ってi+1日目に全部売る or i日目から何もしない

for i in range(n):

    if i == 0:

        dp[i] = 1000

        continue

    dp[i] = dp[i-1]

    for j in range(i):

        # j日目に全部買ってi日目に全部売る

        dp[i] = max(dp[i], (dp[j] // a[j]) * a[i] + dp[j] % a[j])

print((dp[n-1]))

n = int(eval(input()))
a = list(map(int, input().split()))
# 貪欲法
# a[i] < a[i+1]なら全部買う
# a[i] > a[i+1]なら全部売る
money = 1000
stock = 0
for i in range(n):
    if i == n - 1 or a[i] > a[i + 1]:
        money += stock * a[i]
        stock = 0
    elif money > a[i]:
        stock = money // a[i]
        money = money % a[i]
print(money)

n = int(eval(input()))
a = list(map(int, input().split()))
# DP
# dp[i]: i日目の所持金の最大値（持ち株は売る）
dp = [0] * n
dp[0] = 1000
# dp[1] = 0日目に全部買って1日目に全部売る or 0日目から何もしない
# dp[2] = 0 or 1日目に全部買って2日目に全部売る or 1日目から何もしない
# dp[i+1] + 0~i日目のどこかで全部買ってi+1日目に全部売る or i日目から何もしない
for i in range(n):
    if i == 0:
        dp[i] = 1000
        continue
    dp[i] = dp[i - 1]
    for j in range(i):
        # j日目に全部買ってi日目に全部売る
        dp[i] = max(dp[i], (dp[j] // a[j]) * a[i] + dp[j] % a[j])
print((dp[n - 1]))

+# dp[i]: i日目の所持金の最大値（持ち株は売る）

+# dp[1] = 0日目に全部買って1日目に全部売る or 0日目から何もしない

+# dp[2] = 0 or 1日目に全部買って2日目に全部売る or 1日目から何もしない

+# dp[i+1] + 0~i日目のどこかで全部買ってi+1日目に全部売る or i日目から何もしない

+        # j日目に全部買ってi日目に全部売る


Input:
N = int(eval(input()))

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

se = set()

dic = dict()

for i in range(N):

    if A[i] in se:

        dic[A[i]] += 1

    else:

        se.add(A[i])

        dic.update({A[i]: 1})

c = 0

for k in list(dic.keys()):

    c += (dic[k])*(dic[k]-1)//2

result = [0]*N

for i in range(N):

    result[i] = c - (dic[A[i]])*(dic[A[i]]-1)//2 + (dic[A[i]]-1)*(dic[A[i]]-2)//2

print(("\n".join(list(map(str, result)))))

N = int(eval(input()))

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

se = set()

dic = dict()

for i in range(N):

    if A[i] in se:

        dic[A[i]] += 1

    else:

        se.add(A[i])

        dic.update({A[i]: 1})

c = 0

for k in list(dic.keys()):

    c += (dic[k])*(dic[k]-1)//2

for i in range(N):

    print((c - dic[A[i]]+1))

N = int(eval(input()))
A = list(map(int, input().split()))
se = set()
dic = dict()
for i in range(N):
    if A[i] in se:
        dic[A[i]] += 1
    else:
        se.add(A[i])
        dic.update({A[i]: 1})
c = 0
for k in list(dic.keys()):
    c += (dic[k]) * (dic[k] - 1) // 2
result = [0] * N
for i in range(N):
    result[i] = (
        c - (dic[A[i]]) * (dic[A[i]] - 1) // 2 + (dic[A[i]] - 1) * (dic[A[i]] - 2) // 2
    )
print(("\n".join(list(map(str, result)))))

N = int(eval(input()))
A = list(map(int, input().split()))
se = set()
dic = dict()
for i in range(N):
    if A[i] in se:
        dic[A[i]] += 1
    else:
        se.add(A[i])
        dic.update({A[i]: 1})
c = 0
for k in list(dic.keys()):
    c += (dic[k]) * (dic[k] - 1) // 2
for i in range(N):
    print((c - dic[A[i]] + 1))


Input:
n = int(eval(input()))

h = list(map(int, input().split()))

cnt = 0

for i in range(n-1):

  if h[i] - h[i+1] >= 2:

    print('No')

    exit()

  elif h[i] - h[i+1] == 1:

    cnt += -1

  elif h[i] - h[i+1] <= -1:

    cnt = 0

  if cnt == -2:

    print('No')

    exit()

print('Yes')

n = int(eval(input()))

h = list(map(int, input().split()))

cnt = h[0]

for i in range(n):

  if h[i] >= cnt-1:

    if h[i] >= cnt:

      cnt = h[i]

  else:

    print('No')

    exit()

print('Yes')

n = int(eval(input()))
h = list(map(int, input().split()))
cnt = 0
for i in range(n - 1):
    if h[i] - h[i + 1] >= 2:
        print("No")
        exit()
    elif h[i] - h[i + 1] == 1:
        cnt += -1
    elif h[i] - h[i + 1] <= -1:
        cnt = 0
    if cnt == -2:
        print("No")
        exit()
print("Yes")

n = int(eval(input()))
h = list(map(int, input().split()))
cnt = h[0]
for i in range(n):
    if h[i] >= cnt - 1:
        if h[i] >= cnt:
            cnt = h[i]
    else:
        print("No")
        exit()
print("Yes")

-cnt = 0
-for i in range(n - 1):
-    if h[i] - h[i + 1] >= 2:
-        print("No")
-        exit()
-    elif h[i] - h[i + 1] == 1:
-        cnt += -1
-    elif h[i] - h[i + 1] <= -1:
-        cnt = 0
-    if cnt == -2:
+cnt = h[0]
+for i in range(n):
+    if h[i] >= cnt - 1:
+        if h[i] >= cnt:
+            cnt = h[i]
+    else:


Input:
n,m=list(map(int,input().split()))

marks = {i+1:-1 for i in range(n)}

marks[1] = 0

roots = {i+1:[] for i in range(n)}

count = 0

for i in range(m):

    a,b = list(map(int,input().split()))

    roots[a].append(b)

    roots[b].append(a)

before_room = 1

searchs = [{'root':roots[1], 'mark':1}]

while searchs != []:

    next_search = []

    for search in searchs:

        for root in search['root']:

            new_root = []

            if marks[root] == -1:

                marks[root] = search['mark']

                count += 1

                new_root += roots[root]

            else:

                continue

            next_search.append({'root':roots[root], 'mark':root})

    searchs = next_search[:]

del marks[1]

print('Yes')

for v in list(marks.values()):

    print(v)

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

marks = {i+1:-1 for i in range(n)}

roots = {i+1:[] for i in range(n)}

for i in range(m):

    a,b = list(map(int,input().split()))

    roots[a].append(b)

    roots[b].append(a)

searchs = [{'root':roots[1], 'mark':1}]

while searchs != []:

    next_search = []

    for search in searchs:

        for root in search['root']:

            if marks[root] == -1:

                marks[root] = search['mark']

            else:

                continue

            next_search.append({'root':roots[root], 'mark':root})

    searchs = next_search[:]

marks[1] = 'Yes'

for v in list(marks.values()):

    print(v)

n, m = list(map(int, input().split()))
marks = {i + 1: -1 for i in range(n)}
marks[1] = 0
roots = {i + 1: [] for i in range(n)}
count = 0
for i in range(m):
    a, b = list(map(int, input().split()))
    roots[a].append(b)
    roots[b].append(a)
before_room = 1
searchs = [{"root": roots[1], "mark": 1}]
while searchs != []:
    next_search = []
    for search in searchs:
        for root in search["root"]:
            new_root = []
            if marks[root] == -1:
                marks[root] = search["mark"]
                count += 1
                new_root += roots[root]
            else:
                continue
            next_search.append({"root": roots[root], "mark": root})
    searchs = next_search[:]
del marks[1]
print("Yes")
for v in list(marks.values()):
    print(v)

n, m = list(map(int, input().split()))
marks = {i + 1: -1 for i in range(n)}
roots = {i + 1: [] for i in range(n)}
for i in range(m):
    a, b = list(map(int, input().split()))
    roots[a].append(b)
    roots[b].append(a)
searchs = [{"root": roots[1], "mark": 1}]
while searchs != []:
    next_search = []
    for search in searchs:
        for root in search["root"]:
            if marks[root] == -1:
                marks[root] = search["mark"]
            else:
                continue
            next_search.append({"root": roots[root], "mark": root})
    searchs = next_search[:]
marks[1] = "Yes"
for v in list(marks.values()):
    print(v)

-                new_root += roots[root]


Input:
n=int(eval(input()))

a=[]

for i in range(n):

  a_=list(map(int,input().split()))

  a.append(a_)

dp=[[0]*3 for i in range(n)]

dp[0]=a[0]

for i in range(1,n):

  for k in range(3):

    for j in range(3):

      if k!=j:

        dp[i][k]=max(dp[i][k], dp[i-1][j] + a[i][k])

#print(dp)

print((max(dp[n-1])))

n=int(eval(input()))

abc=[[0,0,0]]

for i in range(n):

  labc=list(map(int,input().split()))

  abc.append(labc)

dp=[[0]*3 for i in range(n+1)]

for i in range(n):

  for j in range(3):

    if j==0:

      dp[i+1][j]=max(dp[i][1],dp[i][2])+abc[i+1][j]

    elif j==1:

      dp[i+1][j]=max(dp[i][0],dp[i][2])+abc[i+1][j]

    else:

      dp[i+1][j]=max(dp[i][1],dp[i][0])+abc[i+1][j]

print((max(dp[n])))

#print(dp)

n = int(eval(input()))
a = []
for i in range(n):
    a_ = list(map(int, input().split()))
    a.append(a_)
dp = [[0] * 3 for i in range(n)]
dp[0] = a[0]
for i in range(1, n):
    for k in range(3):
        for j in range(3):
            if k != j:
                dp[i][k] = max(dp[i][k], dp[i - 1][j] + a[i][k])
# print(dp)
print((max(dp[n - 1])))

n = int(eval(input()))
abc = [[0, 0, 0]]
for i in range(n):
    labc = list(map(int, input().split()))
    abc.append(labc)
dp = [[0] * 3 for i in range(n + 1)]
for i in range(n):
    for j in range(3):
        if j == 0:
            dp[i + 1][j] = max(dp[i][1], dp[i][2]) + abc[i + 1][j]
        elif j == 1:
            dp[i + 1][j] = max(dp[i][0], dp[i][2]) + abc[i + 1][j]
        else:
            dp[i + 1][j] = max(dp[i][1], dp[i][0]) + abc[i + 1][j]
print((max(dp[n])))
# print(dp)

-    a_ = list(map(int, input().split()))

+    labc = list(map(int, input().split()))


Input:
n=int(eval(input()))

m=list(range(1,n+1))

even=[]

odd=[]

for x in range (0,len(m)):

    if len(m)==0:

        print('0')

    else:

        if m[x] % 2 == 0:

            even.append(m[x])

        else:

            odd.append(m[x])

output = [[a, b] for a in even

              for b in odd]

print((len(output)))

n = int(eval(input()))
m = list(range(1, n + 1))
even = []
odd = []
for x in range(0, len(m)):
    if len(m) == 0:
        print("0")
    else:
        if m[x] % 2 == 0:
            even.append(m[x])
        else:
            odd.append(m[x])
output = [[a, b] for a in even for b in odd]
print((len(output)))

-output = [[a, b] for a in even for b in odd]


Input:
n,m,k=list(map(int, input().split()))

count=0

for i in range(n+1):

    for j in range(1,m+1):

        count=(i*m)+(j*n)-2*(i*j)

        if count==k:

            print('Yes')

            exit()

print('No')

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

for i in range(n+1):

    for j in range(m+1):

        x=i*m+j*n-2*(i*j)

        if k==x:

            print('Yes')

            exit()

print('No')

n, m, k = list(map(int, input().split()))
count = 0
for i in range(n + 1):
    for j in range(1, m + 1):
        count = (i * m) + (j * n) - 2 * (i * j)
        if count == k:
            print("Yes")
            exit()
print("No")

n, m, k = list(map(int, input().split()))
for i in range(n + 1):
    for j in range(m + 1):
        x = i * m + j * n - 2 * (i * j)
        if k == x:
            print("Yes")
            exit()
print("No")


Input:
first = eval(input())

first = first.split()

K = int(first[0])

N = int(first[1])

second = eval(input())

second = second.split()

distance = []

diff = 0

for i in range(0, N):

    if i == N-1:

        diff0 = 0

        diff1 = 0

        diff0 = int(second[0])

        diff1 = K - int(second[i])

        diff = diff0 + diff1

        distance.append(diff)

    else:

        diff = int(second[i+1]) - int(second[i])

        distance.append(diff)

MaxIndex = distance.index(max(distance))

distance.pop(MaxIndex)

Max = sum(distance)

print(Max)

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

A_list = list(map(int, input().split()))

distance = []

diff = 0

for i in range(0, N):

    if i == N-1:

        diff = K + A_list[0] - A_list[i]

        distance.append(diff)

    else:

        diff = A_list[i+1] - A_list[i]

        distance.append(diff)

print((sum(distance) - max(distance)))

first = eval(input())
first = first.split()
K = int(first[0])
N = int(first[1])
second = eval(input())
second = second.split()
distance = []
diff = 0
for i in range(0, N):
    if i == N - 1:
        diff0 = 0
        diff1 = 0
        diff0 = int(second[0])
        diff1 = K - int(second[i])
        diff = diff0 + diff1
        distance.append(diff)
    else:
        diff = int(second[i + 1]) - int(second[i])
        distance.append(diff)
MaxIndex = distance.index(max(distance))
distance.pop(MaxIndex)
Max = sum(distance)
print(Max)

K, N = list(map(int, input().split()))
A_list = list(map(int, input().split()))
distance = []
diff = 0
for i in range(0, N):
    if i == N - 1:
        diff = K + A_list[0] - A_list[i]
        distance.append(diff)
    else:
        diff = A_list[i + 1] - A_list[i]
        distance.append(diff)
print((sum(distance) - max(distance)))

+A_list = list(map(int, input().split()))

+        diff = K + A_list[0] - A_list[i]

-        diff = int(second[i + 1]) - int(second[i])

+        diff = A_list[i + 1] - A_list[i]


Input:
N = int(eval(input()))

A = [None] * N

B = [None] * N

for i in range(N):

    a, b = list(map(int, input().split()))

    A[i] = a

    B[i] = b

A.sort()

B.sort()

if N % 2 == 0:

    b = (B[N // 2] + B[(N - 1) // 2]) / 2

    a = (A[N // 2] + A[(N - 1) // 2]) / 2

    print((int((b - a) * 2 + 1)))

else:

    print((B[N // 2] - A[N // 2] + 1))

from sys import stdin

readline = stdin.readline

N = int(readline())

A = [None] * N

B = [None] * N

for i in range(N):

    a, b = list(map(int, readline().split()))

    A[i] = a

    B[i] = b

A.sort()

B.sort()

if N % 2 == 0:

    b = (B[N // 2] + B[(N - 1) // 2]) / 2

    a = (A[N // 2] + A[(N - 1) // 2]) / 2

    print((int((b - a) * 2 + 1)))

else:

    print((B[N // 2] - A[N // 2] + 1))

-    a, b = list(map(int, input().split()))

+    a, b = list(map(int, readline().split()))


Input:
def main():

    from math import gcd

    MINFact = [-1] * (10**6 + 10)

    MINFact[0] = 0

    MINFact[1] = 1

    def Eratosthenes(sup: int) -> set:

        primes = [True for i in range(sup+1)]

        primes[0] = False

        primes[1] = False

        for i in range(2, sup+1):

            if primes[i]:

                MINFact[i] = i

                mul = 2

                while i*mul <= sup:

                    primes[i*mul] = False

                    if MINFact[i*mul] == -1:

                        MINFact[i*mul] = i

                    mul += 1

    def prime_factor(n):

        while n != 1:

            prime = MINFact[n]

            while MINFact[n] == prime:

                n //= prime

            B[prime] += 1

    _ = int(eval(input()))

    A = [int(i) for i in input().split()]

    g = A[0]

    for a in A:

        g = gcd(g, a)

    Eratosthenes(10**6 + 5)

    B = [0]*(10**6 + 5)

    for a in A:

        prime_factor(a)

    # 素因数分解したのをBにいれる

    # 1より大きいのがあったら gcd != 1 のペアが存在する

    if g != 1:

        print("not coprime")

    elif max(B) <= 1:

        print("pairwise coprime")

    else:

        print("setwise coprime")

if __name__ == '__main__':

    main()

def main():

    from math import gcd

    _ = int(eval(input()))

    A = [int(i) for i in input().split()]

    class osa_k:

        def __init__(self, sup):

            if sup > 2 * 10**8:

                raise RuntimeError("too big")

            self.MINFact = [-1] * (sup + 1)

            self.MINFact[0] = 0

            self.MINFact[1] = 1

            primes = [True for i in range(sup+1)]

            primes[0] = False

            primes[1] = False

            for i in range(2, sup+1):

                if primes[i]:

                    self.MINFact[i] = i

                    mul = 2

                    while i*mul <= sup:

                        primes[i*mul] = False

                        if self.MINFact[i*mul] == -1:

                            self.MINFact[i*mul] = i

                        mul += 1

        def prime_factor(self, n, B):

            while n > 1:

                prime = self.MINFact[n]

                while self.MINFact[n] == prime:

                    n //= prime

                B[prime] += 1

    maxA = max(A)

    p = osa_k(maxA)

    B = [0] * (maxA + 1)

    for a in A:

        p.prime_factor(a, B)

    g = A[0]

    for a in A:

        g = gcd(g, a)

    if g != 1:

        print("not coprime")

    elif max(B) <= 1:

        print("pairwise coprime")

    else:

        print("setwise coprime")

if __name__ == '__main__':

    main()

def main():
    from math import gcd

    MINFact = [-1] * (10**6 + 10)
    MINFact[0] = 0
    MINFact[1] = 1

    def Eratosthenes(sup: int) -> set:
        primes = [True for i in range(sup + 1)]
        primes[0] = False
        primes[1] = False
        for i in range(2, sup + 1):
            if primes[i]:
                MINFact[i] = i
                mul = 2
                while i * mul <= sup:
                    primes[i * mul] = False
                    if MINFact[i * mul] == -1:
                        MINFact[i * mul] = i
                    mul += 1

    def prime_factor(n):
        while n != 1:
            prime = MINFact[n]
            while MINFact[n] == prime:
                n //= prime
            B[prime] += 1

    _ = int(eval(input()))
    A = [int(i) for i in input().split()]
    g = A[0]
    for a in A:
        g = gcd(g, a)
    Eratosthenes(10**6 + 5)
    B = [0] * (10**6 + 5)
    for a in A:
        prime_factor(a)
    # 素因数分解したのをBにいれる
    # 1より大きいのがあったら gcd != 1 のペアが存在する
    if g != 1:
        print("not coprime")
    elif max(B) <= 1:
        print("pairwise coprime")
    else:
        print("setwise coprime")

if __name__ == "__main__":
    main()

def main():
    from math import gcd

    _ = int(eval(input()))
    A = [int(i) for i in input().split()]

    class osa_k:
        def __init__(self, sup):
            if sup > 2 * 10**8:
                raise RuntimeError("too big")
            self.MINFact = [-1] * (sup + 1)
            self.MINFact[0] = 0
            self.MINFact[1] = 1
            primes = [True for i in range(sup + 1)]
            primes[0] = False
            primes[1] = False
            for i in range(2, sup + 1):
                if primes[i]:
                    self.MINFact[i] = i
                    mul = 2
                    while i * mul <= sup:
                        primes[i * mul] = False
                        if self.MINFact[i * mul] == -1:
                            self.MINFact[i * mul] = i
                        mul += 1

        def prime_factor(self, n, B):
            while n > 1:
                prime = self.MINFact[n]
                while self.MINFact[n] == prime:
                    n //= prime
                B[prime] += 1

    maxA = max(A)
    p = osa_k(maxA)
    B = [0] * (maxA + 1)
    for a in A:
        p.prime_factor(a, B)
    g = A[0]
    for a in A:
        g = gcd(g, a)
    if g != 1:
        print("not coprime")
    elif max(B) <= 1:
        print("pairwise coprime")
    else:
        print("setwise coprime")

if __name__ == "__main__":
    main()

-        primes = [True for i in range(sup + 1)]

-                    primes[i * mul] = False

-                    if MINFact[i * mul] == -1:

-                        MINFact[i * mul] = i

+                raise RuntimeError("too big")

+            self.MINFact = [-1] * (sup + 1)

+            primes = [True for i in range(sup + 1)]

+            for i in range(2, sup + 1):

+                    self.MINFact[i] = i

+                    while i * mul <= sup:

+                        primes[i * mul] = False

+                        if self.MINFact[i * mul] == -1:

+                            self.MINFact[i * mul] = i

+                prime = self.MINFact[n]

+                while self.MINFact[n] == prime:

-    # 素因数分解したのをBにいれる

-    # 1より大きいのがあったら gcd != 1 のペアが存在する


Input:
X = [*list(map(int, input().split()))]

for i in range(len(X)):

    if X[i] == 0: print((i+1))

X = [*list(map(int, input().split()))]
for i in range(len(X)):
    if X[i] == 0:
        print((i + 1))

+print((15 - sum([*list(map(int, input().split()))])))


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

a=[int(i) for i in input().split()]

array=[]

result=a[n-1]-a[0]

array.append(result)

for i in range(1,n):

    result=k+a[i-1]-a[i]

    array.append(result)

#print(min(array))

###################################

len_array=[]

for i in range(n):

    if i==n-1:

        length=a[0]-a[i]+k

    else:

        length=a[i+1]-a[i]

    len_array.append(length)

# len_array.index(max(len_array))

print((sum(len_array)-max(len_array)))

###################################

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

a=[int(i) for i in input().split()]

len_array=[]

for i in range(n):

    if i==n-1:

        length=a[0]-a[i]+k

    else:

        length=a[i+1]-a[i]

    len_array.append(length)

print((sum(len_array)-max(len_array)))

k, n = list(map(int, input().split()))
a = [int(i) for i in input().split()]
array = []
result = a[n - 1] - a[0]
array.append(result)
for i in range(1, n):
    result = k + a[i - 1] - a[i]
    array.append(result)
# print(min(array))
###################################
len_array = []
for i in range(n):
    if i == n - 1:
        length = a[0] - a[i] + k
    else:
        length = a[i + 1] - a[i]
    len_array.append(length)
# len_array.index(max(len_array))
print((sum(len_array) - max(len_array)))
###################################

k, n = list(map(int, input().split()))
a = [int(i) for i in input().split()]
len_array = []
for i in range(n):
    if i == n - 1:
        length = a[0] - a[i] + k
    else:
        length = a[i + 1] - a[i]
    len_array.append(length)
print((sum(len_array) - max(len_array)))


Input:
#!/usr/bin/env python3

(n, ), *s = [[*list(map(int, i.split()))] for i in open(0)]

a, b = list(zip(*s))

c = 0

for i in reversed(list(range(n))):

    q = (a[i] + c) % b[i]

    c += b[i] - q if q > 0 else 0

print(c)

#!/usr/bin/env python3

(n, ), *s = [[*list(map(int, i.split()))] for i in open(0)]

c = 0

for a, b in reversed(s):

    q = (a + c) % b

    c += b * (q > 0) - q

print(c)

#!/usr/bin/env python3
(n,), *s = [[*list(map(int, i.split()))] for i in open(0)]
a, b = list(zip(*s))
c = 0
for i in reversed(list(range(n))):
    q = (a[i] + c) % b[i]
    c += b[i] - q if q > 0 else 0
print(c)

#!/usr/bin/env python3
(n,), *s = [[*list(map(int, i.split()))] for i in open(0)]
c = 0
for a, b in reversed(s):
    q = (a + c) % b
    c += b * (q > 0) - q
print(c)

-a, b = list(zip(*s))
-for i in reversed(list(range(n))):
-    q = (a[i] + c) % b[i]
-    c += b[i] - q if q > 0 else 0
+for a, b in reversed(s):
+    q = (a + c) % b
+    c += b * (q > 0) - q


Input:
from collections import defaultdict, deque

import sys

input = sys.stdin.readline

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

edges = [list(range(N)) for i in range(N)]

for i in range(N):

    edges[i].remove(i)

for _ in range(M):

    a, b = list(map(int, input().split()))

    edges[a-1].remove(b-1)

    edges[b-1].remove(a-1)

size = defaultdict(lambda: [0, 0])

color = [-1]*N

def set_color(root):

    que = deque([root])

    color[root] = 0

    size[root][0]+=1

    while que:

        v = que.pop()

        for nv in edges[v]:

            if color[nv]<0:

                c = 1- color[v]

                color[nv] = c

                size[root][c]+=1

                que.append(nv)

            elif color[nv] == color[v]:

                print((-1))

                sys.exit()

for i in range(N):

    if color[i]<0:

        set_color(i)

S = set([0])

for a, b in list(size.values()):

    S = set(s+b for s in S)|set(s+a for s in S)

ans = min(x*(x-1)//2+(N-x)*(N-x-1)//2 for x in S)

print(ans)

from collections import defaultdict, deque

import sys

input = sys.stdin.readline

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

edges = [set(range(N)) for i in range(N)]

for i in range(N):

    edges[i].remove(i)

for _ in range(M):

    a, b = list(map(int, input().split()))

    edges[a-1].remove(b-1)

    edges[b-1].remove(a-1)

size = defaultdict(lambda: [0, 0])

color = [-1]*N

def set_color(root):

    que = deque([root])

    color[root] = 0

    size[root][0]+=1

    while que:

        v = que.pop()

        for nv in edges[v]:

            if color[nv]<0:

                c = 1- color[v]

                color[nv] = c

                size[root][c]+=1

                que.append(nv)

            elif color[nv] == color[v]:

                print((-1))

                sys.exit()

for i in range(N):

    if color[i]<0:

        set_color(i)

S = set([0])

for a, b in list(size.values()):

    S = set(s+b for s in S)|set(s+a for s in S)

ans = min(x*(x-1)//2+(N-x)*(N-x-1)//2 for x in S)

print(ans)

from collections import defaultdict, deque
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
edges = [list(range(N)) for i in range(N)]
for i in range(N):
    edges[i].remove(i)
for _ in range(M):
    a, b = list(map(int, input().split()))
    edges[a - 1].remove(b - 1)
    edges[b - 1].remove(a - 1)
size = defaultdict(lambda: [0, 0])
color = [-1] * N

def set_color(root):
    que = deque([root])
    color[root] = 0
    size[root][0] += 1
    while que:
        v = que.pop()
        for nv in edges[v]:
            if color[nv] < 0:
                c = 1 - color[v]
                color[nv] = c
                size[root][c] += 1
                que.append(nv)
            elif color[nv] == color[v]:
                print((-1))
                sys.exit()

for i in range(N):
    if color[i] < 0:
        set_color(i)
S = set([0])
for a, b in list(size.values()):
    S = set(s + b for s in S) | set(s + a for s in S)
ans = min(x * (x - 1) // 2 + (N - x) * (N - x - 1) // 2 for x in S)
print(ans)

from collections import defaultdict, deque
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
edges = [set(range(N)) for i in range(N)]
for i in range(N):
    edges[i].remove(i)
for _ in range(M):
    a, b = list(map(int, input().split()))
    edges[a - 1].remove(b - 1)
    edges[b - 1].remove(a - 1)
size = defaultdict(lambda: [0, 0])
color = [-1] * N

def set_color(root):
    que = deque([root])
    color[root] = 0
    size[root][0] += 1
    while que:
        v = que.pop()
        for nv in edges[v]:
            if color[nv] < 0:
                c = 1 - color[v]
                color[nv] = c
                size[root][c] += 1
                que.append(nv)
            elif color[nv] == color[v]:
                print((-1))
                sys.exit()

for i in range(N):
    if color[i] < 0:
        set_color(i)
S = set([0])
for a, b in list(size.values()):
    S = set(s + b for s in S) | set(s + a for s in S)
ans = min(x * (x - 1) // 2 + (N - x) * (N - x - 1) // 2 for x in S)
print(ans)

-edges = [list(range(N)) for i in range(N)]

+edges = [set(range(N)) for i in range(N)]


Input:
import sys

limit = 10 ** 5 * 2

sys.setrecursionlimit(limit)

def calc_depth(n):

    if children[n] == set():

        return 0

    depth_l = [calc_depth(child) for child in children[n]]

    depth_l.sort(reverse=True)

    return max(depth + (i+1) for (i, depth) in enumerate(depth_l))

N = int(eval(input()))

children = [set() for i in range(N + 1)]

for i in range(N - 1):

    a = int(eval(input()))

    children[a].add(i + 2)

ans = calc_depth(1)

# print(children)

print(ans)

from sys import stdin, setrecursionlimit

setrecursionlimit(10**7)

def solve():

    N = int(stdin.readline())

    Adj = [[] for i in range(N)]

    for i in range(N - 1):

        a = int(stdin.readline()) - 1

        Adj[a].append(i + 1)

    ans = dfs(N, Adj, 0, -1)

    print(ans)

def dfs(N, Adj, v, p):

    dl = []

    for u in Adj[v]:

        if u == p:

            continue

        dl.append(dfs(N, Adj, u, v))

    dl.sort(reverse=True)

    if not dl:

        return 0

    res = max(dl[i] + i + 1 for i in range(len(dl)))

    return res

if __name__ == '__main__':

    solve()

import sys

limit = 10**5 * 2
sys.setrecursionlimit(limit)

def calc_depth(n):
    if children[n] == set():
        return 0
    depth_l = [calc_depth(child) for child in children[n]]
    depth_l.sort(reverse=True)
    return max(depth + (i + 1) for (i, depth) in enumerate(depth_l))

N = int(eval(input()))
children = [set() for i in range(N + 1)]
for i in range(N - 1):
    a = int(eval(input()))
    children[a].add(i + 2)
ans = calc_depth(1)
# print(children)
print(ans)

from sys import stdin, setrecursionlimit

setrecursionlimit(10**7)

def solve():
    N = int(stdin.readline())
    Adj = [[] for i in range(N)]
    for i in range(N - 1):
        a = int(stdin.readline()) - 1
        Adj[a].append(i + 1)
    ans = dfs(N, Adj, 0, -1)
    print(ans)

def dfs(N, Adj, v, p):
    dl = []
    for u in Adj[v]:
        if u == p:
            continue
        dl.append(dfs(N, Adj, u, v))
    dl.sort(reverse=True)
    if not dl:
        return 0
    res = max(dl[i] + i + 1 for i in range(len(dl)))
    return res

if __name__ == "__main__":
    solve()

+from sys import stdin, setrecursionlimit

-    depth_l = [calc_depth(child) for child in children[n]]

-    return max(depth + (i + 1) for (i, depth) in enumerate(depth_l))

-children = [set() for i in range(N + 1)]

+    res = max(dl[i] + i + 1 for i in range(len(dl)))


Input:
n=int(eval(input()))

h=list(map(int,input().split()))

dp=[10**9]*n

dp[0]=0

dp[1]=h[1]-h[0]

for i in range(1,n):

    c1=abs(h[i]-h[i-1])

    c2=abs(h[i]-h[i-2])

    dp[i]=min(dp[i-1]+c1,dp[i-2]+c2)

print((dp[n-1]))

N = int(eval(input()))

H = list(map(int, input().split()))

dp = [0] * N

dp[1] = abs(H[1] - H[0])

for i in range(2, N):

	dp[i] = min(dp[i - 1] + abs(H[i] - H[i - 1]), dp[i - 2] + abs(H[i] - H[i - 2]))

print((dp[-1]))


Input:
from fractions import gcd

n=int(eval(input()))

a=list(map(int,input().split()))

for s in range(len(a)-1):

  if s==0:x=a[s]

  x=gcd(x,a[s+1])

print(x)

from fractions import*

from functools import*

n,*a=list(map(int,open(0).read().split()))

print((reduce(gcd,a)))

from fractions import gcd

n = int(eval(input()))
a = list(map(int, input().split()))
for s in range(len(a) - 1):
    if s == 0:
        x = a[s]
    x = gcd(x, a[s + 1])
print(x)

from fractions import *
from functools import *

n, *a = list(map(int, open(0).read().split()))
print((reduce(gcd, a)))

+n, *a = list(map(int, open(0).read().split()))


Input:
n=int(eval(input()))

h=list(map(int,input().split()))

r=[None]*(n-1)

for i in range(n-1):

  if h[i]>=h[i+1]:

    r[i]=True

  else:

    r[i]=False

m=0

count=0

for i in range(len(r)):

  if r[i]:

    count=count+1

    m=max(m,count)

  else:

    count=0

print(m)

N=int(eval(input()))

H=list(map(int,input().split()))

cur=H[0]

streak=0

ans=0

for i in range(1,N):

  if H[i]<=cur:

    streak+=1

    if ans<streak:

      ans=streak

  else:

    streak=0

  cur=H[i]

print(ans)

n = int(eval(input()))
h = list(map(int, input().split()))
r = [None] * (n - 1)
for i in range(n - 1):
    if h[i] >= h[i + 1]:
        r[i] = True
    else:
        r[i] = False
m = 0
count = 0
for i in range(len(r)):
    if r[i]:
        count = count + 1
        m = max(m, count)
    else:
        count = 0
print(m)

N = int(eval(input()))
H = list(map(int, input().split()))
cur = H[0]
streak = 0
ans = 0
for i in range(1, N):
    if H[i] <= cur:
        streak += 1
        if ans < streak:
            ans = streak
    else:
        streak = 0
    cur = H[i]
print(ans)

-n = int(eval(input()))
-h = list(map(int, input().split()))
-r = [None] * (n - 1)
-for i in range(n - 1):
-    if h[i] >= h[i + 1]:
-        r[i] = True
+N = int(eval(input()))
+H = list(map(int, input().split()))
+cur = H[0]
+streak = 0
+ans = 0
+for i in range(1, N):
+    if H[i] <= cur:
+        streak += 1
+        if ans < streak:
+            ans = streak
-        r[i] = False
-m = 0
-count = 0
-for i in range(len(r)):
-    if r[i]:
-        count = count + 1
-        m = max(m, count)
-    else:
-        count = 0
-print(m)
+        streak = 0
+    cur = H[i]
+print(ans)


Input:
def calc(i, j, B1, len_b):

    extra = 0

    d = j - i

    # i < j < k

    k1 = d + j

    if k1 < len_b:

        extra += B1[k1]

    # k < i < j

    k2 = i - d

    if 0 <= k2 and k2 < len_b:

        extra += B1[k2]

    # i < k < j

    s = i + j

    if s % 2 == 0:

        k3 = s // 2

        if k3 < len_b:

            extra += B1[k3]

    return extra

def main():

    N = int(eval(input()))

    S = eval(input())

    R, G, B = [], [], []

    num_r, num_g, num_b = 0, 0, 0

    for i in range(N):

        s = S[i]

        if s == 'R':

            R.append(i)

            num_r += 1

        elif s == 'G':

            G.append(i)

            num_g += 1

        elif s == 'B':

            B.append(i)

            num_b += 1

    if len(R) == 0 or len(G) == 0 or len(B) == 0:

        print((0))

        return

    len_b = (B[-1] + 1)

    B1 = [0]*len_b

    for b in B:

        B1[b] = 1

    # print(R, G, B)

    ans = 0

    for i in R:

        for j in G:

            ans += num_b

            if i < j:

                ans -= calc(i, j, B1, len_b)

            else:

                ans -= calc(j, i, B1, len_b)

    print(ans)

if __name__ == '__main__':

    main()

def calc(i, j, B1, len_b):

    extra = 0

    d = j - i

    # i < j < k

    k1 = d + j

    if k1 < len_b:

        extra += B1[k1]

    # k < i < j

    k2 = i - d

    if 0 <= k2 and k2 < len_b:

        extra += B1[k2]

    # i < k < j

    s = i + j

    if s % 2 == 0:

        k3 = s // 2

        if k3 < len_b:

            extra += B1[k3]

    return extra

def main():

    N = int(eval(input()))

    S = eval(input())

    RGB = {'R': [], 'G': [], 'B': []}

    for i in range(N):

        RGB[S[i]].append(i)

    num_r = len(RGB['R'])

    num_g = len(RGB['G'])

    num_b = len(RGB['B'])

    if num_r == 0 or num_g == 0 or num_b == 0:

        print((0))

        return

    len_b = (RGB['B'][-1] + 1)

    B1 = [0]*len_b

    for b in RGB['B']:

        B1[b] = 1

    # print(R, G, B)

    ans = num_r * num_g * num_b

    for i in RGB['R']:

        for j in RGB['G']:

            ans -= calc(min(i, j), max(i, j), B1, len_b)

    print(ans)

if __name__ == '__main__':

    main()

def calc(i, j, B1, len_b):
    extra = 0
    d = j - i
    # i < j < k
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]
    # k < i < j
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]
    # i < k < j
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(eval(input()))
    S = eval(input())
    R, G, B = [], [], []
    num_r, num_g, num_b = 0, 0, 0
    for i in range(N):
        s = S[i]
        if s == "R":
            R.append(i)
            num_r += 1
        elif s == "G":
            G.append(i)
            num_g += 1
        elif s == "B":
            B.append(i)
            num_b += 1
    if len(R) == 0 or len(G) == 0 or len(B) == 0:
        print((0))
        return
    len_b = B[-1] + 1
    B1 = [0] * len_b
    for b in B:
        B1[b] = 1
    # print(R, G, B)
    ans = 0
    for i in R:
        for j in G:
            ans += num_b
            if i < j:
                ans -= calc(i, j, B1, len_b)
            else:
                ans -= calc(j, i, B1, len_b)
    print(ans)

if __name__ == "__main__":
    main()

def calc(i, j, B1, len_b):
    extra = 0
    d = j - i
    # i < j < k
    k1 = d + j
    if k1 < len_b:
        extra += B1[k1]
    # k < i < j
    k2 = i - d
    if 0 <= k2 and k2 < len_b:
        extra += B1[k2]
    # i < k < j
    s = i + j
    if s % 2 == 0:
        k3 = s // 2
        if k3 < len_b:
            extra += B1[k3]
    return extra

def main():
    N = int(eval(input()))
    S = eval(input())
    RGB = {"R": [], "G": [], "B": []}
    for i in range(N):
        RGB[S[i]].append(i)
    num_r = len(RGB["R"])
    num_g = len(RGB["G"])
    num_b = len(RGB["B"])
    if num_r == 0 or num_g == 0 or num_b == 0:
        print((0))
        return
    len_b = RGB["B"][-1] + 1
    B1 = [0] * len_b
    for b in RGB["B"]:
        B1[b] = 1
    # print(R, G, B)
    ans = num_r * num_g * num_b
    for i in RGB["R"]:
        for j in RGB["G"]:
            ans -= calc(min(i, j), max(i, j), B1, len_b)
    print(ans)

if __name__ == "__main__":
    main()

-    if len(R) == 0 or len(G) == 0 or len(B) == 0:

+    if num_r == 0 or num_g == 0 or num_b == 0:

-                ans -= calc(i, j, B1, len_b)

-                ans -= calc(j, i, B1, len_b)

+            ans -= calc(min(i, j), max(i, j), B1, len_b)


Input:
N=int(eval(input()))

L=[]

for i in range(N):

    x,y=list(map(int,input().split()))

    L.append([x,y])

cnt=0

S=0

for i in range(N-1):

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

        cnt+=1

        S+=((L[i][0]-L[j][0])**2+(L[i][1]-L[j][1])**2)**0.5

AVE=S/cnt

print((AVE*(N-1)))

N=int(eval(input()))

L=[]

for i in range(N):

    x,y=list(map(int,input().split()))

    L.append([x,y])

D=[]

for i in range(N-1):

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

        d=((L[i][0]-L[j][0])**2+(L[i][1]-L[j][1])**2)**0.5

        D.append(d)

Ave=sum(D)/len(D)

print((Ave*(N-1)))

N = int(eval(input()))
L = []
for i in range(N):
    x, y = list(map(int, input().split()))
    L.append([x, y])
cnt = 0
S = 0
for i in range(N - 1):
    for j in range(i + 1, N):
        cnt += 1
        S += ((L[i][0] - L[j][0]) ** 2 + (L[i][1] - L[j][1]) ** 2) ** 0.5
AVE = S / cnt
print((AVE * (N - 1)))

N = int(eval(input()))
L = []
for i in range(N):
    x, y = list(map(int, input().split()))
    L.append([x, y])
D = []
for i in range(N - 1):
    for j in range(i + 1, N):
        d = ((L[i][0] - L[j][0]) ** 2 + (L[i][1] - L[j][1]) ** 2) ** 0.5
        D.append(d)
Ave = sum(D) / len(D)
print((Ave * (N - 1)))


Input:
n = int(eval(input()))

data = {}

names = []

max_poll = 1

for i in range(n):

    s = eval(input())

    try:

        data[s] += 1

    except:

        data[s] = 1

        names.append(s)

    if max_poll < data[s]:

        max_poll = data[s]

winner = []

for key, value in list(data.items()):

    if max_poll == value:

        winner.append(key)

winner.sort()

for i in winner:

    print(i)

n = int(eval(input()))

data = {}

max_poll = 1

for i in range(n):

    s = eval(input())

    try:

        data[s] += 1

    except:

        data[s] = 1

    if max_poll < data[s]:

        max_poll = data[s]

winner = []

for key, value in list(data.items()):

    if max_poll == value:

        winner.append(key)

winner.sort()

for i in winner:

    print(i)


Input:
# coding: utf-8

# Your code here!

import sys

sys.setrecursionlimit(10**6)

readline = sys.stdin.readline

#n = int(input())

n,t = [int(i) for i in readline().split()]

a = [10**9+1]+[int(i) for i in readline().split()]

"""

gain = 0

m = 0

maxid = []

minid = []

memo = {}

for i in range(n-1,-1,-1):

    ai = a[i]

    if ai > m:

        m = ai

        maxid = [i]

        memo.append([minid[:],maxid[:]])

    elif ai == m:

        maxid.append[i]

    m = max(ai,m)

    if gain < m-ai:

        gain = m-ai

        minid = [i]

        memo = []

    elif gain == m-ai:

        minid.append(i)

    print(minid,maxid,memo)

"""

gain = 0

m = 0

for ai in reversed(a):

    m = max(ai,m)

    if gain < m-ai: gain = m-ai

m = 0

mi = ma = 0

ans = 0

for ai in reversed(a):

    if m < ai:

        ans += max(mi,ma)

        mi = ma = 0

    elif m == ai:

        ma += 1

    m = max(m,ai)

    if m-ai == gain:

        mi += 1

print(ans)

# coding: utf-8

# Your code here!

import sys

sys.setrecursionlimit(10**6)

readline = sys.stdin.readline

#n = int(input())

n,t = [int(i) for i in readline().split()]

a = [int(i) for i in readline().split()]

m = gain = 0

ans = 0

for ai in reversed(a):

    m = max(ai,m)

    if gain < m-ai:

        ans = 1

    elif gain == m-ai:

        ans += 1

    gain = max(gain,m-ai)

print(ans)

# coding: utf-8
# Your code here!
import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
# n = int(input())
n, t = [int(i) for i in readline().split()]
a = [10**9 + 1] + [int(i) for i in readline().split()]
"""
gain = 0
m = 0
maxid = []
minid = []
memo = {}
for i in range(n-1,-1,-1):
    ai = a[i]
    if ai > m:
        m = ai
        maxid = [i]
        memo.append([minid[:],maxid[:]])
    elif ai == m:
        maxid.append[i]
    m = max(ai,m)
    if gain < m-ai:
        gain = m-ai
        minid = [i]
        memo = []
    elif gain == m-ai:
        minid.append(i)
    print(minid,maxid,memo)
"""
gain = 0
m = 0
for ai in reversed(a):
    m = max(ai, m)
    if gain < m - ai:
        gain = m - ai
m = 0
mi = ma = 0
ans = 0
for ai in reversed(a):
    if m < ai:
        ans += max(mi, ma)
        mi = ma = 0
    elif m == ai:
        ma += 1
    m = max(m, ai)
    if m - ai == gain:
        mi += 1
print(ans)

# coding: utf-8
# Your code here!
import sys

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
# n = int(input())
n, t = [int(i) for i in readline().split()]
a = [int(i) for i in readline().split()]
m = gain = 0
ans = 0
for ai in reversed(a):
    m = max(ai, m)
    if gain < m - ai:
        ans = 1
    elif gain == m - ai:
        ans += 1
    gain = max(gain, m - ai)
print(ans)

-a = [10**9 + 1] + [int(i) for i in readline().split()]

-        memo.append([minid[:],maxid[:]])

+a = [int(i) for i in readline().split()]


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

a=n//k

print((min(abs(n-k*a), abs(n-k*(a+1)))))

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

print((min(n%k, k-n%k)))

n, k = list(map(int, input().split()))
a = n // k
print((min(abs(n - k * a), abs(n - k * (a + 1)))))

n, k = list(map(int, input().split()))
print((min(n % k, k - n % k)))

-print((min(abs(n - k * a), abs(n - k * (a + 1)))))


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

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

P = pow(1 / 6, K)

now = sum([a * (a + 1) // 2 / a for a in A[:K - 1]])

ans = 0

for left, right in zip(A, A[K - 1:]):

    now += right * (right + 1) // 2 / right

    ans = max(ans, now)

    now -= left * (left + 1) // 2 / left

print(('{:.10f}'.format(ans)))

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

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

Q = [(p + 1) / 2 for p in P]

now = sum(Q[:K])

ans = now

for i in range(N - K):

    now -= Q[i]

    now += Q[i + K]

    ans = max(ans, now)

print(ans)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
P = pow(1 / 6, K)
now = sum([a * (a + 1) // 2 / a for a in A[: K - 1]])
ans = 0
for left, right in zip(A, A[K - 1 :]):
    now += right * (right + 1) // 2 / right
    ans = max(ans, now)
    now -= left * (left + 1) // 2 / left
print(("{:.10f}".format(ans)))

-    now += right * (right + 1) // 2 / right

-    now -= left * (left + 1) // 2 / left


Input:
import sys

sys.setrecursionlimit(500000)

input = sys.stdin.readline

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

G = [set() for _ in range(N)]

cnt = [0]*N

visited = [0]*N

for _ in range(N-1):

    a, b = list(map(int, input().split()))

    a -= 1

    b -= 1

    G[a].add(b)

    G[b].add(a)

for i in range(Q):

    it = list(map(int, input().split()))

    p = next(it) - 1

    x = next(it)

    cnt[p] += x

visited[0]=1

st = [0]

while st:

    v = st.pop()

    for u in G[v]:

        if visited[u]:

            continue

        visited[u] = 1

        cnt[u] += cnt[v]

        st.append(u)

print((*cnt))

def main():

  import sys

  sys.setrecursionlimit(500000)

  input = sys.stdin.readline

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

  G = [set() for _ in range(N)]

  cnt = [0]*N

  visited = [0]*N

  for _ in range(N-1):

      a, b = list(map(int, input().split()))

      a -= 1

      b -= 1

      G[a].add(b)

      G[b].add(a)

  for i in range(Q):

      node, x = list(map(int, input().split()))

      node -= 1

      cnt[node] += x

  visited[0]=1

  st = [0]

  while st:

      v = st.pop()

      for u in G[v]:

          if visited[u]:

              continue

          visited[u] = 1

          cnt[u] += cnt[v]

          st.append(u)

  print((*cnt))

if __name__ == '__main__':

    main()

import sys

sys.setrecursionlimit(500000)
input = sys.stdin.readline
N, Q = list(map(int, input().split()))
G = [set() for _ in range(N)]
cnt = [0] * N
visited = [0] * N
for _ in range(N - 1):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    G[a].add(b)
    G[b].add(a)
for i in range(Q):
    it = list(map(int, input().split()))
    p = next(it) - 1
    x = next(it)
    cnt[p] += x
visited[0] = 1
st = [0]
while st:
    v = st.pop()
    for u in G[v]:
        if visited[u]:
            continue
        visited[u] = 1
        cnt[u] += cnt[v]
        st.append(u)
print((*cnt))

def main():
    import sys

    sys.setrecursionlimit(500000)
    input = sys.stdin.readline
    N, Q = list(map(int, input().split()))
    G = [set() for _ in range(N)]
    cnt = [0] * N
    visited = [0] * N
    for _ in range(N - 1):
        a, b = list(map(int, input().split()))
        a -= 1
        b -= 1
        G[a].add(b)
        G[b].add(a)
    for i in range(Q):
        node, x = list(map(int, input().split()))
        node -= 1
        cnt[node] += x
    visited[0] = 1
    st = [0]
    while st:
        v = st.pop()
        for u in G[v]:
            if visited[u]:
                continue
            visited[u] = 1
            cnt[u] += cnt[v]
            st.append(u)
    print((*cnt))

if __name__ == "__main__":
    main()

-    a, b = list(map(int, input().split()))

-    it = list(map(int, input().split()))

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

+        a, b = list(map(int, input().split()))

+        node, x = list(map(int, input().split()))


Input:
n = int(eval(input()))

S = ''

for i in range(n):

    S += input()[0]

m = S.count('M')

a = S.count('A')

r = S.count('R')

c = S.count('C')

h = S.count('H')

print((m*a*r + m*a*c + m*a*h + m*r*c + m*r*h + m*c*h + a*r*c + a*r*h + a*c*h + r*c*h))

from collections import Counter

n = int(eval(input()))

A = [input()[0] for i in range(n)]

A = Counter(A)

m = A['M']

a = A['A']

r = A['R']

c = A['C']

h = A['H']

print((m*a*r + m*a*c + m*a*h + m*r*c + m*r*h + m*c*h + a*r*c + a*r*h + a*c*h + r*c*h))

n = int(eval(input()))
S = ""
for i in range(n):
    S += input()[0]
m = S.count("M")
a = S.count("A")
r = S.count("R")
c = S.count("C")
h = S.count("H")
print(
    (
        m * a * r
        + m * a * c
        + m * a * h
        + m * r * c
        + m * r * h
        + m * c * h
        + a * r * c
        + a * r * h
        + a * c * h
        + r * c * h
    )
)

from collections import Counter

n = int(eval(input()))
A = [input()[0] for i in range(n)]
A = Counter(A)
m = A["M"]
a = A["A"]
r = A["R"]
c = A["C"]
h = A["H"]
print(
    (
        m * a * r
        + m * a * c
        + m * a * h
        + m * r * c
        + m * r * h
        + m * c * h
        + a * r * c
        + a * r * h
        + a * c * h
        + r * c * h
    )
)

+from collections import Counter
+
-S = ""
-for i in range(n):
-    S += input()[0]
-m = S.count("M")
-a = S.count("A")
-r = S.count("R")
-c = S.count("C")
-h = S.count("H")
+A = [input()[0] for i in range(n)]
+A = Counter(A)
+m = A["M"]
+a = A["A"]
+r = A["R"]
+c = A["C"]
+h = A["H"]


Input:
from collections import Counter

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

n_lst = list(map(int,input().split()))

cnt_lst = Counter(n_lst).most_common()[k:]

ans = 0

for x in cnt_lst:

    ans += x[1]

print(ans)

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

n_lst = list(map(int,input().split()))

cnt_lst = [0 for _ in range(n+1)]

for x in n_lst:

    cnt_lst[x] += 1

cnt_lst.sort(reverse=True)

print((sum(cnt_lst[k:])))

from collections import Counter

n, k = list(map(int, input().split()))
n_lst = list(map(int, input().split()))
cnt_lst = Counter(n_lst).most_common()[k:]
ans = 0
for x in cnt_lst:
    ans += x[1]
print(ans)

n, k = list(map(int, input().split()))
n_lst = list(map(int, input().split()))
cnt_lst = [0 for _ in range(n + 1)]
for x in n_lst:
    cnt_lst[x] += 1
cnt_lst.sort(reverse=True)
print((sum(cnt_lst[k:])))


Input:
A, B, Q = list(map(int, input().split()))

INF = 10 ** 18

S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF]

T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF]

X = [int(eval(input())) for _ in range(Q)]

def binary_search(q, A):

    l, r = 0, len(A)

    while r - l > 1:

        mid = (l + r) // 2

        if q > A[mid]:

            l = mid

        else:

            r = mid

    return A[l], A[r]

def main():

    for x in X:

        s1, s2 = binary_search(x, S)

        t1, t2 = binary_search(x, T)

        print((min(abs(s-t) + min(abs(s-x), abs(t-x)) for s in [s1, s2] for t in [t1, t2])))

    return

if __name__ == '__main__':

    main()

import sys

import bisect

A, B, Q = list(map(int, sys.stdin.readline().split()))

INF = 10 ** 18

S = [-INF] + [int(sys.stdin.readline()) for _ in range(A)] + [INF]

T = [-INF] + [int(sys.stdin.readline()) for _ in range(B)] + [INF]

X = [int(sys.stdin.readline()) for _ in range(Q)]

# def binary_search(q, A):

#     l, r = 0, len(A)

#     while r - l > 1:

#         mid = (l + r) // 2

#         if q > A[mid]:

#             l = mid

#         else:

#             r = mid

#     return A[l], A[r]

def main():

    for x in X:

        ids, idt = bisect.bisect_right(S, x), bisect.bisect_right(T, x)

        print((min(abs(s-t) + min(abs(s-x), abs(t-x)) for s in S[ids-1:ids+1] for t in T[idt-1:idt+1])))

    return

if __name__ == '__main__':

    main()

A, B, Q = list(map(int, input().split()))
INF = 10**18
S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF]
T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF]
X = [int(eval(input())) for _ in range(Q)]

def binary_search(q, A):
    l, r = 0, len(A)
    while r - l > 1:
        mid = (l + r) // 2
        if q > A[mid]:
            l = mid
        else:
            r = mid
    return A[l], A[r]

def main():
    for x in X:
        s1, s2 = binary_search(x, S)
        t1, t2 = binary_search(x, T)
        print(
            (
                min(
                    abs(s - t) + min(abs(s - x), abs(t - x))
                    for s in [s1, s2]
                    for t in [t1, t2]
                )
            )
        )
    return

if __name__ == "__main__":
    main()

import sys
import bisect

A, B, Q = list(map(int, sys.stdin.readline().split()))
INF = 10**18
S = [-INF] + [int(sys.stdin.readline()) for _ in range(A)] + [INF]
T = [-INF] + [int(sys.stdin.readline()) for _ in range(B)] + [INF]
X = [int(sys.stdin.readline()) for _ in range(Q)]
# def binary_search(q, A):
#     l, r = 0, len(A)
#     while r - l > 1:
#         mid = (l + r) // 2
#         if q > A[mid]:
#             l = mid
#         else:
#             r = mid
#     return A[l], A[r]
def main():
    for x in X:
        ids, idt = bisect.bisect_right(S, x), bisect.bisect_right(T, x)
        print(
            (
                min(
                    abs(s - t) + min(abs(s - x), abs(t - x))
                    for s in S[ids - 1 : ids + 1]
                    for t in T[idt - 1 : idt + 1]
                )
            )
        )
    return

if __name__ == "__main__":
    main()

-A, B, Q = list(map(int, input().split()))

+A, B, Q = list(map(int, sys.stdin.readline().split()))

-S = [-INF] + [int(eval(input())) for _ in range(A)] + [INF]

-T = [-INF] + [int(eval(input())) for _ in range(B)] + [INF]

-X = [int(eval(input())) for _ in range(Q)]

+S = [-INF] + [int(sys.stdin.readline()) for _ in range(A)] + [INF]

+T = [-INF] + [int(sys.stdin.readline()) for _ in range(B)] + [INF]

+X = [int(sys.stdin.readline()) for _ in range(Q)]

+        ids, idt = bisect.bisect_right(S, x), bisect.bisect_right(T, x)

+                    for s in S[ids - 1 : ids + 1]

+                    for t in T[idt - 1 : idt + 1]


Input:
n=int(eval(input()))

l=list(eval(input()))

l1=[l[0]]

for i in range(1,n):

  if(l[i]!=l1[len(l1)-1]):

    l1.append(l[i])

print((len(l1)))

n=int(eval(input()))

s=eval(input())

c=1

for i in range(1,n):

  if(s[i]!=s[i-1]):

    c+=1

print(c)

n = int(eval(input()))
l = list(eval(input()))
l1 = [l[0]]
for i in range(1, n):
    if l[i] != l1[len(l1) - 1]:
        l1.append(l[i])
print((len(l1)))

n = int(eval(input()))
s = eval(input())
c = 1
for i in range(1, n):
    if s[i] != s[i - 1]:
        c += 1
print(c)


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

pnt = {}

ac = {}

for i in range(M):

	p,s = input().split()

	p = int(p)

	if not p in pnt:

		pnt[p] = 0

		ac[p] = 0

	if s == "WA" and ac[p] == 0:

		pnt[p] += 1

	elif s == "AC" and ac[p] == 0:

		ac[p] += 1

for i in pnt:

	if pnt[i] != 0 and ac[i] == 0:

		pnt[i] = 0

print((sum(ac.values()),sum(pnt.values())))

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

ac = 0

wa = 0

acs = [0]*(N+1)

was = [0]*(N+1)

for i in range(M):

	p,s = input().split()

	p = int(p)

	if s == "AC":

		if acs[p]==0:

			ac += 1

			wa += was[p]

			acs[p] = 1

	else:

		was[p] += 1

print((ac,wa))

N, M = list(map(int, input().split()))
pnt = {}
ac = {}
for i in range(M):
    p, s = input().split()
    p = int(p)
    if not p in pnt:
        pnt[p] = 0
        ac[p] = 0
    if s == "WA" and ac[p] == 0:
        pnt[p] += 1
    elif s == "AC" and ac[p] == 0:
        ac[p] += 1
for i in pnt:
    if pnt[i] != 0 and ac[i] == 0:
        pnt[i] = 0
print((sum(ac.values()), sum(pnt.values())))

N, M = list(map(int, input().split()))
ac = 0
wa = 0
acs = [0] * (N + 1)
was = [0] * (N + 1)
for i in range(M):
    p, s = input().split()
    p = int(p)
    if s == "AC":
        if acs[p] == 0:
            ac += 1
            wa += was[p]
            acs[p] = 1
    else:
        was[p] += 1
print((ac, wa))


Input:
N, Ma, Mb = list(map(int, input().split()))

INF = 10 ** 8

lst = [[INF] * 401 for i in range(401)]

lst[0][0] = 0

for _ in range(N):

    a, b, c = list(map(int, input().split()))

    for i in range(400, a - 1, -1):

        for j in range(400, b - 1, -1):

            if lst[i- a][j - b] == INF:

                pass

            else:

                lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)

ans = INF

n = 400 // max(Ma, Mb)

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

    ans = min(ans, lst[Ma * i][Mb * i])

if ans == INF:

    print((-1))

else:

    print (ans)

# for i in range(7):

#     print (lst[i][:7])

#masoyさんのコード

import numpy as np

N, Ma, Mb = list(map(int, input().split()))

U = N * 10

dp = np.zeros((U + 1, U + 1), dtype = np.int64)

temp = np.zeros_like(dp)

INF = 10 ** 18

dp += INF

dp[0, 0] = 0

for _ in range(N):

    a, b, c = list(map(int, input().split()))

    temp[:] = dp.copy()

    temp[a:, b:] = np.minimum(temp[a:, b:], dp[:-a, :-b] + c)

    dp = temp #毎回、一つ前の結果を残す

answer = INF

for t in range(1, 401):

    a = Ma * t

    b = Mb * t

    if max(a, b) >= U: #到達できない量に達したら終了

        break

    answer = min(answer, dp[a, b])

if answer == INF:

    answer = -1

print (answer)

N, Ma, Mb = list(map(int, input().split()))
INF = 10**8
lst = [[INF] * 401 for i in range(401)]
lst[0][0] = 0
for _ in range(N):
    a, b, c = list(map(int, input().split()))
    for i in range(400, a - 1, -1):
        for j in range(400, b - 1, -1):
            if lst[i - a][j - b] == INF:
                pass
            else:
                lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)
ans = INF
n = 400 // max(Ma, Mb)
for i in range(1, n + 1):
    ans = min(ans, lst[Ma * i][Mb * i])
if ans == INF:
    print((-1))
else:
    print(ans)
# for i in range(7):
#     print (lst[i][:7])

# masoyさんのコード
import numpy as np

N, Ma, Mb = list(map(int, input().split()))
U = N * 10
dp = np.zeros((U + 1, U + 1), dtype=np.int64)
temp = np.zeros_like(dp)
INF = 10**18
dp += INF
dp[0, 0] = 0
for _ in range(N):
    a, b, c = list(map(int, input().split()))
    temp[:] = dp.copy()
    temp[a:, b:] = np.minimum(temp[a:, b:], dp[:-a, :-b] + c)
    dp = temp  # 毎回、一つ前の結果を残す
answer = INF
for t in range(1, 401):
    a = Ma * t
    b = Mb * t
    if max(a, b) >= U:  # 到達できない量に達したら終了
        break
    answer = min(answer, dp[a, b])
if answer == INF:
    answer = -1
print(answer)

-lst = [[INF] * 401 for i in range(401)]

+dp = np.zeros((U + 1, U + 1), dtype=np.int64)

-            if lst[i - a][j - b] == INF:

-                lst[i][j] = min(lst[i][j], lst[i - a][j - b] + c)

-    ans = min(ans, lst[Ma * i][Mb * i])

+    temp[a:, b:] = np.minimum(temp[a:, b:], dp[:-a, :-b] + c)

+    dp = temp  # 毎回、一つ前の結果を残す

+    if max(a, b) >= U:  # 到達できない量に達したら終了


Input:
import re

s = eval(input())

for _ in range(s.count('B')):

    s = re.sub('.?B', '', s, 1)

print(s)

s=eval(input())

while'B'in s:

    s=s[s[0]=='B':].replace('0B','').replace('1B','')

print(s)

import re

s = eval(input())
for _ in range(s.count("B")):
    s = re.sub(".?B", "", s, 1)
print(s)

s = eval(input())
while "B" in s:
    s = s[s[0] == "B" :].replace("0B", "").replace("1B", "")
print(s)


Input:
N,Q = list(map(int,input().split()))

BIT = [0]*(N+1)

def BIT_query(idx):

    res_sum = 0

    while idx > 0:

        res_sum += BIT[idx]

        idx -= idx&(-idx)

    return res_sum

def BIT_update(idx,x):

    while idx <= N:

        BIT[idx] += x

        idx += idx&(-idx)

    return

a = list(map(int,input().split()))

for i in range(N):

    BIT_update(i+1,a[i])

for i in range(Q):

    a,b,c = list(map(int,input().split()))

    if a == 0:

        BIT_update(b+1,c)

    else:

        print((BIT_query(c)-BIT_query(b)))

import sys

input = sys.stdin.buffer.readline

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

BIT = [0]*(N+1)

def BIT_query(idx):

    res_sum = 0

    while idx > 0:

        res_sum += BIT[idx]

        idx -= idx&(-idx)

    return res_sum

def BIT_update(idx,x):

    while idx <= N:

        BIT[idx] += x

        idx += idx&(-idx)

    return

a = list(map(int,input().split()))

for i in range(N):

    BIT_update(i+1,a[i])

for i in range(Q):

    a,b,c = list(map(int,input().split()))

    if a == 0:

        BIT_update(b+1,c)

    else:

        print((BIT_query(c)-BIT_query(b)))

import sys

input = sys.stdin.buffer.readline
N, Q = list(map(int, input().split()))
BIT = [0] * (N + 1)

def BIT_query(idx):
    res_sum = 0
    while idx > 0:
        res_sum += BIT[idx]
        idx -= idx & (-idx)
    return res_sum

def BIT_update(idx, x):
    while idx <= N:
        BIT[idx] += x
        idx += idx & (-idx)
    return

a = list(map(int, input().split()))
for i in range(N):
    BIT_update(i + 1, a[i])
for i in range(Q):
    a, b, c = list(map(int, input().split()))
    if a == 0:
        BIT_update(b + 1, c)
    else:
        print((BIT_query(c) - BIT_query(b)))

+import sys
+
+input = sys.stdin.buffer.readline


Input:
import itertools

import math

N = int(eval(input()))

S_list = []

for i in range(N):

    S_list.append(eval(input()))

count_M = 0

count_A = 0

count_R = 0

count_C = 0

count_H = 0

for l in S_list:

    if l.startswith('M'):

        count_M += 1

    elif l.startswith('A'):

        count_A += 1

    elif l.startswith('R'):

        count_R += 1

    elif l.startswith('C'):

        count_C += 1

    elif l.startswith('H'):

        count_H += 1

count = [count_M,count_A,count_R,count_C,count_H]

c = list(itertools.combinations(count,3))

x = 0

for i in c:

    i_list = list(i)

    x += i_list[0]*i_list[1]*i_list[2]

print(x)

N = int(eval(input()))

S = [eval(input()) for _ in range(N)]

res = [0] * 5

ini = 'MARCH'

for i in range(N):

    for j in range(5):

        if S[i][0] == ini[j]:

            res[j] += 1

ans = 0

for i in range(5):

    for j in range(i+1,5):

        for k in range(j+1,5):

            ans += res[i]*res[j]*res[k]

print(ans)

import itertools
import math

N = int(eval(input()))
S_list = []
for i in range(N):
    S_list.append(eval(input()))
count_M = 0
count_A = 0
count_R = 0
count_C = 0
count_H = 0
for l in S_list:
    if l.startswith("M"):
        count_M += 1
    elif l.startswith("A"):
        count_A += 1
    elif l.startswith("R"):
        count_R += 1
    elif l.startswith("C"):
        count_C += 1
    elif l.startswith("H"):
        count_H += 1
count = [count_M, count_A, count_R, count_C, count_H]
c = list(itertools.combinations(count, 3))
x = 0
for i in c:
    i_list = list(i)
    x += i_list[0] * i_list[1] * i_list[2]
print(x)

N = int(eval(input()))
S = [eval(input()) for _ in range(N)]
res = [0] * 5
ini = "MARCH"
for i in range(N):
    for j in range(5):
        if S[i][0] == ini[j]:
            res[j] += 1
ans = 0
for i in range(5):
    for j in range(i + 1, 5):
        for k in range(j + 1, 5):
            ans += res[i] * res[j] * res[k]
print(ans)

-count = [count_M, count_A, count_R, count_C, count_H]

-c = list(itertools.combinations(count, 3))

+            ans += res[i] * res[j] * res[k]


Input:
#Convolution_998244353

MOD = 998244353

ROOT = 3

sum_e = (911660635, 509520358, 369330050, 332049552, 983190778, 123842337, 238493703, 975955924, 603855026, 856644456, 131300601, 842657263, 730768835, 942482514, 806263778, 151565301, 510815449, 503497456, 743006876, 741047443, 56250497, 0, 0, 0, 0, 0, 0, 0, 0, 0)

sum_ie = (86583718, 372528824, 373294451, 645684063, 112220581, 692852209, 155456985, 797128860, 90816748, 860285882, 927414960, 354738543, 109331171, 293255632, 535113200, 308540755, 121186627, 608385704, 438932459, 359477183, 824071951, 0, 0, 0, 0, 0, 0, 0, 0, 0)

def butterfly(arr):

    n = len(arr)

    h = (n - 1).bit_length()

    for ph in range(1, h + 1):

        w = 1 << (ph - 1)

        p = 1 << (h - ph)

        now = 1

        for s in range(w):

            offset = s << (h - ph + 1)

            for i in range(p):

                l = arr[i + offset]

                r = arr[i + offset + p] * now

                arr[i + offset] = (l + r) % MOD

                arr[i + offset + p] = (l - r) % MOD

            now *= sum_e[(~s & -~s).bit_length() - 1]

            now %= MOD

def butterfly_inv(arr):

    n = len(arr)

    h = (n - 1).bit_length()

    for ph in range(1, h + 1)[::-1]:

        w = 1 << (ph - 1)

        p = 1 << (h - ph)

        inow = 1

        for s in range(w):

            offset = s << (h - ph + 1)

            for i in range(p):

                l = arr[i + offset]

                r = arr[i + offset + p]

                arr[i + offset] = (l + r) % MOD

                arr[i + offset + p] = (MOD + l - r) * inow % MOD

            inow *= sum_ie[(~s & -~s).bit_length() - 1]

            inow %= MOD

def convolution(a, b):

    n = len(a)

    m = len(b)

    if not n or not m: return []

    if min(n, m) <= 50:

        if n < m:

            n, m = m, n

            a, b = b, a

        res = [0] * (n + m - 1)

        for i in range(n):

            for j in range(m):

                res[i + j] += a[i] * b[j]

                res[i + j] %= MOD

        return res

    z = 1 << (n + m - 2).bit_length()

    a += [0] * (z - n)

    b += [0] * (z - m)

    butterfly(a)

    butterfly(b)

    for i in range(z):

        a[i] *= b[i]

        a[i] %= MOD

    butterfly_inv(a)

    a = a[:n + m - 1]

    iz = pow(z, MOD - 2, MOD)

    for i in range(n + m - 1):

        a[i] *= iz

        a[i] %= MOD

    return a

def autocorrelation(a):

    n = len(a)

    if not n: return []

    if n <= 50:

        res = [0] * (2 * n - 1)

        for i in range(n):

            for j in range(n):

                res[i + j] += a[i] * a[j]

                res[i + j] %= MOD

        return res

    z = 1 << (2 * n - 2).bit_length()

    a += [0] * (z - n)

    butterfly(a)

    for i in range(a):

        a[i] *= a[i]

        a[i] %= MOD

    butterfly_inv(a)

    a = a[:2 * n - 1]

    iz = pow(z, MOD - 2, MOD)

    for i in range(2 * n - 1):

        a[i] *= iz

        a[i] %= MOD

    return a

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

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

B = [int(x) for x in input().split()]

print((*convolution(A, B)))

+import sys
+
+input = sys.stdin.buffer.readline


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

H = list(map(int,input().split()))

INF = 10 ** 9 + 1

dp = [INF] * N

dp[0] = 0

for i in range(1, N):

  dp[i] = min(pre + abs(h - H[i]) for pre,h in zip(dp[max(0,i - K):i],H[max(0,i - K):i]))

print((dp[-1]))

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

import numpy as np

H = np.array(input().split(), dtype = int)

INF = 10 ** 9 + 1

dp = np.full(N,INF,dtype = int)

dp[0] = 0

for i in range(1, N):

  dp[i] = min(dp[max(i - K,0):i] + abs(H[max(i - K,0):i] - H[i]))

print((dp[-1]))

N, K = list(map(int, input().split()))
H = list(map(int, input().split()))
INF = 10**9 + 1
dp = [INF] * N
dp[0] = 0
for i in range(1, N):
    dp[i] = min(
        pre + abs(h - H[i])
        for pre, h in zip(dp[max(0, i - K) : i], H[max(0, i - K) : i])
    )
print((dp[-1]))

N, K = list(map(int, input().split()))
import numpy as np

H = np.array(input().split(), dtype=int)
INF = 10**9 + 1
dp = np.full(N, INF, dtype=int)
dp[0] = 0
for i in range(1, N):
    dp[i] = min(dp[max(i - K, 0) : i] + abs(H[max(i - K, 0) : i] - H[i]))
print((dp[-1]))

+H = np.array(input().split(), dtype=int)

-        for pre, h in zip(dp[max(0, i - K) : i], H[max(0, i - K) : i])