Harryxun/GPT-100-sft-dataset huggingface dataset:
Problem p02949 (python)

Fixed submission:
N, M, P = map(int, input().split())

E = [[0, 0, 0] for m in range(M)]
for m in range(M):
    A, B, C = map(int, input().split())
    E[m][0] = A - 1
    E[m][1] = B - 1
    E[m][2] = P - C


def BellmanFord(edges, num_v, source, end):
    inf = 10 ** 12
    dist = [inf for i in range(num_v)]
    dist[source] = 0
    parent = [-1 for i in range(num_v)]

    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                parent[edge[1]] = edge[0]

    end1 = dist[end]
    trace1 = []
    r = end
    for n in range(num_v):
        trace1.append(r)
        r = parent[r]
        if r == -1:
            break

    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]
                parent[edge[1]] = edge[0]

    end2 = dist[end]
    trace2 = []
    r = end
    for n in range(num_v + 10):
        trace2.append(r)
        r = parent[r]
        if r == -1:
            break

    if trace2[-1] != source:
        return -1

    return max(0, -end1) if end1 == end2 else -1


res = BellmanFord(E, N, 0, N - 1)
print(res)

Buggy submission (verdict: Runtime Error):
N, M, P = map(int, input().split())

E = [[0, 0, 0] for n in range(M)]
for n in range(N):
    A, B, C = map(int, input().split())
    E[n][0] = A - 1
    E[n][1] = B - 1
    E[n][2] = P - C


def BellmanFord(edges, num_v, source, end):
    inf = 10 ** 8
    dist = [inf for i in range(num_v)]
    dist[source] = 0

    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]

    end1 = dist[end]
    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]

    end2 = dist[end]

    return max(0, -end1) if end1 == end2 else -1


res = BellmanFord(E, N, 0, N - 1)
print(res)

Buggy submission (verdict: Runtime Error):
N, M, P = map(int, input().split())

E = [[0, 0, 0] for n in range(M)]
for n in range(N):
    A, B, C = map(int, input().split())
    E[n][0] = A - 1
    E[n][1] = B - 1
    E[n][2] = P - C


def BellmanFord(edges, num_v, source, end):
    inf = float("inf")
    dist = [inf for i in range(num_v)]
    dist[source] = 0

    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]

    end1 = dist[end]
    for i in range(num_v):
        for edge in edges:
            if dist[edge[1]] > dist[edge[0]] + edge[2]:
                dist[edge[1]] = dist[edge[0]] + edge[2]

    end2 = dist[end]

    return max(0, -end1) if end1 == end2 else -1


res = BellmanFord(E, N, 0, N - 1)
print(res)
Problem p02974 (python)

Fixed submission:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: int(x) - 1, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n)


def main():
    N, K = read_values()
    dp = [
        [[0 for i in range(N ** 2 + 1)] for ii in range(N + 1)] for iii in range(N + 1)
    ]
    dp[0][0][0] = 1

    for i in range(1, N + 1):
        for j in range(N + 1):
            for k in range(K + 1):
                if k - 2 * j < 0:
                    continue

                dp[i][j][k] = (
                    (2 * j + 1) * dp[i - 1][j][k - 2 * j]
                    + (
                        (j + 1) * (j + 1) * dp[i - 1][j + 1][k - 2 * j]
                        if j + 1 <= N
                        else 0
                    )
                    + dp[i - 1][j - 1][k - 2 * j]
                ) % mod

    print(dp[N][0][K])


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys

input = sys.stdin.readline
sys.setrecursionlimit(100000)
mod = 10 ** 9 + 7


def read_values():
    return map(int, input().split())


def read_index():
    return map(lambda x: int(x) - 1, input().split())


def read_list():
    return list(read_values())


def read_lists(N):
    return [read_list() for n in range(N)]


class V:
    def __init__(self, f, v=None):
        self.f = f
        self.v = v

    def __str__(self):
        return str(self.v)

    def ud(self, n):
        if n is None:
            return

        if self.v is None:
            self.v = n
            return
        self.v = self.f(self.v, n)


def main():
    N, K = read_values()
    dp = [
        [[0 for i in range(N ** 2 + 1)] for ii in range(N + 1)] for iii in range(N + 1)
    ]
    dp[0][0][0] = 1

    for i in range(1, N + 1):
        for j in range(N + 1):
            for k in range(K + 1):
                if k - 2 * j < 0:
                    continue

                dp[i][j][k] = (
                    (2 * j + 1) * dp[i - 1][j][k - 2 * j]
                    + (j + 1) * (j + 1) * dp[i - 1][j + 1][k - 2 * j]
                    + dp[i - 1][j - 1][k - 2 * j]
                ) % mod

    print(dp[N][0][K])


if __name__ == "__main__":
    main()
Problem p03059 (python)

Fixed submission:
A, B, T = map(int, input().split())
print(B * (T // A))

Buggy submission (verdict: Runtime Error):
A, B, T = map(int, input().split())
pritn(B * (T // A))
Problem p03128 (python)

Fixed submission:
N, M = map(int, input().split())
A = list(map(int, input().split()))
cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
can_cost = sorted([cost[a] for a in A])
D = {}
for a in A:
    c = cost[a]
    D[c] = max(a, D.setdefault(c, -1))

min_cost = can_cost[0]
min_cost_num = D[min_cost]


def f2(v, l, num):
    L = list(str(v))
    min_cost = cost[int(num)]
    num = D.setdefault(min_cost + l, -1)
    if num == -1:
        return -1

    min_cost_num = D[min_cost]
    L[L.index(str(min_cost_num))] = str(num)
    L.sort(reverse=True)
    return int("".join(L))


def f(v, l):
    L = list(str(v))
    set_L = set(L)
    L2 = [f2(v, l, num) for num in set_L]
    return max(L2)

def g(K, r):
    temp = int("".join([str(min_cost_num)] * K))
    dp = [[-1 for _ in range(min(r, K) + 1)] for __ in range(r + 1)]
    dp[0][0] = temp

    for k in range(min(r, K)):
        for n in range(r):
            v = dp[n][k]
            if v == -1:
                continue
            for l in range(1, r - n + 1):
                dp[n + l][k + 1] = max(dp[n + l][k + 1], f(v, l))
    return max(dp[r])

K = N // min_cost
r = N - K * min_cost
res = g(K, r)
if g(K, r) == -1:
    res = g(K - 1, r + min_cost)
print(res)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
A = list(map(int, input().split()))
cost = [-1, 2, 5, 5, 4, 5, 6, 3, 7, 6]
can_cost = sorted([cost[a] for a in A])
D = {}
for a in A:
    c = cost[a]
    D[c] = max(a, D.setdefault(c, -1))

min_cost = can_cost[0]
min_cost_num = D[min_cost]


def f2(v, l, num):
    L = list(str(v))
    min_cost = cost[int(num)]
    num = D.setdefault(min_cost + l, -1)
    if num == -1:
        return -1

    min_cost_num = D[min_cost]
    L[L.index(str(min_cost_num))] = str(num)
    L.sort(reverse=True)
    return int("".join(L))


def f(v, l):
    L = list(str(v))
    set_L = set(L)
    L2 = [f2(v, l, num) for num in set_L]
    return max(L2)

def g(K, r)

    temp = int("".join([str(min_cost_num)] * K))
    dp = [[-1 for _ in range(min(r, K) + 1)] for __ in range(r + 1)]
    dp[0][0] = temp

    for k in range(min(r, K)):
        for n in range(r):
            v = dp[n][k]
            if v == -1:
                continue
            for l in range(1, r - n + 1):
                dp[n + l][k + 1] = max(dp[n + l][k + 1], f(v, l))
    return max(dp[r])

K = N // min_cost
r = N - K * min_cost
res = g(K, r)
if g(K, r) == -1:
    res = g(K - 1, r + min_cost)
print(res)
Problem p03161 (python)

Fixed submission:
import sys

N, K = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))

dp = [0] + [None] * (N - 1)
for n, a in enumerate(A):
    if n:
        l = max(0, n - K)
        dp[n] = min(abs(a - a2) + d for a2, d in zip(A[l : n], dp[l : n]))

print(dp[-1])

Buggy submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

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

dp = [None] * N
for n in range(N):
    if n == 0:
        min_num = 0
    else:
        l = min(n, K)
        min_num = 10 ** 10
        for k in range(1, l + 1):
            min_num = min(abs(A[n] - A[n - k]) + dp[n - k], min_num)
    dp[n] = min_num

print(dp[-1])
Problem p03196 (python)

Fixed submission:
import math

N, P = map(int, input().split())

if N == 1:
    print(P)
    
else:
    T = int(math.pow(P, 1/N)) * 2

    Q = [False, False] + [True] * T
    F = []
    for n in range(T + 1):
        if Q[n]:
            F.append(n)
            for m in range(n, T + 1, n):
                Q[m] = False
    D = 1
    for f in F:
        c = 0
        while P % f == 0:
            c += 1
            P //= f
        D *= f ** (c // N)

    print(D)

Buggy submission (verdict: Runtime Error):
import math

N, P = map(int, input().split())

if N == 1:
    print(P)

T = int(math.pow(P, 1/N))

Q = [False, False] + [True] * T
F = []
for n in range(T + 1):
    if Q[n]:
        F.append(n)
        for m in range(n, T + 1, n):
            Q[m] = False
D = 1
for f in F:
    c = 0
    while P % f == 0:
        c += 1
        P //= f
    D *= f ** (c // N)

print(D)

Buggy submission (verdict: Runtime Error):
import math

N, P = map(int, input().split())

if N == 1:
    print(P)

T = int(math.pow(P, 1/N))
Q = [True] * (T + 4)
F = []

Q[0] = False
Q[1] = False

for n in range(T + 1):
    if Q[n]:
        F.append(n)
        for m in range(n, T + 1, n):
            Q[m] = False
D = 1
for f in F:
    c = 0
    while P % f == 0:
        c += 1
        P //= f
    D *= f ** (c // N)

print(D)

Buggy submission (verdict: Runtime Error):
import math

N, P = map(int, input().split())

if N == 1:
    print(P)

T = int(math.pow(P, 1/N))

if T <= 1:
    print(1)

Q = [True] * (T + 1)
F = []

Q[0] = False
Q[1] = False

for n in range(2, T + 1):
    if Q[n]:
        F.append(n)
        for m in range(n, T + 1, n):
            Q[m] = False
D = 1
for f in F:
    c = 0
    while P % f == 0:
        c += 1
        P //= f
    D *= f ** (c // N)

print(D)

Buggy submission (verdict: Runtime Error):
import math

N, P = map(int, input().split())

if N == 1:
    print(P)

T = int(math.pow(P, 1/N))
Q = [True] * (T + 1)
F = []

Q[0] = False
Q[1] = False

for n in range(2, T + 1):
    if Q[n]:
        F.append(n)
        for m in range(n, T + 1, n):
            Q[m] = False
D = 1
for f in F:
    c = 0
    while P % f == 0:
        c += 1
        P //= f
    D *= f ** (c // N)

print(D)
Problem p03231 (python)

Fixed submission:
N, M = map(int, input().split())
S = input()
T = input()

def f(a, b):
    if b < a:
        a, b = b, a
    
    while b % a != 0:
        a, b = b % a, a
    return a

if S[0] != T[0]:
    print(-1)

else:
    p = f(N, M)
    k = N // p
    l = M // p

    for n in range(p):
        if S[n * k] != T[n * l]:
            print(-1)
            break
    else:
        print(N * M // p)

Buggy submission (verdict: Runtime Error):
import numpy as np
N = int(input())
A = list(map(int, input().split()))
R = np.zeros(N + 1, dtype="int32")
P = np.ones(N + 1, dtype="int32")
mod = 10 ** 9 + 7

def rev(a):
    return pow(a, mod - 2, mod)
    
for n in range(N + 1):
    R[n] = (R[n - 1] + rev(n)) % mod
    if n != 0:
        P[n] = P[n - 1] * n
    
C = [(R[j] + R[N - j + 1] - 1) % mod for j in range(1, N + 1)]

print(sum([a * c for a, c in zip(C, A)]) * P[N] % mod)

Buggy submission (verdict: Runtime Error):
import numpy as np
N = int(input())
A = list(map(int, input().split()))
R = np.zeros(N + 1, dtype=int)
P = np.ones(N + 1, dtype=int)
mod = 10 ** 9 + 7

def rev(a):
    return pow(a, mod - 2, mod)
    
for n in range(N + 1):
    R[n] = (R[n - 1] + rev(n)) % mod
    if n != 0:
        P[n] = P[n - 1] * n
    
C = [(R[j] + R[N - j + 1] - 1) % mod for j in range(1, N + 1)]

print(sum([a * c for a, c in zip(C, A)]) * P[N] % mod)
Problem p03355 (python)

Fixed submission:
S = input().strip()
N = len(S)
K = int(input()) - 1

T = set()
for i in range(N):
    for j in range(i + 1, min(N + 1, i + 6)):
        T.add(S[i:j])
A = sorted(list(T))
print(A[K])

Buggy submission (verdict: Runtime Error):
S = input().strip()
N = len(S)
K = int(input())

T = set()
for i in range(N):
    for j in range(i + 1, N + 1):
        T.add(S[i:j])
A = sorted(list(T))
print(A[K])
Problem p03356 (python)

Fixed submission:
def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class UF:
    def __init__(self, N):
        self.state = [-1] * N
        self.rank = [0] * N
        self.num_group = N
    
    def get_parent(self, a):
        p = self.state[a]
        if p < 0:
            return a
        
        q = self.get_parent(p)
        self.state[a] = q
        return q

    def make_pair(self, a, b):
        pa = self.get_parent(a)
        pb = self.get_parent(b)
        if pa == pb:
            return

        if self.rank[pa] > self.rank[pb]:
            pa, pb = pb, pa
            a, b = b, a
        elif self.rank[pa] == self.rank[pb]:
            self.rank[pb] += 1

        self.state[pb] += self.state[pa]
        self.state[pa] = pb
        self.state[a] = pb
        self.num_group -= 1
    
    def is_pair(self, a, b):
        return self.get_parent(a) == self.get_parent(b)

    def get_size(self, a):
        return -self.state[self.get_parent(a)]


N, M = read_values()
A = read_list()
uf = UF(N)
for _ in range(M):
    i, j = read_index()
    uf.make_pair(A[i] - 1, A[j] - 1)

res = 0
for i, a in enumerate(A):
    if uf.is_pair(i, A[i] - 1):
        res += 1

print(res)

Buggy submission (verdict: Runtime Error):
def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


class UF:
    def __init__(self, N):
        self.state = [-1] * N
        self.rank = [0] * N
        self.num_group = N
    
    def get_parent(self, a):
        p = self.state[a]
        if p < 0:
            return a
        
        q = self.get_parent(p)
        self.state[a] = q
        return q

    def make_pair(self, a, b):
        pa = self.get_parent(a)
        pb = self.get_parent(b)
        if pa == pb:
            return

        if self.rank[pa] > self.rank[pb]:
            pa, pb = pb, pa
            a, b = b, a
        elif self.rank[pa] == self.rank[pb]:
            self.rank[pb] += 1

        self.state[pb] += self.state[pa]
        self.state[pa] = pb
        self.state[a] = pb
        self.num_group -= 1
    
    def is_pair(self, a, b):
        return self.get_parent(a) == self.get_parent(b)

    def get_size(self, a):
        return -self.state[self.get_parent(a)]

N, M = read_values()
A = read_list()
uf = UF(N)
for _ in range(M):
    i, j = read_index()
    uf.make_pair(A[i] - 1, A[j] - 1)

res = 0
for i, a in enumerate(A):
    if uf.is_pair(i, A[i] - 1)

print(res)
Problem p03481 (python)

Fixed submission:
import math
X, Y = map(int, input().split())
r = 0
R = Y // X
while R >= 2:
  R //= 2
  r += 1
print(r + 1)

Buggy submission (verdict: Runtime Error):
import math
X, Y = map(int, input().split())
r = 0
while X < Y:
  Y //= X
  r += 1
print(int(math.log2(r)) + 1)
Problem p03599 (python)

Fixed submission:

import sys
from collections import deque
import bisect
import copy
import heapq
import itertools
import math
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    A, B, C, D, E, F = read_values()
    W = set()
    
    for a in range(F // (A * 100) + 1):
        for b in range(F // (B * 100) + 1):
            w = a * 100 * A + b * 100 * B
            if 0 < w <= F:
                W.add(w)

    S = set()
    for c in range(F // C + 1):
        for d in range(F // D + 1):
            s = c * C + d * D
            if 0 <= s <= F:
                S.add(s)
    S = sorted(list(S))
    
    s, t = 0, 100 * A
    for w in W:
        p = min(F - w, math.ceil(E * w / 100))
        f = S[bisect.bisect_right(S, p) - 1]
        if f * 100 > E * w:
            continue

        if s * (w + f) < t * f:
            s, t = f, w + f
    print(t, s)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
A, B, C, D, E, F = list(map(int, input().split()))

def G(a, b):
  while b > 0:
    a, b = b, a%b
  return a
def L(a, b):
  return a * b // G(a, b)

caps = {(N * 100 * A + M * 100 * B) for N in range(1 + F // (100 * A)) for M in range(1 + (F - N * 100 * A) // (100 * B))}

res = [0,0,0]
for water in caps:
    if water == 0: continue
    capp = min(F - water, E * water // 100)
    resug = capp % L(C, D)  
    cosug = capp - resug
    sugers = [(N * C + M * D + cosug) for N in range(resug // C + 1) for M in range((resug - N * C)//D + 1)]
    suger = max(sugers)
    ratio = suger / (water + suger)
    if res[0] < ratio: res = [ratio, suger + watter, suger] 


print(res[1], res[2])

Buggy submission (verdict: Runtime Error):
A, B, C, D, E, F = list(map(int, input().split()))

cap = 0
sug = 0

def G(a, b):
  while b > 0:
    a, b = b, a%b
  return a
def L(a, b):
  return a * b // G(a, b)


caps = {(N * 100 * A + M * 100 * B) for N in range(1 + F // (100 * A)) for M in range(1 + (F - N * 100 * A) // (100 * B))}
print(caps)

res = [0,0,0]
for water in caps:
    if water == 0: continue
    capp = min(F - water, E * water // 100)
    resug = capp % L(C, D)  
    cosug = capp - resug
    sugers = [(N * C + M * D + cosug) for N in range(resug // C + 1) for M in range((resug - N * C)//D + 1)]
    print(water, sugers)
    suger = max(sugers)
    ratio = suger / (water + suger)
    if res[0] < ratio: res = [ratio, suger + watter, suger] 


print(res[1], res[2])
Problem p03606 (python)

Fixed submission:
N = int(input())
res = 0
for _ in range(N):
    r, l = map(int, input().split())
    res += l - r + 1
print(res)

Buggy submission (verdict: Runtime Error):
N = int(input())
res = 0
for _ in range(N)
    r, l = map(int, input().split())
    res += l - r + 1
print(res)

Buggy submission (verdict: Runtime Error):
N = int(input())
res = 0
for _ in range(N)
	r, l = map(int, input().split())
  	res += l - r + 1
print(res)
Problem p03633 (python)

Fixed submission:
def l(a, b):
  if a > b:
    a, b = b, a
  return l(a, b % a) if b % a else a

def g(a, b):
  return a * b // l(a, b)

N = int(input())
res = 1
for n in range(N):
  res = g(res, int(input()))

print(res)

Buggy submission (verdict: Runtime Error):
def l(a, b):
  if a > b:
    a, b = b, a
  return l(a, b % a) if b % a else a

def g(a, b):
  return a * b // l(a, b)

N = int(input())
res = 1
for n in range(N):
  res = g(res, int(input())

print(res)
Problem p03681 (python)

Fixed submission:
N, M = map(int, input().split())
mod = 10 ** 9 + 7
K = max(N, M)
kaijo = [1] * (K + 1) 

for i in range(K):
    kaijo[i + 1] = ((i + 1) * kaijo[i]) % mod
    
if abs(N - M) > 1:
    print(0)
    
else:
    res = 1
    res *= kaijo[N] * kaijo[M]
    if N == M:
        res *= 2
    
    print(res % mod)

Buggy submission (verdict: Runtime Error):
N, M = map(int, input().split())
mod = 10 ** 9 + 7
K = max(N, M)
kaijo = [1] * (K + 1) 

for i in range(K):
    kaijo[i + 1] = ((i + 1) * kaijo[i]) % mod
    
if abs(N - M) > 1:
    print(0)
    
else:
    res = 1
    res *= kaijo[N] * kaijo[M]
    if N == M:
        res *= 2
    
print(res % mod)
Problem p03711 (python)

Fixed submission:
lis = [[1,3,5,7,8,10,12],[4,6,9,11],[2]]
x, y = list(map(int,input().split()))

for i in lis:
    if x in i:
        if y in i:
            print("Yes")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
H, W = list(map(int,input().split()))


blank = ["#" for i in range(W + 2)]
print("".join(blank))
for line in range(H):
    print("#" + input() + "#")
print("".join(blank))
Problem p03714 (python)

Fixed submission:
from heapq import *

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

sumS1 = [0 for i in range(N + 1)]
S1 = a[0:N]
heapify(S1)
sumS1[0] = sum(S1)

for k in range(1, N + 1):
    if S1[0] < a[N + k - 1]:
        sumS1[k] = sumS1[k - 1] +a[N + k - 1] - S1[0]
        heapreplace(S1, a[N + k - 1])
    else:
        sumS1[k] = sumS1[k - 1]

sumS2 = [0 for i in range(N + 1)]
S2 =  [-1 * i for i in a[2 * N:3 * N]]
heapify(S2)
sumS2[N] = sum(S2)

for k in range(1, N + 1):
    if S2[0] < -1 * a[2 * N - k]:
        sumS2[N - k] = sumS2[N - k + 1] - a[2 * N - k] - S2[0]
        heapreplace(S2, -1 * a[2 * N - k])
    else:
        sumS2[N - k] = sumS2[N - k + 1]


res = [float("-inf")] * (N + 1)
for i in range(N + 1):
    res[i] = sumS1[i] + sumS2[i]

res.sort()
print(res[N])

Buggy submission (verdict: Runtime Error):
N = int(input())
a = list(map(int,input().split()))

S1 , S2 = {}, {}
for i in range(N):
    S1[a[i]] = S1.get(a[i], 0) + 1

for i in range(2 * N, 3* N):
    S2[a[i]] = S2.get(a[i], 0) + 1

i = N
j = 2 * N - 1

minS1 = min(S1)
maxS2 = max(S2)

while i <= j:
    delS1 = a[i] - minS1
    delS2 = maxS2 - a[j]

    if delS1 >= 0 and delS1 >= delS2:
        #print("chang S1")
        val = S1.pop(minS1)
        if val >= 2:
            S1[minS1] = val - 1
        S1[a[i]] = S1.get(a[i], 0) + 1
        minS1 = min(S1)
        i += 1

    elif delS2 >= 0:
        val = S2.pop(maxS2)
        if val >= 2:
            S2[minS2] = val - 1
        S2[a[j]] = S2.get(a[j], 0) + 1
        maxS2 = max(S2)
        j -= 1

    else:
        i += 1

sumS1, sumS2 = 0, 0
for i in S1.items():
    sumS1 += i[0] * i[1]
for j in S2.items():
    sumS2 += j[0] * j[1]

print(sumS1 - sumS2)