Harryxun/stf_run huggingface dataset:
N,M = map(int, input().split())
if M == 0:
    print(-1)
    exit()
pqcs = [list(map(int, input().split())) for _ in range(M)]
c_max = max(list(map(lambda x: x[2],pqcs)))
node_num = (c_max+1)*N
unsearached_nodes = list(range(node_num))#0～N-1(n-1)が乗換経由ノード/路線iの駅nは3*i+(n-1)番ノード
#opt_nodes = [-1]*node_num
distance = [float('inf')]*node_num
distance[0] = 0
cost = 0
edges = [[float('inf')]*node_num for _ in range(node_num)]
#辺へ重み付け
for p,q,c in pqcs:
    edges[3*c+(p-1)][3*c+(q-1)] = 0
    edges[3*c+(q-1)][3*c+(p-1)] = 0
    edges[p-1][3*c+(p-1)] = 1
    edges[q-1][3*c+(q-1)] = 1
    edges[3*c+(p-1)][p-1] = 0
    edges[3*c+(q-1)][q-1] = 0

while (distance[N-1] == float('inf')):
    temp_min_dist = float('inf')
    for unsearched_ind in unsearached_nodes:
        if temp_min_dist > distance[unsearched_ind]:
            temp_min_dist = distance[unsearched_ind]
            target_min_ind = unsearched_ind
    unsearached_nodes.remove(target_min_ind)
    target_node = edges[target_min_ind]
    flag = False
    for index, rota_dist in enumerate(target_node):
        if rota_dist != float('inf'):
            if distance[index] > distance[target_min_ind] + rota_dist:
                distance[index] = distance[target_min_ind] + rota_dist
                #opt_nodes[index] = target_min_ind

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

### FIXED CODE ###

from collections import deque
N, M = map(int, input().split())
F_C_of_S = [{} for i in range(N)]
#Companies of Station
C_of_S = {}

#def Union-find
parent = [-1]*M
def root(x):
	if (parent[x] < 0): return x
	else:
		parent[x] = y = root(parent[x])
		return y

def unite(x, y):
	px = root(x)
	py = root(y)
	if (px == py): return False
	if (parent[px] > parent[py]):
		px = root(y)
		py = root(x)
	parent[px] += parent[py]
	parent[py] = px
	return True


#Union-find
pqcs = []
for i in range(M):
	p, q, c = map(int, input().split())
	p -= 1; q -= 1;
	pqcs += [(p, q, c)]
	
	#The first
	if not c in F_C_of_S[p]: F_C_of_S[p][c] = i
	#else
	else: unite(F_C_of_S[p][c], i)
	C_of_S.setdefault(p, set()).add(c)
	
	#The first
	if not (c in F_C_of_S[q]):F_C_of_S[q][c] = i
	#else
	else: unite(F_C_of_S[q][c], i)
	C_of_S.setdefault(q, set()).add(c)

#stations of company
S_of_C = {}
for i in range(M):
	p, q, c = pqcs[i]
	c_set = S_of_C.setdefault(root(i), set())
	c_set.add(p)
	c_set.add(q)


Q = deque([(0, 0, 0)])
#cost to go to the stations from station_0
dist = [float('inf')]*N
dist[0] = 0

gdist = [float('inf')]*M

while Q:
	cost, v, flag = Q.popleft()
	
	if not (flag):#(flag == 0) then gdist_update
		if (dist[v] < cost) or (v not in C_of_S):
			continue
		for l in F_C_of_S[v].values():
			l = root(l)
			if (cost < gdist[l]):
				gdist[l] = cost
				Q.appendleft((cost, l, 1))
	
	else:#(flag == 1) then dist_update
		if (gdist[v] < cost) or (v not in S_of_C):
			continue
		for s in S_of_C[v]:
			if (cost+1 < dist[s]):
				dist[s] = cost+1
				Q.append((cost+1, s, 0))

print(dist[N - 1] if dist[N - 1] < float('inf') else -1)
N = int(input())
A = [int(i) for i in input().split()]

maxdig = 60
mod = 10**9+7

ans = 0
for i in range(60+1):
  cnt1 = 0
  for a in A:
    if (a >> i) & 1:
      cnt1 += 1
  ans += (cnt1*(N-cnt1)*(1<<j))
  ans = ans%mod

print(ans)

### FIXED CODE ###

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

maxdig = 60
mod = 10**9+7

ans = 0
for i in range(60+1):
  cnt1 = sum([1 for a in A if (a >> i) & 1])
  ans += (cnt1*(N-cnt1)*(1<<i))
  ans = ans%mod

print(ans)
n,k=map(int,input().split())
a=list(map(int,input().split()))
dic={}
wk=1
start=0
for i in range(1,n+1):
  wk=a[wk-1]
  if wk in dic:
    start=dic[wk]
    break
  dic[wk]=i
end=len(dic)
loopcnt=end-start+1
keys=list(dic.keys())
if k<start:
  print(keys[k-1])
else:
  chk=(k-start+1)%loopcntac
  if chk==0:
    chk=loopcnt
  print(keys[(start-1)+chk-1])


### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
dic={}
wk=1
start=0
for i in range(1,n+1):
  wk=a[wk-1]
  if wk in dic:
    start=dic[wk]
    break
  dic[wk]=i
end=len(dic)
loopcnt=end-start+1
keys=list(dic.keys())
if k<start:
  print(keys[k-1])
else:
  chk=(k-start+1)%loopcnt
  if chk==0:
    chk=loopcnt
  print(keys[(start-1)+chk-1])
from sys import stdin
from functools import lru_cache


# 最小化問題
def chmin(a,b):
    if a > b: return b
    else: return a


@lru_cache(maxsize=10000)
def rec(i):
    if i==0:
        return 0
    else:
        res = float('inf')
        if i>0:
            res = chmin(res,rec(i-1)+abs(h_arr[i]-h_arr[i-1]))
        if i>1:
            res = chmin(res,rec(i-2)+abs(h_arr[i]-h_arr[i-2]))
        return res


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    h_arr = [0] * (N+2)
    h_arr[0:N] = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    # dp[i]:=カエルが足場iへと移動するのに必要な最小コスト
    dp = [float('inf')] * (N+2)  # 最小化問題
    dp[0] = 0                    # 初期条件
    
    # 貰うDP
    #for i in range(0,N):
    #    dp[i] = chmin(dp[i],dp[i-1]+abs(h_arr[i]-h_arr[i-1]))
    #    if i > 1:
    #        dp[i] = chmin(dp[i],dp[i-2]+abs(h_arr[i]-h_arr[i-2]))
    #cnt = dp[N-1]

    # 配るDP
    #for i in range(0,N):
    #    dp[i+1] = chmin(dp[i+1],dp[i]+abs(h_arr[i]-h_arr[i+1]))
    #    dp[i+2] = chmin(dp[i+2],dp[i]+abs(h_arr[i]-h_arr[i+2]))
    #cnt = dp[N-1]

    # メモ化再帰
    cnt = rec(N-1)
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)


### FIXED CODE ###

from sys import stdin


# 最小化問題
def chmin(a,b):
    if a > b: return b
    else: return a


if __name__ == "__main__":
    _in = [_.rstrip() for _ in stdin.readlines()]
    N = int(_in[0])  # type:int
    h_arr = list(map(int,_in[1].split(' ')))  # type:list(int)
    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    dp = [float('inf')] * N  # 最小化問題
    dp[0] = 0                # 初期条件
    
    for i in range(0,N):
        dp[i] = chmin(dp[i],dp[i-1]+abs(h_arr[i]-h_arr[i-1]))
        if i > 1:
            dp[i] = chmin(dp[i],dp[i-2]+abs(h_arr[i]-h_arr[i-2]))
    cnt = dp[N-1]
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    print(cnt)
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from collections import defaultdict

def main():
    N = II()
    xy = []
    for _ in range(N):
        xy.append(LI())
    x2g = defaultdict(int)
    y2g = defaultdict(int)
    g2y = defaultdict(set)
    g2x = defaultdict(set)
    for x, y in xy:
        gx = x2g[x]
        gy = y2g[y]
        if gx != 0 and gy != 0:  # merge
            if len(g2x[gx]) + len(g2y[gx]) < len(g2x[gy]) + len(g2x[gy]):
                gx, gy = gy, gx
            for x in g2x[gy]:
                x2g[x] = gx
            for y in g2y[gy]:
                y2g[y] = gx
            g2y[gx] += g2y[gy]
            g2x[gx] += g2x[gy]
            g2y[gy] = set()
            g2x[gy] = set()
        else:
            g = x2g[x] or y2g[y] or x
            y2g[y] = g
            x2g[x] = g
            g2x[g].add(x)
            g2y[g].add(y)
    ans = -N
    for g in g2x.keys():
        ans += len(g2x[g]) * len(g2y[g])
    return ans

print(main())

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from collections import defaultdict

def main():
    N = II()
    xy = []
    for _ in range(N):
        xy.append(LI())
    xy2g = defaultdict(int)
    g2xy = defaultdict(set)
    for x, y in xy:
        y = -y
        gx = xy2g[x]
        gy = xy2g[y]
        if gx != 0 and gy != 0:  # merge
            if gx == gy:
                continue
            if len(g2xy[gx]) < len(g2xy[gy]):
                gx, gy = gy, gx
            for xy in g2xy[gy]:
                xy2g[xy] = gx
            g2xy[gx] |= g2xy[gy]
            g2xy[gy].clear()
        else:
            g = gx or gy or x
            xy2g[x] = xy2g[y] = g
            g2xy[g] |= {x, y}
    ans = -N
    for xys in g2xy.values():
        m, p = 0, 0
        for xy in xys:
            if xy < 0:
                m += 1
            else:
                p += 1
        ans += m * p
    return ans

print(main())
import math

def overlap(p1, p2, d):
    return (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2 <= d

def intersection(o1,o2):
    a  = 2*(o2[0] - o1[0])
    b  = 2*(o2[1] - o1[1])
    c  = (o1[0] - o2[0])*(o1[0] + o2[0]) + (o1[1] - o2[1])*(o1[1] + o2[1])
    
    a2 = a**2 + b**2
    b2 = a*c + a*b*o1[1] - b**2*o1[0]
    c2 = b**2*(o1[0]**2 + o1[1]**2 - 1) + c**2 + 2*b*c*o1[1]
    x1 = (-b2 + math.sqrt(b2**2 - a2*c2))/a2
    x2 = (-b2 - math.sqrt(b2**2 - a2*c2))/a2
    if b == 0:
        y1 = (o1[1] + o2[1])/2
        y2 = (o1[1] + o2[1])/2
    else:
        y1 = -(a*x1 + c)/b
        y2 = -(a*x2 + c)/b
    if b2**2 - a2*c2 < 10e-6:
        return [x1, y1], [None, None]
    else:
        return [x1, y1], [x2, y2]

while True:
    n = int(input())
    if n == 0:
        break
    p = []
    for i in range(n):
        p.append(list(map(float, input().split(","))))
    ans = 0
    for i in range(n - 1):
        for j in range(i + 1, n):
            if not overlap(p[i], p[j], 4):
                continue
            p1, p2 = intersection(p[i], p[j])
            cnt1 = 0
            cnt2 = 0
            for k in range(n):
                if overlap(p1, p[k], 1):
                    cnt1 += 1
                if p2[0] != None and overlap(p2, p[k], 1):
                    cnt2 += 1
            ans = max([ans, cnt1, cnt2])
    print(ans)



### FIXED CODE ###

import math

def dist(p1, p2):
    return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)

def intersection(o1,o2):
    if dist(o1,o2) > 2:
        p1, p2 =  [None, None], [None, None]
    elif dist(o1,o2) == 2:
        p1, p2 =  [(o1[0] + o2[0])/2, (o1[1] + o2[1])/2], [None, None]
    else:
        m = [(o1[0] + o2[0])/2, (o1[1] + o2[1])/2]
        v = [m[0] - o1[0], m[1] - o1[1]]
        v_abs = dist(v,[0,0])
        w = [v[1], -v[0]]
        w_abs = dist(w,[0,0])
        n = [w[0]/w_abs, w[1]/w_abs]
        l = math.sqrt(1 - v_abs**2)
        p1 = [m[0] + l*n[0], m[1] + l*n[1]]
        p2 = [m[0] - l*n[0], m[1] - l*n[1]]
    return p1, p2
err = 1.0e-6
while True:
    n = int(input())
    if n == 0:
        break
    p = []
    for i in range(n):
        p.append(list(map(float, input().split(","))))
    ans = 1
    for i in range(n - 1):
        for j in range(i + 1, n):
            p1, p2 = intersection(p[i], p[j])
            cnt1 = 0
            cnt2 = 0
            for k in range(n):
                if p1[0] and dist(p1, p[k]) <= 1 + err:
                    cnt1 += 1
                if p2[0] and dist(p2, p[k]) <= 1 + err:
                    cnt2 += 1
            ans = max([ans, cnt1, cnt2])
    print(ans)
n=int(input())
a=list(map(int,input().split()))
val=max(a)
inx=a.index(val)

for i in range(0,n):
  if i==inx:
    del a[inx]
    print(max(a))
  else:
    print(val)
  


### FIXED CODE ###

n=int(input())
a=[]

for j in range(0,n):
  a.append(int(input()))
  
x=max(a)
y=a.index(x)
del a[y]
z=max(a)

for i in range(0,n):
  if i!=y:
    print(x)
  else:
    print(z)
# -*- coding: utf-8 -*-
# 整数の入力
a = int(input())
# スペース区切りの整数の入力
b, c = map(int, input().split())
# 文字列の入力
s = input()
# 出力
print("{} {}".format(a+b+c, s))

### FIXED CODE ###

a = input()

if a.isupper():
    print('A')
else:
    print('a')
N,M = map(int, input().split())
A = list(map(int,input().split()))
A.sort()
max = 0 

for i in range(M):
    B,C = map(int, input().split())
    for j in range(B) :
        if  C >= A[j] : 
            A[j] = C
        else :
            continue
    A.sort()
sum = 0 
for i in range(N):
    sum += A[i] 

print(sum)

### FIXED CODE ###

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

if A >= 13 : 
    print(B)
elif A >= 6 and A <= 12 : 
    print(B//2)
else :
    print("0")
a,b=map(int,input(),split())
print(-(-a/b))

### FIXED CODE ###

a,b=map(int,input().split())
print(-(-a//b))
A = int(input())
B = int(input())
C = int(input())

# A = 5
# B = 7
# C = 5
arr = [A, B, C]
fives = 0
sevens = 0

for num in arr:
    if num == 5:
        fives += 1
    if num == 7:
        sevens += 1

if fives == 2 and sevens == 1:
    print("YES")
else:
    print("NO")

### FIXED CODE ###

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

arr = [A, B, C]
fives = 0
sevens = 0

for num in arr:
    if num == 5:
        fives += 1
    if num == 7:
        sevens += 1

if fives == 2 and sevens == 1:
    print("YES")
else:
    print("NO")
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

k,q=map(int,readline().split())
D=list(map(int,readline().split()))
NXM = map(int, read().split())

NXM=iter(NXM)
NXM=zip(NXM,NXM,NXM)

for n,x,m in NXM:
    tmpD=D%m
    SD=np.array([0]+tmpD,dtype=np.int64)
    SD=np.cumsum(SD)
    n-=1
    x=x%m
    x+=SD[-1]*(n//k)
    x+=SD[n%k]
    count=tmpD.count(0)*(n//k)
    count+=tmpD[:n%k].count(0)
    ans=n-x//m-count
    print(ans)

### FIXED CODE ###

import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

k,q=map(int,readline().split())
D=np.array(readline().split(),dtype=np.int64)
NXM = map(int, read().split())

NXM=iter(NXM)
NXM=zip(NXM,NXM,NXM)

for n,x,m in NXM:
    tmpD=D%m
    SD=np.hstack((np.zeros(1,dtype=np.int64), tmpD))#結合
    SD=np.cumsum(SD)
    n-=1
    x=x%m
    x+=SD[-1]*(n//k)
    x+=SD[n%k]
    count=(k-np.count_nonzero(tmpD))*(n//k)
    count+=n%k-np.count_nonzero(tmpD[:n%k])
    ans=n-x//m-count
    print(ans)
print((int(input)+1)//2)

### FIXED CODE ###

print((int(input())+1)//2)
import bisect 
n,m = map(int,input().split())
a = sorted(list(map(int,input().split())))
 
for i in range(m):
  b = a[-1]
  a.remove(b)
  b = int(b/2)
  for j in range(99*99):
    if b >= a[-1]:
      b = int(b/2)
    else:
      break
  bisect.insort(a,b)
  
print(sum(a))

### FIXED CODE ###

import heapq

n,m = map(int,input().split())
a = list(map(int,input().split()))
a = list(map(lambda x: x*(-1), a))  # 各要素を-1倍することで，最小最大を逆転

heapq.heapify(a)#優先度付きキューに変換

for i in range(m):
  tmp = heapq.heappop(a)
  heapq.heappush(a,(-tmp//2)*(-1))#負の剰余では切り捨て方向が異なってしまうので，いったんプラスにする．
  
print(-sum(a))
X = int(input())

b = 0
while 1:
    a = (X + b ** 5) ** (1/5)
    if a.is_integer() == True:
        break
    b = -b
    a = (X + b ** 5) ** (1/5)
    if a.is_integer() == True:
        break
    b = -b
    b += 1
print(int(a), b)

### FIXED CODE ###

import sys

X = int(input())
for i in range(-118, 120):
    for j in range(-119, 119):
        if i ** 5 - j ** 5 == X:
            print(i, j)
            sys.exit(0)
N,K,M = input().split()
N = int(N)
K = int(K)
M = int(M)

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

AN = M * N - sum(A)

if (AN <= K) and (AN >= 0):
	print (AN)
elif AN < 0:
	print (0)
else:
	print (-1)

### FIXED CODE ###

a = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
C = input()
if (C != 'z') and (C in a):
	i = a.index(C)
	print (a[i+1])
#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod=10**9+7

h,w=MI()
lis=[LI() for i in range(h)]
pos=[0 for i in range(h*w)]
nums=[0 for i in range(h*w)]
count=0
for i in range(h):
    for j in range(w):
        if i%2==0:
            k=j
        else:
            k=w-j-1
        nums[count]=lis[i][k]
        pos[count]=[i+1,k+1]
        count+=1
#print(nums)
#print(pos)
c=0
for i in range(h*w):
    if nums[i]%2==1:
        c+=1
u=[]
for i in range(h*w-1):
    if nums[i]%2==1:
        nums[i+1]+=1
        u.append([pos[i],pos[i+1]])
        #print(*pos[i],*pos[i+1])
print(len(u))
for i in range(len(u)):
    print(*u[i][0],*u[i][1])

### FIXED CODE ###

#dpでできないかな？
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,sqrt
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod=10**9+7

h,w=MI()
lis=[LI() for i in range(h)]
pos=[0 for i in range(h*w)]
nums=[0 for i in range(h*w)]
count=0
for i in range(h):
    for j in range(w):
        if i%2==0:
            k=j
        else:
            k=w-j-1
        nums[count]=lis[i][k]
        pos[count]=[i+1,k+1]
        count+=1
#print(nums)
#print(pos)
c=0
for i in range(h*w):
    if nums[i]%2==1:
        c+=1
u=[]
for i in range(h*w-1):
    if nums[i]%2==1:
        nums[i+1]+=1
        u.append([pos[i],pos[i+1]])
        #print(*pos[i],*pos[i+1])
print(len(u))
for i in range(len(u)):
    print(*u[i][0],end=" ")
    print(*u[i][1])
import sys
N = map(int, input().split())
N = N[0]
for i in range(int(100/7)+1):
	for j in range(int(100/4)+1):
		a = i*7 + j*4
		if int(N) == a:
			print('Yes')
			sys.exit();

print('No')


### FIXED CODE ###

N = int(input())
res = 'No'
for i in range(int(100/7)+1):
	for j in range(int(100/4)+1):
		a = i*7 + j*4
		if int(N) == a:
			res='Yes'
print(res)
#include<iostream>
#define ll long long
using namespace std;
ll n,a,b;
int main(){
    cin>>n>>a>>b;
    cout<<(b*(n-2)-a*(n-2)+1);
    return 0;
}

### FIXED CODE ###

n,anb,b=map(int,input().split()) 
if n>1 and b>=anb or n==1 and anb==b: 
    print ((n-2)*(b-anb)+1); 
else:
    print ("0");
A, B = map(int,input().split())
import math
g = math.gcd(A,B)
l = A * B // g
print(l)

### FIXED CODE ###

A, B = map(int,input().split())
import fractions
def lcm(A,B):
    return (A * B) // fractions.gcd(A, B)
print(lcm(A,B))
n = int(input())
INF = 10**9
P = []
for i in range(n):
    p = map(int, input().split())
    P.append()
m = [[0]*n for i in range(n)]

for l in range(2, n + 1):
    for i in range(n - l + 1):
        j = i + l - 1
        m[i][j] = INF
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i][0] * p[k][1] * p[j][1])

print(m[0][n - 1])

### FIXED CODE ###

n = int(input())
INF = 10**9
P = [list(map(int, input().split())) for i in range(n)]
m = [[0]*n for i in range(n)]

for l in range(2, n + 1):
    for i in range(n - l + 1):
        j = i + l - 1
        m[i][j] = INF
        for k in range(i, j):
            m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + P[i][0] * P[k][1] * P[j][1])

print(m[0][n - 1])
N ,Q = map(int, input().split())
S = list(input())
ans = [0]*N
jdg = S[0]
cnt = 0
for i in range(1,N):
    if jdg == "A" and S[i] == "C":
        cnt += 1
        ans[i] = cnt
        jdg[i] = S[i]
    else:
        jdg = S[i]
        ans[i] = cnt

for i in range(Q):
    l, r = map(int, input().split())
    print(ans[r-1]-ans[l-1])

### FIXED CODE ###

N ,Q = map(int, input().split())
S = list(input())
ans = [0]*N
jdg = S[0]
cnt = 0
for i in range(1,N):
    if jdg == "A" and S[i] == "C":
        cnt += 1
        ans[i] = cnt
        jdg = S[i]
    else:
        jdg = S[i]
        ans[i] = cnt

for i in range(Q):
    l, r = map(int, input().split())
    print(ans[r-1]-ans[l-1])
def sonu(a,count,pivot):
    if(a[pivot]!=a[pivot+1]):
        a.pop(pivot)
        a.pop(pivot)
        pivot=0
        count+=1
        if(len(set(a))!=1 and len(a)!=0):
            sonu(a,count,pivot)
        else:
            print(count*2)
        

    else:
        
        pivot+=1
        if(len(set(a))!=1):
            sonu(a,count,pivot)
        else:
            print(count*2)
        
    
    




a=list(input())
if(len(a)==1):
    print(0)
else:
    count=0
    pivot=0
    sonu(a,count,pivot)
            


### FIXED CODE ###

def main(s):
    a = s.count("0")
    b = s.count("1")
    return 2*min(a,b)

s = input()


print(main(s))
N = int(input())
inputs = [int(i) for i in input().split()]

for i in range(1, N):
    if inputs[i-1] > inputs[i] and inputs[i-1] >= 1:
        inputs[i-1] -= 1

if inputs[-1] < inputs[-2] and inputs[-1] >= 1:
    inputs[-2] -= 1

for i in range(1, N):
    if inputs[i-1] > inputs[i]:
        print('No')
        break
else:
    print('Yes')

### FIXED CODE ###

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

for i in range(N-1, 1, -1):
    if inputs[i-1] > inputs[i]:
        inputs[i-1] -= 1

for i in range(1, N):
    if inputs[i-1] > inputs[i]:
        print('No')
        break
else:
    print('Yes')
def main():
    S = input()
    len_s = len(S)
    check = "AKIHABARA"
    idx = 0
    len_check = len(check)

    i = 0
    while i < len_s:
        s = S[i]
        if check[idx] == s:
            idx += 1
        elif idx+1 < len_check and s == check[idx+1] and "A" == check[idx]:
            idx += 2
        else:
            print("NO")
            exit()
        i += 1
    if idx >= 7:
        print("YES")
    else:
        print("NO")
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
    S = input()

    check = []
    for i in ["A", ""]:
        for j in ["A", ""]:
            for k in ["A", ""]:
                for l in ["A", ""]:
                    check.append(i + "KIH" + j + "B" + k + "R" + l)

    for C in check:
        if C == S:
            print("YES")
            exit()

    print("NO")
if __name__ == '__main__':
    main()