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

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from functools import lru_cache
def resolve():
    n=int(input())
    A=[tuple(map(lambda x:int(x)-1,input().split())) for _ in range(n)]

    N=0
    V={}
    for i in range(n):
        for j in range(i+1,n):
            V[(i,j)]=N
            N+=1

    E=[[] for _ in range(N)] # N=n*(n-1)//2
    for i in range(n):
        for j in range(n-2):
            u=V[(min(i,A[i][j]),max(i,A[i][j]))]
            v=V[(min(i,A[i][j+1]),max(i,A[i][j+1]))]
            E[u].append(v)

    # longest path problem
    calculated=[0]*N
    visited=[0]*N

    @lru_cache(None)
    def dfs(v):
        visited[v]=1
        res=0
        for nv in E[v]:
            if((not calculated[nv]) and visited[nv]):
                print(-1)
                exit(0)
            res=max(res,dfs(nv)+1)
        calculated[v]=1
        return res

    print(max(dfs(v) for v in range(N))+1)
resolve()

### FIXED CODE ###

import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from itertools import product
    n=int(input())
    match=[[0]*n for _ in range(n)]
    V=0 # V=n(n-1)/2
    for i,j in product(range(n),repeat=2):
        if(i>=j): continue
        match[i][j]=V
        V+=1

    def toID(i,j):
        if(i>j): i,j=j,i
        return match[i][j]

    E=[[] for _ in range(V)]
    for i in range(n):
        A=list(map(lambda x:int(x)-1,input().split()))
        for j in range(1,n-1):
            E[toID(i,A[j-1])].append(toID(i,A[j]))
    # あとは頂点VのグラフをdfsしてLongest pathの長さを求める
    # -1: unchecked, -2: checked and uncalculated
    dp=[-1]*V
    flag=True
    def dfs(v):
        global flag
        dp[v]=-2
        length=0
        for nv in E[v]:
            if(dp[nv]==-2):
                print(-1)
                exit()
            if(dp[nv]==-1):
                dfs(nv)
            length=max(length,dp[nv]+1)
        dp[v]=length
    # calculate
    for i in range(V): dfs(i)
    print(max(dp)+1 if(flag) else -1)
resolve()
### BUGGY CODE ###

n = int(input())
a = list(int(input()) for _ in range(n))

for i in range(n):
    b = a.copy()
    int(b.pop(i))
    print(max(b))

### FIXED CODE ###

n = int(input())
a = list(int(input()) for _ in range(n))

max_list = sorted(a,reverse=True)[:2]
num = max_list[0]
num2 = max_list[1]

for i in range(n):
    if a[i] < num:
        print(num)
    else:
        print(num2)
### BUGGY CODE ###

n=int(input())
a= [int(input()) for i in range(n)] 

a.sort()
b=set(a)
ans=0
for i in b:
    if a.count(i)%2!=0 :ans+=1
print(ans)

### FIXED CODE ###

n=int(input())
a= [int(input()) for i in range(n)] 

a.sort()
ans=0
m=1

for i in range(1,n):
    if a[i-1] == a[i]:
        m+=1
    else:
        ans+=(m%2)
        m=1

ans+=(m%2)

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

def mergecount(A):
    cnt = 0
    n = len(A)
    if n>1:
        A1 = A[:n>>1]
        A2 = A[n>>1:]
        cnt += mergecount(A1)
        cnt += mergecount(A2)
        i1 = 0
        i2 = 0
        for i in range(n):
            if i2 == len(A2):
                A[i] = A1[i1]
                i1 += 1
            elif i1 == len(A1):
                A[i] = A2[i2]
                i2 += 1
            elif A1[i1] <= A2[i2]:
                A[i] = A1[i1]
                i1 += 1
            else:
                A[i] = A2[i2]
                i2 += 1
                cnt += n//2 - i1
    return cnt


n = int(input())
l = [int(_) for _ in input().split()]
p = (n*n+n)/2
q = 0
if p%2 == 0:
    q = p//2
else:
    q = p//2 + 1
l_set = list(set(l))
l_sorted = sorted(l_set)
ans = 0
l_t = [0 for _ in range(n)]
s = [0 for _ in range(n+1)]

low = 0
high = len(l_sorted) - 1


while low <= high:
    mid = (low + high) // 2
    m = l_sorted[mid]
    for j in range(n):
        if l[j] < m:
            l_t[j] = -1
        else:
            l_t[j] = 1
    for k in range(n):
        s[k+1] = s[k] + l_t[k]

    x = mergecount(s)
    if p-x >= q:
        low = mid+1
        ans = max(m,ans)
    else:
        high = mid-1
print(ans)


### FIXED CODE ###

def mergecount(A):
    cnt = 0
    n = len(A)
    if n>1:
        A1 = A[:n>>1]
        A2 = A[n>>1:]
        cnt += mergecount(A1)
        cnt += mergecount(A2)
        i1 = 0
        i2 = 0
        for i in range(n):
            if i2 == len(A2):
                A[i] = A1[i1]
                i1 += 1
            elif i1 == len(A1):
                A[i] = A2[i2]
                i2 += 1
            elif A1[i1] <= A2[i2]:
                A[i] = A1[i1]
                i1 += 1
            else:
                A[i] = A2[i2]
                i2 += 1
                cnt += n//2 - i1
    return cnt
 
 
n = int(input())
l = [int(_) for _ in input().split()]
p = (n*n+n)/2
q = 0
if p%2 == 0:
    q = p//2
else:
    q = p//2 + 1
l_set = list(set(l))
l_sorted = sorted(l_set)
ans = 0
l_t = [0 for _ in range(n)]
s = [0 for _ in range(n+1)]
 
low = 0
high = len(l_sorted) - 1
 
 
while low <= high:
    mid = (low + high) // 2
    m = l_sorted[mid]
    for j in range(n):
        if l[j] < m:
            l_t[j] = -1
        else:
            l_t[j] = 1
    for k in range(n):
        s[k+1] = s[k] + l_t[k]
 
    x = mergecount(s)
    if p-x >= q:
        low = mid+1
        ans = max(m,ans)
    else:
        high = mid-1
print(ans)
### BUGGY CODE ###

import numpy as np
n, p = map(int ,input().split())

m = int(np.floor(p**(1/n)))+1

for i in range(m,0,-1):
    if p%(i**n)==0:
        print(i)
        exit()

### FIXED CODE ###

import numpy as np
n, p = map(int ,input().split())

def prime_decomposition(n):
    i = 2
    n0 = n
    primes = {}
    while i**2<n0:
        #print(i)
        if n%i==0:
            primes[i] = 0
        while n%i==0:
            n=n//i
            primes[i] += 1
        i += 1
    if n!=1:
        primes[n] = 1
    return primes

primes = prime_decomposition(p)
#print(primes)

ans = 1
for prime in primes:
    ans *= prime**(primes[prime]//n)

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

N = int(input())
a = list(map(int,input().split()))
ans = a[0]
for i in range(1,N):
    ans *= a[i]
if ans > 10**18:
    print(-1)
else:
    print(ans)


### FIXED CODE ###

N = int(input())
A = list(map(int,input().split()))
A.sort()
A.reverse()
if A.count(0) > 0:
    print(0)
else:
    ans = A[0]
    flag = 0
    for i in range(1,N):
        ans *= A[i]
        if ans > 10**18:
            flag += 1
            print(-1)
            break
    if flag == 0:
        print(ans)
### BUGGY CODE ###

from collections import defaultdict
import math

N = int(input())
A = list(map(int, input().split()))
d = defaultdict(lambda: 0)
for i in range(len(A)):
    d[A[i]] += 1

r = 2
total = 0
calc_dict = {}
for k, v in zip(d.keys(), d.values()):
    if v > 1:
        ans1 = math.factorial(v) // (math.factorial(v - r) * math.factorial(r))
    else:
        ans1 = 0
    if v > 2:
        ans2 = math.factorial(v - 1) // (math.factorial(v - 1 - r) * math.factorial(r))
    else:
        ans2 = 0
    total += ans1
    calc_dict[k] = ans1 - ans2
for i in range(N):
    print(total - calc_dict[A[i]])


### FIXED CODE ###

from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
d = defaultdict(lambda: 0)
for i in range(len(A)):
    d[A[i]] += 1
total = 0
for k,v in d.items():
    total += v*(v-1)//2
for a in A:
    print(total - (d[a]-1))
### BUGGY CODE ###

n = int(input())
ans = float('inf')

for i in range(1, 10**8+1):
    q, m = divmod(n, i)
    if m == 0:
        ans = min(ans, q+i)

print(ans-2)


### FIXED CODE ###

n = int(input())


def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors


r = make_divisors(n)

if len(r) % 2 == 1:
    a = r[len(r)//2]
    print((a-1)+(a-1))
else:
    a = int(len(r)/2)
    print((r[a-1]-1)+(r[a]-1))
### BUGGY CODE ###

import numpy as np

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

magic = np.zeros((N+1, 2), dtype=int)

for i in range(1, N+1):
    magic[i][0], magic[i][1] = map(int, input().split())

# print(magic)

INF = 110000000  # 10^4 * 10^4 + spare

#dp = np.full((N+1, H*2+1), INF)
#dp[0][0] = 0

dp = np.full(H+1, INF, dtype=int)
dp[0] = 0

for i in range(1, N+1):
    for j in range(H + 1):
        # 貰うDP
        if j-magic[i][0] >= 0:
            dp[j] = min(dp[j - magic[i][0]] + magic[i][1], dp[j])
        else:
            dp[j] = min(dp[0] + magic[i][1], dp[j])

#        print(dp[i])
#    print(dp)

print(dp[H])


### FIXED CODE ###

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

A = [0] * (N+1)
B = [0] * (N+1)
#magic = np.zeros((N+1, 2), dtype=int)

for i in range(1, N+1):
    A[i], B[i] = map(int, input().split())

# print(magic)
# print(A)
# print(B)

INF = 100000010  # 10^4 * 10^4 + spare

#dp = np.full((N+1, H*2+1), INF)
#dp[0][0] = 0

dp = [INF]*(H+1)
#dp = np.full(H+1, INF, dtype=int)
dp[0] = 0

for i in range(1, N+1):
    for j in range(H + 1):
        # 貰うDP
        if j-A[i] >= 0:
            dp[j] = min(dp[j - A[i]] + B[i], dp[j])
        else:
            dp[j] = min(dp[0] + B[i], dp[j])

#        print(dp[i])
#    print(dp)

print(dp[H])
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 9)
# 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 MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): 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, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate

def solve():
    N, K = MI()
    A = LI()
    for i in range(K):
        B = [0] * N
        for idx, a in enumerate(A):
            l = max(0, idx - a)
            r = idx + a + 1
            # print(idx, a, l, r)
            B[l] += 1
            if N > r:
                B[r] -= 1
        # print(B)
        B = list(accumulate(B))
        # print(B)
        if all([j == N for j in B]):
            printlist(B)
            return
        A = B
    printlist(A)

if __name__ == '__main__':
    solve()


### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 9)
# 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 MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): 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, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate

def solve():
    N, K = MI()
    A = LI()
    for i in range(K):
        B = [0] * N
        for idx, a in enumerate(A):
            l = max(0, idx - a)
            r = idx + a + 1
            # print(idx, a, l, r)
            B[l] += 1
            if N > r:
                B[r] -= 1
        # print(B)
        B = list(accumulate(B))
        # print(B)
        if all([j == N for j in B]):
            printlist(B)
            return
        A = B
    printlist(A)



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

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

c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += sum(abs(l[i]-l[j]) < x < l[i]+l[j] for x in l[j+1:])

print(c)


### FIXED CODE ###

from bisect import bisect_left
 
n = int(input())
l = list(map(int, input().split()))
l.sort()
 
c = 0
for i in range(n-2):
  for j in range(i+1, n-1):
    c += bisect_left(l, l[i]+l[j]) - j - 1
 
print(c)
### BUGGY CODE ###

import sys

H, W, M = map(int,input().split())

X = [0] * H
Y = [0] * W

A = []
m = M
while m:
    m -= 1
    x, y = map(int,input().split())
    x, y = x -1, y -1
    A.append((x, y))
    X[x] += 1
    Y[y] += 1
    
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

xm = max(X)
ym = max(Y)
xms = my_index_multi(X, xm)
yms = my_index_multi(Y, ym)

ans = xm + ym - 1

bl = False
for i in xms:
    for j in yms:
        if not (i, j) in A:
            print(ans + 1)
            sys.exit()
            bl = True
            
print(ans)

### FIXED CODE ###

import sys

input = sys.stdin.readline
H, W, M = map(int,input().split())
q=set(tuple(map(lambda x :int(x)-1,input().split())) for _ in range(M))

X = [0] * H
Y = [0] * W

for i, j in q:
    X[i] += 1
    Y[j] += 1
    
def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

xm = max(X)
ym = max(Y)
xms = my_index_multi(X, xm)
yms = my_index_multi(Y, ym)

ans = xm + ym - 1

bl = False
for i in xms:
    for j in yms:
        if not (i, j) in q:
            print(ans + 1)
            sys.exit()
            bl = True
            
print(ans)
### BUGGY CODE ###


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

input = sys.stdin.readline

def slv():
    N, M, L = list(map(int, input().split()))
    g = [[sys.maxsize]*(N) for _ in range(N)]
    for i in range(N):
        g[i][i] = 0
    for _ in range(M):
        a, b, c = list(map(int, input().split()))
        if c <= L:
            g[a-1][b-1] = c
            g[b-1][a-1] = c
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                g[i][j] = min(g[i][j], g[i][k] + g[k][j])
    gg = [[sys.maxsize]*(N) for _ in range(N)]
    for u in range(N):
        for v in range(N):
            if g[u][v] <= L:
                gg[u][v] = 1
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                gg[i][j] = min(gg[i][j], gg[i][k] + gg[k][j])

    Q = int(input())
    for _ in range(Q):
        s, t = list(map(int, input().split()))
        c = gg[s-1][t-1]
        if c != sys.maxsize:
            print(c-1)
        else:
            print(-1)



def main():
    slv()
    # N, M, L = read_int_n()
    # ABC = [read_int_n() for _ in range(M)]
    # Q = read_int()
    # ST = [read_int_n() for _ in range(Q)]
    
    # (slv(N, M, L, ABC, Q, ST))


if __name__ == '__main__':
    main()


### FIXED CODE ###

def slove():
    import sys
    input = sys.stdin.readline
    n, m, l = list(map(int, input().rstrip('\n').split()))
    INF = 10 ** 11
    w = [[INF] * n for _ in range(n)]
    wl = [[INF] * n for _ in range(n)]

    for i in range(n):
        w[i][i] = 0

    for i in range(m):
        a, b, c = list(map(int, input().rstrip('\n').split()))
        if c <= l:
            w[a - 1][b - 1] = c
            w[b - 1][a - 1] = c

    for i in range(n):
        for j in range(n):
            for k in range(n):
                w[j][k] = min(w[j][k], w[j][i] + w[i][k])

    for i in range(n):
        for j in range(n):
            if w[i][j] <= l:
                wl[i][j] = 1
    for i in range(n):
        for j in range(n):
            for k in range(n):
                wl[j][k] = min(wl[j][k], wl[j][i] + wl[i][k])

    q = int(input().rstrip('\n'))
    for i in range(q):
        s, t = list(map(int, input().rstrip('\n').split()))
        if wl[s-1][t-1] == INF:
            print(-1)
        else:
            print(wl[s-1][t-1] - 1)


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

N = int(input())
from collections import deque


def p_f(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


hoge = p_f(N)
DP = deque()
DP.append(1)

if len(hoge) == 1:
    print(hoge[0]-1)
else:
    Min = 10**12

    for i in range(len(hoge)):
        for j in range(len(DP)):
            x = DP[j]*hoge[i]
            DP.append(x)
            if (x + (N // x) - 2) < Min:
                Min = (x + (N // x) - 2)

    print(Min)

### FIXED CODE ###

N = int(input())


def make_divisors(n):  # 任意の整数の約数をリストで返す関数
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    divisors.sort()
    return divisors


List = make_divisors(N)

if len(List) % 2 == 1:
    print(List[len(List) // 2] * 2 - 2)
else:
    print(List[len(List) // 2 - 1] + (List[len(List) // 2] - 2))
### BUGGY CODE ###

N = int(input())

input_1 = input()
input_2 = input_1.split()

for k in range(N):
    A = map(int, input_2)

    #?
    def half(num):
        return num / 2
    def double(num):
        return num * 2
    A = [half(x) for x in A]
    A = [double(x) for x in A]
    #?

    ans = 0

    A.remove(A[k])

    for l in range(N - 1):
        for m in range(N - 1):
            if A[l] == A[m]:
                ans += 1
        
        ans -= 1

    ans = ans / 2

    print(int(ans))

### FIXED CODE ###

#TLE
n = int(input())
a = input().split()
ans = 0
import collections
dic = collections.Counter(a)
for i in dic.values():
    ans += i * (i-1)
ans /= 2
for i in range(n):
    print(int(ans - dic[a[i]] + 1))
### BUGGY CODE ###

# Educational DP contest B frog_2

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

INF = 10**9
dp = [INF for _ in range(N)]
dp[0] = 0
dp[1] = abs(h[1] - h[0])

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

print(dp[-1])

### FIXED CODE ###

import sys
import numpy as np

input = sys.stdin.readline

N, K = map(int, input().split())
h = [int(i) for i in input().split()]

dp = np.zeros(N, dtype=int)
h = np.array(h)

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

print(dp[N-1])
### BUGGY CODE ###

import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N = int(readline())
    A = list(map(int, readline().split()))

    gcdnum = 1
    gcd = math.gcd

    def lcm(x, y):
        return (x * y) // gcd(x, y)
    for a in A:
        gcdnum = lcm(gcdnum, a)
    ans = 0
    for a in A:
        a = gcdnum // a
        ans += a
        ans %= mod

    print(ans)




if __name__ == '__main__':
    solve()

### FIXED CODE ###

import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N = int(readline())
    A = list(map(int, readline().split()))

    lcmnum = 1
    def gcd(a, b):
        if (a == 0):
            return b
        return gcd(b%a, a)

    def lcm(x, y):
        return (x * y) // gcd(x, y)
    for a in A:
        lcmnum = lcm(lcmnum, a)

    ans = 0
    for a in A:
        a = lcmnum // a
        ans += a
    ans %= mod

    print(ans)




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

s=list(input())
n=len(s)+1
a=[0]*n
x=[0]*n
y=[0]*n
ans=0

for i in range(0,n):
  c=i
  while i>0 and s[i-1]=="<":
    x[c]+=1
    i-=1
for j in range(0,n):
  d=j
  while j<n-1 and s[j]==">":
    y[d]+=1
    j+=1

for k in range(0,n):
  a[k]=max(x[k],y[k])
  
for l in range(0,n):
  ans+=a[l]

print(ans)  
    
    
  
  

### FIXED CODE ###

s=list(input())
n=len(s)+1
a=[0]*n
x=[0]*n
y=[0]*n

for i in range(1,n):
  if s[i-1]=="<":
    x[i]=x[i-1]+1
  else:
    x[i]=0
    
for j in range(n-2,-1,-1):
  if s[j]==">":
    y[j]=y[j+1]+1
  else:
    y[j]=0

for k in range(0,n):
  a[k]=max(x[k],y[k])
  
print(sum(a))
### BUGGY CODE ###

#!/usr/bin/env python3

def main():
    H, W, M = map(int, input().split())
    bomb = [list(map(int, input().split()))for i in range(M)]
    H_bomb = [0] * (H + 1)
    W_bomb = [0] * (W + 1)
    for b in bomb:
        H_bomb[b[0]] += 1
        W_bomb[b[1]] += 1
    ans = max(H_bomb) + max(W_bomb)
    H_bomb_index = [i for i, v in enumerate(H_bomb) if v == max(H_bomb)]
    W_bomb_index = [i for i, v in enumerate(W_bomb) if v == max(W_bomb)]
    x_point = []
    for h in H_bomb_index:
        for w in W_bomb_index:
            x_point.append([h, w])
    flag = True
    for x in x_point:
        if x not in bomb:
            flag = False
    if flag:
        ans -= 1
    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

#!/usr/bin/env python3

def main():
    H, W, M = map(int, input().split())
    bomb = [list(map(int, input().split()))for i in range(M)]
    H_bomb = [0] * (H + 1)
    W_bomb = [0] * (W + 1)
    for b in bomb:
        H_bomb[b[0]] += 1
        W_bomb[b[1]] += 1
    max_h, max_w = max(H_bomb), max(W_bomb)
    cnt = H_bomb.count(max_h) * W_bomb.count(max_w)
    for b in bomb:
        if H_bomb[b[0]] == max_h and W_bomb[b[1]] == max_w:
            cnt -= 1
    print(max_h + max_w + bool(cnt) - 1)


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

N = int(input())
answer = 0
for i in range(1,N+1):
    if N//i == N%i:
        answer += i
print(answer)

### FIXED CODE ###

import sys
def sample_code():
    n = int(input())
    if n == 1 or n == 2:
        print(0)
        sys.exit(0)
    # print('# 素因数')
    fct = factorize(n)
    # print(fct, num(fct))
    answer = 0
    # print('# 約数')
    for div in divisorize(fct):
        if num(div)-1 > n//num(div):
            answer += num(div) - 1
    print(answer)


def divisorize(fct):
    b, e = fct.pop()  # base, exponent
    pre_div = divisorize(fct) if fct else [[]]
    suf_div = [[(b, k)] for k in range(e + 1)]
    return [pre + suf for pre in pre_div for suf in suf_div]


def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


def num(fct):
    a = 1
    for base, exponent in fct:
        a = a * base**exponent
    return a


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

N, K = map(int, input().split())
H = [int(a) for a in input().split()]
C = [0] * N

for i in range(1, N):
    C[i] = 10**10
    for k in range(1, min(K, i)+1):
        C[i] = min(C[i], C[i-k]+abs(H[i]-H[i-k]))
print(C[N-1])

### FIXED CODE ###

N, K = map(int, input().split())
H = [int(a) for a in input().split()]
C = [0] * N

for i in range(1, N):
    C[i] = 10**10
    for k in range(1, min(K, i)+1):
        C[i] = min(C[i], C[i-k]+abs(H[i]-H[i-k]))
print(C[N-1])