Buggy python submission (verdict: Time Limit Exceeded):
W,H = map(int,input().split())

side = []
for i in range(W):
    side.append({"cost":int(input()),"num":i,"dir":0})
for i in range(H):
    side.append({"cost":int(input()),"num":i,"dir":1})
side = sorted(side,key = lambda x:x["cost"])

node =[[{"v":j,"men":[j]} for j in range(i*(W+1),(i+1)*(W+1))]for i in range(H+1)]
ans = 0
for s in side:
    cost,num,di = s["cost"],s["num"],s["dir"]
    if di == 0 :
        for i in range(H+1):
            if node[i][num]["v"] != node[i][num+1]["v"]: # 親が違ったら
                # print(i,num,0)
                ans += cost
                v1 = node[i][num]["v"]
                v2 = node[i][num+1]["v"]
                
                for j in node[v2//(W+1)][v2%(W+1)]["men"]: # v2とつながった要素の親を書き換え
                    node[j//(W+1)][j%(W+1)]["v"] = v1
                node[v1//(W+1)][v1%(W+1)]["men"] += node[v2//(W+1)][v2%(W+1)]["men"]
    else:
        for i in range(W+1):
            if node[num][i]["v"] != node[num+1][i]["v"]: # 親が違ったら
                # print(num,i,1)
                ans += cost
                v1 = node[num][i]["v"]
                v2 = node[num+1][i]["v"]
                
                for j in node[v2//(W+1)][v2%(W+1)]["men"]: # v2とつながった要素の親を書き換え
                    node[j//(W+1)][j%(W+1)]["v"] = v1
                node[v1//(W+1)][v1%(W+1)]["men"] += node[v2//(W+1)][v2%(W+1)]["men"]
print(ans)



Fixed submission:
W, H = map(int, input().split())

side = [{"cost":int(input()), "di":0}for i in range(W)] + [{"cost":int(input()), "di":1}for i in range(H)]
side = sorted(side, key=lambda x:x["cost"])
ans = 0
ty = [H+1,W+1]
for s in side:
    cost, di = s["cost"], s["di"]
    ans += ty[di] * cost
    ty[(di+1)%2] -= 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
W,H = map(int,input().split())

side = []
for i in range(W):
    side.append({"cost":int(input()),"num":i,"dir":0})
for i in range(H):
    side.append({"cost":int(input()),"num":i,"dir":1})
side = sorted(side,key = lambda x:x["cost"])

node =[[{"v":j,"men":[j]} for j in range(i*(W+1),(i+1)*(W+1))]for i in range(H+1)]
ans = 0
for s in side:
    cost,num,di = s["cost"],s["num"],s["dir"]
    if di == 0 :
        for i in range(H+1):
            if node[i][num]["v"] != node[i][num+1]["v"]: # 親が違ったら
                # print(i,num,0)
                ans += cost
                v1 = node[i][num]["v"]
                v2 = node[i][num+1]["v"]
                
                for j in node[v2//(W+1)][v2%(W+1)]["men"]: # v2とつながった要素の親を書き換え
                    node[j//(W+1)][j%(W+1)]["v"] = v1
                node[v1//(W+1)][v1%(W+1)]["men"] += node[v2//(W+1)][v2%(W+1)]["men"]
    else:
        for i in range(W+1):
            if node[num][i]["v"] != node[num+1][i]["v"]: # 親が違ったら
                # print(num,i,1)
                ans += cost
                v1 = node[num][i]["v"]
                v2 = node[num+1][i]["v"]
                
                for j in node[v2//(W+1)][v2%(W+1)]["men"]: # v2とつながった要素の親を書き換え
                    node[j//(W+1)][j%(W+1)]["v"] = v1
                node[v1//(W+1)][v1%(W+1)]["men"] += node[v2//(W+1)][v2%(W+1)]["men"]
print(ans)



Fixed submission:
W, H = map(int, input().split())

side = [{"cost":int(input()), "di":0}for i in range(W)] + [{"cost":int(input()), "di":1}for i in range(H)]
side = sorted(side, key=lambda x:x["cost"])
ans = 0
ty = [H+1,W+1]
for s in side:
    cost, di = s["cost"], s["di"]
    ans += ty[di] * cost
    ty[(di+1)%2] -= 1
print(ans)