Buggy python submission (verdict: Runtime Error):
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    *D, = map(int, input().split())

    from itertools import permutations
    ans = 10**18
    for P in permutations(S):
        Q = [0]*(n+1)
        for i, e in enumerate(P):
            Q[i+1] = P[i] + Q[i]
        ans = min(ans, sum(min(abs(d - q) for q in Q) for d in D))
    print(ans)

Fixed submission:
from collections import deque
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    D = sorted(map(int, input().split()))

    SA = sum(S)
    rest = sum(d-SA for d in D if SA <= d)

    memo = {2**n-1: rest}
    def dfs(state, su, idx):
        if state in memo:
            return memo[state]
        res = 10**18
        for i in range(n):
            if (state >> i) & 1 == 0:
                s = 0; j = idx
                nxt = su + S[i]
                while j < m and D[j] <= nxt:
                    s += min(nxt - D[j], D[j] - su); j += 1
                res = min(res, s + dfs(state | (1 << i), su + S[i], j))
        memo[state] = res
        return res
    print(dfs(0, 0, 0))

Buggy python submission (verdict: Runtime Error):
from itertools import permutations
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    *D, = map(int, input().split())

    def solve(S, D):
        for P in permutations(S):
            Q = [0]*(n+1)
            for i, e in enumerate(P):
                Q[i+1] = P[i] + Q[i]
            yield sum(min(abs(d - q) for q in Q) for d in D)
    print(min(solve(S, D)))

Fixed submission:
from collections import deque
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    D = sorted(map(int, input().split()))

    SA = sum(S)
    rest = sum(d-SA for d in D if SA <= d)

    memo = {2**n-1: rest}
    def dfs(state, su, idx):
        if state in memo:
            return memo[state]
        res = 10**18
        for i in range(n):
            if (state >> i) & 1 == 0:
                s = 0; j = idx
                nxt = su + S[i]
                while j < m and D[j] <= nxt:
                    s += min(nxt - D[j], D[j] - su); j += 1
                res = min(res, s + dfs(state | (1 << i), su + S[i], j))
        memo[state] = res
        return res
    print(dfs(0, 0, 0))

Buggy python submission (verdict: Runtime Error):
from itertools import permutations
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    *D, = map(int, input().split())

    def solve(S, D):
        Q = [0]*(n+1)
        for P in permutations(S):
            for i, e in enumerate(P):
                Q[i+1] = e + Q[i]
            yield sum(min(abs(d - q) for q in Q) for d in D)
    print(min(solve(S, D)))

Fixed submission:
from collections import deque
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    D = sorted(map(int, input().split()))

    SA = sum(S)
    rest = sum(d-SA for d in D if SA <= d)

    memo = {2**n-1: rest}
    def dfs(state, su, idx):
        if state in memo:
            return memo[state]
        res = 10**18
        for i in range(n):
            if (state >> i) & 1 == 0:
                s = 0; j = idx
                nxt = su + S[i]
                while j < m and D[j] <= nxt:
                    s += min(nxt - D[j], D[j] - su); j += 1
                res = min(res, s + dfs(state | (1 << i), su + S[i], j))
        memo[state] = res
        return res
    print(dfs(0, 0, 0))

Buggy python submission (verdict: Runtime Error):
from collections import deque
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    *D, = map(int, input().split())

    deq = deque(S)
    D.sort()
    def dfs(r, idx, prev):
        if idx == m:
            return 0
        if r == n:
            s = 0
            while idx < m:
                s += D[idx] - prev
                idx += 1
            return s
        res = 10**18
        for i in range(n - r):
            v = deq.popleft()
            nxt = prev + v
            s = 0; j = idx
            while j < m and D[j] <= nxt:
                s += min(nxt - D[j], D[j] - prev); j += 1
            res = min(res, s + dfs(r+1, j, nxt))
            deq.append(v)
        return res
    print(dfs(0, 0, 0))

Fixed submission:
from collections import deque
while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    *S, = map(int, input().split())
    D = sorted(map(int, input().split()))

    SA = sum(S)
    rest = sum(d-SA for d in D if SA <= d)

    memo = {2**n-1: rest}
    def dfs(state, su, idx):
        if state in memo:
            return memo[state]
        res = 10**18
        for i in range(n):
            if (state >> i) & 1 == 0:
                s = 0; j = idx
                nxt = su + S[i]
                while j < m and D[j] <= nxt:
                    s += min(nxt - D[j], D[j] - su); j += 1
                res = min(res, s + dfs(state | (1 << i), su + S[i], j))
        memo[state] = res
        return res
    print(dfs(0, 0, 0))