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

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

dp = [1e9 for _ in range(n)]
dp[0] = 0
for i in range(1, n):
  if(i < k):
    for j in range(i):
      dp[i] = min(dp[0] + abs(h[i] - h[0]), dp[i])
  else:
    for j in range(k):
      dp[i] = min(dp[i+j-k] + abs(h[i] - h[i+j-k]), dp[i])
print(dp[n-1])

### FIXED CODE ###

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

dp = [1e9 for _ in range(n)]
dp[0] = 0
for i in range(1, n):
  if(i < k):
    for j in range(i):
      dp[i] = min(dp[0] + abs(h[i] - h[0]), dp[i])
  else:
    for j in range(k):
      dp[i] = min(dp[i+j-k] + abs(h[i] - h[i+j-k]), dp[i])
print(dp[n-1])
### BUGGY CODE ###

import sys
s = input()[::-1]
mod = 10**9 + 7
dp = [0 for i in range(13)]

if s[0] == '?':
    for i in range(10):
        dp[i] += 1
else:
    dp[int(s[0])] += 1

if len(s) == 1:
    print(dp[5])
    sys.exit()

for i, x in enumerate(s[1:], start=1):
    nextdp = [0 for j in range(13)]
    if x != '?':
        a = int(x) * pow(10, i, 13)
        for k in range(13):
            nextdp[(k + a)%13] += dp[k]%mod
            nextdp[(k + a)%13] %= mod
    else:
        for y in range(10):
            a = y * pow(10, i, 13)
            for k in range(13):
                nextdp[(k + a)%13] += dp[k]%mod
                nextdp[(k + a)%13] %= mod
    dp = nextdp
print(dp[5])

### FIXED CODE ###

import sys
s = input()[::-1]
mod = 10**9 + 7
dp = [0 for i in range(13)]

if s[0] == '?':
    for i in range(10):
        dp[i] += 1
else:
    dp[int(s[0])] += 1

if len(s) == 1:
    print(dp[5])
    sys.exit()

for i, x in enumerate(s[1:], start=1):
    nextdp = [0 for j in range(13)]
    if x != '?':
        a = int(x) * pow(10, i, 13)
        for k in range(13):
            nextdp[(k + a)%13] += dp[k]%mod
            nextdp[(k + a)%13] %= mod
    else:
        for y in range(10):
            a = y * pow(10, i, 13)
            for k in range(13):
                nextdp[(k + a)%13] += dp[k]%mod
                nextdp[(k + a)%13] %= mod
    dp = nextdp
print(dp[5])
### BUGGY CODE ###

# -*- coding: utf-8 -*-

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

B = []
C = []
for _ in range(Q):
    B_i, C_i = list(map(int, input().split()))
    
    B.append(B_i)
    C.append(C_i)

for i in range(Q):
    A = [A[j] if A[j] != B[i] else C[i] for j in range(len(A))]
    print(sum(A))


### FIXED CODE ###

# -*- coding: utf-8 -*-

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

B = []
C = []
for _ in range(Q):
    B_i, C_i = map(int, input().split())
    
    B.append(B_i)
    C.append(C_i)

max_i = max(max(A), max(C))

A_N = [0 for _ in range(max_i + 1)]
sum_i = 0

for a in A:
    A_N[a] += a
    sum_i += a

for i in range(Q):
    diff_replace = int(A_N[B[i]] / B[i]) * C[i]
    sum_i += diff_replace - A_N[B[i]]
    
    A_N[C[i]] += diff_replace
    A_N[B[i]] = 0
    
    print(sum_i)
### BUGGY CODE ###

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop

sys.setrecursionlimit(10 ** 7)

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

E = []
for _ in range(N):
    x, h = map(int, input().split())
    E.append((x, h))


E = sorted(E, key=lambda x: x[0])

damage = 0
BOMB = defaultdict(int)
x = 0
ans = 0
for i in range(N):
    x, h = E[i]

    dlt = []
    for xd, d in BOMB.items():
        if x > xd:
            damage -= d
            dlt.append(xd)
    for dl in dlt:
        BOMB.pop(dl)

    h -= damage

    if h <= 0:
        continue
    xm = x + 2 * D
    cnt = -1 * (-h // A)
    damage += cnt * A
    BOMB[xm] = cnt * A
    ans += cnt

print(ans)


### FIXED CODE ###

import sys
from collections import defaultdict

sys.setrecursionlimit(10 ** 7)

N, D, A = map(int, input().split())
E = []
for _ in range(N):
    x, h = map(int, input().split())
    E.append((x, h))
    E.append((x+2*D, 0))

E = sorted(E, key=lambda x: (x[0], -x[1]))

for i in range(len(E)-1, -1, -1):
    x, h = E[i]
    if h != 0:
        xmax = x
        break

bomb = defaultdict(int)
damage = 0
ans = 0
for i in range(len(E)):
    x, h = E[i]

    if x > xmax:
        break

    # x+2D: damageをマイナス
    if h == 0:
        damage -= bomb[x]
        continue

    h -= damage

    if h <= 0:
        continue

    x = x + 2 * D
    cnt = -1 * (-h // A)
    damage += cnt * A
    bomb[x] = cnt * A
    ans += cnt

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

s = input()
K = int(input())
sSet = set()
for i in range(len(s)):
    for j in range(len(s)-i):
        sSet.add(s[i : i+j+1])
list = list(sSet)
list.sort()
print(list[K - 1])


### FIXED CODE ###

s = input()
K = int(input())
list = ["zzz" for i in range(K)]
for i in range(len(s)):
    for j in range(len(s)-i):
        if j > 5:
            continue
        if len(list) < K or s[i : i+j+1] < list[K-1]:
            if not s[i : i+j+1] in list:
                list[K-1] = s[i : i+j+1]
                list.sort()
print(list[K - 1])
### BUGGY CODE ###

N = int(input())
S = input()
S = list(S)
count = 0
for i in range(N):
  for j in range(i+1, N):
    for k in range(j+1, N):
      if j-i == k-j:
          continue
      if S[i] != S[j] and S[j] != S[k] and S[i] != S[k]:
        count += 1
print(count)

### FIXED CODE ###

n=int(input());s=[*input()];count=0
count=s.count('R')*s.count('G')*s.count('B')
for i in range(n+1):
  for j in range(i,n+1):
    k=2*j-i
    if k<n:
      if s[i]!=s[j] and s[j]!=s[k] and s[i]!=s[k]:
        count-=1
print(count)
### BUGGY CODE ###

N = int(input())
l = sorted([int(item) for item in input().split(' ')])
count = 0

for i in range(N):
    for j in range(i + 1, N):
        for k in range(j + 1, N):
            if l[i] + l[j] > l[k]:
                count += 1
            else:
                break

print(count)


### FIXED CODE ###

import bisect
n = int(input())
l = sorted(map(int, input().split()))

ans = 0
for i in range(n):
    for j in range(i+1, n):
        end = bisect.bisect_left(l, l[i]+l[j])
        ans += end - j - 1
print(ans)
### BUGGY CODE ###

s=input()
si=len(s)//2+1
ns0=len(s)
ssum=0
for i in range(si):
    nsi=len(s)
#    s=s.replace("01","")
#    s=s.replace("10","")
    s=s.replace("01","").replace("10","")
    if nsi==len(s):
        break
print(ns0-nsi)


### FIXED CODE ###

s=input()
nall=len(s)
nzero=s.count("0")
none=nall-nzero
if nall==0:
    print(0)
elif none<=nzero:
    print(none*2)
else:
    print(nzero*2)
### BUGGY CODE ###


class Solve:
    def __init__(self):
        self.N, self.K = [int(i) for i in input().split()]
        self.W = [int(input()) for i in range(self.N)]

        self.a = max(self.W)-1  # >= 0; False
        self.b = sum(self.W)  # <= 10**9; True

    def check(self, P):
        k, p, W = 1, P, [w for w in self.W]
        while W:
            if p >= W[0]:
                p -= W.pop(0)
            elif k < self.K:
                k, p = k+1, P
            else:
                return False
        return True

    def solve(self):
        while self.b - self.a > 1:
            m = (self.a + self.b) // 2
            if self.check(m):
                self.b = m
            else:
                self.a = m
        return self.b

print(Solve().solve())

### FIXED CODE ###


class Solve:
    def __init__(self):
        self.N, self.K = [int(i) for i in input().split()]
        self.W = [int(input()) for i in range(self.N)]

        self.a = max(self.W)-1  # >= 0; False
        self.b = sum(self.W)  # <= 10**9; True

    def check(self, P):
        k, p, W = 1, P, [w for w in self.W]
        for w in W:
            if p >= w:
                p -= w
            elif P >= w and k < self.K:
                k, p = k+1, P-w
            else:
                return False
        return True

    def solve(self):
        while self.b - self.a > 1:
            m = (self.a + self.b) // 2
            if self.check(m):
                self.b = m
            else:
                self.a = m
        return self.b

print(Solve().solve())
### BUGGY CODE ###

n = input()
m = [0] * n
max = [0] * n
kijun = [0] * n
rieki = [0] * n
max_rieki = 0


for i in xrange(n):
 m[i] = input()
 
 
for i in xrange(n):
 if i != n-1:    
  max[i] = m[i+1] 
  kijun[i] = m[i]
  for j in xrange(i+1,n): 
   if max[i] < m[j]:
      max[i] = m[j]
  rieki[i] = max[i] - kijun[i]
  if i == 0:
   max_rieki = rieki[0]
  if rieki[i] > max_rieki:
   max_rieki = rieki[i]

print max_rieki

### FIXED CODE ###

n = input()
m = [0] * n
max_rieki = -1000000000



for i in xrange(n):
 m[i] = input()

min_rieki = m[0]
for i in xrange(1,n): 
  if max_rieki < m[i] - min_rieki:
     max_rieki = m[i] - min_rieki
  if min_rieki > m[i]:
     min_rieki = m[i]

print max_rieki
### BUGGY CODE ###

h,w = map(int,input().split())
sy, sx = map(int,input().split())
start = (sy-1, sx-1)
gy, gx = (map(int,input().split()))
goal = (gy-1, gx-1)

yukaset = set()

for i in range(h):
	chizu = input()
	for j in range(w):
		if chizu[j] == ".":
			yukaset.add((i, j))

settansaku = set()
stock = []
setmada = {start}
mode = 1
cost = 0

while len(setmada) > 0:
	while len(setmada) > 0:
		for i, j in list(setmada):
			settansaku.add((i, j))
			stock.append((i, j))
			setmada.remove((i, j))
			for k in [(i+1, j), (i-1, j), (i, j-1), (i, j+1)]:
				if k in yukaset:
					if k not in settansaku:
						setmada.add(k)
						
	if goal in settansaku:
		mode = 0
		break
	
	cost += 1
	for i, j in stock:
		for k in [(i-2, j-2), (i-1, j-2), (i, j-2), (i+1, j-2), (i+2, j-2), (i-2, j-1), (i-1, j-1), (i, j-1), (i+1, j-1), (i+2, j-1), (i-2, j), (i-1, j), (i+1, j), (i+2, j), (i-2, j+1), (i-1, j+1), (i, j+1), (i+1, j+1), (i+2, j+1), (i-2, j+2), (i-1, j+2), (i, j+2), (i+1, j+2), (i+2, j+2)]:
			if k in yukaset:
				if k not in settansaku:
					setmada.add(k)
	
	stock = []
	
	if goal in settansaku:
		mode = 0
		break

if mode == 0:
	print(cost)
else:
	print(-1)

### FIXED CODE ###

h,w = map(int,input().split())
sy, sx = map(int,input().split())
start = (sy-1, sx-1)
gy, gx = (map(int,input().split()))
goal = (gy-1, gx-1)

yukaset = set()

for i in range(h):
	chizu = input()
	for j in range(w):
		if chizu[j] == ".":
			yukaset.add((i, j))

settansaku = {start}
stock = []
listmada = [start]
mode = 1
cost = 0

while len(listmada) > 0:
	for i, j in listmada:
		stock.append((i, j))
		for k in [(i+1, j), (i-1, j), (i, j-1), (i, j+1)]:
			if k in yukaset:
				if k not in settansaku:
					listmada.append(k)
					settansaku.add(k)

	if goal in settansaku:
		mode = 0
		break
	
	listmada = []
	cost += 1
	for i, j in stock:
		for k in [(i-2, j-2), (i-1, j-2), (i, j-2), (i+1, j-2), (i+2, j-2), (i-2, j-1), (i-1, j-1), (i, j-1), (i+1, j-1), (i+2, j-1), (i-2, j), (i-1, j), (i+1, j), (i+2, j), (i-2, j+1), (i-1, j+1), (i, j+1), (i+1, j+1), (i+2, j+1), (i-2, j+2), (i-1, j+2), (i, j+2), (i+1, j+2), (i+2, j+2)]:
			if k in yukaset:
				if k not in settansaku:
					listmada.append(k)
					settansaku.add(k)
	
	stock = []

if mode == 0:
	print(cost)
else:
	print(-1)
### BUGGY CODE ###

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

ys = [0] * (max(a)+ 1)

for x in range(len(ys)):
    y = a.count(x-1) + a.count(x) + a.count(x+1)
    ys[x] = y

print(max(ys))

### FIXED CODE ###

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

d = {}

for i in range(max(a)+1):
  d[i] = 0

for a_ in a:
  d[a_] += 1

d[-1] = 0
d[max(a)+1] = 0

ans = 0

for x in range(max(a)+1):
  if x-1 in d:
    ans = max(ans, d[x-1] + d[x] + d[x+1])

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

K = int(input())
total = 0

def gcd(a, b):
    if(a > b):
        a, b = b, a

    while(b > 0):
        r = a % b
        a = b
        b = r

    return a


for a in range(1, K+1):
    for b in range(1, K+1):
        for c in range(1, K+1):
           total += gcd(a, gcd(b, c))


print(total)

### FIXED CODE ###

K = int(input())
total = 0

def gcd(a, b):
    if(a > b):
        a, b = b, a

    while(b > 0):
        r = a % b
        a = b
        b = r

    return a

dp = [[0 for i in range(K+1)] for j in range(K+1)]

for a in range(1, K+1):
    for b in range(1, K+1):
        if(dp[a][b] == 0):
            dp[a][b] = gcd(a, b)

        res = dp[a][b]

        for c in range(1, K+1):
            if(dp[res][c] == 0):
                dp[res][c] = gcd(res, c)

            total += dp[res][c]


print(total)
### BUGGY CODE ###

def main():
    n = int(input())
    A = list(map(int, input().split()))
    b = []

    for a in A:
        b.append(a)
        b.reverse()
    print(*b)

main()

### FIXED CODE ###

from collections import deque
def main():
    n = int(input())
    a = [0] + list(map(int, input().split()))
    b = deque(maxlen=n+1)
    for i in range(1,n+1):
        if (i % 2 == 0 and n % 2 == 0 ) or (i % 2 != 0 and n % 2 != 0 ):
            b.appendleft(a[i])
        else:
            b.append(a[i])

    print(*b)

main()
### BUGGY CODE ###

def solve(n, k, H):
    dp = [0] * n
    dp[0] = 0
    for i in range(1, n):
        dp[i] = min(dp[i - j - 1] + abs(H[i] - H[i - j - 1]) for j in range(min(k, i)))
    return dp[n - 1]

_n, _k = map(int, input().split())
_H = list(map(int, input().split()))
print(solve(_n, _k, _H))


### FIXED CODE ###

n, k = map(int, input().split())
H = list(map(int, input().split()))
dp = [0] * n
for i in range(1, n):
    dp[i] = min(dp[i - j - 1] + abs(H[i] - H[i - j - 1]) for j in range(min(k, i)))

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

N, M = map(int, input().split())
ans = 1
if N == 1:
	ans = M

else:
	for i in range(2,int(M/N)+1):
		if M%i == 0:
			ans = i
		else:
			pass
print(ans)


### FIXED CODE ###

N, M = map(int, input().split())
d = []
for i in range(1, int(M**0.5)+1):
	if M%i == 0:
		d.append(i)
		d.append(M//i)
d.sort()
for i in range(1,len(d)+1):
	if d[-i] <= M//N:
		print(d[-i])
		exit()
### BUGGY CODE ###

from functools import reduce

def gcd(x, y):
 
    if x > y:
        small = y
    else:
        small = x
    for i in range(1, small+1):
        if((x % i == 0) and (y % i == 0)):
            gcd = i
             
    return gcd
 
def lcm_base(x, y):
    return (x * y) // gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

    
def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    v = lcm(*a) -1
    ans = sum(v % x for x in a)
    print(ans)
    
        
if __name__ == '__main__':
    main()


### FIXED CODE ###

from functools import reduce

import fractions

def gcd(x, y):
 
    if x > y:
        small = y
    else:
        small = x
    for i in range(1, small+1):
        if((x % i == 0) and (y % i == 0)):
            gcd = i
             
    return gcd
 
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

    
def main():
    n = int(input())
    a = [int(x) for x in input().split()]

    v = lcm(*a) -1
    ans = sum(v % x for x in a)
    print(ans)
    
        
if __name__ == '__main__':
    main()
### BUGGY CODE ###

N,K = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
if a[0] >= K:
    print(0)
    exit()

a = [a[i] for i in range(N) if a[i]<K]
a.reverse()
N = len(a)
ans = N
dp = [False]*K
dp[0] = True
Smax = 0
for i,a_ in enumerate(a):
    if Smax + a_ >= K:
        ans = N-i-1
    updated = True
    for j in range(min(Smax,K-a_-1),-1,-1):
        if dp[j]:
            dp[j+a_]=True
            if updated:
                Smax = max(Smax,j+a_)
                updated = True

print(ans)

### FIXED CODE ###

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

def f(N,K,a):
    a.sort()
    if a[0] >= K:
        print(0)
        exit()

    a = [a[i] for i in range(N) if a[i]<K]
    N = len(a)
    ans = N
    dp = [False]*K
    dp[0] = True
    Smax = 0
    for i,a_ in reversed(list(enumerate(a))):
        if Smax + a_ >= K:
            ans = i
        updated = True
        for j in range(min(Smax,K-a_-1),-1,-1):
            if dp[j]:
                dp[j+a_]=True
                if updated:
                    Smax = max(Smax,j+a_)
                    updated = False
    return ans

print(f(N,K,a))
### BUGGY CODE ###

N=int(input())
A=list(map(int,input().split()))
ls=[0 for _ in range(max(A))]
for i in range(N):
  ls[A[i]-1]+=1
lsk=ls
for k in range(N):
  lsk=ls
  lsk[A[k]-1]-=1
  s=0
  for j in range(len(ls)):
    m=lsk[j]*(lsk[j]-1)//2
    s+=m
  lsk[A[k]-1]+=1
  print(s)

### FIXED CODE ###

N=int(input())
A=list(map(int,input().split()))
ls=[0 for _ in range(N)]
for i in range(N):
  ls[A[i]-1]+=1
s=0
for i in range(len(ls)):
    m=ls[i]*(ls[i]-1)//2
    s+=m
for j in range(N):
  print(s-(ls[A[j]-1]-1))
### BUGGY CODE ###

def main():
    N = int(input())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    ans_list = []
    i_cycle = 1
    manage_dict = {}
    for n_cycle in range(1,N+1) :
        ans_list.append(A_list.count(n_cycle))
    for ans in ans_list :
        print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    from collections import Counter
    N = int(input())
    A_list = [A for A in input().split()]
    A_list = list(map(int, A_list))
    #ans_list = []
    ans = Counter(A_list)

    for n_cycle in range(N) :
        n_cycle = n_cycle + 1
        #ans_list.append(A_list.count(n_cycle))
        print(ans[n_cycle])

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

N, M = [int(i) for i in input().split()]
H = [int(i) for i in input().split()]

G = [[0] * N for i in range(N)]
#print(G)

for i in range(M):
  a, b = [int(j) for j in input().split()]
  #print(a, b)
  G[a-1][b-1] = 1
  G[b-1][a-1] = 1

#for g in G:
#  print(g)
NG = []  
for ixa, g in enumerate(G):
  #print(g)
  ha = H[ixa]
  for ixb, gg in enumerate(g):
    if gg == 0:
      continue    
    hb = H[ixb]
    if ha <= hb:
      if ixa not in NG:
        NG.append(ixa)
        break
#print(NG)    
print(N - len(NG))

### FIXED CODE ###

import heapq

N, M = [int(i) for i in input().split()]
H = [int(i) for i in input().split()]

NG = []

for i in range(M):
  a, b = [int(j) for j in input().split()]
  if H[a-1] < H[b-1]:
    NG.append(a)
  elif H[a-1] > H[b-1]:
    NG.append(b)
  else:
    NG.append(a)
    NG.append(b)
NG = set(NG)
#print(NG)
print(N - len(NG))
### BUGGY CODE ###

N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    def solve25(N, P, S):
        c = 0
        for i in range(N-1, -1, -1):
            if int(S[i]) % P == 0:
                # 以降桁を増やしてもOK
                c += i+1
        return c
    print(solve25(N, P, S))
else:
    def solve(N, P, S):
        rcounts = [0] * P
        rcounts[0] = 1 # sそのものが割り切れた場合に引き算する相手がいないから
        ints = 0
        for i, s in enumerate(S[::-1]):
            ints += int(s) * pow(10, i)
            r = ints % P
            rcounts[r] += 1
        c = 0
        for v in rcounts:
            c += v*(v-1)//2
        return c
    print(solve(N, P, S))

### FIXED CODE ###

N, P = map(int, input().split())
S = input()

if P == 2 or P == 5:
    def solve25(N, P, S):
        c = 0
        for i in range(N-1, -1, -1):
            if int(S[i]) % P == 0:
                # 以降桁を増やしてもOK
                c += i+1
        return c
    print(solve25(N, P, S))
else:
    def solve(N, P, S):
        rcounts = [0] * P
        rcounts[0] = 1 # sそのものが割り切れた場合に引き算する相手がいないから
        ints = 0
        for i, s in enumerate(S[::-1]):
            ints += int(s) * pow(10, i, P)
            r = ints % P
            rcounts[r] += 1
        c = 0
        for v in rcounts:
            c += v*(v-1)//2
        return c
    print(solve(N, P, S))
### BUGGY CODE ###

from collections import defaultdict
import sys
sys.setrecursionlimit(10**6+5)

class Node:
    def __init__(self):
        self.child = {}
        self.counts = defaultdict(int)

    def add(self, s, bucket, idx_s):
        c = s[idx_s]
        if c not in self.child:
            self.child[c] = Node()
        for i, cnt in enumerate(bucket):
            if cnt:
                self.counts[i] += 1
        bucket[c] -= 1
        if idx_s >= 1:
            self.child[c].add(s, bucket, idx_s-1)
        else:
            Leaves.append(self.counts)

    # def get(self, s, idx_s):
    #     c = s[idx_s]
    #     if idx_s > 0:
    #         return self.child[c].get(s, idx_s-1)
    #     else:
    #         return self.counts[c]

Leaves = []
trie = Node()
N = int(input())
S = [[ord(c)-97 for c in input()] for _ in range(N)]
for s in S:
    b = [0] * 26
    for c in s:
        b[c] += 1
    trie.add(s, b, len(s)-1)
ans = -N
# for s in S:
#     ans += trie.get(s, len(s)-1)
for s, l in zip(S, Leaves):
    ans += l[s[0]]
print(ans)


### FIXED CODE ###

class Node:
    def __init__(self):
        self.child = [None] * 26
        self.counts = [0] * 26

    # def add(self, s, bucket, idx_s):
    #     c = s[idx_s]
    #     if self.child[c] is None:
    #         self.child[c] = Node()
    #     for i, cnt in enumerate(bucket):
    #         if cnt:
    #             self.counts[i] += 1
    #     bucket[c] -= 1
    #     if idx_s >= 1:
    #         self.child[c].add(s, bucket, idx_s - 1)
    #     else:
    #         Leaves.append(self.counts)

    def add(self, s, bucket):
        node = self
        for idx_s, c in zip(range(len(s)-1, -1, -1), s[::-1]):
            if node.child[c] is None:
                node.child[c] = Node()
            for i, cnt in enumerate(bucket):
                if cnt:
                    node.counts[i] += 1
            bucket[c] -= 1
            if idx_s > 0:
                node = node.child[c]
        Leaves.append(node.counts)

    # def get(self, s, idx_s):
    #     c = s[idx_s]
    #     if idx_s > 0:
    #         return self.child[c].get(s, idx_s-1)
    #     else:
    #         return self.counts[c]

Leaves = []
trie = Node()
N = int(input())
S = [[ord(c)-97 for c in input()] for _ in range(N)]
for s in S:
    b = [0] * 26
    for c in s:
        b[c] += 1
    trie.add(s, b)
ans = -N
# for s in S:
#     ans += trie.get(s, len(s)-1)
for s, l in zip(S, Leaves):
    ans += l[s[0]]
print(ans)
### BUGGY CODE ###

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

l = len(A)
A.sort(reverse=True)

if N>=2:
  while M>=1:
    div = A[0]//2
    
    for i in range(l):
      if A[i]>=div:
        if M>=1:
          A[i] = A[i]//2
        else:
          break
      else:
        break
 
  print(sum(A))
else:
    i=1
    while 2**(i+1)<=A[0]:
      i+=1
    if M>=i:
      print(0)
    else:
      print(A[0]//2**M)

### FIXED CODE ###

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

l = len(A)

if N>=2:
  while M>=1:
    A.sort(reverse=True)
    div = A[0]//2
    
    for i in range(l):
      if A[i]>=div:
        if M>=1:
          A[i] = A[i]//2
          M -= 1
        else:
          break
      else:
        break
 
  print(sum(A))
else:
    i=1
    while 2**(i+1)<=A[0]:
      i+=1
    if M>=i:
      print(0)
    else:
      print(A[0]//2**M)
### BUGGY CODE ###

from collections import defaultdict

s = list(input())
n = len(s)
k = int(input())

part = defaultdict(set)
for i in range(n):
    for j in range(i, n):
        part[s[i]].add("".join(s[i:j+1]))

cnt = 0
for i in range(ord('a'), ord('z')+1):
    m = len(part[chr(i)])
    if cnt < k <= cnt + m:
        l = sorted(list(part[chr(i)]))
        print(l[k-cnt-1])
        exit()
    cnt += m


### FIXED CODE ###

s = list(input())
n = len(s)
k = int(input())

part = set()
for i in range(n):
    for j in range(k):
        part.add("".join(s[i:i+j+1]))

print(sorted(part)[k-1])
### BUGGY CODE ###

from math import factorial

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

for _ in range(n):
	rel = sorted(list(input()))
	string_list.append(''.join(rel))
    
string_distinct = set(string_list)
idx = 0
for item in string_distinct:
	s_count = string_list.count(item)
	if s_count > 1:
		idx += factorial(s_count) // factorial(2) // factorial(s_count - 2)
print(idx)

### FIXED CODE ###

N = int(input())

def sort_alpabetical(s):
	
	s_list = list(s)
	s_list.sort()
	return ''.join(s_list)

list_s = []
for n in range(N):
	list_s.append(input())

list_s = [sort_alpabetical(s) for s in list_s]

dict_s = {s : 0 for s in set(list_s)}

n_of_pairs = 0


for s in list_s:
	
	n_of_pairs += dict_s[s]
	dict_s[s] +=1
	
print(n_of_pairs)