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

city,dnum,qnum=map(int,input().split())

data,querry=[],[]
dapp=data.append
qapp=querry.append
for i in range(dnum):
	dapp(list(map(int,input().split())))

for i in range(qnum):
	qapp(list(map(int,input().split())))
	
def solve(start,end):
	res=0
	for j in range(dnum):
		if start<=data[j][0] and data[j][1]<=end:
			res+=1
	return res
	
for i in range(qnum):
	print(solve(querry[i][0],querry[i][1]))

### FIXED CODE ###

def main():
    n, m, Q = map(int, input().split())
    Train= [[0 for _ in range(n)] for _ in range(n)]
    
    for i in range(m):
        l, r = map(int, input().split())
        l, r = l - 1, r - 1
        Train[l][r] += 1
    
    P_Train = [[0 for _ in range(n)] for _ in range(n)]
    for i in range(n):
        tmp = 0
        for j in range(n):
            tmp += Train[i][j]
            P_Train[i][j] = tmp

    for i in range(Q):
        p, q = map(int, input().split())
        p, q = p - 1, q - 1
        ans = solve(P_Train, n, p, q)
        print(ans)

def solve(P_Train, n, p, q):
    ans = 0
    for i in range(p, q + 1):
        if  p - 1 < 0:
            ans += P_Train[i][q]
        else:
            ans += P_Train[i][q] - P_Train[i][p - 1]
    return ans


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

import sys
from functools import reduce
input = sys.stdin.readline

# python > pypy
MOD = 10**9 + 7
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm, numbers)

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

b = list(map(lambda x: lcm_//x, a))
ans = 0
for i in range(len(b)):
    ans += b[i]
    ans %= MOD
print(ans)


### FIXED CODE ###

MAX = 1000001

mod = 1000000007

prime = [0 for i in range(MAX)]

max_map = dict()

# function to return a^n

def power(a, n):

    if n == 0:
        return 1
    p = power(a, n // 2) % mod
    p = (p * p) % mod

    if n & 1:
        p = (p * a) % mod
    return p

# function to find the smallest prime
# factors of numbers upto MAX


def sieve():
    prime[0], prime[1] = 1, 1
    for i in range(2, MAX):
        if prime[i] == 0:
            for j in range(i * 2, MAX, i):
                if prime[j] == 0:
                    prime[j] = i
            prime[i] = i

# function to return the LCM modulo M


def lcmModuloM(arr, n):

    for i in range(n):
        num = arr[i]

        temp = dict()

        # temp stores mapping of prime factors
        # to its power for the current element
        while num > 1:

            # factor is the smallest prime
            # factor of num
            factor = prime[num]

            # Increase count of factor in temp
            if factor in temp.keys():
                temp[factor] += 1
            else:
                temp[factor] = 1

            # Reduce num by its prime factor
            num = num // factor

        for i in temp:
            # store the higest power of every prime
            # found till now in a new map max_map
            if i in max_map.keys():
                max_map[i] = max(max_map[i], temp[i])
            else:
                max_map[i] = temp[i]

    ans = 1

    for i in max_map:

        # LCM is product of primes to their
        # higest powers modulo M
        ans = (ans * power(i, max_map[i])) % mod
    return ans


def rep_sqr(base, k, mod):
    if k == 0:
        return 1
    elif k % 2 == 0:
        return (rep_sqr(base, k / 2, mod) ** 2) % mod
    else:
        return (rep_sqr(base, k - 1, mod) * base) % mod

# a^(-1) == a^(m-2)


def flt(a, mod):
    return rep_sqr(a, mod-2, mod)

# Driver code
sieve()

# python > pypy
MOD = 10**9 + 7

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

lcm_ = lcmModuloM(a, n)

b = list(map(lambda x: lcm_ * flt(x, mod), a))
ans = 0
for i in range(len(b)):
    ans += b[i]
    ans %= MOD
print(ans)
### BUGGY CODE ###

import fractions


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def check():
    N, M = map(int, input().split())
    S = input()
    T = input()
    length = lcm(N,M)
        
    for i in range(length):
        if i%(length//N)==0 and i%(length//M)==0:
            if S[i//(length//N)] != T[i//(length//M)]:
                return -1
    return length
  
print(check())

### FIXED CODE ###

import fractions


def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def check():
    N, M = map(int, input().split())
    S = input()
    T = input()
    length = lcm(N,M)
        
    for i in [a*(length//N) for a in range(N)]:
        if i%(length//M)==0:
            if S[i//(length//N)] != T[i//(length//M)]:
                return -1
    return length
  
print(check())
### BUGGY CODE ###

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    p = list(map(int, input().split()))
    l = []
    for i in range(n):
        l.append([p[i], i])
    l.sort(reverse=True)
    s = [-1, n]
    res = 0
    
    for num, idx in l:
        if len(s) == 2:
            s = [s[0], idx, s[1]]
        else:
            a = bisect_left(s, idx)
            if a == 1:
                res += num * ((s[a+1]-s[a]) * (idx+1))
            elif a== len(s)-1:
                res += num * ((n-idx) * (s[a-1]-s[a-2]))
            else:
                res += num * ((s[a+1]-s[a]) * (idx-s[a-1]) + (s[a]-idx) * (s[a-1]-s[a-2]))
            s.insert(a, idx)
    print(res)

if __name__ == '__main__':
    main()

### FIXED CODE ###

# 区間ではなく各要素を独立に考える典型のやつ

# ある値について条件を満たすようにしたいとき
# その値より大きいものだけ探せば良い
# 具体的には自分より大きい値を1つだけ含むような区間が条件を満たす

# 愚直にやると各値について, その左右を近い方から順に見ていかなければならず間に合わない
# 自分より大きい値だけ知りたいので, 要素の大きい順に見ていくことで計算量が減る

# 普通にやったのが下のコードでAC+TLE(恐らく合ってる)

def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

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

    # 1-indexed
    # f[i]:要素iのインデックス
    f = [0]*(n+1)
    for i in range(n):
        f[p[i]] = i+1

    # 各要素よりも大きい値のインデックスを参照するための配列
    l = [0] + [i for i in range(n+1)]
    r = [i + 1 for i in range(n+1)] + [n+1]

    res = 0

    for i in range(1,n+1):
        # x:要素iのインデックス
        x = f[i]
        
        # iは1から見ていくので, 選んだ時点でiより大きい数のインデックスが分かる
        l1,r1 = l[x],r[x]
        l2,r2 = l[l1],r[r1]

        res += ((l1-l2)*(r1-x) + (x-l1)*(r2-r1))*i

        # 要素iのインデックスをこれ以降参照することはないので消す
        l[r1],r[l1] = l1,r1

    print(res)

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

import itertools
import numpy as np

if __name__ == "__main__":

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

	# print(P)
	# 一度計算したサイクル情報をキャッシュしておくための配列
	cycleIDs = np.full( N, -1, np.int64 )
	cycleInfs = []
	cycleID = 0

	for n in range(N):

		v = n

		if cycleIDs[v] != -1:
			continue
		else:
			currentCycleItemCnt = 0
			currentCycleCosts = []
			while True:
				# 全頂点について、属するサイクルを計算する
				currentCycleItemCnt += 1
				currentCycleCosts.append( C[v] )
				cycleIDs[v] = cycleID

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


	scores = []
	for cycleID in cycleIDs:

		currentCycleItemCnt = cycleInfs[cycleID][0]
		currentCycleCosts = cycleInfs[cycleID][1]
		currentCycleTotalScore = np.sum(currentCycleCosts)

		# ループを含めない最大の処理回数
		procCnt = K % currentCycleItemCnt
		# ただし、割り切れる場合も、1サイクル分は処理するものとする
		# (1回以上はやらなければならないのを考慮？)
		if procCnt == 0:
			procCnt = currentCycleItemCnt

		# サイクル内でループしてスコアを稼ぐ場合の考慮
		loopScore = 0
		if currentCycleTotalScore > 0:
			cycleLoopCnt =  ( K - procCnt ) // currentCycleItemCnt
			loopScore = cycleLoopCnt * currentCycleTotalScore

		# このサイクルに属する全頂点分をまとめて計算する
		for i in range(currentCycleItemCnt):
			# 累積和
			# acc = list(itertools.accumulate( np.roll( currentCycleCosts, i )[:procCnt] ) )
			scores.append( max( itertools.accumulate( np.roll( currentCycleCosts, i )[:procCnt] ) ) + loopScore )

	print(max(scores))


### 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 ###

h,w = map(int,input().split())
meiro = [list(input()) for i in range(h)]
max_d = 0
from collections import deque



def bfs(sx,sy,t_i,t_j):
    que = deque([(sx,sy)])
    INF= float('inf')
    d = [[INF]*w for i in range(h)]
    d[sx][sy] = 0
    while que:
        x,y =que.popleft()
        for dx,dy in [(0,1),(1,0),(-1,0),(0,-1)]:
            nx,ny = x+dx,y+dy
            if 0 <= nx < h and 0 <= ny < w and d[nx][ny] == INF and meiro[nx][ny] =='.':
                d[nx][ny] = d[x][y] +1
                que.append((nx,ny))
    return d[t_i][t_j] if d[t_i][t_j] != float('inf') else 0

for s_i in range(h):
    for s_j in range(w):
        for t_i in range(h):
            for t_j in range(w):
                if not (s_i == t_i and s_j == t_j):
                    # visited = [[0 for i in range(w)] for i in range(h)]
                    max_d = max(bfs(s_i,s_j,t_i,t_j), max_d)
print(max_d)

### FIXED CODE ###

h,w = map(int,input().split())
meiro = [list(input()) for i in range(h)]
max_d = 0
from collections import deque

def bfs(sx,sy):
    que = deque([(sx,sy,0)])
    d = [[0]*w for i in range(h)]
    d[sx][sy] = 1
    max_len=0
    while que:
        x,y,tmp_len = que.popleft()
        for dx,dy in [(0,1),(1,0),(-1,0),(0,-1)]:
            tmp = tmp_len
            nx,ny = x+dx,y+dy
            if 0 <= nx < h and 0 <= ny < w and d[nx][ny] == 0 and meiro[nx][ny] =='.':
                d[nx][ny] = 1
                max_len = max(max_len, tmp+1)
                que.append((nx,ny,tmp+1))
    return max_len

for s_i in range(h):
    for s_j in range(w):
        if meiro[s_i][s_j] == "#":
            continue
        max_d = max(bfs(s_i,s_j), max_d)
print(max_d)
### BUGGY CODE ###

#create date: 2020-05-03 21:37

import sys
stdin = sys.stdin

import numpy as np

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = np.array(na())
    b = np.arange(1, n+1)
    #print(a, b)
    ans = 0
    for i in range(n):
        anow = a[i]
        j = i+anow+1
        if j < n:
            ans += sum(a[j:]== b[:n-j])
    print(ans)


if __name__ == "__main__":
    main()

### FIXED CODE ###

#create date: 2020-05-03 21:37

import sys
stdin = sys.stdin
from collections import defaultdict

import numpy as np

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = np.array(na())
    b = np.arange(1, n+1)
    c = a + b
    d = b - a
    l = defaultdict(lambda: 0)
    ans = 0
    for ci in c:
        l[ci] += 1
    for di in d:
        ans += l[di]
    print(ans)


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

N, M = map(int, input().split())
D = []
for i in range(N):
    t, v = map(int, input().split())
    D.append((t, v))

D.sort(key=lambda x:x[0])

DP = [[0 for j in range(M)] for i in range(N + 1)]

for i in range(1, N + 1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP[i][j] = max(DP[i - 1][j], DP[i - 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP[i][j] = DP[i - 1][j]
mv = 0
for l in range(N): #ラストオーダー
    mv = max(DP[l][M - 1] + D[l][1], mv)

print(mv)





### FIXED CODE ###

N, M = map(int, input().split())
D = []
for i in range(N):
    t, v = map(int, input().split())
    D.append((t, v))

D.sort(key=lambda x:x[0])

DP = [[0 for j in range(M)] for i in range(N + 1)]

for i in range(1, N + 1):
    for j in range(1, M):
        if j - D[i - 1][0] >= 0:
            DP[i][j] = max(DP[i - 1][j], DP[i - 1][j - D[i - 1][0]] + D[i - 1][1])
        else:
            DP[i][j] = DP[i - 1][j]
mv = 0
for l in range(N): #ラストオーダー
    mv = max(DP[l][M - 1] + D[l][1], mv)

print(mv)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
n,m = map(int,input().split())
d = {}
for i in range(n):
  a,b = map(int,input().split())
  if a in d.keys():
    d[a] += b
  else:
    d.setdefault(a,b)
l = list(d.keys())
l.sort()
count = 0
mon = 0
i = 0
while count < m:
  mon += l[i]*d[l[i]]
  count += d[l[i]]
  i += 1
i -= 1
if count > m:
  mon -= (count-m)*l[i]
print(mon)

### FIXED CODE ###

import sys
input = sys.stdin.readline
n,m = map(int,input().split())
l = []
for i in range(n):
  a,b = map(int,input().split())
  l.append([a,b])
l.sort()
i = 0
count = 0
money = 0
while count < m:
  count += l[i][1]
  money += l[i][0]*l[i][1]
  i += 1
i -= 1
if count > m:
  money -= (count-m)*l[i][0]
print(money)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
from fractions import gcd
MOD = 10**9 + 7

n = int(input())
a = [int(item) for item in input().split()]

lcm = 1
for item in a:
    lcm = lcm * item // gcd(lcm, item)

ans = 0
for item in a:
    ans += lcm // item
    ans %= MOD

print(ans)

### FIXED CODE ###

#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
from fractions import gcd
MOD = 10**9 + 7

n = int(input())
a = [int(item) for item in input().split()]


lcm = 1
for item in a:
    lcm = lcm * item // gcd(lcm, item)
lcm %= MOD

ans = 0
for item in a:
    ans += lcm * pow(item, MOD-2, MOD)
    ans %= MOD

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

a,b,c,d = map(int,input().split())
cnt = 0
for i in range(a,b+1):
    if i%c==0 or i%d==0:
        cnt += 1
print(b+1-a-cnt)

### FIXED CODE ###

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)

A, B, C, D = map(int, input().split())

b_num = B//C + B//D - B//lcm(C, D)
a_num = (A-1)//C + (A-1)//D - (A-1)//lcm(C, D)

print((B-A+1) -(b_num - a_num))
### BUGGY CODE ###

INF = float("inf")

H, N, *AB = map(int, open(0).read().split())

dp = [0] + [INF] * H

for a, b in zip(*[iter(AB)] * 2):
    for i in range(H + 1):
        idx = min(i + a, H)
        dp[idx] = min(dp[idx], dp[i] + b)

print(dp[H])

### FIXED CODE ###

INF = float("inf")

H, N, *AB = map(int, open(0).read().split())

dp = [INF] * (H + 10 ** 4 + 10)
dp[0] = 0

for a, b in zip(*[iter(AB)] * 2):
    for i in range(a, len(dp)):
        if dp[i - a] != INF:
            dp[i] = min(dp[i], dp[i - a] + b)

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

import sys
import heapq
input = sys.stdin.readline

class Node:
    def __init__(self, id):
        self.id = id
        self.check = False
        self.link = []
        self.cost = []
    
    def __lt__(self, other):
        return self.id < other.id

def main():
    N = int(input())
    
    nodes = [Node(i) for i in range(N)]

    xy = []
    for i in range(N):
        x, y = map(int, input().split())
        xy.append((i, x, y))
    
    for t in range(2):
        if t == 0:
            xy = sorted(xy, key=lambda x: x[1])# x昇順
        else:
            xy = sorted(xy, key=lambda x: x[2])# y昇順
        p_n, p_x, p_y = xy[0]
        for i in range(1, N):
            n, x, y = xy[i]
            if t == 0:
                cost = abs(x - p_x)# x
            else:
                cost = abs(y - p_y)# y
            nodes[p_n].link.append(nodes[n])
            nodes[p_n].cost.append(cost)
            nodes[n].link.append(nodes[p_n])
            nodes[n].cost.append(cost)
            p_n, p_x, p_y = n, x, y
    q = []
    ans = 0
    heapq.heappush(q, (0, nodes[0]))# edge_cost, node
    while len(q) > 0:
        edge_cost, node = heapq.heappop(q)
        if node.check:
            continue
        node.check = True
        ans += edge_cost
        for i, next_node in enumerate(node.link):
            if not next_node.check:
                heapq.heappush(q, (node.cost[i], next_node))
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

def same_tree(node_a, node_b):
    if node_a.root() == node_b.root():
        return True

def unite(node_a, node_b):
    root_a = node_a.root()
    root_b = node_b.root()
    if root_a == root_b:
        return
    if root_b.depth > root_a.depth:
        root_a, root_b = root_b, root_a
    root_b.is_root = False
    root_b.parent = root_a
    root_a.depth = max(root_a.depth, root_b.depth+1)

class Node:
    def __init__(self):
        self.is_root = True
        self.parent = None
        self.depth = 1
    
    def root(self):
        node = self
        while not node.is_root:
            node = node.parent
        return node

def main():
    N = int(input())
    
    nodes = [Node() for i in range(N)]

    # 各町の座標をタプル(i, x, y)に入れて配列に格納
    xy = []
    for i in range(N):
        x, y = map(int, input().split())
        xy.append((i, x, y))
    
    # 使う可能性のある辺のみを取り出す
    edges = []
    xy = sorted(xy, key=lambda x: x[1])# x座標昇順
    # print("x:", xy)
    for i in range(1, N):
        edges.append((xy[i][1] - xy[i-1][1], xy[i-1][0], xy[i][0]))
    xy = sorted(xy, key=lambda x: x[2])# y座標昇順
    # print("y:", xy)
    for i in range(1, N):
        edges.append((xy[i][2] - xy[i-1][2], xy[i-1][0], xy[i][0]))
    
    ans = 0
    edges = sorted(edges)
    # print(edges)
    for (cost, a, b) in edges:
        if not same_tree(nodes[a], nodes[b]):
            unite(nodes[a], nodes[b])
            ans += cost
    print(ans)




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

N, M = (int(x) for x in input().split())
p = [0]*M
S = ["a"]*M
wa = 0
wapre = 0
ac = 0
index = []
for i in range(M):
  p[i], S[i] = (x for x in input().split())
  if S[i] == "AC" and p[i] not in index:
    ac = ac + 1
    index.append(p[i])
  elif S[i] == "WA" and p[i] not in index:
    wa = wa + 1
for i in range(M):
  if p[i] not in index:
    wa = wa-1
print(ac, wa)

### FIXED CODE ###

N,M=map(int,input().split())
A=[0]*N
B=[0]*N
pen=0
for i in range(M):
  p,S=input().split()
  if S=="AC":
    A[int(p)-1]=1
  elif S == "WA" and A[int(p)-1] == 0:
    B[int(p)-1] += 1
penalty=[A[i]*B[i] for i in range(N)]
print(sum(A),sum(penalty))
### BUGGY CODE ###

import sys
sys.setrecursionlimit(100000000)


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

INF=float("inf")

DP=[INF]*(N+1)
DP[0]=0

def rec(n):
    if DP[n]!=INF:return DP[n]
    res=INF
    for i in range(1,K+1):
        if n-i<0:break
        res=min(res,rec(n-i)+abs(h[n-i]-h[n]))
    DP[n]=res
    return res

ans=rec(N-1)

print(ans)


### FIXED CODE ###

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

DP=[10000000000]*N

DP[0]=0
for i in range(1,N):
    temp=[]
    if i<K:
        for j in range(i):
            temp.append(DP[j]+abs(h[j]-h[i]))
    else:
        for j in range(1,K+1):
            temp.append(DP[i-j]+abs(h[i]-h[i-j]))
    DP[i]=min(temp)

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

#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1
sys.setrecursionlimit(10**7)

n=int(input())
L=list(map(int,input().split()))
A = L.count(1)
B = L.count(2)
C = L.count(3)
ABC = A + B + C
dp=[[[-1] * (301) for i in range(301)] for i in range(301)]

def rec (a,b,c):
  if not (a >= 0 and b >= 0 and c >= 0):
    return 0
  if a == b == c == 0:
    return 0
  if dp[a][b][c] != -1:
    return dp[a][b][c]
  result = n
  result += rec(a-1,b,c) * a
  result += rec(a+1,b-1,c) * b
  result += rec(a,b+1,c-1) * c
  dp[a][b][c] = result / (a+b+c)
  return dp[a][b][c]

print(rec(A,B,C))

### FIXED CODE ###

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

S = [0] * 3
for a in A:
  S[a-1] += 1
I, J, K = S

dp = [[[0.0] * (N+1) for j in range(N+1)] for i in range(N+1)]

for k in range(S[2] + 1):
  for j in range(S[1] + S[2] - k + 1):
    for i in range(S[0] + S[1] + S[2] - j - k + 1):
      if i == j == k == 0: continue
      dp[i][j][k] = (i * (dp[i-1][j][k] if i > 0 else 0) + j * (dp[i+1][j-1][k] if j > 0 else 0) + k * (dp[i][j+1][k-1] if k > 0 else 0) + N) / (i + j + k)

print(dp[I][J][K])
### BUGGY CODE ###

import sys

s = str(sys.stdin.readline())
count = 0
f = False
while True:
    p1 = s.find("01")
    p2 = s.find("10")
    if p1 == -1 and p2 == -1:
        break
    elif p1 == -1:
        i = p2
    elif p2 == -1:
        i = p1
    else:
        i = p1 if p1 < p2 else p2

    count += 2
    s = s[:i] + s[i + 2:]
    
print(count)

### FIXED CODE ###

import sys

s = str(sys.stdin.readline())
c0 = s.count('0')
c1 = s.count('1')
r = c0 if c0 < c1 else c1
print(r*2)
### BUGGY CODE ###

N = int(input())
*a,= map(int, input().split())
count={}
for i in range(N):
  count[a[i]]=0
for i in range(N):
  count[a[i]]+=1

def ncn(n):
  return n*(n-1)//2

def without(a,k):
  cc = 0
  for key in count.keys():
    kc = count[key]
    if key==a[k]:
       kc-=1
    if kc>1:
       cc+=ncn(kc)
  return cc
   
for k in range(N):
  print(without(a,k))

### FIXED CODE ###

N = int(input())
*a,= map(int, input().split())
count,result={},{}

for i in range(N):
  count[a[i]]=0
  result[a[i]]=0
  
def ncn(n):
  return n*(n-1)//2

for i in range(N):
  count[a[i]]+=1

total=0
for key in count.keys():
  kc = count[key]
  result[key]=ncn(kc)
  total += result[key]
  
def without(k):
  return total-count[a[k]] + 1

for k in range(N):
  print(without(k))
### BUGGY CODE ###

import numpy as np
n, k = map(int, input().split())
x = np.array(list(map(int, input().split())))

ans = []
for i in range(n - k + 1):
    left = 0
    right = 0
    arr = x[i: i + k]
    if arr.min() < 0:
        left = (np.abs(arr[np.where(arr < 0)])).max()
    if arr.max() >= 0:
        right = (arr[np.where(arr >= 0)]).max()
    ans.append(min(left * 2 + right, left + right * 2))

print(min(ans))

### FIXED CODE ###

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

ans = []
for i in range(n - k + 1):
    left = x[i]
    right = x[i + k - 1]
    ans.append(min(abs(left) + abs(right - left),
                   abs(right) + abs(left - right)))

print(min(ans))