Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Runtime Error):
def mark(s, x):
    for i in xrange(x + x, len(s), x):
        s[i] = False

def sieve(n):
    s = [True] * n
    for x in xrange(2, int(n**0.5) + 1):
        if s[x]: mark(s, x)
    return [i for i in xrange(0,n) if s[i] and i > 1]

n = input()
tmpn =n
primes = sieve(n/2+1)
# print primes
anslist = []
x = 0
while True:
    if tmpn == 1:
        break
    if tmpn%primes[x]==0:
        anslist.append(primes[x])
        tmpn = tmpn/primes[x]
        x -= 1
    x+=1
# print anslist
ansset = set(anslist)
# print ansset
ans = n
for tmp in ansset:
    ans *= (1-1.0/tmp)
print int(ans)

Fixed submission:
def prime_decomposition(n):
    i = 2
    table = []
    while i * i <= n:
        while n % i == 0:
            n /= i
            table.append(i)
        i += 1
    if n > 1:
        table.append(n)
    return table
n = input()
pdlist = prime_decomposition(n)
pdset = set(pdlist)
if len(pdlist)==1:
    for x in pdset:
        print x-1
else:
    ans = n
    for x in pdset:
        ans *= 1-1.0/x
    print int(ans)
Buggy python submission (verdict: Time Limit Exceeded):
import sys


def count_prime(n):
    global count, pool
    if not pool:
        return count
    p = pool[0]
    while p <= n:
        pool = tuple(i for i in pool if i % pool[0])
        count += 1
        if not pool:
            break
        p = pool[0]
    return count


inset = list(map(int, sys.stdin))
max_in = max(inset)
pool, count = tuple(range(3, max_in + 1, 2)), 1 if max_in > 1 else 0
inset = [(n, i) for i, n in enumerate(inset)]
inset.sort()
outset = [(t[1], count_prime(t[0])) for t in inset]
outset.sort()

print('\n'.join(map(str, [t[1] for t in outset])))

Fixed submission:
import sys

input_list = list(int(i) for i in sys.stdin)
max_n = min(1000000, max(input_list) + 1)

prime_list = list(i % 2 for i in range(max_n))
prime_list[1], prime_list[2] = 0, 1
i = 3
while i * i < max_n:
    if prime_list[i]:
        for j in range(i * 2, max_n, i):
            prime_list[j] = 0
    i += 2

for i in range(1, max_n):
    prime_list[i] += prime_list[i - 1]

inset = sorted((n, i) for i, n in enumerate(input_list))
outset = sorted((t[1], prime_list[t[0]]) for t in inset)
for t in outset:
    print(t[1])
Buggy python submission (verdict: Runtime Error):
import fractions


def calc(x1, y1, x2, y2):
    return 2 * (x2 - x1), 2 * (y2 - y1), x2 ** 2 + y2 ** 2 - x1 ** 2 - y1 ** 2


n = int(input())
for _ in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    cx1, cy1, z1 = calc(x1, y1, x2, y2)
    cx2, cy2, z2 = calc(x1, y1, x3, y3)
    print(cx1, cy1, z1, cx2, cy2, z2)
    gcd = fractions.gcd(cx1, cx2)
    r1, r2 = cx2 // gcd, cx1 // gcd
    dcy = r1 * cy1 - r2 * cy2
    dz = r1 * z1 - r2 * z2
    y = dz / dcy
    x = (z1 - cy1 * y) / cx1
    r = ((x - x1) ** 2 + (y - y1) ** 2) ** 0.5
    print(*map(lambda x: '{:.3f}'.format(round(x, 3)), (x, y, r)))

Fixed submission:
def calc(x1, y1, x2, y2):
    return 2 * (x2 - x1), 2 * (y2 - y1), x2 ** 2 + y2 ** 2 - x1 ** 2 - y1 ** 2


n = int(input())
for _ in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    cx1, cy1, z1 = calc(x1, y1, x2, y2)
    cx2, cy2, z2 = calc(x1, y1, x3, y3)
    dcy, dz = cx2 * cy1 - cx1 * cy2, cx2 * z1 - cx1 * z2
    y = dz / dcy
    x = (z1 - cy1 * y) / cx1 if cx1 else (z2 - cy2 * y) / cx2
    r = ((x - x1) ** 2 + (y - y1) ** 2) ** 0.5
    print('{:.3f} {:.3f} {:.3f}'.format(x, y, r))

Buggy python submission (verdict: Runtime Error):
import fractions


def calc(x1, y1, x2, y2):
    return 2 * (x2 - x1), 2 * (y2 - y1), x2 ** 2 + y2 ** 2 - x1 ** 2 - y1 ** 2


n = int(input())
for _ in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    cx1, cy1, z1 = calc(x1, y1, x2, y2)
    cx2, cy2, z2 = calc(x1, y1, x3, y3)
    gcd = fractions.gcd(cx1, cx2)
    r1, r2 = cx2 // gcd, cx1 // gcd
    dcy = r1 * cy1 - r2 * cy2
    dz = r1 * z1 - r2 * z2
    y = dz / dcy
    x = (z1 - cy1 * y) / cx1
    r = ((x - x1) ** 2 + (y - y1) ** 2) ** 0.5
    print(*map(lambda x: '{:.3f}'.format(round(x, 3)), (x, y, r)))

Fixed submission:
def calc(x1, y1, x2, y2):
    return 2 * (x2 - x1), 2 * (y2 - y1), x2 ** 2 + y2 ** 2 - x1 ** 2 - y1 ** 2


n = int(input())
for _ in range(n):
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    cx1, cy1, z1 = calc(x1, y1, x2, y2)
    cx2, cy2, z2 = calc(x1, y1, x3, y3)
    dcy, dz = cx2 * cy1 - cx1 * cy2, cx2 * z1 - cx1 * z2
    y = dz / dcy
    x = (z1 - cy1 * y) / cx1 if cx1 else (z2 - cy2 * y) / cx2
    r = ((x - x1) ** 2 + (y - y1) ** 2) ** 0.5
    print('{:.3f} {:.3f} {:.3f}'.format(x, y, r))
Buggy python submission (verdict: Runtime Error):
import string
import sys


def check_exists(word_set, rotate_dict, search):
    for word in word_set:
        rotated_word = ''.join(rotate_dict[s] for s in word)
        if rotated_word == search:
            return True
    return False


for line in sys.stdin:
    s0 = line.strip()
    alphabets = string.ascii_lowercase
    frequency = [t[1] for t in sorted([(s0.count(s), ord(s)) for s in alphabets], reverse=True)]
    freq_chr_offset = (4, 0, 19, 8, 14)
    ss = s0.strip('.').split()
    sl3, sl4 = set(s for s in ss if len(s) == 3), set(s for s in ss if len(s) == 4)

    i, rotate_dict = 0, None
    for order in frequency:
        for offset in freq_chr_offset:
            i = 97 + offset - order
            rotate_dict = {a: b for a, b in zip(alphabets, alphabets[i:] + alphabets[:i])}
            if (check_exists(sl3, rotate_dict, 'the')
                    + check_exists(sl4, rotate_dict, 'this')
                    + check_exists(sl4, rotate_dict, 'that')) > 0:
                break
        else:
            continue
        break

    print(''.join(rotate_dict[s] if s.isalpha() else s for s in s0))

Fixed submission:
import string
import sys


def check_exists(word_set, rotate_dict, search):
    for word in word_set:
        rotated_word = ''.join(rotate_dict[s] for s in word)
        if rotated_word == search:
            return True
    return False


for line in sys.stdin:
    alphabets = string.ascii_lowercase
    frequency = [t[1] for t in sorted([(line.count(s), ord(s)) for s in alphabets], reverse=True)]
    freq_chr_offset = (4, 0, 19, 8, 14)
    ss = list(map(lambda word: word.strip('.\n'), line.split()))
    sl3, sl4 = set(s for s in ss if len(s) == 3), set(s for s in ss if len(s) == 4)

    i, rotate_dict = 0, None
    for order in frequency:
        for offset in freq_chr_offset:
            i = 97 + offset - order
            rotate_dict = {a: b for a, b in zip(alphabets, alphabets[i:] + alphabets[:i])}
            if (check_exists(sl3, rotate_dict, 'the')
                    + check_exists(sl4, rotate_dict, 'this')
                    + check_exists(sl4, rotate_dict, 'that')) > 0:
                break
        else:
            continue
        break

    print(''.join(rotate_dict[s] if s.isalpha() else s for s in line), end='')
Buggy python submission (verdict: Runtime Error):
n = int(input())
for _ in range(n):
    x1, y1, x2, y2, x3, y3, x4, y4 = map(float, input().split())
    print('YES' if (x1 - x2) / (y1 - y2) == (x3 - x4) / (y3 - y4) else 'NO')

Fixed submission:
n = int(input())
for _ in range(n):
    x1, y1, x2, y2, x3, y3, x4, y4 = map(float, input().split())
    dx12, dx34, dy12, dy34 = x1 - x2, x3 - x4, y1 - y2, y3 - y4
    print('YES' if abs(dx12 * dy34 - dy12 * dx34) < 1e-10 else 'NO')
Buggy python submission (verdict: Runtime Error):
class Node:
    prev = next = None

    def __init__(self, value):
        self.value = value

    def get_nxnx(self):
        next_n = self.next
        if not next_n: return None, None
        return next_n, next_n.next

    def get_prpr(self):
        prev_n = self.prev
        if not prev_n: return None, None
        return prev_n, prev_n.prev

    def combine(self):
        next_n, nxnx_n = self.get_nxnx()
        if not next_n or not nxnx_n: return False

        if self.value > abs(next_n.value) < nxnx_n.value:
            self.value += next_n.value + nxnx_n.value
            self.next = nxnx_n.next
            _, prpr = self.get_prpr()
            if prpr:
                prpr.combine()
            return True
        return False

    def iter(self):
        a = self
        while a:
            yield a.value
            a = a.next


while True:
    n = int(input())
    if not n:
        break
    a0 = a = Node(int(input()))
    n -= 1
    while n:
        an = int(input())
        if an * a.value < 0:
            at = Node(an)
            at.prev = a
            a.next = at
            a = at
        else:
            a.value += an
        n -= 1
    if a0.value < 0:
        a0 = a0.next
    a = a0
    while a:
        while a.combine():
            pass
        _, a = a.get_nxnx()
    print(max(v for v in a0.iter()))

Fixed submission:
while True:
    n = int(input())
    if not n:
        break
    a = [0] * n
    a[0] = int(input())
    for i in range(1, n):
        an = int(input())
        a[i] = max(an, a[i - 1] + an)
    print(max(a))
Buggy python submission (verdict: Time Limit Exceeded):
def furoshiki(i, w_limit):
    global treasures
    if not i or not w_limit:
        return (0, 0)

    v, w = treasures[i - 1]
    not_stole = furoshiki(i - 1, w_limit)
    if w_limit < w:
        return not_stole
    stole_v_total, stole_w_total = furoshiki(i - 1, w_limit - w)
    stole_v_total += v
    stole_w_total -= w
    return max(not_stole, (stole_v_total, stole_w_total))


case = 1
while True:
    w = int(input())
    if not w:
        break
    n = int(input())
    treasures = [None] * n
    for i in range(n):
        treasures[i] = tuple(map(int, input().split(',')))

    total_v, total_w = furoshiki(n, w)
    print('Case {}:'.format(case))
    print(total_v)
    print(-total_w)
    case += 1

Fixed submission:
case = 1
while True:
    w_limit = int(input())
    if not w_limit:
        break
    n = int(input())
    treasures = [tuple(map(int, input().split(','))) for _ in range(n)]

    furoshiki = [0] * (w_limit + 1)
    for v, w in treasures:
        for j in range(w_limit, w - 1, -1):
            furoshiki[j] = max(furoshiki[j], furoshiki[j - w] + v)

    max_v = max_v_w = 0
    for w in range(w_limit + 1):
        if furoshiki[w] > max_v:
            max_v = furoshiki[w]
            max_v_w = w

    print('Case {}:'.format(case))
    print(max_v)
    print(max_v_w)
    case += 1

Buggy python submission (verdict: Time Limit Exceeded):
memo = {}


def furoshiki(i, w_limit):
    global memo, treasures

    if not i or not w_limit:
        return (0, 0)

    if (i, w_limit) in memo:
        return memo[(i, w_limit)]

    v, w = treasures[i - 1]
    not_stole = furoshiki(i - 1, w_limit)
    if w_limit < w:
        return not_stole
    stole_v_total, stole_w_total = furoshiki(i - 1, w_limit - w)
    stole_v_total += v
    stole_w_total -= w
    return max(not_stole, (stole_v_total, stole_w_total))


case = 1
while True:
    w = int(input())
    if not w:
        break
    n = int(input())
    treasures = [None] * n
    for i in range(n):
        treasures[i] = tuple(map(int, input().split(',')))

    total_v, total_w = furoshiki(n, w)
    print('Case {}:'.format(case))
    print(total_v)
    print(-total_w)
    memo.clear()
    case += 1

Fixed submission:
case = 1
while True:
    w_limit = int(input())
    if not w_limit:
        break
    n = int(input())
    treasures = [tuple(map(int, input().split(','))) for _ in range(n)]

    furoshiki = [0] * (w_limit + 1)
    for v, w in treasures:
        for j in range(w_limit, w - 1, -1):
            furoshiki[j] = max(furoshiki[j], furoshiki[j - w] + v)

    max_v = max_v_w = 0
    for w in range(w_limit + 1):
        if furoshiki[w] > max_v:
            max_v = furoshiki[w]
            max_v_w = w

    print('Case {}:'.format(case))
    print(max_v)
    print(max_v_w)
    case += 1
Buggy python submission (verdict: Time Limit Exceeded):
from math import sqrt, floor

p = [2]
for i in range(3, 50000, 2):
    for j in range(3, floor(sqrt(i)) + 1, 2):
        if not i % j:
            break
    else:
        p.append(i)

while True:
    n = int(input())
    if not n:
        break
    n2 = n // 2
    cnt = 0
    for i in p:
        if n2 < i:
            break
        if n - i in p:
            cnt += 1
    print(cnt)

Fixed submission:
from math import sqrt, floor

p = [2]
for i in range(3, 50000, 2):
    for j in range(3, floor(sqrt(i)) + 1, 2):
        if not i % j:
            break
    else:
        p.append(i)
sp = set(p)

while True:
    n = int(input())
    if not n:
        break
    if n & 1:
        print(int(n - 2 in sp))
        continue
    n2 = n // 2
    cnt = 0
    for i in p:
        if n2 < i:
            break
        if n - i in sp:
            cnt += 1
    print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
from math import sqrt, floor
from bisect import bisect_left


def search(x):
    global p, lp
    i = bisect_left(p, x)
    return i != lp and p[i] == x


p = [2]
for i in range(3, 50000, 2):
    for j in range(3, floor(sqrt(i)) + 1, 2):
        if not i % j:
            break
    else:
        p.append(i)
lp = len(p)

while True:
    n = int(input())
    if not n:
        break
    if n & 1:
        print(int(search(n - 2)))
        continue
    n2 = n // 2
    cnt = 0
    for i in p:
        if n2 < i:
            break
        if search(n - i):
            cnt += 1
    print(cnt)

Fixed submission:
from math import sqrt, floor

p = [2]
for i in range(3, 50000, 2):
    for j in range(3, floor(sqrt(i)) + 1, 2):
        if not i % j:
            break
    else:
        p.append(i)
sp = set(p)

while True:
    n = int(input())
    if not n:
        break
    if n & 1:
        print(int(n - 2 in sp))
        continue
    n2 = n // 2
    cnt = 0
    for i in p:
        if n2 < i:
            break
        if n - i in sp:
            cnt += 1
    print(cnt)
Buggy python submission (verdict: Time Limit Exceeded):
import sys
from itertools import permutations

for line in sys.stdin:
    n, s = map(int, line.split())
    count = 0
    for combi in permutations(range(10), n):
        if sum(x * y for (x, y) in zip(range(1, 10), combi)) == s:
            count += 1
    print(count)

Fixed submission:
import sys


def memoize(f):
    memo = {}

    def main(*args):
        if args in memo:
            return memo[args]
        result = memo[args] = f(*args)
        return result

    return main


def get_num(remains):
    i = 0
    while remains:
        if remains & 1:
            yield i
        i += 1
        remains >>= 1


@memoize
def calc(n, s, remains):
    if n == 1:
        if remains & (1 << s):
            return 1
        else:
            return 0
    if s <= 0:
        return 0

    return sum(calc(n - 1, s - n * m, remains ^ (1 << m)) for m in get_num(remains) if s - n * m >= 0)


for line in sys.stdin:
    n, s = map(int, line.split())
    print(calc(n, s, (1 << 10) - 1))
Buggy python submission (verdict: Time Limit Exceeded):
n, m, _ = map(int, input().split())
n0 = n
exem = [1] * n0
i = 0

for a in map(int, input().split()):
    is_even = not a & 1
    a %= n
    if is_even: a = n - a
    c = 0
    while a:
        c += 1
        if exem[i - c]:
            a -= 1
    exem[i - c] = 0
    i = (i - c + 1) % n0
    n -= 1

for q in map(int, input().split()):
    print(exem[q])

Fixed submission:
n, m, _ = map(int, input().split())
exem = list(range(n))
i = 0

for a in map(int, input().split()):
    is_odd = a & 1
    i += -a if a & 1 else a
    i %= n
    exem.pop(i)
    n -= 1

exem = set(exem)
for q in map(int, input().split()):
    print(int(q in exem))
Buggy python submission (verdict: Runtime Error):
while True:
    m = sum(map(int, input().split()))
    n = int(input())
    fixed = [2] * m
    failures = []
    for _ in range(n):
        i, j, k, r = map(int, input().split())
        i, j, k = (x - 1 for x in (i, j, k))
        if r:
            fixed[i] = fixed[j] = fixed[k] = 1
        else:
            failures.append((i, j, k))

    for i, j, k in failures:
        fi, fj, fk = (fixed[x] for x in (i, j, k))
        if fi == 1:
            if fj == 1:
                fixed[k] = 0
            elif fk == 1:
                fixed[j] = 0
        elif fj == 1 and fk == 1:
            fi = 0

    print('\n'.join(str(x) for x in fixed) + '\n')

Fixed submission:
while True:
    m = sum(map(int, input().split()))
    if not m:
        break
    n = int(input())
    fixed = [2] * m
    failures = []
    for _ in range(n):
        i, j, k, r = map(int, input().split())
        i, j, k = (x - 1 for x in (i, j, k))
        if r:
            fixed[i] = fixed[j] = fixed[k] = 1
        else:
            failures.append((i, j, k))

    for i, j, k in failures:
        fi, fj, fk = (fixed[x] for x in (i, j, k))
        if fi == 1:
            if fj == 1:
                fixed[k] = 0
            elif fk == 1:
                fixed[j] = 0
        elif fj == 1 and fk == 1:
            fixed[i] = 0

    print('\n'.join(str(x) for x in fixed))
Buggy python submission (verdict: Time Limit Exceeded):
def warshall_floyd(n, ds):
    for k in range(n):
        ds_k = ds[k]
        for i in range(n):
            ds_i = ds[i]
            ds_i_k = ds_i[k]
            for j in range(n):
                ds_i[j] = min(ds_i[j], ds_i_k + ds_k[j])
    return ds


while True:
    n, k = map(int, input().split())
    if not n:
        break
    ds = [[0 if i == j else float('inf') for j in range(n)] for i in range(n)]
    pre_op = 0
    for _ in range(k):
        o = map(int, input().split())
        op = next(o)
        if op:
            c, d, e = o
            c, d = c - 1, d - 1
            ds[c][d] = ds[d][c] = min(e, ds[c][d])
        else:
            a, b = o
            if pre_op:
                ds = warshall_floyd(n, ds)
            ans = ds[a - 1][b - 1]
            print(-1 if ans == float('inf') else ans)
        pre_op = op

Fixed submission:
from heapq import heappush, heappop, heapify


def dijkstra(s, t, links):
    heap = list(links[s])
    heapify(heap)
    visited = set()
    while heap:
        fare, node = heappop(heap)
        if node == t:
            return fare
        if node in visited:
            continue
        visited.add(node)
        for fare2, node2 in links[node]:
            if node2 not in visited:
                heappush(heap, (fare + fare2, node2))
    return -1


while True:
    n, k = map(int, input().split())
    if not n:
        break
    links = [set() for _ in range(n)]
    for _ in range(k):
        o = map(int, input().split())
        if next(o):
            c, d, e = o
            c, d = c - 1, d - 1
            links[c].add((e, d))
            links[d].add((e, c))
        else:
            a, b = o
            print(dijkstra(a - 1, b - 1, links))
Buggy python submission (verdict: Time Limit Exceeded):
while True:
    n, m = map(int, input().split())
    if not n:
        break

    ps = {int(input()) for _ in range(n)}
    ss = [False] * (m + 1)
    min_p, max_p = 1e8, 0
    for p in ps:
        ss[p] = True
        if min_p > p: min_p = p
        if max_p < p: max_p = p

    max_s = 0

    for t in range(1, 4):
        new_ss = [False] * (m + 1)
        for i in range(min(m, max_p * t), min_p * t - 1, -1):
            if ss[i]:
                for p in ps:
                    if i + p <= m:
                        new_ss[i + p] = True

        ss = new_ss

        p = m
        while p:
            if ss[p]:
                if max_s < p:
                    max_s = p
                break

    print(max_s)

Fixed submission:
from bisect import bisect_right

while True:
    n, m = map(int, input().split())
    if not n:
        break

    ps = [0] + sorted(int(input()) for _ in range(n))

    p2 = set()
    for i, pi in enumerate(ps):
        for pj in ps[i:]:
            if pi + pj > m:
                break
            p2.add(pi + pj)

    p2 = sorted(p2)

    print(max(pi + p2[bisect_right(p2, m - pi) - 1] for pi in p2))

Buggy python submission (verdict: Time Limit Exceeded):
from bisect import bisect_right

while True:
    n, m = map(int, input().split())
    if not n:
        break

    ps = [0] + sorted(int(input()) for _ in range(n))

    p2 = set()
    for i, pi in enumerate(ps):
        for pj in ps[i:]:
            if pi + pj > m:
                break
            p2.add(pi + pj)

    p2 = sorted(p2)

    max_s = 0
    for pi in p2:
        j = bisect_right(p2, m - pi)
        s = pi + p2[j - 1]
        if max_s < s:
            max_s = s
    print(max_s)

Fixed submission:
from bisect import bisect_right

while True:
    n, m = map(int, input().split())
    if not n:
        break

    ps = [0] + sorted(int(input()) for _ in range(n))

    p2 = set()
    for i, pi in enumerate(ps):
        for pj in ps[i:]:
            if pi + pj > m:
                break
            p2.add(pi + pj)

    p2 = sorted(p2)

    print(max(pi + p2[bisect_right(p2, m - pi) - 1] for pi in p2))
Buggy python submission (verdict: Time Limit Exceeded):
def allocate(remains, limit_h, limit_w):
    global m
    if not remains:
        return 1
    if limit_w == 1:
        return int(remains <= limit_h)
    return sum(allocate(remains - i, i, limit_w - 1) for i in
               range(min(limit_h, remains, m - limit_w), (remains - 1) // limit_w, -1))


while True:
    n, m, s = map(int, input().split())
    if not n:
        break
    remains = s - sum(range(1, n ** 2 + 1))
    print(allocate(remains, remains, n ** 2))

Fixed submission:
while True:
    n, m, s = map(int, input().split())
    if not n:
        break
    n2 = n ** 2
    dpp = [0] * (s + 1)
    dpp[0] = 1

    for i in range(1, n2 + 1):
        dpn = [0] * (s + 1)
        for j in range(i * (i + 1) // 2, s + 1):
            dpn[j] += dpp[j - i] + dpn[j - i]
            if j - m - 1 >= 0:
                dpn[j] -= dpp[j - m - 1]
            dpn[j] %= 100000
        dpp = dpn

    print(dpp[s])
Buggy python submission (verdict: Runtime Error):
from bisect import bisect_left

while True:
    d = int(input())
    if not d:
        break
    n = int(input())
    m = int(input())
    stores = {0, d}
    for i in range(2, n + 1):
        stores.add(int(input()))
    sorted_stores = sorted(stores)

    total_dist = 0
    while m:
        k = int(input())
        i = bisect_left(sorted_stores, k)
        l, r = sorted_stores[i - 1:i + 1]
        total_dist += min(k - l, r - k)
        m -= 1
    print(total_dist)

Fixed submission:
from bisect import bisect_left

while True:
    d = int(input())
    if not d:
        break
    n = int(input())
    m = int(input())
    stores = {0, d}
    for i in range(2, n + 1):
        stores.add(int(input()))
    sorted_stores = sorted(stores)

    total_dist = 0
    while m:
        k = int(input())
        i = bisect_left(sorted_stores, k)
        if i:
            l, r = sorted_stores[i - 1:i + 1]
            total_dist += min(k - l, r - k)
        m -= 1
    print(total_dist)