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

import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

S = input()

count = 0
for times in range(1, 101):
    num = 2019 * times
    for i in range(len(S) - 3):
        for j in range(i + 3, len(S)):
            sub_S = int(S[i:j+1])
            if num == sub_S:
                count += 1

print(count)

### FIXED CODE ###

S = list(input())
N = len(S)
# N 文字の文字列 S から、長さ N+1 の ---累積和--- s[0], s[1], ..., s[N] を作る
# これを 2019 で割ったあまりで分類する (具体的には counter[ r ] := あまりが r のやつが何個あるか)
# 各 r ごとに、counter[r] 個から 2 個選ぶ方法の数を、足し上げる

counter = [0] * 2019
k = 10
for i in range(N + 1):
    if i == 0:
        sub = 0
    elif i == 1:
        sub = int(S[N - 1])
    else:
        sub += int(S[N - i]) * k
        k *= 10
        k %= 2019
    rest = sub % 2019
    counter[rest] += 1

ans = 0
for r in counter:
    ans += r * (r - 1) // 2
print(ans)
### BUGGY CODE ###

import math
 
n, m = [int(i) for i in input().split()]
a = [-1]
for mi in range(m):
    a.append(int(input()))
a.append(n+1)
 
ans = 1
for mi in range(1, m+2):
    if a[mi] == a[mi-1]:
        break
    elif a[mi] == a[mi-1] + 1:
        ans = 0
        break
    k = a[mi] - a[mi-1] - 2
    max_2 = k//2
    cnt = 0
    for i in range(max_2+1):
        cnt += math.factorial(k-i) // (math.factorial(k-2*i) * math.factorial(i)) % 1000000007
    ans = ans * cnt % 1000000007
    if ans == 0:
        break
 
print(ans)

### FIXED CODE ###

n, m = [int(i) for i in input().split()]

b = [0 for i in range(n+1)]

for mi in range(m):
    b[int(input())] = 1
cnt = 0
s = [0 for i in range(n+1)]
s[0] = 1
if b[1] == 0:
    s[1] = 1
else:
    s[1] = 0
for ni in range(2,n+1):
    if b[ni] == 0:
        s[ni] = (s[ni-1] + s[ni-2]) % 1000000007
    else:
        s[ni] = 0
print(s[n])
### BUGGY CODE ###

import fractions
 
n = int(input())
a = list(map(int, input().split()))
 
def lcm(x, y):
	return (x * y) // fractions.gcd(x, y)
 
num = a[0]
for i in range(1, n):
	num = lcm(num, a[i])
 
ans = 0
for i in range(n):
	ans += (num//a[i]) % 1000000007
	if ans > 1000000007:
		ans = ans%1000000007
 
print(ans)

### FIXED CODE ###

import fractions
 
n = int(input())
a = list(map(int, input().split()))
 
l = 1
for i in range(n):
	l = l // fractions.gcd(l, a[i])*a[i]
 
ans = 0
for i in range(n):
	ans += l//a[i]
print(ans%(10**9+7))
### BUGGY CODE ###

s = input()
q = int(input())
queries = []

for _ in range(q):
    query = input()
    if query == "1":
        queries.append({"t": 1})
    else:
        _, f, c = query.split(" ")
        queries.append({"t": 2, "f": int(f), "c": c})

i = 0
while(True):
    if queries[i]["t"] == 1:
        rev = 1
        while(True):
            if i < q-1 and queries[i+1]["t"] == 1:
                rev += 1
                i += 1
            else:
                break
        if rev % 2 == 1:
            s = s[::-1]
        i += 1
    else:
        s = queries[i]["c"] + s if queries[i]["f"] == 1 else s + queries[i]["c"]
        i += 1
    if i > q-1:
        break

print(s)


### FIXED CODE ###

s = input()
q = int(input())
rev = False
L = ""
R = ""
for _ in range(q):
    query = input()
    if query == "1":
        rev = not rev
    else:
        _, f, c = query.split(" ")
        if f == "1":
            if rev:
                R = R + c
            else:
                L = c + L
        else:
            if rev:
                L = c + L
            else:
                R = R + c

if rev:
    s = s[::-1]
    L = L[::-1]
    R = R[::-1]
    print(R + s + L)
else:
    print(L + s + R)
### BUGGY CODE ###

import heapq
def prim_heap():
    used = [True] * n #True:不使用
    edgelist = []
    for e in edge[0]:
        heapq.heappush(edgelist,e)
    used[0] = False
    res = 0
    counter = 1
    while counter != n:
        v = -1
        while len(edgelist) != 0:
            minedge = heapq.heappop(edgelist)
            if used[minedge[1]]:
                v = minedge[1]
                break
        if v == -1:
            break
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,e)
        res += minedge[0]
        counter += 1
    return res

#########################
n = int(input())

#隣接リストの作成 edge[i]:[コスト,行先]
edge = [[] for i in range(n)]
x_sort = [] #[Xi,Yi,点番号]
y_sort = [] #[Yi,Xi,点番号]
for i in range(n):
    X,Y = map(int,input().split())
    x_sort.append([X,Y,i])
    y_sort.append([Y,X,i])
x_sort.sort()
y_sort.sort()
for i in range(1,n):
    edge[x_sort[i][2]].append([min(abs(x_sort[i][0]-x_sort[i-1][0]),abs(x_sort[i][1]-x_sort[i-1][1])),x_sort[i-1][2]])
    edge[x_sort[i-1][2]].append([min(abs(x_sort[i][0]-x_sort[i-1][0]),abs(x_sort[i][1]-x_sort[i-1][1])),x_sort[i][2]])
    edge[y_sort[i][2]].append([min(abs(y_sort[i][0]-y_sort[i-1][0]),abs(y_sort[i][1]-y_sort[i-1][1])),y_sort[i-1][2]])
    edge[y_sort[i][2]].append([min(abs(y_sort[i][0]-y_sort[i-1][0]),abs(y_sort[i][1]-y_sort[i-1][1])),y_sort[i-1][2]])
print(prim_heap())

### FIXED CODE ###

import heapq
def prim_heap():
    used = [True] * n #True:不使用
    edgelist = []
    for e in edge[0]:
        heapq.heappush(edgelist,e)
    used[0] = False
    res = 0
    while len(edgelist) != 0:
        minedge = heapq.heappop(edgelist)
        if not used[minedge[1]]:
            continue
        v = minedge[1]
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,e)
        res += minedge[0]
    return res

#########################
n = int(input())

#隣接リストの作成 edge[i]:[コスト,行先]
edge = [[] for i in range(n)]
p=[]
edge=[[] for i in range(n)]
for i in range(n):
    p.append(tuple(map(int,input().split()))+tuple([i]))
x_sorted=sorted(p,key=lambda x:x[0])
y_sorted=sorted(p,key=lambda x:x[1])
for i in range(n-1):
    edge[x_sorted[i][2]].append((x_sorted[i+1][0]-x_sorted[i][0],x_sorted[i+1][2]))
    edge[x_sorted[i+1][2]].append((x_sorted[i+1][0]-x_sorted[i][0],x_sorted[i][2]))
    edge[y_sorted[i][2]].append((y_sorted[i+1][1]-y_sorted[i][1],y_sorted[i+1][2]))
    edge[y_sorted[i+1][2]].append((y_sorted[i+1][1]-y_sorted[i][1],y_sorted[i][2]))

print(prim_heap())
### BUGGY CODE ###

import sys
input = sys.stdin.readline
from collections import defaultdict

n,q = map(int, input().split())
child = defaultdict(list)
parent = defaultdict(int)
query = defaultdict(int)
for _ in range(n - 1):
	a, b = map(int, input().split())
	child[a].append(b)
	parent[b] = a
for _ in range(q):
	a, b = map(int, input().split())
	query[a] += b
ans = [-1 for _ in range(n)]
for i in range(1, n + 1):
	if parent[i] == 0:
		break
l = [i]
while l != []:
	i = l[0]
	ans[i - 1] = query[i]
	if child[i] != []:
 		for x in child[i]:
 			query[x] += query[i]
 			l.append(x)
	l.pop(0)
print(*ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline
from collections import defaultdict, deque

n,q = map(int, input().split())
child = defaultdict(list)
parent = defaultdict(int)
query = defaultdict(int)
for _ in range(n - 1):
	a, b = map(int, input().split())
	child[a].append(b)
	parent[b] = a
for _ in range(q):
	a, b = map(int, input().split())
	query[a] += b
ans = [-1 for _ in range(n)]
for i in range(1, n + 1):
	if parent[i] == 0:
		break
l = deque([i])
while l != deque([]):
	i = l[0]
	ans[i - 1] = query[i]
	if child[i] != []:
 		for x in child[i]:
 			query[x] += query[i]
 			l.append(x)
	l.popleft()
print(*ans)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

def main():

    n, q = map(int, input().split())

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

    for i in range(q):
        print(ans[i])

if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

def main():

    n, q = map(int, input().split())

    V = []

    for i in range(n):
        s, t, x = map(int, input().split())
        V.append((s-x, x, 1)) #1: insert
        V.append((t-x, x, 0)) #0: delete

    D = []
    for i in range(q):
        d = int(input())
        V.append((d, i, 2)) #2: query

    V.sort(key=lambda x:(x[0], x[2]))
    se = set()
    hq = []
    import heapq
    heapq.heapify(hq)
    ans = [-1]*q
    for t, v, p in V:
        if p == 1:
            se.add(v)
            heapq.heappush(hq, v)
        elif p == 0:
            se.discard(v)
        else:
            while hq and hq[0] not in se:
                heapq.heappop(hq)
            if hq:
                ans[v] = hq[0]

    for i in range(q):
        print(ans[i])

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

k, s = map(int,input().split())

count=0
for i in range(k+1):
  for j in range(k+1):
    for k in range(k+1):
      if i+j+k == s:
        count += 1
print(count)

### FIXED CODE ###

k, s = map(int,input().split())

count=0

for i in range(k+1):
  for j in range(k+1):
      if s-k <= i+j <= s:
        count += 1
print(count)
### BUGGY CODE ###

n,m=map(int,input().split())
s1=[]
sn=[]
ans='IMPOSSIBLE'
for i in range(m):
  a,b=map(int,input().split())
  if a==1:
    s1.append(b)
  elif b==n:
    sn.append(a)
for i in s1:
  if i in sn:
    ans='POSSIBLE'
    break
print(ans)

### FIXED CODE ###

n,m=map(int,input().split())
s1=[]
sn=[]
ans='IMPOSSIBLE'
for i in range(m):
  a,b=map(int,input().split())
  if a==1:
    s1.append(b)
  elif b==n:
    sn.append(a)
s=sorted(s1+sn)
for i in range(len(s)-1):
  if s[i]==s[i+1]:
    ans='POSSIBLE'
    break
print(ans)
### BUGGY CODE ###

n = int(input())
D = list(map(int, input().split()))
m = int(input())
T = list(map(int, input().split()))

r = 'YES'
for i in range(m):
    if T[i] in D:
        D.remove(T[i])
    else:
        r = 'NO'
        break
print(r)

### FIXED CODE ###

n = int(input())
dD = input().split()
m = int(input())
dT = input().split()
d = dict()

for di in dD:
    if di in d:
        d[di] += 1
    else:
        d[di] = 1

ans = 'YES'
for ti in dT:
    if ti not in d or d[ti]==0:
        ans = 'NO'
        break
    else:
        d[ti] -= 1
print(ans)
### BUGGY CODE ###

# 公式解説を見てからやった
# 複数サイクルでき、1-K回移動可能
# N < 5000?多分、O(N * 2N)くらいで行けそうに見えるが。。。

if __name__ == "__main__":

	N,K = map(int,input().split())
	P = [ int(p)-1 for p in input().split() ]
	C = list(map(int,input().split()))

	# 一度計算したサイクル情報を一応キャッシュしておく。。。
	# あんまり意味なさそう
	cycleIDs = [ -1 for _ in range(N) ]
	cycleInfs = []


	# print(P)

	ans = -1e10
	cycleID = 0

	for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			currentCycleItemCnt, currentCycleTotal = cycleInfs[ cycleIDs[v] ]
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleInfs.append( (currentCycleItemCnt, currentCycleTotal) )
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID

		procCnt = 0
		currentCycleSumTmp = 0

		while True:

			# 頂点vにコマが置かれた時の最高スコアを計算し、
			# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する
			procCnt += 1
			currentCycleSumTmp += C[v]
			if K < procCnt:
				break

			cycleLoopCnt = 0
			if procCnt < K and 0 < currentCycleTotal:
				cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

			# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, procCnt)
			tmp = currentCycleSumTmp + cycleLoopCnt * currentCycleTotal
			ans = max( ans, tmp )

			v = P[v]
			if v == n:
				# サイクル終了
				break

	print(ans)


### FIXED CODE ###

# 公式解説を見てからやった
# 複数サイクルでき、1-K回移動可能
# N < 5000?多分、O(N * 2N)くらいで行けそうに見えるが。。。

if __name__ == "__main__":

	N,K = map(int,input().split())
	P = [ int(p)-1 for p in input().split() ]
	C = list(map(int,input().split()))

	# 一度計算したサイクル情報を一応キャッシュしておく。。。
	# あんまり意味なさそう
	cycleIDs = [ -1 for _ in range(N) ]
	cycleInfs = []


	# print(P)

	ans = -1e10
	cycleID = 0

	for n in range(N):

		v = n
		currentCycleItemCnt = 0
		currentCycleTotal = 0

		if cycleIDs[v] != -1:
			currentCycleItemCnt, currentCycleTotal = cycleInfs[ cycleIDs[v] ]
			# print(currentCycleItemCnt, currentCycleTotal)
		else:
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleTotal += C[v]

				v = P[v]
				if v == n:
					# サイクル発見
					cycleInfs.append( (currentCycleItemCnt, currentCycleTotal) )
					cycleID += 1
					break

				# 一応、一度サイクルを計算した頂点については、
				# その頂点の属するサイクルの情報をメモっておく。。。
				cycleIDs[v] = cycleID

		procCnt = 0
		currentCycleSumTmp = 0

		while True:

			# 頂点vにコマが置かれた時の最高スコアを計算し、
			# これまでの最高スコアを上回ったら、これまでの最高スコアを更新する
			procCnt += 1
			currentCycleSumTmp += C[v]
			if K < procCnt:
				break

			cycleLoopCnt = 0
			if procCnt < K and 0 < currentCycleTotal:
				cycleLoopCnt = ( K - procCnt ) // currentCycleItemCnt

			# print("v=", v, "currentCycleSumTmp=", currentCycleSumTmp, procCnt)
			tmp = currentCycleSumTmp + cycleLoopCnt * currentCycleTotal
			ans = max( ans, tmp )

			v = P[v]
			if v == n:
				# サイクル終了
				break

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

num = int(input())
count = 0
for n in range(num + 1):
  is3 = False
  is5 = False
  is7 = False
  nStr = str(n)
  for i in range(len(nStr)):
    if nStr[i] == '3':
      is3 = True
    elif nStr[i] == '5':
      is5 = True
    elif nStr[i] == '7':
      is7 = True
    else:
      is3 = False
      break
  if is3 and is5 and is7:
    count += 1
print(str(count))

### FIXED CODE ###

def next_rank_num(nStr):
  if nStr == '3':
    return '5'
  elif nStr == '5':
    return '7'
  elif nStr == '7':
    return '3'

def next_num(nStr):
  length = len(nStr)
  i = 1
  nrn = '3'
  nList = []
  while i <= length and nrn == '3':
    nrn = next_rank_num(nStr[length-i])
    nList.append(nrn)
    if nrn == '3':
      i += 1
  nList.reverse()
  if i > length:
    return '3' + ''.join(nList)
  else:
    return nStr[0:length-i] + ''.join(nList)
   
num = int(input())
count = 0
nStr = "357";
while int(nStr) <= num:
  if len({'3', '5', '7'} - set(nStr)) == 0:
    count += 1
  nStr = next_num(nStr)
print(str(count))
### BUGGY CODE ###

n,k = map(int, input().split())
lst = list(map(int, input().split()))
exp = []
ans = []

a=0
for i in lst:
    a = (i+1)/2
    exp.append(a)

for i in range(n-k+1):
    ans.append(sum(exp[i:i+k]))

print(max(ans))


### FIXED CODE ###

from itertools import accumulate


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

# 累積和
B = [0] + A
B = list(accumulate(B))

ans = []
for i in range(N-K+1):
    ans.append(B[i+K]-B[i])

print((max(ans)+K)/2)
### BUGGY CODE ###

n = int(input())
dic = {}

for word in range(n):
    word = input()
    if not word in dic:
        dic[word] = 1
    
    else:
        dic[word] += 1

ans = [kv[0] for kv in dic.items() if kv[1] == max(dic.values())]

ans.sort()
for i in ans:
    print(i)

### FIXED CODE ###

n = int(input())
dic = {}

for word in range(n):
    word = input()
    if not word in dic:
        dic[word] = 1
    else:
        dic[word] += 1
max_v = max(dic.values())
ans = [kv[0] for kv in dic.items() if kv[1] == max_v]

ans.sort()
for i in ans:
    print(i)
### BUGGY CODE ###

import numpy as np

h, w, k = [int(i) for i in input().split()]
s = np.zeros([h,w])
ans = (w-1)*(h-1)

for i in range(h):
    st = [int(j) for j in list(input())]
    s[i,:] = st
idx = []
for i in range(1<<h-1):
    tmp = []
    g = []
    for j in range(h-1):
        if ((i>>j)&1):
            tmp.append(j+1)

    n = len(tmp)
    choco = np.zeros([n+1,w])
    for j in range(n+1):
        if n == 0:
            choco = np.sum(s,axis=0)
        elif n > 0:
            if j == n:
                choco[j] = np.sum(s[tmp[j-1]:,:],axis=0)
            elif j == 0:
                choco[j] = np.sum(s[:tmp[j],:], axis=0)
            else:
                choco[j] = np.sum(s[tmp[j-1]:tmp[j],:], axis=0)
    w_div = 0
    w_div0 = 0
    nn = 0
    ans0 = (w-1)*(h-1)
    
    while w_div < w+1:
        if w_div == 0:
            num_choco = np.zeros((1,1))
            num_choco[0,0] = np.array(np.sum(choco))
        else:
            if n == 0:
                num_choco = np.zeros((1, 2))
                if w_div0 == 0:
                    num_choco[:,0] = np.sum(choco[:w_div])
                else:
                    num_choco[:,0] = np.sum(choco[w_div0:w_div])
                num_choco[:,1] = np.sum(choco[w_div:])
                
            else:
                num_choco = np.zeros((n+1,2))
                if w_div0 == 0:
                    num_choco[:,0] = np.sum(choco[:,:w_div],axis=1).T
                else:
                    num_choco[:,0] = np.sum(choco[:,w_div0:w_div],axis=1).T
                num_choco[:,1] = np.sum(choco[:,w_div:],axis=1).T
                
        if np.any(num_choco > k):
            if np.any(num_choco[:,0] > k) and w_div != 0:
                if w_div - w_div0 == 1:
                    break
                w_div0 = w_div-1
                nn += 1
                w_div += 0
            else:
                w_div +=1
        else:
            if w_div == w:
                ans0 = n + nn
                ans = min(ans,ans0)
            w_div += 1
print(ans)

### FIXED CODE ###

import numpy as np
import itertools
H, W, K = [int(_) for _ in input().split()]
S = np.zeros((H + 1, W + 1), dtype=int)
S[1:, 1:] = [[int(_) for _ in input()] for _ in range(H)]
cum = S.cumsum(axis=0).cumsum(axis=1)
# print(cum)

ans = 10**10
for bs in itertools.product([True, False], repeat=H - 1):
    bs = [True] + list(bs) + [True]
#     print(bs)
    idxs = []
    for i, b in enumerate(bs):
        if b:
            idxs += [i]
    cum2 = np.array([cum[i2] - cum[i1] for i1, i2 in zip(idxs, idxs[1:])])
    i = 0
    cnt = sum(bs) - 3
    while i < W:
        di = min(
            np.searchsorted(c[i + 1:] - c[i], K, side='right') for c in cum2)
        if di == 0:
            cnt = 10**10
            break
        i += di
        cnt += 1
    ans = min(ans, cnt)
print(ans)
### BUGGY CODE ###

N = int(input())
li = []
for i in range(N):
  s = int(input())
  li.append(s)
  
  new = sorted(li, reverse=True)

s = max(li)
for index, i in enumerate(li):
  if s == li[index]:
    print(new[1])
  else:
    print(new[0])

### FIXED CODE ###

N = int(input())
li = []
for i in range(N):
  s = int(input())
  li.append(s)
  
new = sorted(li, reverse=True)

s = max(li)
for index, i in enumerate(li):
  if s == li[index]:
    print(new[1])
  else:
    print(new[0])
### BUGGY CODE ###

# https://atcoder.jp/contests/abc107/tasks/arc101_a
# C - Candles

n, k = list(map(int, input().split()))
x_list = list(map(int, input().split()))
x_list = sorted(x_list)
min_l = float('inf')

minus_list = [x for x in x_list if x < 0]
plus_index = len(minus_list)
# print('plus_index: {}'.format(plus_index))
# plus_list = [x for x in work_list if x >= 0]

for i, x in enumerate(x_list):
    l = 0
    minus_list = []
    plus_list = []
    if i + k < plus_index:
        # 全部負の数
        minus_list = x_list[i:i+k]
        plus_list = []
    elif i > plus_index:
        # 全部正の数
        plus_list = x_list[i:i+k]
        minus_list = []
    else:
        minus_list = x_list[i:plus_index]
        diff = k - len(minus_list)
        plus_list = x_list[plus_index: plus_index + (k - len(minus_list))]

    # print(minus_list)
    # print(plus_list)

    minus_l = -minus_list[0] if minus_list else 0
    plus_l = plus_list[-1] if plus_list else 0
    # print('{}, {}'.format(minus_l, plus_l))

    if plus_l > minus_l:
        l = minus_l * 2 + plus_l
    else:
        l = plus_l * 2 +  minus_l

    if l < min_l:
        min_l = l

    if i + k > n - 1:
        break

print(min_l)

### FIXED CODE ###

# https://atcoder.jp/contests/abc107/tasks/arc101_a
# C - Candles

n, k = list(map(int, input().split()))
x_list = list(map(int, input().split()))
x_list = sorted(x_list)
min_l = float('inf')

minus_list = [x for x in x_list if x < 0]
plus_index = len(minus_list)

for i, x in enumerate(x_list):
    left, right = x_list[i], x_list[i+k-1]
    # 最左端に行ってから、最右端に行く
    a = abs(left) + abs(left-right)
    # 最右端に行ってから、最左端に行く
    b = abs(right) + abs(right - left)
    l = min(a, b)

    if l < min_l:
        min_l = l

    if i + k > n - 1:
        break

print(min_l)
### BUGGY CODE ###

_,*a=map(int,open(0).read().split())
a=[i for i in a if a.count(i)==1]
a.sort()
b=0
while a:
    b+=1
    a=[i for i in a[1:] if i%a[0]!=0]
print(b)

### FIXED CODE ###

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

c.sort()
a = c[N-1]
list_c = [0]*(a+1)
for i in c:
  mal = 2
  b = i
  while i<=a:
    list_c[i]+=1
    i = b*mal
    mal+=1
count = 0
for i in c:
  if list_c[i]==1:
    count+=1
print(count)
### BUGGY CODE ###

N=int(input())
x=list(map(int,input().split()))
x.sort(reverse=True)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
start=0
for i in range(len(x)):
    if x[i] != 0:
        #print("X=",x[i])
        for j in range(start,len(t)):
            #print(j)
            if t[j] > 2*x[i]:
                start=j
            if t[j] < x[i]:
                break
            if t[j]-x[i] in x[i+1:]:
                if t[j]-x[i] != 0:
                    x[i+1+x[i+1:].index(t[j]-x[i])] = 0
                    x[i]=0
                    count+=1
                    break
#print(x)
print(count)

### FIXED CODE ###

import collections
N=int(input())
x=list(map(int,input().split()))
c=collections.Counter(x)
t=[2**i for i in range(len(str(bin(max(x)*2))[2:])+1)[::-1]]
count=0
start=0
x.sort()
for i in range(len(x))[::-1]:
    for j in range(start,len(t)):
        if t[j] > 2*x[i]:
            start = j
        if t[j] < x[i]:
            break
        if t[j]-x[i] in c.keys():
            if t[j]-x[i] == x[i]:
                if c[x[i]] >= 2:
                    c[x[i]] -= 2
                    count +=1
            else:
                if c[t[j]-x[i]]>0 and c[x[i]]>0:
                    c[t[j]-x[i]] -= 1
                    c[x[i]] -= 1
                    count+=1
print(count)
### BUGGY CODE ###

N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

Amax = max(A)

i = 1
while i * Amax <= 1e18:
  fl = False
  for a in A:
    if i * Amax % a != 0:
      fl = True
      break
  if fl:
    i += 1
    continue
  else:
    break

print(i*Amax)

### FIXED CODE ###

N = int(input())

A = []
for i in range(N):
  A.append(int(input()))

def GCD(a,b):
  if a < b:
    a, b = b, a
  while b > 0:
    a, b = b, a%b
  
  return a

def LCM(a,b):
  return a*b // GCD(a,b)

lcm = A[0]
for i in range(1,N):
  lcm = LCM(lcm, A[i])

print(lcm)
### BUGGY CODE ###

N = int(input())
si = [sorted(list(input())) for i in range(N)]

result = []
for j in range(len(si)):
    a = si.count(si[j])
    result.append(a-1)

print(int(sum(result)/2))

### FIXED CODE ###

from collections import Counter

N = int(input())
si = [''.join(sorted(list(input()))) for i in range(N)]

count = Counter(si)

ans = 0
for s in set(si):
    n = count[s]
    ans += n * (n-1) // 2

print(ans)