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

def main():
    mod = 2**31-1
    pow3 = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 200001):
        p = p*3 % mod
        pow3[i] = p

    def rolling_hash(seq):
        h = rolling_hash2(seq)
        H0s = [0]*n
        for i, j in enumerate(seq):
            H0s[i] = h
            h = (h-j-1+(j+1)*pn)*i3 % mod
        return H0s

    def rolling_hash2(seq):
        p = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow3[i]
        return p % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    m = max(a+b)
    a1 = [0]*n
    a2 = [0]*n
    b1 = [0]*n
    memo = [0]*n
    cnt = n
    pn = pow3[n]

    for k in range(len(bin(m))-2):
        k2 = 2**k
        for i, j in enumerate(a):
            p = j & k2
            a1[i] = p
            a2[i] = p ^ k2
        for i, j in enumerate(b):
            b1[i] = j & k2
        b_hash = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        A2 = rolling_hash(a2)
        for i, (j, k, l) in enumerate(zip(memo, A1, A2)):
            if j is None:
                continue
            elif k == b_hash:
                pass
            elif l == b_hash:
                memo[i] += k2
            else:
                memo[i] = None
                cnt -= 1
        if cnt == 0:
            return

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()


### FIXED CODE ###

def main():
    mod = 2**31-1
    pow3 = [1]*200001
    p = 1
    i3 = pow(3, mod-2, mod)
    for i in range(1, 200001):
        p = p*3 % mod
        pow3[i] = p

    def rolling_hash(seq):
        h = 0
        for i, j in enumerate(seq):
            h += (j+1)*pow3[i]
        h %= mod
        H0s = [0]*n
        for i, j in enumerate(seq):
            H0s[i] = h
            h = (h-j-1+(j+1)*pn)*i3 % mod
        return H0s

    def rolling_hash2(seq):
        p = 0
        p2 = 0
        for i, j in enumerate(seq):
            p += (j+1)*pow3[i]
            p2 += (2-j)*pow3[i]
        return p % mod, p2 % mod

    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    m = max(a+b)
    a1 = [0]*n
    b1 = [0]*n
    memo = [0]*n
    cnt = n
    pn = pow3[n]

    for k in range(len(bin(m))-2):
        k2 = 2**k
        for i, j in enumerate(a):
            a1[i] = (j & k2) >> k
        for i, j in enumerate(b):
            b1[i] = (j & k2) >> k
        b_hash, b_hash2 = rolling_hash2(b1)
        A1 = rolling_hash(a1)
        for i, (j, k) in enumerate(zip(memo, A1)):
            if j is None:
                continue
            elif k == b_hash:
                pass
            elif k == b_hash2:
                memo[i] += k2
            else:
                memo[i] = None
                cnt -= 1
        if cnt == 0:
            return

    for i, j in enumerate(memo):
        if j is not None:
            print(i, j)


main()
### BUGGY CODE ###

# solution 1
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
#A.sort(reverse=True)
#B.sort(reverse=True)
C.sort(reverse=True)
AB = []
for i in range(X):
    for j in range(Y):
        AB.append(A[i]+B[j])
AB.sort(reverse=True)
ABC = []
for k in range(Z):
    for ij in range(min(K, len(AB))):
        ABC.append(AB[ij]+C[k])
ABC.sort(reverse=True)
for l in range(K):
    print(ABC[l])

### FIXED CODE ###

import heapq
X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)
Q = []
heapq.heappush(Q, (-A[0]-B[0]-C[0], 0, 0, 0))
for n in range(K):
    ans = heapq.heappop(Q)
    print(-ans[0])
    i, j, k = ans[1], ans[2], ans[3]
    if i < X-1:
        if (- A[i+1] - B[j] - C[k], i+1, j, k) not in Q:
            heapq.heappush(Q, (- A[i+1] - B[j] - C[k], i+1, j, k))
    if j < Y-1:
        if (- A[i] - B[j+1] - C[k], i, j+1, k) not in Q:
            heapq.heappush(Q, (- A[i] - B[j+1] - C[k], i, j+1, k))
    if k < Z-1:
        if (- A[i] - B[j] - C[k+1], i, j, k+1) not in Q:
            heapq.heappush(Q, (- A[i] - B[j] - C[k+1], i, j, k+1))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

N, M = inpl()

X = inpl()
Y = inpl()

Sx = 0
Sy = 0

for i in range(N-1):
    for j in range(i+1, N):
        Sx += X[j] - X[i]

for i in range(M-1):
    for j in range(i+1, M):
        Sy += Y[j] - Y[i]

print((Sx*Sy)%(10**9+7))

### FIXED CODE ###

# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

def make(N):
    if N%2:
        L = [N + (N//2)**2 - n**2 + 2*n - 1 for n in range(1, N//2+2)]
        L = L[1::][::-1] + L
        return L
    else:
        L = [N + N*(N-2)//4 -n**2 + n for n in range(1, N//2+1)]
        L = L[::-1] + L
        return L

N, M = inpl()

X = inpl()
Y = inpl()

A = make(N-1)
B = make(M-1)

Sx = 0
Sy = 0

for i, a in enumerate(A):
    Sx += a * (X[i+1] - X[i])

for j, b in enumerate(B):
    Sy += b * (Y[j+1] - Y[j])

print((Sx*Sy)%(10**9 + 7))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)

def main():
    S = [int(s) for s in input()]
    N = len(S)
    K = 2019

    V = [0] * (N+1)
    U = [0]
    dic = {0:1} # value:count
    ans = 0
    for i in reversed(range(N)):
        rev_i = (N-1)-i
        V[rev_i+1] = V[rev_i] + S[i] * (10**(rev_i+1))
        u = V[rev_i+1] % K
        if u in dic:
            dic[u] += 1
            cnt = dic[u]
            ans -= (cnt-1) * (cnt-2) // 2
            ans += cnt * (cnt-1) // 2
        else:
            dic[u] = 1
            # U.append(u)


    # for u in U:
    #     u_count = dic[u]
    #     ans += u_count * (u_count-1) // 2

    # print(S)
    # print(V)
    # print(dic)
    print(ans)

if __name__ == "__main__":
  main()

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**9)

def main():
    S = [int(s) for s in input()]
    N = len(S)
    K = 2019

    V = [0] * (N+1)
    for i in reversed(range(N)):
        rev_i = (N-1)-i
        V[rev_i+1] = V[rev_i] + S[i] * (pow(10,rev_i+1,K))
        # print(V)

    dic = {} # value:count
    U = set()
    for v in V:
        u = v % K
        if u in U:
            dic[u] += 1
        else:
            dic[u] = 1
            U.add(u)

    ans = 0
    for u in U:
        u_count = dic[u]
        ans += u_count * (u_count-1) // 2

    # print(S)
    # print(V)
    # print(dic)
    print(ans)

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

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

for i in range(n):
    c.append(a.index(i+1)+1)

print(' '.join(map(str, c)))


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
index=[0]*n

for i in range(n):
    index[a[i]-1] = i+1

print(' '.join(map(str, index)))
### BUGGY CODE ###

s=input()
n=int(input())
front=True
for _ in range(n):
    q=input()
    if q=="1":front=not front;continue
    l=q.split()
    if l[1]=="1":
        if front:s=l[2]+s
        else:s=s+l[2]
    if l[1]=="2":
        if front:s=s+l[2]
        else:s=l[2]+s

print(s if front else s[::-1])

### FIXED CODE ###

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

front=True
f=""
b=""

for q in a:
    if q=="1":front=not front;continue
    l=q.split()
    if l[1]=="1":
        if front:f=l[2]+f
        else:b=b+l[2]
    if l[1]=="2":
        if front:b=b+l[2]
        else:f=l[2]+f

print(f+s+b if front else (f+s+b)[::-1])
### BUGGY CODE ###

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

result=0

for j in range(N-1):
    for k in range(j+2,N):
        if k-j==A[k]+A[j]:
            result +=1

print(result)

### FIXED CODE ###

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

result=0

D={}

for j in range(N):
    if j+A[j] in D:
        D[j+A[j]] += 1
    else:
        D[j+A[j]]=1

for j in range(N):
    if j-A[j] in D:
        result += D[j-A[j]]

print(result)
### BUGGY CODE ###

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

minA = min(A)
C = [[j]*i for i,j in BC if j > minA]
for i in range(len(C)):
    A.extend(C[i])

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


### FIXED CODE ###

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

A.sort()
BC.sort(key=lambda x: x[1], reverse=True)

idx = 0
for b, c in BC:
  for j in range(idx, min(N, idx + b)):
    if A[j] < c:
      A[j] = c
  idx += b
print(sum(A))
### BUGGY CODE ###

N,K = map(int, input().strip().split(" "))
H = list(map(int, input().strip().split(" ")))

dp = [[0]*K for i in range(N)]

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


### FIXED CODE ###

input = raw_input
N,K = map(int, input().strip().split(" "))
H = list(map(int, input().strip().split(" ")))

dp = [[0]*K for i in range(N)]

for i in range(1,N):
    for j in range(1, K+1):
        jj = max(0, i-j)
        dp[i][j-1] = dp[jj][0] + abs(H[i]-H[jj])
    dp[i][0] = min(dp[i])
print(min(dp[N-1]))
### BUGGY CODE ###

def gcd(a, b):
    while True:
        a, b = b, a % b
        if b == 0:
            break
    return a


def compute_gcd(ns):
    a = gcd(ns[0], ns[1])
    for i in range(2, len(ns)):
        a = gcd(a, ns[i])
    return a


n = int(input())
nums = list(map(int, input().split()))
if n == 2:
    print(max(nums))
else:
    gcds = []
    for i in range(n):
        ns = [nums[j] for j in range(n) if j != i]
        a = compute_gcd(ns)
        gcds.append(a)
    print(max(gcds))

### FIXED CODE ###

def gcd(a, b):
    while True:
        a, b = b, a % b
        if b == 0:
            break
    return a


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

if n == 2:
    print(max(nums))
else:
    l_gcds = [nums[0]]
    r_gcds = [nums[-1]]
    for i in range(1, n - 1):
        l = gcd(l_gcds[-1], nums[i])
        r = gcd(r_gcds[-1], nums[n - i - 1])
        l_gcds.append(l)
        r_gcds.append(r)
    r_gcds = r_gcds[::-1]
    gcds = [l_gcds[-1], r_gcds[0]]
    for i in range(1, n - 1):
        gcds.append(gcd(l_gcds[i - 1], r_gcds[i]))
    print(max(gcds))
### BUGGY CODE ###

import itertools

N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0

seq = list(itertools.combinations(L, 3))
num = len(seq)

for i in range(num):
    a = seq[i][0]
    b = seq[i][1]
    c = seq[i][2]
    if (a < b + c) and (b < a + c) and (c < a + b):
        ans += 1

print(ans)

### FIXED CODE ###

N = int(input())
L = list(map(int, input().split()))
L.sort()
ans = 0

from bisect import bisect_left
for i in range(N):
    for j in range(i+1, N):
        ab = L[i] + L[j]
        index = bisect_left(L, ab)
        num = index - j - 1
        ans += max(0, num)

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

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

ans = 99999999999999999

for i in range(len(a)-1):
    tmp = abs(sum(a[:i+1]) - sum(a[i+1:]))
    if tmp < ans:
        ans = tmp

print(ans)

### FIXED CODE ###

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

su = sum(a)
diff = 1e10
cc = 0

for i in range(len(a)-1):
    cc += a[i]
    diff = min(diff, abs(su-2*cc))
print(diff)
### BUGGY CODE ###

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

dp = [1<<30]*(N)
dp[0] = 0

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

print(dp[N-1])

### FIXED CODE ###

N, K = map(int, input().split())
h = list(map(int, input().split()))
 
dp = [1<<30]*(N)
dp[0] = 0
 
for i in range(1, N):
  dp[i] = min((dp[j]+abs(h[i]-h[j])) for j in range(max(0, i-K), i))
 
print(dp[N-1])
### BUGGY CODE ###

N,d,a = map(int,input().split())

data0 = [0]*(N+1)
data1 = [0]*(N+1)
# 区間[l, r)に x を加算
def _add(data, k, x):
    while k <= N:
        data[k] += x
        k += k & -k
def add(l, r, x):
    _add(data0, l, -x*(l-1))
    _add(data0, r, x*(r-1))
    _add(data1, l, x)
    _add(data1, r, -x)

# 区間[l, r)の和を求める
def get(data, k):
    s = 0
    while k:
        s += data[k]
        k -= k & -k
    return s
def query(l, r):
  return get(data1,r-1)*(r-1)+get(data0,r-1)-get(data1,l-1)*(l-1)-get(data0,l-1)

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

xh = sorted(xh)
xl = [x for x,h in xh]

for i in range(N):
    add(i+1, i+2, xh[i][1])

import bisect,math

ans = 0
for i in range(N):
    hp = query(i + 1, i + 2)
    #print(data0,data1,hp)    
    if hp > 0:
        count = math.ceil(hp/a)
        ans += count
        damage = count * a
        idx = bisect.bisect_right(xl, xl[i] + 2*d)
        add(i+1, idx+1, -damage)
print(ans)

### FIXED CODE ###

def main():  
  import sys
  input = sys.stdin.readline
  n, d, a = [int(i) for i in input().split()]
  chk = []
  for i in range(n):
    x, h = [int(i) for i in input().split()]
    chk.append((x, 0, h))

  from heapq import heapify, heappop, heappush
  heapify(chk)
  atk_cnt = 0; ans = 0
  while chk:
    x, t, h = heappop(chk)
    if t == 0:
      if atk_cnt * a >= h:
          continue
      remain = h - atk_cnt * a
      new_atk = (remain - 1) // a + 1
      heappush(chk, (x + 2 * d, 1, new_atk))
      ans += new_atk
      atk_cnt += new_atk
    else:
      atk_cnt -= h
  print(ans)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

n=int(input())
ans=1
l=list(map(int, input().split()))
for x in l:
    ans*=x

if ans>10**18:
    print(-1)
    exit(0)

print(ans)

### FIXED CODE ###

n = int(input())
ans = 1
l = list(map(int, input().split()))
if 0 in l:
    print(0)
    exit(0)

for x in l:
    ans *= x

    if ans > 10 ** 18:
        print(-1)
        exit(0)

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

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

    # dp=[9999999999]*n
    # dp[0]=0
    # dp[1]=abs(h[1]-h[0])

    # for i in range(2,n):
    #     for j in range(1,k+1):
    #         if i-j>=0:
    #             dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
    #         else:
    #             break

    # print(dp[n-1])


    #part2

    dp=[9999999999]*n
    dp[0]=0

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

    print(dp[n-1])




if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    
    n,k=map(int,input().split())
    h=list(map(int,input().split()))
 
    # dp=[9999999999]*n
    # dp[0]=0
    # dp[1]=abs(h[1]-h[0])
 
    # for i in range(2,n):
    #     for j in range(1,k+1):
    #         if i-j>=0:
    #             dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]))
    #         else:
    #             break
 
    # print(dp[n-1])
 
 
    #part2
 
    dp=[9999999999]*n
    dp[0]=0
 
    for i in range(n):
        for j in range(1,k+1):
            if i+j<n:
                dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
            else:
                break
 
    print(dp[n-1])
 
 
 
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
K = int(input())

x = set(list(S))

for w in range(2, len(S) + 1):
    for i in range(len(S)):
        if i + w > len(S):
            continue
        x.add(S[i:i+w])


x = sorted(list(x))
print(x[K - 1])

### FIXED CODE ###

import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
S = input()
K = int(input())

d = sorted(list(S))
x = set(d)
for c in d:
    left = S.index(c)
    while left < len(S):
        if S[left] != c:
            left += 1
            continue
        j = 2
        while j <= K:
            x.add(S[left:left+j])
            j += 1
        left += 1
    if len(x) >= K:
        break
x = sorted(list(x))
print(x[K - 1])
### BUGGY CODE ###

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

S = []
for _ in range(H):
    tmp = list(input())
    S.append(tmp)
    
import numpy as np

Gs = np.asarray(S)
Gs_ = np.rot90(Gs, k=1)

def count_light(Gs):
    Gc = []
    for gs in Gs:
        count = 0
        gc = []
        for s in gs:
            if s == '#':
                count = 0
                gc.append(0)
            else:
                count += 1
                gc.append(count)
        Gc.append(gc)

    Gc = np.asarray(Gc)

    Gn = []
    for gc in Gc:
        count = -1
        gn = []
        for c in gc[::-1]:
            if c == 0:
                count = -1
                gn.append(0)
            else:
                if count == -1:
                    count = c
                gn.append(count)
        Gn.append(gn[::-1])

    Gn = np.asarray(Gn)
    return Gn

Gn = count_light(Gs)
Gn_ = count_light(Gs_)
Gn_ = np.rot90(Gn_, k=-1)

Gsum = Gn+Gn_
ans = np.max(Gsum)-1

        
print(ans)

### FIXED CODE ###

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

S = []
for _ in range(H):
    tmp = list(input())
    S.append(tmp)
    
import numpy as np

def count_light(Gs, h, w):
    Gc = np.empty((h,w))
    for i in range(w):
        if i == 0:
            Gc[:, i] = Gs[:, i]
        else:
            Gc[:, i] = (Gc[:, i-1] + 1) * Gs[:, i]

    return Gc

Gs1 = np.asarray(S) == "."
Gn1 = count_light(Gs1, H, W)

Gs2 = np.rot90(Gs1, k=1)
Gn2 = count_light(Gs2, W, H)

Gs3 = np.rot90(Gs2, k=1)
Gn3 = count_light(Gs3, H, W)

Gs4 = np.rot90(Gs3, k=1)
Gn4 = count_light(Gs4, W, H)

Gn2 = np.rot90(Gn2, k=-1)
Gn3 = np.rot90(Gn3, k=-2)
Gn4 = np.rot90(Gn4, k=-3)

Gsum = Gn1+Gn2+Gn3+Gn4
ans = int(np.max(Gsum)-3)
print(ans)
### BUGGY CODE ###

N = int(input())

A_list = list( map(int, input().split() ) )
B_list = list( map(int, input().split() ) )
C_list = list( map(int, input().split() ) )
# print(d,n,m)
# print(A_list)
# print(B_list)
# print(C_list)

A_sort = sorted(A_list)
C_sort = sorted(C_list)
# print(A_sort)
# print(C_sort)

import numpy as np

A_np = np.array(A_sort)
C_np = np.array(C_sort)

sum_conbi = 0
for B in B_list:
    a_result = A_np[A_np < B]
    c_result = C_np[C_np > B]

    # print(a_result)
    # print(c_result)

    sum_conbi += len(a_result) * len(c_result)

print(sum_conbi)


### FIXED CODE ###

N = int(input())

A_list = list( map(int, input().split() ) )
B_list = list( map(int, input().split() ) )
C_list = list( map(int, input().split() ) )
# print(d,n,m)
# print(A_list)
# print(B_list)
# print(C_list)

A_sort = sorted(A_list)
C_sort = sorted(C_list)

# print("A:", A_sort)
# print("C:", C_sort)

def is_ok_lower(list_data, index: int, key: int):
    if (list_data[index] >= key):
        return True
    else:
        return False

def is_ok_upper(list_data, index: int, key: int):
    if (list_data[index] > key):
        return True
    else:
        return False


def binary_search(list_data, key: int, is_ok_func):
    ng = -1  #「index = 0」が条件を満たすこともあるので、初期値は -1
    ok = len(list_data) # 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()

    # どんな二分探索でもここの書き方を変えずにできる！
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2

        if (is_ok_func(list_data, mid, key)):
            ok = mid
        else:
            ng = mid

    # left は条件を満たさない最大の値、ok は条件を満たす最小の値になっている
    return ok

sum_conbi = 0
for B in B_list:
    a_key = binary_search(A_sort, B, is_ok_lower)
    c_key = binary_search(C_sort, B, is_ok_upper)

    # print(a_key, N-c_key)
    # print(B,">", A_sort[:a_key])
    # print(B,"<", C_sort[c_key:])

    sum_conbi += (a_key) * (N-c_key)

print(sum_conbi)
### BUGGY CODE ###

N = int(input())
abc = [list(map(int, input().split())) for _ in range(N-1)]

Q, K = map(int, input().split())

g = [[] for _ in range(N+1)]
dist = [[-1]*(N+1) for _ in range(N+1)]
for a, b, c in abc:
    g[a].append(b)
    g[b].append(a)
    dist[a][b] = c
    dist[b][a] = c

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q] != -1: continue
        cost[q] = cost[p] + dist[p][q]
        temp.append(q)

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])


### FIXED CODE ###

import sys
input = sys.stdin.readline
N = int(input())

g = [[] for _ in range(N+1)]

for i in range(N-1):
    a, b, c = map(int, input().split())
    g[a].append([b, c])
    g[b].append([a, c])
    
Q, K = map(int, input().split())

cost = [-1] * (N+1)
cost[K] = 0
temp = [K]
while temp:
    p = temp.pop()
    for q in g[p]:
        if cost[q[0]] == -1:
            cost[q[0]] = cost[p] + q[1]
            temp.append(q[0])

for i in range(Q):
    x, y = map(int, input().split())
    print(cost[x] + cost[y])
### BUGGY CODE ###

n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[None for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
DP[0][0][0] = 0.0
def dp(i, j, k):
    if i < 0 or j < 0 or k < 0:
        return 0.0
    if DP[i][j][k] != None:
        return DP[i][j][k]
    DP[i][j][k] = (n+dp(i-1, j, k)*i+dp(i+1, j-1, k)*j+dp(i, j+1, k-1)*k)/(i+j+k)
    return DP[i][j][k]
print(dp(a, b, c))


### FIXED CODE ###

n = int(input())
*A, = map(int, input().split())
a, b, c = A.count(1), A.count(2), A.count(3)
DP = [[[0.0 for k in range(n+1)] for j in range(n+1)] for i in range(n+1)]
for k in range(c+1):
    for j in range(b+c-k+1):
        for i in range(n-j-k+1):
            if i+j+k == 0:
                continue
            tmp = n
            if i > 0:
                tmp += DP[i-1][j][k]*i
            if j > 0:
                tmp += DP[i+1][j-1][k]*j
            if k > 0:
                tmp += DP[i][j+1][k-1]*k
            DP[i][j][k] = tmp/(i+j+k)
print(DP[a][b][c])
### BUGGY CODE ###

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

def isok(b):
    for i in range(N-1):
        for j in range(i+1, N):
            i2 = (i+b)%N
            j2 = (j+b)%N
            if src[i][j2] != src[j][i2]:
                return False
    return True

ans = 0
for i in range(-N+1,N):
    if isok(i):
        ans += N-abs(i)
print(ans)

### FIXED CODE ###

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

def isok(b):
    for i in range(N-1):
        for j in range(i+1, N):
            i2 = (i+b)%N
            j2 = (j+b)%N
            if src[i][j2] != src[j][i2]:
                return False
    return True

ans = 0
for i in range(-N+1,N):
    if isok(i):
        ans += N-abs(i)
print(ans)
### BUGGY CODE ###

N, K = map(int, input().split())
h = [None] * N
for _ in range(N):
    h[_] = int(input())

h.sort()
ans = 10000000000
for i in range(N - K + 1):
    ans = min(ans, max(h[i:i + K]) - min(h[i:i + K]))

print(ans)


### FIXED CODE ###

N, K = map(int, input().split())
h = [None] * N
for _ in range(N):
    h[_] = int(input())

h.sort()
ans = 10000000000
tmp = sum(h[:K])
for i in range(N - K + 1):
    ans = min(ans, h[i + K - 1] - h[i])
print(ans)
### BUGGY CODE ###

N,K = map(int,input().split())
x = list(map(int,input().split()))
if K==1 and (0 in x):
    print(0)
else:
    if 0 in x:
        pass
    else:
        K += 1
        counter = 0
        for i in x:
            if i > 0:
                break
            else:
                counter += 1
        x.insert(counter,0)

    result = 10000000000000000000
    g,s = x.index(0),x.index(0)-K+1
    while (not s>x.index(0)):
        if s<0 or g>(len(x)-1):
            s += 1
            g += 1
            continue
        else:
            ad_1 = 2*(abs(x[s])) + abs(x[g])
            ad_2 = abs(x[s]) + 2*(abs(x[g]))
            result = min(ad_1,ad_2,result)
            s += 1
            g += 1
    print(result)

### FIXED CODE ###

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

result = []
for i in range(N-K+1):
    s,g = i,i+K-1
    result.append(abs(x[s]) + abs(x[s] - x[g]))
    result.append(abs(x[g]) + abs(x[g] - x[s]))
result.sort()
print(result[0])