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



def main():
    
    input_lines_n = input()
    n = int(input_lines_n)
    
    for i in range(1, n+1):
        ans = 0
        cnt = 0

        for x in range(1, n+1):
            for y in range(1, n+1):
                for z in range(1, n+1):
                    ans = x**2 + y**2 + z**2 + x*y + y*z + z*x
                    if ans == i:
                        cnt = cnt + 1
        print(cnt)
                    
    
if __name__ == "__main__":
    main()


### FIXED CODE ###


import itertools

def main():
    
    input_lines_n = input()
    n = int(input_lines_n)
    xyz_max = 100
    l = [0] * 10001
    
    for x, y, z in itertools.product(range(xyz_max), range(xyz_max), range(xyz_max)):
        fn = 0
        if x > 0 and y > 0 and z > 0:
            fn = x**2 + y**2 + z**2 + x*y + y*z + z*x
            if fn <= n:
                l[fn] += 1
    
    for i in range(1, n+1):
        print(l[i])
                    
    
if __name__ == "__main__":
    main()
### BUGGY CODE ###

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

A.sort(reverse = True)
ans = []
while(len(A)>0):
    ans.append(A[1])
    A = A[2:len(A)-1]
print(sum(ans))

### FIXED CODE ###

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

A.sort(reverse = True)
ans = []
for i in range(int(len(A)/3)): # whileは遅い
    ans.append(A[1+2*i])
print(sum(ans))
### BUGGY CODE ###

import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

d0 = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
}

n, m = ril()
ls = ril()

d = collections.defaultdict(int)
for x in ls:
    d[d0[x]] = max(x, d[d0[x]])

dp = [0] * (n + 1)
for i in range(2, n + 1):
    for count, num in d.items():
        if count > i:
            continue
        k = dp[i - count]
        if count != i and k == 0:
            continue
        dp[i] = max(dp[i], 10 * k + num)
        if k != 0:
            dp[i] = max(dp[i], int(str(num) + str(k)))
print(dp[n])

### FIXED CODE ###

import collections
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

d0 = {
    1: 2,
    2: 5,
    3: 5,
    4: 4,
    5: 5,
    6: 6,
    7: 3,
    8: 7,
    9: 6,
}

n, m = ril()
ls = ril()

d = collections.defaultdict(int)
for x in ls:
    d[d0[x]] = max(x, d[d0[x]])

dp = [(0, 0)] * (n + 1)
for i in range(2, n + 1):
    for count, num in d.items():
        if count > i:
            continue
        k, l = dp[i - count]
        if count != i and l == 0:
            continue
        dp[i] = (max(dp[i][0], 10 * k + num), max(dp[i][1], l + 1))
        if k != 0:
            dp[i] = (max(dp[i][0], num * 10**l + k), max(dp[i][1], l + 1))
print(dp[n][0])
### BUGGY CODE ###

# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
N = int(input())

if int(float(N)**0.5) <= 1:
  xyzMax = 1
else:
  xyzMax = int(float(N)**0.5)

Ans = 0

for i in range(1,N+1):
  for x in range(1,xyzMax+1):
    if x**2 > i:
      break
    else:
      for y in range(1,xyzMax+1):
        if x**2 + y**2 > i:
          break
        else:
          for z in range(1,xyzMax+1):
            if x**2 + y**2 + z**2 > i:
              break
            else:
              if x**2 + y**2 + z**2 + (x * y) + (y * z) + (z * x) == i:
                Ans += 1
  
  print(Ans)
  Ans = 0

### FIXED CODE ###

# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
N = int(input())

Ans = 0
numlist = [0] * 10001

if int(float(N)**0.5) <= 1:
  rMax = 1
else:
  rMax = int(float(N)**0.5)  

for x in range(1,rMax+1):
  for y in range(1,rMax+1):
    for z in range(1,rMax+1):
      Ans = x**2 + y**2 + z**2 + (x * y) + (y * z) + (z * x)
      if Ans <= 10000:
        numlist[Ans] += 1
  
for r in range(1,N+1):
  print(numlist[r])
### BUGGY CODE ###

n, a, b = map(int, input().split())
all = (2**n - 1) % (10**9 + 7)
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = n
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

tmp1, tmp2 = cmb(n,a,mod), cmb(n,b,mod)
result = (all - tmp1 - tmp2)%(10**9 + 7)
print(result)


### FIXED CODE ###

n, a, b = [int(i) for i in input().split()]
mod = 10**9 + 7
 
total = pow(2, n, mod) - 1
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
 
at = combination(n,a)
bt = combination(n,b)
 
result = total - at - bt
result %= mod
if result < 0:
  result += mod
print(result)
### BUGGY CODE ###

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

def Lsearch(B,key):
    for i in range(len(B)):
        if B[i] == key:
            return i
    return -1

num,pt,n_pt,count = 1,0,0,0
while Lsearch(A[pt:],num) != -1:
    n_pt = Lsearch(A[pt:],num) + 1
    count += n_pt - pt -1
    pt = n_pt
    num += 1
if pt != 0:
    count += len(A) - pt
    print(count)
else:
    print(-1)


### FIXED CODE ###

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

num,pt,n_pt,count = 1,-1,0,0
while n_pt < len(A):
    if A[n_pt] == num:
        count += n_pt - pt -1
        pt = n_pt
        num += 1
    n_pt += 1
if pt != -1:
    count += len(A) - pt -1
    print(count)
else:
    print(-1)
### BUGGY CODE ###

N = int(input())
l = [input() for i in range(N)]
l2 = []
ans = 0
for a in l:
    if a not in l2:
        ans+=1
        l2.append(a)
    else:
        pass
print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
l = [input() for i in range(N)]

print(len(Counter(l)))
### BUGGY CODE ###

#C - Exception Handling
N = int(input())
A = list(int(input()) for _ in range(N))
A_sorted = list(set(sorted(A, reverse = False)))

ans = []
for i in A:
    A_copy = A.copy()
    A_copy.remove(i)
    ans.append(max(A_copy))

for i in ans:
    print(i)

### FIXED CODE ###

# C - Exception Handling
N = int(input())
A = list(int(input()) for _ in range(N))
A_sorted = list((sorted(A, reverse = True)))
A_sorted = A_sorted[0:2]

ans = []
for i in A:
    if i == A_sorted[0]:
        ans.append(A_sorted[1])
    else:
        ans.append(A_sorted[0])

for j in ans:
    print(j)
### BUGGY CODE ###

# -*- coding:utf-8 -*-
import itertools

N,K = map(int,input().split())
l = []*N
l = list(map(int,input().split()))

l_set = list(itertools.combinations(l,K))

max_s = 0
for i in range(len(l_set)):
    if sum(l_set[i])>max_s:
        max_s = sum(l_set[i])

print(max_s)


### FIXED CODE ###

# -*- coding:utf-8 -*-
import itertools
import numpy as np

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

ans = 0
for i in range(K):
    max_i = np.argmax(l)
    ans += l[max_i]
    l = np.delete(l,max_i)

print(ans)
### BUGGY CODE ###

def main():
    N, K = map(int, input().split())
    XY = [tuple(map(int, input().split())) for _ in range(N)]
    # XY = [list(map(int, input().split())) for _ in range(N)]
    XY.sort()
    XY = tuple(XY)

    def count(x_min, x_max, y_min, y_max, i, j):
        OK = 0
        NG = 0
        a = j - i + 1 - K
        for m in range(i, j + 1):
            if y_min <= XY[m][1] <= y_max:
                OK += 1
            else:
                NG += 1
            if OK >= K:
                return True
            if NG > a:
                return False

    ans = 10 ** 20
    for i in range(N - 1):
        for j in range(i + K - 1, N):
            for k in range(N):
                for l in range(N):
                    if XY[k][1] < XY[l][1]:
                        if count(XY[i][0], XY[j][0], XY[k][1], XY[l][1], i, j):
                            # print ('A')
                            ans = min(ans, (XY[j][0] - XY[i][0]) * (XY[l][1] - XY[k][1]))
    print (ans)
    return

if __name__ == '__main__':
    main()


### FIXED CODE ###

#PythonだとTLEする
N, K = map(int, input().split())
X = [0] * N
Y = [0] * N
for i in range(N):
    x, y = map(int, input().split())
    X[i] = x
    Y[i] = y

def count(x_min, x_max, y_min, y_max):
    cnt = 0
    for i in range(N):
        if x_min <= X[i] <= x_max and y_min <= Y[i] <= y_max:
            cnt += 1
    return cnt

ans = 10 ** 20
for i in range(N):
    for j in range(N):
        for k in range(N):
            for l in range(N):
                # print ('i, j , k, l = ', i, j, k, l)
                if X[i] < X[j] and Y[k] < Y[l]:
                    if count(X[i], X[j], Y[k], Y[l]) >= K:
                        # print ('A')
                        ans = min(ans, (X[j] - X[i]) * (Y[l] - Y[k]))
print (ans)
### BUGGY CODE ###

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []
    for i in range(M):
        bc = list(map(int, input().split()))
        BC.append(bc)

    # A.sort()


    for b, c in BC:
        A = A + [c] * b

    A.sort(reverse=True)
    ans = sum(A[:N])
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

from operator import itemgetter

def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split()))
    BC = []
    for i in range(M):
        bc = list(map(int, input().split()))
        BC.append(bc)

    BC.sort(key=itemgetter(1), reverse=True)
    # B = []
    # for b, c in BC:
    #     B = B + [c] * b
    #     if len(B) > N:
    #         break

    A.sort(reverse=True)
    
    i = 0
    j = 0
    r = []

    cnt = 0
    while len(r) != N:
        # print(i, j)
        if j > M - 1:
            r.append(A[i])
            i += 1
            continue

        b, c = BC[j]
        if A[i] >= c:
            r.append(A[i])
            i += 1
        else:
            r.append(c)
            cnt += 1
            if cnt == b:
                j += 1
                cnt = 0

    # print(r)
    ans = sum(r)
    print(ans)


if __name__ == "__main__":
    main()
### BUGGY CODE ###

R, G, B, N = map(int, input().split())
counter = 0
r = N // R + 1
g = N // G + 1
for s in range(r):
	for t in range(g):
		if s*R + t*G <= N and (N - (s*R + t*G)) % B == 0:
			counter += 1
print(counter)


### FIXED CODE ###

r, g, b, n = map(int, input().split())
counter = 0
for s in range(n+1):
  for t in range(n-s+1):
    u = n - r*s - g*t
    if u >= 0 and u % b == 0:
      counter += 1
print(counter)
### BUGGY CODE ###

n = int(input())
cnt = 0
for a in range(1,n):
    for c in range(1,n-a+1):
        if ((n-c)/a) % 1 == 0:
            cnt += 1
print(cnt)

### FIXED CODE ###

n = int(input())
cnt = 0
for a in range(1,n):
    cnt += (n-1) // a
    
print(cnt)
### BUGGY CODE ###

n,m=map(int,input().split())
g=[[] for _ in range(n)]
for _ in range(m):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  g[a].append(b)
s,t=map(int,input().split())
s,t=s-1,t-1
gg=[set(()) for _ in range(n)]
for i in range(n):
  for j in g[i]:
    for k in g[j]:
      for l in g[k]:
        if l!=i:
          gg[i].add(l)
from collections import deque
todo=deque([s])
seen=[-1]*n
seen[s]=0
while todo:
  v=todo.popleft()
  for l in gg[v]:
    if seen[l]==-1:
      todo.append(l)
      seen[l]=seen[v]+1
print(seen[t])

### FIXED CODE ###

n,m=map(int,input().split())
g=[[] for _ in range(n)]
gg=[[] for _ in range(3*n)]
for _ in range(m):
  a,b=map(int,input().split())
  a,b=a-1,b-1
  gg[a].append(n+b)
  gg[n+a].append(2*n+b)
  gg[2*n+a].append(b)
s,t=map(int,input().split())
s,t=s-1,t-1
from collections import deque
todo=deque([s])
seen=[-1]*(3*n)
seen[s]=0
while todo:
  v=todo.popleft()
  for l in gg[v]:
    if seen[l]==-1:
      todo.append(l)
      seen[l]=seen[v]+1
      if l==t:
        print(seen[l]//3)
        exit()
if seen[t]>0:
  print(seen[t]//3)
else:
  print(-1)
### BUGGY CODE ###

def calc():
  n,k=map(int,input().split())
  arr=list(map(int,input().split()))
  flag=format(k,'b')[::-1]
  l=len(flag)
  xor1=0
  cnt=0
  for i in range(l-2,-1,-1):
    for j in arr:
      if j&(2**i)!=0:
        cnt+=1
    if cnt<=n//2:
      xor1+=2**i
    cnt=0
  ans1=0
  for i in arr:
    ans1+=i^xor1
  xor2=0
  cnt=0
  check=False
  for i in range(l-1,-1,-1):
    if check==False and flag[i]=='0':
      continue
    for j in arr:
      if j&(2**i)!=0:
        cnt+=1
    if cnt<=n//2:
      if check==True or flag[i]=='1':
        xor2+=2**i
    else:
      if flag[i]=='1':
        check=True
    cnt=0
  ans2=0
  for i in arr:
    ans2+=i^xor2
  print(max(ans1,ans2))

calc()

### FIXED CODE ###

n,k=map(int,input().split())
arr=list(map(int,input().split()))
flag=format(k,'b')[::-1]
l=len(flag)
xor1=0
cnt=0
for i in range(l-2,-1,-1):
  for j in arr:
    if j&(2**i)!=0:
      cnt+=1
  if cnt<=n//2:
    xor1+=2**i
  cnt=0
ans1=0
for i in arr:
  ans1+=i^xor1
xor2=0
cnt=0
check=False
for i in range(l-1,-1,-1):
  if check==False and flag[i]=='0':
    continue
  for j in arr:
    if j&(2**i)!=0:
      cnt+=1
  if cnt<=n//2:
    if check==True or flag[i]=='1':
      xor2+=2**i
  else:
    if flag[i]=='1':
      check=True
  cnt=0
ans2=0
for i in arr:
  ans2+=i^xor2
print(max(ans1,ans2))
### BUGGY CODE ###

def main():
    N, M = map(int, input().split())
    A = [0] * N
    for _ in range(M):
        L, R = map(int, input().split())
        for i in range(L - 1, R):
            A[i] += 1
    print(A.count(M))


main()


### FIXED CODE ###

def main():
    N, M = map(int, input().split())
    A = [0] * (N + 1)
    for _ in range(M):
        L, R = map(int, input().split())
        A[L - 1] += 1
        A[R] -= 1
    c = 0
    ans = 0
    for i, a in enumerate(A):
        c += a
        if c == M:
            ans += 1
    print(ans)


main()
### BUGGY CODE ###

x=int(input())
p=x**0.5
while any(x%i==0 for i in range(2,x//1+1)):
  x+=1
else:
  print(x)

### FIXED CODE ###

x=int(input())
p=x**0.5
while any(x%i==0 for i in range(2,int(p)+1)):
  x+=1
else:
  print(x)
### BUGGY CODE ###

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

product = 1
for num in A:
    product = product * num
if product > 10 ** 18:
    print('-1')
else:
    print(product)

### FIXED CODE ###

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


def ans():
    if 0 in A:
        return print(0)
    product = 1
    for num in A:
        product = product * num
        if product > 10 ** 18:
            return print(-1)

    print(product)


ans()
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**10

h, n = [int(item) for item in input().split()]
ab = []
for i in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
dp = [[INF] * (2 * 10**4) for _ in range(n + 1)]
dp[0][0] = 0
dp[1][0] = 0
for i in range(n):
    v = ab[i][0]
    w = ab[i][1]
    for j in range(10**4):
        if dp[i][j] == INF:
            continue
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        dp[i+1][j + v] = min(dp[i][j + v], dp[i][j] + w)
        dp[i][j+v] = min(dp[i][j+v], dp[i][j] + w)
ans = min(dp[n][h:])
print(ans)

### FIXED CODE ###

#!/usr/bin/env python3
import sys
input = sys.stdin.readline
INF = 10**9

def main(): 
    h, n = [int(item) for item in input().split()]
    MAX_LEN = h + 10**4 - 2
    dp = [INF] * MAX_LEN
    dp[0] = 0
    vw = []
    for _ in range(n):
        v, w = map(int, input().split())
        vw.append((v, w))
    vw.sort(reverse=True, key=lambda x:x[0])
    for v, w in vw:
        for j in range(h):
            if dp[j] == INF:
                continue
            tmp = dp[j] + w
            if tmp < dp[j+v]:
                dp[j+v] = tmp
    print(min(dp[h:]))

if __name__ == "__main__":
    main()
### BUGGY CODE ###

n = int(input())
st = list(map(int,input().split()))
lst = [] 
for i in range(n):
  lst.append(st.index(i+1)+1)
for i in lst:
  print(i,end=" ")

### FIXED CODE ###

n = int(input())
st = list(map(int,input().split()))
srtkey = lambda x: x[1] 
ans = []
for i,j in zip(range(n),st):
  ans.append([i+1,j])
srt = sorted(ans,key=srtkey)
for i,j in srt:
  print(i,end=" ")
### BUGGY CODE ###

N,W = map(int,input().split(" "))
INF =  1e11
v = [INF]*100010
v[0] = 0
for i in range(N):
  weight,value = map(int,input().split(" "))
  for j in range(100001,0,-1):
    if j < value:
      break
    v[j] = min(v[j],v[j-value]+weight)

for i in range(100001,0,-1):
  if v[i] <= W:
    print(i)
    break



### FIXED CODE ###

N,W = map(int,input().split(" "))
INF =  1e11
v = [INF]*100010
v[0] = 0
for i in range(N):
  weight,value = map(int,input().split(" "))
  for j in range(100001,0,-1):
    if j < value:
      break
    v[j] = min(v[j],v[j-value]+weight)

ans = 0
for i in range(100001,0,-1):
  if v[i] <= W:
    ans = i
    break
print(ans)
### BUGGY CODE ###

from collections import defaultdict, deque

class Node:

    def __init__(self, id, parent_id=None):
        self.id = id
        self.parent_id = parent_id
        self.distance = 0 

def distance_between(id1, id2):
    return distance_dict[(id1, id2)]


N = int(input())

distance_dict = {}
adj_dict = defaultdict(list)
for i in range(N-1):
    ui, vi, wi = map(int, input().split())
    distance_dict[(ui, vi)] = wi
    distance_dict[(vi, ui)] = wi
    adj_dict[ui].append(vi)
    adj_dict[vi].append(ui)

id_to_node = {}
cur_node = Node(id=2)
id_to_node[cur_node.id] = cur_node
open_list = deque()
open_list.append(cur_node)
closed_list = []
while True:
    if len(open_list) == 0:
        break
    cur_node = open_list.pop()
    closed_list.append(cur_node.id)
    adj_id_list = adj_dict[cur_node.id]
    for adj_id in adj_id_list:
        if adj_id in closed_list: 
            continue
        adj_node = Node(id=adj_id, parent_id=cur_node.id)
        adj_node.distance = distance_between(cur_node.id, adj_node.id)+cur_node.distance
        id_to_node[adj_id] = adj_node
        open_list.append(adj_node)

for id in range(1, N+1):
    if id_to_node[id].distance%2==0:
        print(0)
    else:
        print(1)

### FIXED CODE ###

from collections import defaultdict

N = int(input())
edge = defaultdict(list)
# 辺の情報と距離を記録
for i in range(N-1):
    u, v, w = map(int, input().split())
    edge[u].append((v, w))
    edge[v].append((u, w))

# 幅優先探索
distance = [0]*N
queue = [(1, -1)]
while True:
    if len(queue) == 0:
        break
    cur_id, parent_id = queue.pop()
    for child_id, w in edge[cur_id].copy():
        if child_id == parent_id:
            continue
        queue.append((child_id, cur_id))
        distance[child_id-1] = distance[cur_id-1] + w

for i in range(N):
    print(distance[i]%2)
### BUGGY CODE ###

from fractions import gcd
a,b,c,d=map(int,input().split())
count=0
for i in range(a,b+1):
  if i%c!=0 and i%d!=0:
    count=count+1
print(count)

### FIXED CODE ###

from fractions import gcd
a,b,c,d=map(int,input().split())
count=b-a+1
temp1=b//c-(a-1)//c
temp2=b//d-(a-1)//d
temp3=b//int((c*d)/gcd(c,d))-(a-1)//int((c*d)/gcd(c,d))
print(int(count-temp1-temp2+temp3))
### BUGGY CODE ###

N, P = [int(i) for i in input().split()]
S = input()

if P == 2 or P == 5:
#if False:
  count = 0
  for n in range(N):
    if int(S[n]) % P==0:
      count += (n + 1)
  print(count)
else:    
  Pa = [0 for _ in range(P)]
  for i in range(N, 0, -1):
    ui = int(S[i-1:])
    mp = ui % P
    Pa[mp] += 1
  count = 0
  if Pa[0] > 0:
    count += Pa[0]
  for p in range( P):
    pmod = Pa[p]
    if pmod > 0:
      count += (pmod * (pmod - 1))//2
  print(count)
  


### FIXED CODE ###

N, P = [int(i) for i in input().split()]
S = input()


def mod_pow(a, n, mod):
  res = 1
  while  n > 0:
    if n & 1:
      res = (res * a ) % mod
    a = a * a  % mod
    n = n >> 1
  return res

if P == 2 or P == 5:
#if False:
  count = 0
  for n in range(N):
    if int(S[n]) % P==0:
      count += (n + 1)
  print(count)
else:    
  Pa = [0 for _ in range(P)]
  number = 0
  for i in range(N, 0, -1):
    number = (number + int(S[i-1])*mod_pow(10, N - i, P)) % P
    Pa[number] += 1
  count = 0
  if Pa[0] > 0:
    count += Pa[0]
  for p in range( P):
    pmod = Pa[p]
    if pmod > 0:
      count += (pmod * (pmod - 1))//2
  print(count)
### BUGGY CODE ###

INF = 10e15
N, M = map(int, input().split())
LRC = [tuple(map(int, input().split())) for _ in range(M)]
LRC.sort()
# print(LRC)

dp = [INF] * N
dp[0] = 0

for i in range(M):
    l, r, c = LRC[i]
    l -= 1
    r -= 1
    # print(l, r, c)
    if dp[l] == INF:
        continue
    for i in range(l+1, r+1):
        dp[i] = min(dp[i], dp[l] + c)

# print(dp)
if dp[-1] == INF:
    print(-1)
else:
    print(dp[-1])


### FIXED CODE ###

INF = 10e15
N, M = map(int, input().split())
LRC = [tuple(map(int, input().split())) for _ in range(M)]
LRC.sort()
# print(LRC)

dp = [INF] * N
dp[0] = 0

for l, r, c in LRC:
    l -= 1
    r -= 1
    # print(l, r, c)
    if dp[l] == INF:
        continue
    for i in range(r, l, -1):
        if dp[i] > dp[l] + c:
            dp[i] = dp[l] + c
        else:
            break

# print(dp)
if dp[-1] == INF:
    print(-1)
else:
    print(dp[-1])
### BUGGY CODE ###

from operator import itemgetter

n = int(input())
a = []
res = ('Yes')
for i in range(n):
    a.append(list(map(int,input().split())))

a.sort(key=itemgetter(1))
for i in range(0,n):
    suma = 0
    for j in range(0,i+1):
        suma += a[j][0]
    if suma > a[i][1]:
        res = ('No')
        break


print(res)

### FIXED CODE ###

from operator import itemgetter

n = int(input())
a = []
res = ('Yes')
for i in range(n):
    a.append(list(map(int,input().split())))

a.sort(key=itemgetter(1))
suma = 0
for i in range(0,n):
    suma += a[i][0]
    if suma > a[i][1]:
        res = ('No')
        break


print(res)
### BUGGY CODE ###

import statistics

N = int(input())
X = list(map(int, input().split()))
sorted(X)

for i in range(len(X)):
    print(statistics.median(X[:i]+X[i+1:]))

### FIXED CODE ###

N = int(input())
X = list(map(int, input().split()))
Y = X[:]
Y = sorted(Y)

med1 = Y[N // 2 - 1]
med2 = Y[N // 2]

for x in X:
    if x < med2:
        print(med2)
    else:
        print(med1)
### BUGGY CODE ###

from numba import jit

@jit
def solve():
    n, k = map(int,input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split())) 
    max_score = -10000000000
    for i in range(len(p)):
        score = 0
        nx = i
        for _ in range(k):
            score = score + c[p[nx]-1]
            nx = p[nx] -1 
            max_score = max(max_score, score)
    
    print(max_score)
 
solve()

### FIXED CODE ###

from numba import jit

@jit
def solve():
    n, k = map(int,input().split())
    p = list(map(int, input().split()))
    c = list(map(int, input().split()))
    max_score = -10**10

    for i in range(n):
        count = 1
        val = c[i]

        next_i = p[i] -1
        while next_i != i:
            count += 1
            val += c[next_i]
            next_i = p[next_i] - 1
        
        if val > 0:
            loop_num = (k//count -1)
            tmp_score = loop_num * val
            max_score = max(tmp_score, max_score)
            num = k%count+count

        else:
            tmp_score = 0
            num = min(k, count)

        next_i = i
        for _ in range(num):
            next_i = p[next_i] - 1
            tmp_score += c[next_i]
            max_score = max(tmp_score, max_score)
    
    print(max_score)

solve()
### BUGGY CODE ###

N = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(1, N+1):
    ans.append(a.index(i) + 1)
for an in ans:
    print(an, end=(" "))

### FIXED CODE ###

N = int(input())
a = list(map(int, input().split()))
rev = [0]*N
for i in range(N):
    rev[a[i] - 1] = i +1
for i in range(N):
    print(rev[i], end=" ")