Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from heapq import *
from array import *

def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def mst_prim(adj, n):
    pred = defaultdict(lambda: -1)
    key = defaultdict(lambda: float('inf'))
    key[0] = 0
    pq = [(key[v], v) for v in range(n)]
    heapify(pq)
    in_pq = array('B', (True for _ in range(n)))
    while pq:
        _,u = heappop(pq)
        in_pq[u] = False
        for v, v_cost in adj[u]:
            if in_pq[v]:
                weight = v_cost
                if weight < key[v]:
                    pred[v] = u
                    key[v] = weight
                    heappush(pq, (weight, v))
                    in_pq[v] = True
    return (pred, key)

def main():
    w, h = reads()
    p = [read() for _ in range(w)]
    q = [read() for _ in range(h)]

    adj = defaultdict(set)
    for x in range(w+1):
        for y in range(h+1):
            if x < w:
                adj[x+y*(w+1)].add((x+1+y*(w+1), p[x]))
                adj[x+1+y*(w+1)].add((x+y*(w+1), p[x]))
            if y < h:
                adj[x+y*(w+1)].add((x+(y+1)*(w+1), q[y]))
                adj[x+(y+1)*(w+1)].add((x+y*(w+1), q[y]))
    print(sum(mst_prim(adj, (w+1)*(h+1))[1].values()))

main()


Fixed submission:
def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    w, h = reads()
    p = sorted([read() for _ in range(w)])[::-1]
    q = sorted([read() for _ in range(h)])[::-1]

    pi = w-1
    qi = h-1
    res = 0
    while 0<=pi and 0<=qi:
        if p[pi] < q[qi]:
            res += p[pi] * (h+1)
            pi -= 1
            w -= 1
        else:
            res += q[qi] * (w+1)
            qi -= 1
            h -= 1
    res += sum(p[:pi+1]) + sum(q[:qi+1])
    print(res)

main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from heapq import *

def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        return '({},{})'.format(self.x, self.y)

def dist(p1, p2, p, q):
    if (p1.x,p1.y) == (p2.x, p2.y):
        return float('inf')
    if p1.x == p2.x and abs(p1.y - p2.y) == 1:
        return q[min(p1.y, p2.y)]
    elif p1.y == p2.y and abs(p1.x - p2.x) == 1:
        return p[min(p1.x, p2.x)]
    return float('inf')

def mst(ps, p, q):
    m =  [[dist(ps[i], ps[j], p, q) for i in range(len(ps))] for j in range(len(ps))]

    X = set([0])
    Y = set(range(len(ps))[1:])
    cost = 0
    edges = []
    while len(Y) != 0:
        mcost = float('inf')
        edge = None
        for ex in X:
            for ey in Y:
                if mcost > m[ex][ey]:
                    mcost = m[ex][ey]
                    edge = [ex, ey]
        X.add(edge[1])
        Y.remove(edge[1])

        cost += mcost
        edges.append(edge)
    return cost, edges

def main():
    w, h = reads()
    p = [read() for _ in range(w)]
    q = [read() for _ in range(h)]
    points = []
    for x in range(w+1):
        for y in range(h+1):
            points.append((Point(x,y)))
    print(int(mst(points, p, q)[0]))

main()


Fixed submission:
def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    w, h = reads()
    p = sorted([read() for _ in range(w)])[::-1]
    q = sorted([read() for _ in range(h)])[::-1]

    pi = w-1
    qi = h-1
    res = 0
    while 0<=pi and 0<=qi:
        if p[pi] < q[qi]:
            res += p[pi] * (h+1)
            pi -= 1
            w -= 1
        else:
            res += q[qi] * (w+1)
            qi -= 1
            h -= 1
    res += sum(p[:pi+1]) + sum(q[:qi+1])
    print(res)

main()