Harryxun/stf_run huggingface dataset:
from heapq import heappop,heappush
n=int(input())
pair=[i for i in range(n)]
que=[]
e=[None]*n
for i in range(n):
  x,y=map(int,input().split())
  e[i]=(i,x,y)
ex=sorted(e,key=lambda x:x[1])
ey=sorted(e,key=lambda x:x[2])
for i in range(n-1):
  w=ex[i+1][1]-ex[i][1]
  heappush(que,(w,ex[i][0],ex[i+1][0]))
for i in range(n-1):
  w=ey[i+1][2]-ey[i][2]
  heappush(que,(w,ey[i][0],ey[i+1][0]))
def find(x):
  if x==pair[x]:return x
  else:
    tmp=find(pair[x])
    pair[x]=tmp
    return tmp
q=[]
ans=0
while que:
  w,s,t=heappop(que)
  root_s=find(s)
  root_t=find(t)
  if root_s!=root_t:
    pair[root_s]=root_t
    ans+=w
print(ans)

### FIXED CODE ###

from heapq import heappop,heappush
import sys
sys.setrecursionlimit(10**6)
n=int(input())
pair=[i for i in range(n)]
que=[]
e=[None]*n
for i in range(n):
  x,y=map(int,input().split())
  e[i]=(i,x,y)
ex=sorted(e,key=lambda x:x[1])
ey=sorted(e,key=lambda x:x[2])
for i in range(n-1):
  w=ex[i+1][1]-ex[i][1]
  heappush(que,(w,ex[i][0],ex[i+1][0]))
for i in range(n-1):
  w=ey[i+1][2]-ey[i][2]
  heappush(que,(w,ey[i][0],ey[i+1][0]))
def find(x):
  if x==pair[x]:return x
  else:
    tmp=find(pair[x])
    pair[x]=tmp
    return tmp
q=[]
ans=0
while que:
  w,s,t=heappop(que)
  root_s=find(s)
  root_t=find(t)
  if root_s!=root_t:
    pair[root_s]=root_t
    ans+=w
print(ans)
a = x*x*x
 return a

### FIXED CODE ###

x = input()
ans = x**3
print ans
# coding:utf-8

import math

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

dist_list = [abs(x-y) for x in map(int, input().split())]

if n == 1:
    d = dist_list[0]
else:
    d = math.gcd(dist_list[1], dist_list[0])
    for i in range(n-2):
        d = math.gcd(d, dist_list[i+1])

print(d)

### FIXED CODE ###

# coding:utf-8

from fractions import gcd 

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

dist_list = [abs(x-y) for x in map(int, input().split())]

if n == 1:
    d = dist_list[0]
else:
    d = gcd(dist_list[1], dist_list[0])
    for i in range(n-2):
        d = gcd(d, dist_list[i+1])

print(d)
a, b = map(int,input().split())
if b < 0:
    if (b-a+1) % 2 == 0:
        ans = 'Positive'
    elif (b-a+1) % 2 == 1:
        ans = 'Negative'
        
elif a > 0:
     ans='Positive'
else
     ans='Zero'
  
print(ans)

### FIXED CODE ###

a,b=map(int,input().split())
if b < 0:
    if (b-a+1) % 2 == 0:
        ans = 'Positive'
    elif (b-a+1) % 2 == 1:
        ans = 'Negative'
elif a > 0:
 ans='Positive'
else:
 ans='Zero'
  
print(ans)
import sys
import math
import collections

readline = sys.stdin.readline

def myindex(t, x, default=-1):
    if x in t:
        return t.index(x)
    else:
        return default


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

    lenB = len(B)
    log2lenB = int(math.log2(lenB))

    A.sort()
    C.sort()

    countAlist = list(zip(*list(collections.Counter(A).items())))
    countClist = list(zip(*list(collections.Counter(C).items())))

    #print(countClist)

    sumAlist = [0] * (max(A)+1)
    sumClist = [0] * (max(C)+1)

    for i in range(max(A)+1):
        index = myindex(countAlist[0], i)
        sumAlist[i] = sumAlist[i-1] + (index >= 0) * countAlist[1][index]

    for i in range(max(C)+1):
        j = max(C) - i + 1
        index = myindex(countClist[0], j)
        sumClist[i] = sumClist[i-1] + (index >= 0) * countClist[1][index]

    ans = 0

    for i in range(lenB):
        target = B[i]
        upper = lenB - 1
        lower = 0
        middle = (upper + lower) // 2

        for _ in range(log2lenB+1):
            if target > max(A):
                lower = len(A) - 1
                break
            if target == A[middle]:
                lower = middle - 1
                break
            elif target < A[middle]:
                upper = middle
            else:
                lower = middle
            middle = (upper + lower) // 2 + 1
        
        ansA = sumAlist[A[lower]]
        print('A:{} B:{} C:{}'.format(A, B, C))
        print('A:{} B:{}, ansA:{}'.format(A[lower], target, ansA))

        upper = lenB - 1
        lower = 0
        middle = (upper + lower) // 2

        for _ in range(log2lenB+1):
            if target == C[middle]:
                upper = middle + 1
                break
            elif target < C[middle]:
                upper = middle
            else:
                lower = middle
            middle = (upper + lower) // 2

        ansC = sumClist[-C[upper]]
        print('B:{} C:{}, ansC:{}'.format(target, C[upper], ansC))
        ans += ansA * ansC


    print(ans)


if __name__ == "__main__":
    main()


### FIXED CODE ###

import sys
import bisect

readline = sys.stdin.readline

def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))
    C = list(map(int, readline().split()))

    A.sort()
    C.sort()

    ans = 0
    for b in B:
        ansA = bisect.bisect_left(A, b)
        ansC = N - bisect.bisect_right(C, b)
        ans += ansA * ansC

    print(ans)


if __name__ == "__main__":
    main()
from collections import Counter

N = int(input())

def prime(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

c = Counter(prime(N))

ans = 0
for i in c.values():
    if i > 0:
        a = 0
        for j in range(1, cnt+1):
            a += j
            if a > cnt:
                break
            b += 1    
        ans += b

print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())

def prime(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

c = Counter(prime(N))

ans = 0
for i in c.values():
    if i > 0:
        a = 0
        b = 0
        for j in range(1, i+1):
            a += j
            if a > i:
                break
            b += 1    
        ans += b

print(ans)
n=input_raw()
x=map(int,raw_input().split())

for i in range(len(x), 1):
  print "%d " %(x[i])
print "%d" %(x[0])

### FIXED CODE ###

n = int(raw_input())
a = map(int,raw_input().split())
a.reverse()
for i in a:
    print i,
n, m, l = [int(i) for i in input().split()]

A = [input(range(n))]
B = [input(range(m))]
C = [[0] * 1 for n in range(n)]


for ni in range(n):
    A.append([int(i) for i in input().split()])

for mi in range(m):
    B.append([int(i) for i in input().split()])

C[n][m] = sum([A[n][l] * B[l][m] for l in range(m)])

print(" ".join([str(v) for v in C]))

### FIXED CODE ###

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

A = []
B = []
C = []

for ni in range(n):
    A.append([int(i) for i in input().split()])

for mi in range(m):
    B.append([int(i) for i in input().split()])

for i in range(n):
    C.append([])
    for j in range(l):
        C[i].append(0)
        for k in range(m):
            C[i][j] += A[i][k] * B[k][j]

for li in range(n):
    print(" ".join([str(s) for s in C[li]]))
import sys
from itertools import accumulate
import numpy as np


def solve():
    N, K, *P = map(int, sys.stdin.read().split())
    n = np.convolve(np.array(P), np.ones(K), mode='valid').argmax()
    cusum = tuple(accumulate(range(1000+1)))
    return sum(cusum[i] / i for i in P[n:n+K])


if __name__ == "__main__":
    answer = solve()
    print(answer)


### FIXED CODE ###

import sys
from itertools import accumulate


def solve():
    N, K = map(int, sys.stdin.readline().split())
    cusum_p = (0,) + tuple(accumulate(map(int, sys.stdin.read().split())))
    return (max(i - j for i, j in zip(cusum_p[K:], cusum_p[:-K])) + K) / 2


if __name__ == "__main__":
    answer = solve()
    print(answer)
n = int(input())
v = [list(map(int,input().split())) for _ in range(n-1)]
g = {}
for a,b,w in v:
    if a not in g:
        g[a] = []
    g[a].append([b,w])
from collections import deque
queue = deque([[1,0]])
b = deque()
done = set([])
while queue:
    q,d = queue.popleft()
    if d%2==1:
        b.appendleft(q)
    done |= set([q])
    for new_q in g[q]:
        v_,d_ = new_q
        if v_ not in done:
            queue.appendleft([v_,d+d_])
for i in range(1,n+1):
    if i in b:
        print(1)
    else:
        print(0)

### FIXED CODE ###

n = int(input())
v = [list(map(int,input().split())) for _ in range(n-1)]
g = {}
for a,b,w in v:
    if a not in g:
        g[a] = []
    if b not in g:
        g[b] = []
    g[a].append([b,w])
    g[b].append([a,w])
from collections import deque
queue = deque([[1,0]])
b = [-1]*n
b[0] = 0
while queue:
    q,d = queue.popleft()
    if d%2==1:
        b[q-1] = 0
    else:
        b[q-1] = 1
    for new_q in g[q]:
        v_,d_ = new_q
        if b[v_-1]==-1:
            queue.appendleft([v_,d+d_])
for i in b:
    print(i)
n=int(input())
for s in range(n//4+1):
    for t in range(n//7+1):
        if 4*s+7*t==n:
             print("Yes")
            break
        else:
            print("No")

### FIXED CODE ###

n=int(input())
d=0
for i in range(n//4+1):
    for j in range(n//7+1):
        if 4*i+7*j==n:
            d+=1
print("Yes" if d!=0 else "No")
y, m, d = input().split("/")
if y > 2019:
    print("TBD")
elif y == 2019:
    if m > 5:
        print("TBD")
else:
    print("Heisei")

### FIXED CODE ###

y, m, d = map(int, input().split("/"))
if y > 2019:
    print("TBD")
elif y == 2019:
    if m > 4:
        print("TBD")
    else:
        print("Heisei")
else:
    print("Heisei")
import sys
from math import factorial
from collections import Counter
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
import numpy as np
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, k = MAP()
a = LIST()
a = np.array(a)
def update(a):
    b = np.zeros((n, n))
    for i in range(n):
        b[i, max(0, i-int(a[i])):min(n, i+int(a[i])+1)] = 1
    return np.sum(b, axis=0)
end = np.full(n, n)

for i in range(k):
    if np.all(a == end):
        break
    a = update(a)
for i in range(n):
    print(str(int(a[i]))+" ",end = "" )

### FIXED CODE ###

import sys
from math import factorial
from collections import Counter
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
MOD = 10**9 + 7

n, k = MAP()
a = LIST()


end = [n]*n

for i in range(min(k, 41)):
    b = [0]*(n+1)
    for j in range(n):
        b[max(j-a[j], 0)] +=1
        if j+a[j] <n:
            b[j+a[j]+1] -=1
    for j in range(1,n):
        b[j] += b[j-1]
    a = b[:n]

for i in range(n):
    print(str(int(a[i]))+" ",end = "" )
n = input()
l = len(n)
n = int(n)
ans = 0


def c(l, n, ans):

    a = n // (10 ** (l - 1))
    if l == 1:
        if a <= 5:
            return ans + a
        else:
            return ans + 1 + (10 - a)
    elif a == 5 and l > 1:
        if (n // (10 ** (l - 2))) % 10 <= 4:
            return c(l - 1, n % (10 ** (l - 1)), ans + a)
        else:
            return c(l, 10 ** l - n, ans + 1)
    elif a <= 4 and l > 1:
        return c(l - 1, n % (10 ** (l - 1)), ans + a)
    else:
        return c(l, 10 ** l - n, ans + 1)


print(c(l, n, ans))


### FIXED CODE ###

n = str(input())[::-1]
n = n + "0"
l = len(n)
ans = 0
flg = 0
check = 0

for i in range(l):
    check = int(n[i])
    check += flg
    if check == 5:
        if int(n[i + 1]) >= 5:
            ans += 5
            flg = 1
        else:
            ans += 5
            flg = 0
    elif check > 5:
        ans += 10 - check
        flg = 1
    else:
        ans += check
        flg = 0
print(ans + flg)
# numpyを使用した場合
import numpy as np

h, w = map(int, input().split())
s = [list(input()) for _ in range(h)]
f = np.zeros((h+2, w+1), dtype='int64')
d = np.ones((3, 3), dtype='int64')

for i in range(h):
    for j in range(w):
        if s[i][j] == '#':
            f[i:i+3, j:j+3] += d 

for i in range(h):
    ans = ''
    for j in range(w):
        if s[i][j] == '#':
            ans += '#'
        else:
            ans += str(f[i+1, j+1])
    print(ans)

### FIXED CODE ###

H, W = map(int, input().split())
s = [list(input()) for i in range(H)]
n = [[0]*(W+2) for i in range(H+2)]
dxy = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)]

for h in range(H):
  for w in range(W):
    if s[h][w] == '#':
      for i in dxy:
        n[h + 1 + i[0]][w + 1 + i[1]] += 1

for i in range(H):
  for j in range(W):
    if s[i][j] == '#':
      n[i + 1][j + 1] = '#'
    else:
      n[i + 1][j + 1] = str(n[i + 1][j + 1])

for a in n[1:H+1]:
    print(''.join(a[1:W+1]))
from itertools import accumulate
n, k = map(int, input().split())
p = list(map(int, input().split()))
p_acc = list(accumulate(p))
ans_max = 0
for i in range(n-k):
    ans_max = max(ans_max, p_acc[i+k] - p_acc[i])
    if p_acc[i+k] - p_acc[i] == ans_max:
        ans = i
print((p_acc[i+k] - p_acc[i] + k) / 2)


### FIXED CODE ###

from itertools import accumulate
n, k = map(int, input().split())
p = list(map(int, input().split()))
p_acc = list(accumulate(p))
ans_max = p_acc[k-1]
ans = 0
if n > k:
    for i in range(n-k):
        ans_max = max(ans_max, p_acc[i+k] - p_acc[i])
        if p_acc[i+k] - p_acc[i] == ans_max:
            ans = i
    print((p_acc[ans+k] - p_acc[ans] + k) / 2)
else:
    print((sum(p) + n) / 2)
h,w,k=map(int,input().split())
S=[]
for i in range(h):
    s=int(input(),2)
    S.append(s)
ans=100000
for i in range(2**(h-1)):
    now=0 #区切った数
    cnt=0 #1の数
    y=0
    for j in range(h-1):
        if (i>>j)&1:
            y+=1
    C=[0]*(y+1) #1の数保存配列
    for x in range(w):
        T=[]　#現在の列での1の数保存配列
        for j in range(h):      
            cnt+=(S[j]>>x)&1
            if (i>>j)&1: #横の区切り判定
                T.append(cnt)
                cnt=0
            if j==h-1: #最後の列
                T.append(cnt)
                cnt=0
        for j in range(len(C)):
            if C[j]+T[j]>k:
                now+=1
                C=T
                break
            else:
                C[j]+=T[j]
    ans=min(ans,now+y)
print(ans)

### FIXED CODE ###

h,w,k=map(int,input().split())
S=[]
for i in range(h):
    s=int(input(),2)
    S.append(s)
ans=h+w-2
for i in range(2**(h-1)):
    flag=False
    now=0 #区切った数
    cnt=0 #1の数
    y=0
    for j in range(h-1):
        if (i>>j)&1:
            y+=1
    C=[0]*(y+1) #1の数保存配列
    for x in range(w):
        T=[] #現在の列での1の数保存配列
        for j in range(h):      
            cnt+=(S[j]>>x)&1
            if cnt>k:
                flag=True
                break
            if (i>>j)&1: #横の区切り判定
                T.append(cnt)
                cnt=0
            if j==h-1: #最後の列
                T.append(cnt)
                cnt=0
        if flag:
            break
        for j in range(len(C)): #それぞれのブロックに対して
            if C[j]+T[j]>k:
                now+=1
                C=T
                break
            else:
                C[j]+=T[j]
    if flag:
        continue
    ans=min(ans,now+y)
print(ans)
A,B=input().split()
a=int(A[0])*10+int(A[1])
b=int(B[0])*10+int(B[1])
d=int(A[1])*10+int(A[0])
e=int(B[1])*10+int(B[0])
count_small=0
count_large=0
if a==b:
    for i in range(0,10):
        if a*1000+d+i*100>=int(A) and b*1000+e+i*100<=int(B):
            count_small+=1
            ans=count_small
else:
    for i in range(0,10):
        if a*1000+d+i*100>=int(A):
            count_small+=1
        if b*1000+e+i*100<=int(B):
            count_large+=1
    ans=(b-a-1)*10+count_small+count_large
print(ans)


### FIXED CODE ###

A,B=input().split()
a=int(A[0])*10+int(A[1])
b=int(B[0])*10+int(B[1])
d=int(A[1])*10+int(A[0])
e=int(B[1])*10+int(B[0])
count_small=0
count_large=0
if a==b:
    for i in range(0,10):
        if a*1000+d+i*100>=int(A) and b*1000+e+i*100<=int(B):
            count_small+=1
    ans=count_small
else:
    for i in range(0,10):
        if a*1000+d+i*100>=int(A):
            count_small+=1
        if b*1000+e+i*100<=int(B):
            count_large+=1
    ans=(b-a-1)*10+count_small+count_large
print(ans)
x,y,z=input().split()
x=int(x)
y=int(y)
z=int(z)
for i in range(10):
  d=x*z-y
  print(d)
  z=d

### FIXED CODE ###

a,b=input().split()
a=int(a)
b=int(b)
C=int(b/2)
if a>=13:
  print(b)
elif a<13 and a>=6:
  print(C)
else:
  print(0)
#
# agc012 a
#
 
import sys
from io import StringIO
import unittest
 
 
class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
 
    def test_入力例_1(self):
        input = """2
5 2 8 5 1 5"""
        output = """10"""
        self.assertIO(input, output)
 
    def test_入力例_2(self):
        input = """10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000"""
        output = """10000000000"""
        self.assertIO(input, output)
 
 
def resolve():
    N = int(input())
    a = list(map(int, input().split()))
    a.sort()
    ans = 0
    for i in range(N):
      	a.pop(0)
        a.pop()
        ans += a.pop()
    print(ans)
 
 
if __name__ == "__main__":
    # unittest.main()
    resolve()

### FIXED CODE ###

#
# agc012 a
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2
5 2 8 5 1 5"""
        output = """10"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000"""
        output = """10000000000"""
        self.assertIO(input, output)


def resolve():
    N = int(input())
    a = list(map(int, input().split()))
    a.sort()
    ans = 0
    for i in range(N):
        a.pop()
        ans += a.pop()
    print(ans)


if __name__ == "__main__":
    # unittest.main()
    resolve()
n = int(input())
list1 = [int(x) for x in input().split()]
#print(list1)

set1 = set(list1)
#print(set1)

listans = []

for i in set1:
  if list1.count(i) > 2:
    listans.append(i)

#print(listans)

if len(listans) < 2:
  print(0)
else:
  print(sorted(listans)[-1]*sorted(listans)[-2])

### FIXED CODE ###

x, a, b = input().split()
x = int(x)
a = int(a)
b = int(b)

if x > a:
	x_a = x - a
else:
	x_a = a - x

if x > b:
	x_b = x - b
else:
	x_b = b - x
	


if x_a > x_b:
	print("B")
else:
	print("A")
a = input()
total = 0
for i in a[range(1,4)]:
  if i = R:
    total +=1
  else:
    total = 0

### FIXED CODE ###

a = input()
if a =='SSS':
  print(0)
elif a=='RRR':
  print(3)
elif a == 'RRS' or a== 'SRR':
  print(2)
else:
  print(1)
while True:
    e = int(input())
    if e != 0:
        ans = float('inf')
        for z in range(e):
            r1 = e - z ** 3
            if r1 >= 0:
                for y in range(r1+1):
                    x = r1 - y ** 2
                    if x >= 0:
                        s = z + y + x
                        if ans > s:
                            ans = s
                        else:
                            pass
                    else:
                        break
            else:
                break
        print(ans)
    else:
        break


### FIXED CODE ###

while True:
    e = int(input())
    if e == 0:
        break
    z, ans = 0, float('inf')
    while z ** 3 <= e:
        r = e - z ** 3
        y = int(r ** 0.5)
        x = r - y ** 2
        ans = min(ans, x + y + z)
        z += 1
    print(ans)
S = input()
K = int(input())

i = 0
while True:
    if S[i] != "1":
        print(S[i])
        break
    if i == K:
        print(S[i])
        break
    i += 1

### FIXED CODE ###

S = input()
K = int(input())

for i in range(len(S)):
    if S[i] != "1":
        break

if K > i:
    print(S[i])
else:
    print(1)
from math import floor,ceil
x = input().split()
a = int(x[0])
b = int(x[1])
c = int(x[2])
print(ceil(ceil(a*c/b)-a*floor(c/b)))


### FIXED CODE ###

k = int(input())
x = input().split()
a = int(x[0])
b = int(x[1])
c = 0
for i in range(a,b+1):
  if i%k==0:
    c = 1
    break
if c==1:
  print("OK")
else:
  print("NG")
def find(l, e, start, end):
	mid = (start+end)//2
	if (l[mid] == e):
		return (e, e)
	elif (l[mid] > e):
		if (l[mid-1] < e): 
			return (l[mid-1], l[mid])
		return find(l, e, start, mid)
	elif (l[mid] < e):
		if (l[mid+1] > e): 
			return (l[mid], l[mid+1])
		return find(l, e, mid, end)

def calc_sum(input):
	d, n, m = input[0], input[1], input[2]
	S, K = input[3:3+n-1], input[3+n-1:]
	S = sorted(S)
	S = [S[-1]-d, 0] + S + [d]
	sum = 0
	for k in K:
		low, high = find(S, k, 0, len(S))
		sum += min(k-low, high-k)
	return sum

output = []
while (True):
	d = int(raw_input())
	if d == 0: break
	n, m = int(raw_input()), int(raw_input())
	input = [d, n, m]
	for i in range(n-1+m):
		next_int = int(raw_input())
		input.append(next_int)
	output.append[str(calc_sum(input))]
print "\n".join(output)

### FIXED CODE ###

def find(l, e):
	start, end = 0, len(l)
	while True:
		mid = (start+end)//2
		if l[mid] == e: 
			return (e, e)
		elif (l[mid] > e):
			if (l[mid-1] < e):
				return (l[mid-1], l[mid])
			end = mid
			continue
		elif (l[mid] < e):
			if (l[mid+1] > e):
				return (l[mid], l[mid+1])
			start = mid

def print_sum():
	d = int(raw_input())
	if d == 0: return False
	n, m = int(raw_input()), int(raw_input())
	S = [int(raw_input()) for _ in range(n-1)]
	K = [int(raw_input()) for _ in range(m)]
	S = [0] + sorted(S) + [d]
	sum = 0
	for k in K:
		low, high = find(S, k)
		sum += min(k-low, high-k)
	print sum
	return True

while (print_sum()):
	pass
K,T=map(int,input().split())
A=list(map(int,input().split()))
M=max(A)
print(max(M-1-(K-M),0))

### FIXED CODE ###

s=input()[::-1]
ans="No"
try:
  c=s.index("C")
  f=s.index("F")
  if c>f:
    ans="Yes"
except:
  pass
print(ans)
N = list(map(int, input().split())) 
for i in N:
  if N[i] == 0:
    print(i+1)
    

### FIXED CODE ###

a=list(map(int,input().split()))
print(a.index(0)+1)
import math
import numpy

A, B, H, M = input.split()

theta = abs(int(H)*30 - int(M)*6)
cos = math.cos(math.radians(theta))

res = math.sqrt(int(A)**2 + int(B)**2 - 2 * int(A) * int(B) * cos)
print(res)


### FIXED CODE ###

import math

A, B, H, M = (float(x) for x in input().split())

theta = H*30 + (M/60)*30 - M*6
cos = math.cos(math.radians(theta))
res = math.sqrt(A**2 + B**2 - 2*A*B*cos)
print(res)