Harryxun/stf_run huggingface dataset:
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
sushis = [list(map(int, input().split())) for _ in range(N)]

sushis = sorted(sushis, key=lambda x: (x[1], x[0]), reverse=True)

p = 0


def calcPoint(selected_sushis):
    p = 0
    vari = set()
    for i, v in selected_sushis:
        vari.add(i)
        p += v
    p += len(vari)**2
    return p, vari


selected = sushis[:K]
unSelected = sushis[K:]

p, vari = calcPoint(selected)
cand = [p]

va = len(vari)
v = [0]*(N+1)
for i, _ in selected:
    v[i] += 1

can_del = []
for i, v in reversed(selected):
    if vari[i] > 2:
        vari[i] -= 1
        can_del.append(v)

can_in = []
for i, v in unSelected:
    if not i in vari:
        vari.add(i)
        can_in.append(v)

for od, nd in zip(can_del, can_in):
    nextp = p-od+nd-(vari)**2+(vari+1)**2
    cand.append(nextp)
    vari += 1
print(max(cand))


### FIXED CODE ###

# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
sushis = [list(map(int, input().split())) for _ in range(N)]

sushis = sorted(sushis, key=lambda x: (x[1], x[0]), reverse=True)

p = 0


def calcPoint(selected_sushis):
    p = 0
    vari = set()
    for i, v in selected_sushis:
        vari.add(i)
        p += v
    p += len(vari)**2
    return p, vari


selected = sushis[:K]
unSelected = sushis[K:]

p, vari = calcPoint(selected)
cand = [p]

va = len(vari)
v = [0]*(N+1)
for i, _ in selected:
    v[i] += 1

can_del = []
for i, d in reversed(selected):
    if v[i] >= 2:
        v[i] -= 1
        can_del.append(d)

can_in = []
for i, d in unSelected:
    if not i in vari:
        vari.add(i)
        can_in.append(d)

#print("del->", can_del, can_in)
for od, nd in zip(can_del, can_in):
    nextp = p-od+nd-(va)**2+(va+1)**2
    p = nextp
    cand.append(p)
    va += 1
print(max(cand))
import numpy as np

N = int(input())
S = input()
L = [0] * N
for i in range(N):
    if S[i] == "#":
        # white
        L[i] = 1
    else:
        L[i] = 0
L = np.array(L, dtype=np.int8)
L2 = np.zeros(N, dtype=np.int8)
ans = np.abs((L - L2)).sum()
for i in range(N):
    L2[N-1-i] = 1
    ans_tmp = np.abs((L - L2)).sum()
    ans = min(ans, ans_tmp)
print(ans)

### FIXED CODE ###

N = int(input())
S = input()
L = [0] * N
for i in range(N):
    if S[i] == "#":
        L[i] = 1
    else:
        L[i] = 0

leftblack = 0
rightwhite = len([x for x in L if x == 0])
ans = leftblack + rightwhite

for i in range(N):
    if S[i] == "#":
        leftblack += 1
    else:
        rightwhite -=1
    ans = min(ans, leftblack + rightwhite)
print(ans)
import numpy as np
N,K = map(int,input().split())
A = list(map(int,input().split()))
MAXBITS = 40
bits = np.ndarray((N, MAXBITS), dtype=int)
for i in range(N):
    a = A[i]
    bits[i] = np.array([(a >> j) & 1 for j in range(MAXBITS-1, -1, -1)])
sm = bits[0]
for b in bits[1:]:
    sm += b
lower = False
Kbits = np.array([(K >> j) & 1 for j in range(MAXBITS-1, -1, -1)])
ans = 0
for i in range(MAXBITS):
    ones = sm[i]
    zeros = N-sm[i]
    base = 2**(MAXBITS-i-1)
    if ones >= zeros:
        b = 0
    elif lower or Kbits[i] == 1:
        b = 1
    if Kbits[i] > b:
        lower = True
    ans += ones*base if b == 0 else zeros*base
print(ans)


### FIXED CODE ###

import numpy as np
N,K = map(int,input().split())
A = list(map(int,input().split()))
MAXBITS = 40
bits = np.ndarray((N, MAXBITS), dtype=int)
for i in range(N):
    a = A[i]
    bits[i] = np.array([(a >> j) & 1 for j in range(MAXBITS-1, -1, -1)])
sm = bits[0]
for b in bits[1:]:
    sm += b
lower = False
Kbits = np.array([(K >> j) & 1 for j in range(MAXBITS-1, -1, -1)])
ans = 0
for i in range(MAXBITS):
    ones = sm[i]
    zeros = N-sm[i]
    base = 2**(MAXBITS-i-1)
    bit = 0
    if ones >= zeros:
        bit = 0
    elif lower or Kbits[i] == 1:
        bit = 1
    if Kbits[i] > bit:
        lower = True
    ans += ones*base if bit == 0 else zeros*base
print(ans)
s=input()
if S==ABC:
    print("ARC")
else:
    print("ARC")


### FIXED CODE ###

S=input()
B="ABC"
if S==B:
    print("ARC")
else:
    print("ABC")
a,b,c=map(int,input().split())
if a+b+c >= 22:
  print("bust")
else 
  print("win")

### FIXED CODE ###

a,b,c=map(int,input().split())
if a+b+c >= 22:
  print("bust")
else:
  print("win")
import math
X=int(input())
for i in range(0,X):
  a=X+i
  c=int(math.sqrt(a))
  for j in range(2,c):
    if a%j!=0:
      print(a)
break
      

### FIXED CODE ###

X=int(input())
b=X**0.5
while any(X%i==0 for i in range(2,int(b)+1)):
  X+=1
else:
  print(X)
def get_int(): return int(input())
def get_float(): return float(input())
def get_line(): return input().split()
def get_lines(v): return [get_line() for _ in range(v)]
def get_int_line(): return list(map(int, get_line()))
def get_int_lines(v): return [get_int_line() for _ in range(v)]
def get_float_line(): return list(map(float, get_line()))
def get_float_lines(v): return [get_float_line() for _ in range(v)]
  
import functools
import math

N = get_int()
A = get_int_line()

print(functools.reduce(math.gcd, A))

### FIXED CODE ###

def get_int(): return int(input())
def get_float(): return float(input())
def get_line(): return input().split()
def get_lines(v): return [get_line() for _ in range(v)]
def get_int_line(): return list(map(int, get_line()))
def get_int_lines(v): return [get_int_line() for _ in range(v)]
def get_float_line(): return list(map(float, get_line()))
def get_float_lines(v): return [get_float_line() for _ in range(v)]

import functools
import math

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


N = get_int()
A = get_int_line()

print(functools.reduce(gcd, A))
def calc_sadness(points, b):
    return sum([abs(y - (b + x)) for x, y in points])

N = int(input())
A = [int(i) for i in input().split()]

points = [(i+1, y) for i, y in enumerate(A)]

b_list = [y - x for x, y in points]
b_list.sort(reverse=True)

sad_list = [(b, calc_sadness(points, b)) for b in b_list]
sad_list.sort(key=lambda x:x[1])

peak_sads = sorted(sad_list[:2], key=lambda x:x[0])
    
sadness_list = [calc_sadness(points, b) for b in range(peak_sads[0][0], peak_sads[1][0]+1)]

print(min(sadness_list))

### FIXED CODE ###

N = int(input())
A = [int(i) for i in input().split()]
def calc_sadness(points, b):
    return sum([abs(y - (b + x)) for x, y in points])

points = [(i+1, y) for i, y in enumerate(A)]

# y = x + b
# b = y - x
b_list = [y - x for x, y in points]
b_list.sort(reverse=True)

max_b = b_list[0]
min_b = b_list[-1]

best_ind = len(b_list)//2

minimum = calc_sadness(points, b_list[best_ind])

print(minimum)
cnt = int(input())
a = []
for i in range(cnt):
    li = list(map(int,input().split()))
    if li[0] == 0:
        a.append(n)
    elif li[0] ==1:
        print(a[n])
    else:
        a.pop(-1)


### FIXED CODE ###

cnt = int(input())
a = []
for i in range(cnt):
    li = list(map(int,input().split()))
    if li[0] == 0:
        a.append(li[1])
    elif li[0] ==1:
        print(a[li[1]])
    else:
        a.pop(-1)
a, b, c, d, e, f = map(int, input().split())
sug = 0
wat = 100*a
cap = a*e
for i in range(100):
    for j in range(100):
        if (c*i+d*j<=cap) and (wat+c*i+d*j)<=f:
            sug = max(c*i+j*d, sug)
print(wat1+sug, sug)

### FIXED CODE ###

a, b, c, d, e, f = map(int, input().split())
sug, dif2 = 0, 100
s = {}
wat1, wat2 = 100*a, 100*b
cap1, cap2 = a*e, b*e
for i in range(1,f//wat1+1):
    for j in range(f//wat2+1):
        if (wat1+cap1)*i+(wat2+cap2)*j<=f:
            s[cap1*i+cap2*j] = wat1*i+wat2*j
if s == {}:
    dif1 = f-wat1
    for i in range(10):
        for j in range(10):
            if 0<=(f-wat1)-(c*i+d*j)<dif1:
                dif1 = (f-wat1)-(c*i+d*j)
                sug3 = c*i+d*j
    print(wat1+sug3, sug3)
    exit(0)
for i in s.keys():
    for j in range(i//c+1):
        for k in range(i//d+1):
            if 0<=i-(c*j+d*k)<dif2:
                dif2 = i-(c*j+d*k)
                wat,sug1,sug2 = s[i],j,k
print(c*sug1+d*sug2+wat, c*sug1+d*sug2)
from collections import Counter
n=int(input())
a=[int(i) for i in input().split()]
dic =Counter(a)

li=[]
li2=[]
for i,j in dic.items():
    if j>=2:
        li.append(i)
    if j>=4:
        li2.append(i)
li.sort()
li2.sort()

if len(li)==1 and len(li2)==1:
    print(li2[0]**2)
else:
    print(li[-1]*max(li[-2],li2[-1]) if len(li)>=2 else 0)

### FIXED CODE ###

from collections import Counter
n=int(input())
a=[int(i) for i in input().split()]
dic =Counter(a)

li=[]

for i,j in dic.items():
    if j>=2:
        li.append(i)
    if j>=4:
        li.append(i)
li.sort()


if len(li)>=2:
    print(li[-1]*li[-2])
else:
    print(0)
n = input()
list_v = list(map(int, input.split()))
list_c = list(map(int, input.split()))

max_value = 0

for i in range(n):
    if list_v[i]>list_c[i]:
        max_value = max_value + (list_v[i]-list_c[i])
        
print(max_value)

### FIXED CODE ###

n = int(input())
list_v = list(map(int, input().split()))
list_c = list(map(int, input().split()))

max_value = 0

for i in range(n):
    if list_v[i]>list_c[i]:
        max_value = max_value + (list_v[i]-list_c[i])

print(max_value)
a,b,x=map(int,input(),split())
if 0<=x-a<=b:
  print("YES")
else:
  print("NO")

### FIXED CODE ###

a,b,c=map(int,input().split())
if a<=c<=a+b:
  print("YES")
else:
  print("NO")
from sys import stdin

S = stdin.readline().rstrip()

countlist = []

count = 0
for s in S:
    if s == "A" or s == "C" or s == "G" or s == "T":
        count += 1
    else:
        countlist.append(count)
        count = 0

print(max(countlist))

### FIXED CODE ###

from sys import stdin

S = stdin.readline().rstrip()
countlist = []

count = 0
for s in S:
    if s == "A" or s == "C" or s == "G" or s == "T":
        count += 1
    else:
        countlist.append(count)
        count = 0
countlist.append(count)
print(max(countlist))
from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(m)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v+1]
        pre[i] = v
        q.append(i)

for i in range(n):
    ans = pre[i]
    ans += 1
    print(ans)


### FIXED CODE ###

from collections import deque
n, m = map(int, input().split())
INF = 100000000

to = [[] for _ in range(100005)]
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    to[a].append(b)
    to[b].append(a)
print('Yes')
q = deque([])
dist = [INF] * n
pre = [-1] * n
dist[0] = 0
q.append(0)

while len(q) > 0:
    v = q.popleft()
    for i in to[v]:
        if dist[i] != INF:
            continue
        dist[i] = dist[v] + 1
        pre[i] = v
        q.append(i)

for i in range(1, n):
    ans = pre[i]
    ans += 1
    print(ans)
N = int(input())
S = input()
K = int(input())

s = S[K]
for i in range(N):
    if (S[i] in s) == 'True':
        pass
    else:
        S[i].replace(s,'*')

print(S)

### FIXED CODE ###

N = int(input())
S = input()
K = int(input()) - 1
s = S[K]
ans = ''

for i in S:
    if i == s:
        ans += i
    else:
        ans += '*'

print(ans)
N = int(input())
H = list(map(int, input().split()))

H.append(10**10) 
r, ans = 0, 0
for l in range(N):
    while r < N and H[r] >= H[r+1]:
        r += 1
    ans = max(ans, r-l)
    if l == r:
        r += 1
        l += 1
    print(H[l:r+1])
print(ans)

### FIXED CODE ###

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

H.append(10**10) 
r, ans = 0, 0
for l in range(N):
    while r < N and H[r] >= H[r+1]:
        r += 1
    ans = max(ans, r-l)
    if l == r:
        r += 1
        l += 1
print(ans)
X = int(input())
default = 100
rishi = 

year = 1
while True:
    default = default + 1
    if default == X:
        print(year)
        break
    year += 1

### FIXED CODE ###

import math

X = int(input())
year = 0
yokin = 100

while yokin < X:
    year += 1
    yokin += math.floor(yokin * 0.01)
print(year)
N = argv[1]
K = argv[2]
if N > K:
  return "YES"
else:
  return "NO"

### FIXED CODE ###

n, k = (int(i) for i in input().split())
if n%2 == 1:
  n = (n//2) + 1
  if k > n:
    print("NO")
  else: print("YES")
else:
  n = n/2
  if k > n:
    print("NO")
  else: print("YES")
N = int(input())
P = list(map(int, input().split()))
ans = 0
for l in range(N-1):
    first = max(P[l], P[l+1])
    second = min(P[l], P[l+1])
    r = l+2
    ans += second
    while r < N:
        w = P[r]
        r += 1
        if second <= w <= first:
            second = w
        elif first < w:
            second = first
            first = w
        ans += second

print(ans)

### FIXED CODE ###

n = int(input())
print(n*n*n)
n,m,*a=map(int,open(0).read().split())
b=[-1]*(n+1)
if m!=0:
    for i in a:
        b[i]=0
    if m>=2:    
        if a[0]==1 and a[1]==2:
            b[1],b[2]=0,0
    elif a[0]==1:
        b[2]=1
    elif a[0]==2:
        b[1]=1
    else:    
        b[1],b[2]=1,2
else:    
    b[1],b[2]=1,2  
for i in range(3,n+1):
    if b[i]==-1:
        b[i]=(b[i-1]+b[i-2])%1000000007
print(b[-1])

### FIXED CODE ###

n,m,*a=map(int,open(0).read().split())
if n==1:
    print(1)
    exit()
b=[-1]*(n+1)
a+=[0,0]
for i in a:
    b[i]=0
if a[0]==1 and a[1]==2:
    b[1],b[2]=0,0
elif a[0]==1:
    b[2]=1
elif a[0]==2:
    b[1]=1
else:    
    b[1],b[2]=1,2
for i in range(3,n+1):
    if b[i]==-1:
        b[i]=(b[i-1]+b[i-2])%1000000007
print(b[-1])
n = input()
x_str = input_row().split()
xs = [int(c) for c in x_str]

xs_len = len(xs)
center = (xs_len - 1)//2

c_xs = xs[:]
c_xs.sort()

for x in xs:
    print c_xs[center+1] if x <= c_xs[center] else c_xs[center]


### FIXED CODE ###


import numpy as np

n = int(input())
x_str = input().split()
xs = np.array([int(c) for c in x_str])

xs_len = len(xs)
center = (xs_len - 1)//2

c_xs = np.sort(xs)

for x in xs:
    if x <= c_xs[center]:
        print(c_xs[center+1])
    else:
        print(c_xs[center])
N = int(input())
h = list(map(int, input().split()))
import math
dp = [math.inf] * N
dp[0] = 0
dp[1] = abs(h[1] -h[0])
if N >= 3:
    for i in range(2, N):
        dp[i] = min(dp[i - 1] + abs(h[i] -h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]))
print(dp[N - 1])

### FIXED CODE ###

N = int(input())
h = list(map(int, input().split()))
import math
dp = [0] * N
dp[0] = 0
dp[1] = abs(h[1] -h[0])
if N >= 3:
    for i in range(2, N):
        dp[i] = min(dp[i - 1] + abs(h[i] -h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]))
print(dp[N - 1])
def gcd(a,b):
    r = a%b
    #print(a,b,r)
    if(r==0): return b
    return gcd(b,r)
n = int(input()) 
#n=4
hps = [int(i) for i in input().split()]
#lst = [2, 2, 8, 40]
#lst = [5, 13, 8, 1000000000]
#lst = [0, 1, 0]
    ##2 4 5 -> 1
    ## minと全ての要素のgcd
hps = sorted(hps)
for i in range(1,n):
    hps[0] = gcd(hps[i], hps[0])
print(hps)
for i in range(len(hps)):
    if(hps[i]==0): continue
    print(hps[i])
    break

### FIXED CODE ###

a, b= (int(i) for i in input().split())  

if(b%a==0):
    print(a+b)
else:
    print(b-a)
a, b, c, k = [int(x) for x in input().split()]

if k <= a:
  print(k)
elif a < k and k<= a+b:
  print(a)
else:
  print(a-(k-a-b))


### FIXED CODE ###

s= str(input())
t= str(input())

if len(t) == len(s) + 1 and len(s) >=1 and len(s) <= 10 and t.islower() and s.islower() and s==t[:-1]:
  print("Yes")
else:
  print("No")
n = int(input())
a = list(map(int, input().split()))

ans = []
minus_num = 0

for num in a:
    if num < 0:
        ans.append( num * -1 )
        minus_num += 1
    else:
        ans.append( num )

if minus_num % 2 == 0:
    print(sum(ans))
else:
    ans.sort()
    print(sum(ans[1:]) + ans[0] * -1)

### FIXED CODE ###

import math

a, b, t = map(int, input().split())

print( math.floor(t / a) * b )
N = int(input())
ans = []
lis = []
for i in range(N):
    lis.append(i + 1)
if N % 2 == 0:
    M = (N - 2) * N // 2
    for i in range(1, N // 2 + 1):
        for j in range(1, N):
            if j != i and i != lis[-j]:
                ans.append([i, lis[-j]])
    print(M)
    for i in range(M):
        print(' '.join(map(str, ans[i])))
else:
    M = (N - 1) ** 2 // 2
    for i in range(1, (N - 1) // 2 + 1):
        for j in range(2, N):
            if j != i + 1 and i != lis[-j]:
                ans.append([i, lis[-j]])
    for i in range(1, N):
        ans.append([N, i])
    print(M)
    for i in range(M):
        print(' '.join(map(str, ans[i])))

### FIXED CODE ###

N = int(input())
ans = []
lis = []
for i in range(N):
    lis.append(i + 1)
if N % 2 == 0:
    M = (N - 2) * N // 2
    for i in range(1, N):
        for j in range(1, N):
            if j != i and i != lis[-j]:
                if sorted([i, lis[-j]]) not in ans:
                    ans.append(sorted([i, lis[-j]]))
    print(M)
    for i in range(M):
        print(' '.join(map(str, ans[i])))
else:
    M = (N - 1) ** 2 // 2
    for i in range(1, N):
        for j in range(2, N):
            if j != i + 1 and i != lis[-j]:
                if sorted([i, lis[-j]]) not in ans:
                    ans.append([i, lis[-j]])
    for i in range(1, N):
        ans.append([N, i])
    print(M)
    for i in range(M):
        print(' '.join(map(str, ans[i])))
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = []
for i in range(M):
    b, c = map(int, input().split())
    BC.append([b,c])

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

a_itr = 0
bc_itr = 0
bc = BC[0]
while bc_itr < N:
    if bc[0] == 0:
        bc_itr += 1
        if bc_itr < M:
            bc = BC[bc_itr]
        else:
            break
    else:
        if A[a_itr] < bc[1]:
            A[a_itr] = bc[1]
            a_itr += 1
            bc[0] -= 1
        else:
            break

print(sum(A))



### FIXED CODE ###

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

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

a_itr = 0
bc_itr = 0
bc = BC[0]
while a_itr < N:
    if bc[0] == 0:
        bc_itr += 1
        if bc_itr < M:
            bc = BC[bc_itr]
        else:
            break
    else:
        if A[a_itr] < bc[1]:
            A[a_itr] = bc[1]
            a_itr += 1
            bc[0] -= 1
        else:
            break

print(sum(A))
import numpy as np
n, k = map(int, input().split())
hs = []
for i in range(n):
    hs.append(int(input()))
sorted_hs = np.sort(hs)
print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k)]))

### FIXED CODE ###

import numpy as np
n, k = map(int, input().split())
hs = []
for i in range(n):
    hs.append(int(input()))

if list(np.diff(hs)) == list(np.zeros(n-1, dtype=int)):
    print('0')
else:
    sorted_hs = np.sort(hs)
    try:
        print(min([min(np.diff(sorted_hs[i::k-1])) for i in range(k) if not len(sorted_hs[i::k-1]) == 1]))
    except:
        print('0')
import sys
import math
input = sys.stdin.readline

used = []
connect = []
N = 0
M = 0


def main():
    global used
    global connect
    global N
    global M
    sc = Scan()
    N, M = sc.intarr()
    a = [0] * M
    b = [0] * M
    for i in range(M):
        aa, bb = sc.intarr()
        a[i] = aa - 1
        b[i] = bb - 1
    connect = [[0] * M for i in range(M)]
    used = [0] * N
    for i in range(M):
        connect[a[i]][b[i]] = 1
        connect[b[i]][a[i]] = 1

    print(dfs(0, 1))


def dfs(now, depth):
    global used
    global connect
    if used[now] == 1:
        return 0

    if depth == N:
        return 1
    ans = 0
    used[now] = 1
    for i in range(N):
        if connect[now][i] == 1:
            ans += dfs(i, depth+1)
    used[now] = 0
    return ans


class Scan():

    def intarr(self):
        num_array = list(map(int, input().split()))
        return num_array

    def intarr_ver(self, n):
        return [int(input()) for _ in range(n)]

    def strarr(self):
        line = input()
        array = line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


def display(array):
    for a in range(len(array)):
        if len(array) - a != 1:
            print(array[a], end=' ')
        else:
            print(array[a])


def gcd(a, b):  # 最大公約数
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):  # 最小公倍数
    return a * b // gcd(a, b)


main()


### FIXED CODE ###

import sys
import math
input = sys.stdin.readline

used = []
connect = []
N = 0
M = 0


def main():
    global used
    global connect
    global N
    global M
    sc = Scan()
    N, M = sc.intarr()
    a = [0] * M
    b = [0] * M
    for i in range(M):
        aa, bb = sc.intarr()
        a[i] = aa - 1
        b[i] = bb - 1
    connect = [[0] * N for i in range(N)]
    used = [0] * N
    for i in range(M):
        connect[a[i]][b[i]] = 1
        connect[b[i]][a[i]] = 1

    print(dfs(0, 1))


def dfs(now, depth):
    global used
    global connect
    if used[now] == 1:
        return 0

    if depth == N:
        return 1
    ans = 0
    used[now] = 1
    for i in range(N):
        if connect[now][i] == 1:
            ans += dfs(i, depth+1)
    used[now] = 0
    return ans


class Scan():

    def intarr(self):
        num_array = list(map(int, input().split()))
        return num_array

    def intarr_ver(self, n):
        return [int(input()) for _ in range(n)]

    def strarr(self):
        line = input()
        array = line.split(' ')
        array[-1] = array[-1].strip('\n')
        return array


def display(array):
    for a in range(len(array)):
        if len(array) - a != 1:
            print(array[a], end=' ')
        else:
            print(array[a])


def gcd(a, b):  # 最大公約数
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):  # 最小公倍数
    return a * b // gcd(a, b)


main()
import copy
num = input().split(" ")
zahyou = input().split(" ")

num[1] = int(num[1])
plus = []
minus = []
rousoku = []
for i in range(int(num[0])):
    if(int(zahyou[i]) < 0):
        minus.append(int(zahyou[i]))
    else:
        plus.append(int(zahyou[i]))

# プラス方向で終了する場合

if(num[1] > len(plus)):
    plus_tmp = minus[(len(plus)-num[1]):] + plus
    minus_tmp = minus[:(len(minus)+len(plus_tmp)-num[1]-1)]
rousoku = plus_tmp[0:num[1]]
minus_tmp_2 = copy.deepcopy(minus_tmp)
while(True):
    if(rousoku[-1] - rousoku[-2] >= (min(min(rousoku), 0) - minus_tmp_2[-1]) * 2):
        rousoku.pop(-1)
        rousoku.insert(0, minus_tmp_2.pop(-1))
    else:
        break
rousoku_move_plus = []
for i in rousoku:
    rousoku_move_plus.append(i - min(rousoku) * 2)

# マイナス方向で終了する場合
if(num[1] > len(minus)):
    minus_tmp = minus + plus[:(num[1]-len(minus))]
    plus_tmp = plus[(num[1]-len(minus)):]
rousoku = minus_tmp[(-(num[1]+1)):]
plus_tmp_2 = copy.deepcopy(plus_tmp)
while(True):
    if(rousoku[1] - rousoku[0] >= (plus_tmp_2[0] - max(max(rousoku), 0)) * 2):
        rousoku.pop(0)
        rousoku.append(plus_tmp_2.pop(0))
    else:
        break
rousoku_move_minus = []
for i in rousoku:
    rousoku_move_minus.append(max(rousoku) * 2 - i)

print(min(max(rousoku_move_plus), max(rousoku_move_minus)))


### FIXED CODE ###

num = input().split(" ")
zahyou = input().split(" ")
zahyou_int = [int(i) for i in zahyou]
num[0] = int(num[0])
num[1] = int(num[1])

rousoku = []
flag = 1
time = 3 * 10 ** 8
for i in range(num[0]-num[1]+1):
    rousoku_min = zahyou_int[i]
    rousoku_max = zahyou_int[(i+num[1]-1)]
    if(flag == 0):
        break
    elif(rousoku_min > 0):
        flag = 0
    time_p = -min(0, rousoku_min) * 2 + rousoku_max
    time_m = max(0, rousoku_max) * 2 - rousoku_min
    if(min(time_p, time_m) < time):
        time = min(time_p, time_m)

print(time)