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

from sys import stdin
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True
N=list(map(int,stdin.readline().strip().split()))[0]
for _ in range(N):
    count=0
    a,b=map(int,(input().split()))
    if a==b:
        if is_prime(a) and is_prime((a+1)/2):
            count+=1
    for i in range(a,b,2):
        if (i + 1) // 2!=0:
            if is_prime(i) and is_prime((i+1)/2):
                count+=1
    print(count)



### FIXED CODE ###

from sys import stdin
def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True
prime_list=[i for i in range(2,100000) if is_prime(i)]
oppai=[0]*100000
oppai[1]=0
before=1
for num in prime_list:
    if (num+1)//2 in prime_list:
        oppai[before:num - 2] = [oppai[before]] * (num-1 - before)
        oppai[num-1]=oppai[before]+1
    else:oppai[before:num-1]=[oppai[before]]*(num-before)
    before=num-1
oppai[99991:99999]=[oppai[99990]]*8
N=list(map(int,stdin.readline().strip().split()))[0]
for _ in range(N):
    count = 0
    a,b=list(map(int,stdin.readline().strip().split()))
    if a==b:
        bb = (oppai[b - 1])
        aa = (oppai[a - 2])
        count = bb - aa
        print(count)
    else:
        if a>=2:
            bb = oppai[b - 1]
            aa = oppai[a - 2]
            count = bb - aa
            print(count)
        else:
            bb = oppai[b - 1]
            count = bb
            print(count)
### BUGGY CODE ###

H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[set() for j in range(W)] for i in range(H)]
dp[0][0].add(abs(A[0][0]-B[0][0]))
for i in range(H):
    for j in range(W):
        dij=A[i][j]-B[i][j]
        if i>0:
            for x in dp[i-1][j]:
                dp[i][j].add(abs(x+dij))
                dp[i][j].add(abs(x-dij))
        if j>0:
            for y in dp[i][j-1]:
                dp[i][j].add(abs(y+dij))
                dp[i][j].add(abs(y-dij))
print(min(dp[-1][-1]))

### FIXED CODE ###

H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=161*161
dp[0][0]|=1<<(A[0][0]-B[0][0]+M)
dp[0][0]|=1<<(-A[0][0]+B[0][0]+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for k in range(2*M+1):
    if (1<<k)&dp[-1][-1]!=0:
        ans.append(abs(k-M))
print(min(ans))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

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

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
import heapq


def solve():
    n, m = MI()

    AB = []
    for i in range(n):
        a, b = MI()
        AB.append((a, b))

    AB = sorted(AB, key=lambda x: x[0])
    # print(AB)

    q = [AB[0][1]]
    pre = AB[0][0]
    memo = {}
    for i in range(1, n):
        a, b = AB[i]
        if a == pre:
            q.append(b)
        else:
            memo[pre] = deque(sorted(q))
            q = [b]
            pre = a
    memo[pre] = deque(sorted(q))

    # print(memo)

    ans = 0
    for i in range(1, m+1):
        q = memo.get(i, deque([]))

        # print(i, hp)
        if len(q) > 0:
            ans += q.pop()

        q.extend(memo.get(i + 1, []))

        memo[i+1] = deque(sorted(q)[-m-i:])
    print(ans)


if __name__ == '__main__':
    solve()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

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

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

from math import ceil, floor, log2
from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product
from heapq import heapify, heappush, heappop


def solve():
    n, m = MI()

    jobs = [[] for _ in range(m)]
    for i in range(n):
        a, b = MI()
        if m-a >= 0:
            jobs[m-a].append(b)

    # print(jobs)
    hp = []
    heapify(hp)
    ans = 0
    for i in range(m-1, -1, -1):
        js = jobs[i]
        for j in js:
            heappush(hp, -j)

        if len(hp) > 0:
            ans += -heappop(hp)
    print(ans)





if __name__ == '__main__':
    solve()
### BUGGY CODE ###

# 解説

def main():
    N = int(input())
    s = input()

    dp = [[0] * N for _ in range(N)]
    ans = 0
    for i in range(N - 1, -1, -1):
        for j in range(N - 1, -1, -1):
            if s[i] == s[j]:
                if i + 1 < N and j + 1 < N:
                    dp[i][j] = min(dp[i + 1][j + 1] + 1, j - i)
                    ans = max(ans, dp[i][j])
                else:
                    dp[i][j] = 1
            else:
                dp[i][j] = 0
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

n = int(input())
s = input()

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

res = 0
for i in range(n - 1, -1, -1):
    for j in range(n - 1, i, -1):
        if s[i] == s[j]:
            dp[i][j] = dp[i + 1][j + 1] + 1
            # dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + 1)
        res = max(res, min(dp[i][j], j - i))
print(res)
### BUGGY CODE ###

import math

K = int(input())

ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        for c in range(1,K+1):
            ans += math.gcd(a, math.gcd(b,c))

print(ans)


### FIXED CODE ###

import math

K = int(input())

ans = 0
for a in range(1,K+1):
    for b in range(1,K+1):
        num = math.gcd(a,b)
        for c in range(1,K+1):
            ans += math.gcd(num,c)

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

# encoding: utf-8
import itertools


n = int(input())
xlist = []
ylist = []
hlist = []
for i in range(n):
	xtmp, ytmp, htmp = map(int, input().split())
	xlist.append(xtmp)
	ylist.append(ytmp)
	hlist.append(htmp)
h = 0
end_flag = False
while True:
	for cx, cy in itertools.product(range(101), range(101)):
		if all([hlist[i] == max(h - abs(xlist[i] - cx) - abs(ylist[i] - cy), 0) for i in range(n)]):
			print('{} {} {}'.format(cx, cy, h))
			end_flag = True
			break
	if end_flag:
		break
	h += 1

### FIXED CODE ###

# encoding: utf-8
import itertools


n = int(input())
xlist = []
ylist = []
hlist = []
for i in range(n):
	xtmp, ytmp, htmp = map(int, input().split())
	xlist.append(xtmp)
	ylist.append(ytmp)
	hlist.append(htmp)
h = max(hlist)
end_flag = False
while True:
	for cx, cy in itertools.product(range(101), range(101)):
		if all([hlist[i] == max(h - abs(xlist[i] - cx) - abs(ylist[i] - cy), 0) for i in range(n)]):
			print('{} {} {}'.format(cx, cy, h))
			end_flag = True
			break
	if end_flag:
		break
	h += 1
### BUGGY CODE ###

N, M = map(int, input().split()) 
HEIGHT = list(map(int, input().split()))
tenbou = list(range(1,N+1))
for i in range(M):
    hasi1, hasi2 = map(int, input().split())
    if HEIGHT[hasi1-1] > HEIGHT[hasi2-1] and hasi2 in tenbou:
        tenbou.remove(hasi2)
    elif HEIGHT[hasi1-1] < HEIGHT[hasi2-1] and hasi1 in tenbou:
        tenbou.remove(hasi1)
    elif HEIGHT[hasi1-1] == HEIGHT[hasi2-1]:
        if hasi1 in tenbou:
            tenbou.remove(hasi1)
        if hasi2 in tenbou:
            tenbou.remove(hasi2)
print(len(tenbou))

### FIXED CODE ###

N, M = map(int, input().split()) 
HEIGHT = list(map(int, input().split()))
MA = []
for i in range(N):
    MA.append(0)
for i in range(M):
    hasi1, hasi2 = map(int, input().split())
    #print(max(MA[hasi1-1], HEIGHT[hasi2-1]))
    MA[hasi1-1] = max(MA[hasi1-1], HEIGHT[hasi2-1])
    MA[hasi2-1] = max(MA[hasi2-1], HEIGHT[hasi1-1])
    #間違い：リストの要素　〇LIST[i]　×LIST(i)
    #MA.insert(hasi1-2,max(MA[hasi1-2], HEIGHT(hasi1-1)))
result = 0
for i in range(N):
    if HEIGHT[i] > MA[i]:
        result += 1
print(result)
### BUGGY CODE ###

N = int(input())
A = list(map(int,input().split()))
B = [0]*N
for i in range(0,N):
  B[i] = A.count(i+1)
for j in range(N):
  print(B[j])

### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
B = {}
for i in A:
  if i not in B:
    B[i] = 0
  B[i] += 1
  
for j in range(1,N+1):
  try:
    print(B[j])
  except KeyError:
    print(0)
### BUGGY CODE ###

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

a=[]#種類の記憶

for i in range(n):
  if a.count(lst[i]) == 0:
    a.append(lst[i])
c=[]#カウント用
for j in a:
  c.append(lst.count(j))
ans = 0
while k < len(c):
  i = c.index(min(c)) #最小の個数のインデックス
  ans += min(c)
  del c[i]
print(ans)

### FIXED CODE ###

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

num = []
cnt = []

for i in range(N):
    if i == 0:
        num.append(A[i])
        cnt.append(1)
    else:
        if A[i-1] != A[i]:
            num.append(A[i])
            cnt.append(1)
        else:
            cnt[-1] += 1
cnt.sort()
#print(num,cnt)

res = 0
M = len(cnt)
if M <= K:
    res = 0
else:
    for i in range(M-K):
        res += cnt[i]
print(res)
### BUGGY CODE ###

MOD = 10**9 + 7
N = int(input())
A = list(map(int,input().split()))

ans = 0
for i in range(N-1):
  for j in range(i+1, N):
    ans += (A[i]^A[j])%MOD
print(ans%MOD)

### FIXED CODE ###

MOD = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
maxA = max(A)

maxA_len = len(bin(maxA)[2:])
num0_each_digit = [0 for _ in range(maxA_len)]
num1_each_digit = [0 for _ in range(maxA_len)]

for i in range(N):
    for i_bit, bit in enumerate(bin(A[i])[2:].zfill(maxA_len)):
        if bit == '0':
            num0_each_digit[i_bit] += 1
            num0_each_digit[i_bit] %= MOD
        else:
            num1_each_digit[i_bit] += 1
            num1_each_digit[i_bit] %= MOD
ans = 0
for i in range(maxA_len):
    factor = (2 ** (maxA_len - i - 1)) % MOD
    ans += (num0_each_digit[i] * num1_each_digit[i] * factor) % MOD

print(ans % MOD)
### BUGGY CODE ###

import math
a, b, n = map(int,input().split())
ans = 0
m = math.floor(n/10)
for i in range(m,n+1):
    s = math.floor(a * i / b)
    t = math.floor (i/b)
    ans = max(ans,s - a*t)     
print(ans) 

### FIXED CODE ###

a, b, n = map(int,input().split())
ans = 0
x = min(n , b-1)
print(x*a //b)
### BUGGY CODE ###

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

ANSWER = []
for i in range(N):
    A_ = []
    for j in range(N):
        if j != i:
            A_.append(A[j])
            
        else: continue

    answer_i = 0
    for j in set(A_):
        answer_i += (A_.count(j) * (A_.count(j)-1)/2)
    
    ANSWER.append(int(answer_i))

for i in ANSWER:
    print(i)

### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
import numpy as np
 
N = int(readline())
A = np.array(read().split(), np.int64)
counter = np.bincount(A)
 
total = (counter * (counter - 1)).sum() // 2
answer = (total - (counter - 1))[A]
 
print('\n'.join(answer.astype(str)))
### BUGGY CODE ###

# python には結構厳しいTLE・・・python以外のお勉強時間ないよ～( ;∀;)
# . から見た # の最大距離の方が楽ではあるが・・・
# 嫌いだけど def main も使ってみた．
def main():
  H, W = list(map(int, input().split()))
  all_count = H * W
  L = [j == "#" for i in range(H) for j in input()]
  blacks = [i for i in range(all_count) if L[i]]
  black_count = len(blacks)
 
  cands_H = [-W, W]
  cands_W = [-1, 1]
  result = 0
  while black_count < all_count:
    next_steps = [black+cand for black in blacks for cand in cands_H]
    next_steps.extend([black-1 for black in blacks if black%W != 0])
    next_steps.extend([black+1 for black in blacks if black%W != W-1])
    next_steps = set(next_steps)
    blacks = [i for i in next_steps if i >= 0 and i < all_count and not L[i]]
    black_count += len(blacks)
    result += 1
    if black_count >= all_count:
      break
    for i in blacks:
      L[i] = True

  print(result)

if __name__ == "__main__":
  main()


### FIXED CODE ###

# python には結構厳しいTLE・・・python以外のお勉強時間ないよ～( ;∀;)
# . から見た # の最大距離の方が楽ではあるが・・・
# 嫌いだけど def main も使ってみた．
from collections import deque
import copy

def main():
  H, W = list(map(int, input().split()))
  L = [[j=="#" for j in input()] for i in range(H)]
  q = deque([(i, j) for i in range(H) for j in range(W) if L[i][j]])
  cands = [(0, -1), (0, 1), (-1, 0), (1, 0)]
  result = 0
  while True:
    nq = deque()
    while q:
      i, j = q.popleft()
      if i > 0 and not L[i-1][j]:
        L[i-1][j] = True
        nq.append((i-1, j))
      if j > 0 and not L[i][j-1]:
        L[i][j-1] = True
        nq.append((i, j-1))
      if i < H-1 and not L[i+1][j]:
        L[i+1][j] = True
        nq.append((i+1, j))
      if j < W-1 and not L[i][j+1]:
        L[i][j+1] = True
        nq.append((i, j+1))
    if nq:
      q = copy.copy(nq)
    else:
      break    
    result += 1
  print(result)

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


# 初期入力
import sys
input = sys.stdin.readline
N = int(input())
H = list(map(int, input().split()))

#
differ =0
ans="Yes"
max_h=max(H)
max_index =H.index(max_h)
#最大値-1 と最大値の右側の最小値を比較,この時はfor不要
if max_index +1 <= N-1:
    if H[max_index]-1 > min(H[max_index+1:]):
        ans ="No"
        #print(ans)
        exit

for i in range(N-1):

    differ = H[i+1] -H[i]
    if differ <-1:      #右側が小さのは１までOK
        ans ="No"
        break
    elif differ ==-1:
        if H[i]-1 > min(H[i:]) :
            ans ="No"
            break
print(ans)

### FIXED CODE ###


# 初期入力
from collections import deque
import sys
#input = sys.stdin.readline
N = int(input())
H = list(map(int, input().split()))

#
differ =0
ans="Yes"
max_h   =max(H)
max_index =H.index(max_h)
#最大値-1 と最大値の右側の最小値を比較,この時はfor不要
if max_index +1 <= N-1:
    if H[max_index]-1 > min(H[max_index+1:]):
        ans ="No"
        print(ans)
        sys.exit()

dq    =deque(H)
before  =dq.popleft()
for i in range(N-1):
    after   =dq.popleft()
    differ    =after - before
    #differ = H[i+1] -H[i]
    if differ <-1:      #右側が小さのは１までOK
        ans ="No"
        break
  
    else:
        before = after

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

from sys import stdin
lis =[]
#k = int(stdin.readline().rstrip())
lis= stdin.readline().rstrip().split()
k = int(lis[0])
s = int(lis[1])
count = 0
x = 0
y = 0
z = 0
for x in range(k+1):
    y = 0
    for y in range(k+1):
        z = 0
        z = s - x - y
        if x <= k and y <= k and z <= k and z >= 0:
                count +=1



print(count)


### FIXED CODE ###

from sys import stdin
lis =[]
#k = int(stdin.readline().rstrip())
lis= stdin.readline().rstrip().split()
k = int(lis[0])
s = int(lis[1])
count = 0
x = 0
y = 0
z = 0
for x in range(k+1):
    y = 0
    for y in range(k+1):
        z = 0
        z = s - x - y
        if z <= k and z >= 0:
                count +=1



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

from functools import reduce

n=int(input())
a=list(map(int,input().split()))
ans=reduce(lambda x,y:x*y,a)
if ans > 10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

n=int(input())
a=list(map(int,input().split()))
limit=10**18
a.sort()
result=a[0]
for k in a[1:]:
    result*=k
    if result > limit:
        print(-1)
        exit()
print(result)
### BUGGY CODE ###

n,k=list(map(int, input().split()))
h=list(map(int, input().split()))
c1 = abs(h[0]-h[1])
c=[0,c1]
for i in range(n-2):
    d = []
    if i+2<k:
        for j in range(i+2):
            d.append(c[i+1-j]+abs(h[i+1-j]-h[i+2]))
    else:
        for j in range(k):
            d.append(c[i+1-j]+abs(h[i+1-j]-h[i+2]))
    c.append(min(d))
print(c[-1])

### FIXED CODE ###

N,K=list(map(int, input().split()))
H=list(map(int, input().split()))
"""c=[0]
for i in range(N-1):
    if i+1<K:
        d = [c[i-j]+abs(H[i-j]-H[i+1]) for j in range(i+1)]
    else:
        d = [c[i-j]+abs(H[i-j]-H[i+1]) for j in range(K)]
    c.appeNd(miN(d))
#print(c[-1])"""
H = [H[0]] * K + H
#print(H)
INF = 10**18
dp = [INF] *(K+N)
#print(dp)
dp[0]=0
#print(dp)
for i in range(K, N+K):
    h = H[i]
    costs = [x+abs(y-h) for x, y in zip(dp[i - K:i], H[i - K:i])]
    dp[i] = min(costs)
#print(dp)
print(dp[-1])
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
A.sort()
count=0
now=0
for i in range (N):
    counting=A.count(A[i])
    if A[i]==now:
        pass
    elif counting>A[i]:
        count+=A.count(A[i])-A[i]
    elif counting<A[i]:
        count+=counting
    now=A[i]
print(count)

### FIXED CODE ###

N=int(input())
list=list(map(int,input().split()))
while len(list) != 0and max(list) > len(list):
    list = [i for i in list if i < len(list)]
#list2 = [i for i in list if i < N]
set = set(list)
cnt=0
for i in set:
    c=list.count(i)
    if c > i:
        cnt +=c-i
    elif c < i:
        cnt +=c
print(N-len(list)+cnt)

##できなかった．
### BUGGY CODE ###


from operator import mul
from functools import reduce
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    limit = 10**18

    _prod = reduce(mul, A)
    if _prod <= limit:
        print(_prod)
    else:
        print(-1)

if __name__ == "__main__":
    resolve()

### FIXED CODE ###


def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    limit = 10**18

    if 0 in A:
        print(0)
        return

    _mul = 1
    for a in A:
        _mul *= a
        if _mul > limit:
            print(-1)
            return

    print(_mul)

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

from sys import exit
N = int(input())
A = list(map(int, input().split()))
if N % 2 == 0:
    for i in range(N // 2):
        if A.count(i * 2 + 1) != 2:
            print(0)
            exit()
        else:
            continue
    print(int(2 ** (N / 2)) % (10 ** 9 + 7))
else:
    n = 0
    if A.count(0) != 1:
        print(0)
        exit()
    else:
        for i in range(N // 2):
            if A.count(i * 2 + 2) != 2:
                print(0)
                exit()
            else:
                continue
    print(int(2 ** (N // 2)) % (10 ** 9 + 7))
  

### FIXED CODE ###

N = int(input())
a = list(map(int, input().split()))
a_comb = []
mod = 10 ** 9 + 7

for i in range(N):
    a_comb.append(abs(i - (N - 1 - i)))

if sorted(a_comb) == sorted(a):
    print(2 ** (N // 2) % mod)
else:
    print(0)
### BUGGY CODE ###

N, W = map(int, input().split())
wv = []
for _ in range(N):
  wv += list(map(int, input().split()))
dp = [[0 for i in range(W+1)] for j in range(N+1)]
for i in range(N):
  for w in range(W+1):  
    if w-wv[2*i] >= 0:
      dp[i+1][w] = max(dp[i+1][w], dp[i][w-wv[2*i]]+wv[2*i+1])
    dp[i+1][w] = max(dp[i+1][w], dp[i][w])
print(dp[N][W])

### FIXED CODE ###

N, W = map(int, input().split())
wv = []
for _ in range(N):
  wv += list(map(int, input().split()))
MAX_N, MAX_V = 110, 100100
dp = [[10e+09 for i in range(MAX_V)] for j in range(MAX_N)]
dp[0][0] = 0
for i in range(N):
  for v in range(MAX_V):  
    if v-wv[2*i+1] >= 0:
      dp[i+1][v] = min(dp[i+1][v], dp[i][v-wv[2*i+1]]+wv[2*i])
    dp[i+1][v] = min(dp[i+1][v], dp[i][v])
for v in range(MAX_V):
  if dp[N][v] <= W: res = v
print(res)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

trees =  [ [] for i in range(n+1) ]
for i in range(n-1):
    a, b = map(int, input().split())
    trees[a].append(b)
    trees[b].append(a)

values = [ 0 for i in range(n+1) ]
for i in range(q):
    p, x = map(int, input().split())
    values[p] += x
#print(values)

def dfs2(i, parent):
    for j in trees[i]:
        if j == parent: continue
        values[j] += values[i]
        dfs2(j, i)

dfs2(1, -1)
print(*values[1:])

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())
 
ki = [[] for i in range(n + 1)]
for i in range(n - 1):
    a, b = map(int, input().split())
    ki[a].append(b)
    ki[b].append(a)
# print(ki)
 
cnt = [0] * (n + 1)
for i in range(q):
    p, x = map(int, input().split())
    cnt[p] += x
 
 
# print(cnt)
 
 
def d(y, parent):
    for i in ki[y]:
        if i == parent:
            continue
        cnt[i] += cnt[y]
        d(i, y)
 
 
d(1, -1)
print(*cnt[1:])
### BUGGY CODE ###

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

ans = 0
T = min(N,K)
for l in range(1,T+1):
    f = sum(A[:l])
    if f % K == l:
        ans += 1
    for j in range(l,N):
        f = f - A[j-l] + A[j]
        if f % K == l:
            ans += 1
print(ans)



### FIXED CODE ###

import numpy as np
from collections import defaultdict,deque
N,K= map(int,input().split())
A = np.array([1]+list(map(int,input().split())))-1

Acs = np.cumsum(A)
Acs %= K

ans = 0
cnt = defaultdict(deque)
for i,c in enumerate(Acs):
    cnt[c].append(i)
    while cnt[c]:
        if i-cnt[c][0]> K-1:
            cnt[c].popleft()
        else:
            break
    ans += len(cnt[c]) - 1
print(ans)
### BUGGY CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=1000000007
dp=[[0]*(k+1) for i in range(n+1)] 
dp[0][0]=1
for i in range(n):
  sm=0
  for j in range(k+1):
    sm+=dp[i][j]
    sm%=mod
    dp[i+1][j]=sm
    if j>=a[i]:
      sm-=dp[i][j-a[i]]
      sm=(sm+mod)%mod
print(dp[n][k])

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
mod=1000000007
dp=[[0]*(k+1) for i in range(n+1)] 
dp[0][0]=1
for i in range(n):
  sm=0
  for j in range(k+1):
    sm+=dp[i][j]
    sm%=mod
    dp[i+1][j]=sm
    if j>=a[i]:
      sm-=dp[i][j-a[i]]
      sm=(sm+mod)%mod
print(dp[n][k])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N, K = map(int,input().split())
h = list(map(int,input().split()))
dp = [float("inf")]*(N)
dp[0] = 0

for i in range(1,N):
    for j in range(max(i-K,0),i):
        dp[i] = min(abs(h[i]-h[j])+dp[j],dp[i])

print(dp[N-1])


### FIXED CODE ###

import sys
input = sys.stdin.readline
N, K = map(int,input().split())
h = list(map(int,input().split()))
dp = [0]*(N)

for i in range(1,N):
    now = h[i]
    dp[i] = min([abs(now-h[j])+dp[j] for j in range(max(i-K,0),i)])

print(dp[N-1])