Buggy python submission (verdict: Runtime Error):
w,h= map(int, raw_input().split())
p =[]
q = []
for i in xrange(w):
    t = input()
    p.append(t)

for i in xrange(h):
    t = input()
    q.append(t)
p =sorted(p,reverse=True)
q =sorted(q, reverse=True)
ac = p[:]
acq = q[:]
i = w-2
while i >=0:
    ac[i] += ac[i+1]
    i -=1
i = h - 2
while i >= 0:
    acq[i] += acq[i + 1]
    i -= 1
c = ac[0] + acq[0]
k = 0
# for i in xrange(h):
i = 0
while i < h:
    # for j in xrange(k,w):
        j = k
        if q[i] > p[j]:
            c += ac[j]
            i += 1
            # break
        else:
            c +=acq[i]
            k +=1
        # c += q[i]
print c

Fixed submission:
w,h= map(int, raw_input().split())
p =[]
q = []
for i in xrange(w):
    t = input()
    p.append(t)

for i in xrange(h):
    t = input()
    q.append(t)
p =sorted(p,reverse=True)
q =sorted(q, reverse=True)
ac = p[:]
acq = q[:]
i = w-2
while i >=0:
    ac[i] += ac[i+1]
    i -=1
i = h - 2
while i >= 0:
    acq[i] += acq[i + 1]
    i -= 1
c = ac[0] + acq[0]
k = 0
# for i in xrange(h):
i = 0
while i < h:
    # for j in xrange(k,w):
        j = k
        # print i,j
        if j >= w:
            break
        if q[i] > p[j]:
            c += ac[j]
            i += 1
            # break
        else:
            c +=acq[i]
            k +=1
        # c += q[i]
print c

Buggy python submission (verdict: Time Limit Exceeded):
w,h= map(int, raw_input().split())
p =[]
q = []
for i in xrange(w):
    t = input()
    p.append(t)

for i in xrange(h):
    t = input()
    q.append(t)
p =sorted(p,reverse=True)
q =sorted(q, reverse=True)
ac = p[:]
i = w-2
while i >=0:
    ac[i] += ac[i+1]
    i -=1
c = ac[0] + sum(q)
for i in xrange(h):
    for j in xrange(w):
        if q[i] > p[j]:
            c += ac[j]
            break
        c += q[i]
print c

Fixed submission:
w,h= map(int, raw_input().split())
p =[]
q = []
for i in xrange(w):
    t = input()
    p.append(t)

for i in xrange(h):
    t = input()
    q.append(t)
p =sorted(p,reverse=True)
q =sorted(q, reverse=True)
ac = p[:]
acq = q[:]
i = w-2
while i >=0:
    ac[i] += ac[i+1]
    i -=1
i = h - 2
while i >= 0:
    acq[i] += acq[i + 1]
    i -= 1
c = ac[0] + acq[0]
k = 0
# for i in xrange(h):
i = 0
while i < h:
    # for j in xrange(k,w):
        j = k
        # print i,j
        if j >= w:
            break
        if q[i] > p[j]:
            c += ac[j]
            i += 1
            # break
        else:
            c +=acq[i]
            k +=1
        # c += q[i]
print c

Buggy python submission (verdict: Time Limit Exceeded):
w,h= map(int, raw_input().split())
p =[]
q = []
for i in xrange(w):
    t = input()
    p.append(t)

for i in xrange(h):
    t = input()
    q.append(t)
c = sum(p) + sum(q)
for i in xrange(h):
    for j in xrange(w):
        c += min(p[j], q[i])
print c

Fixed submission:
w,h= map(int, raw_input().split())
p =[]
q = []
for i in xrange(w):
    t = input()
    p.append(t)

for i in xrange(h):
    t = input()
    q.append(t)
p =sorted(p,reverse=True)
q =sorted(q, reverse=True)
ac = p[:]
acq = q[:]
i = w-2
while i >=0:
    ac[i] += ac[i+1]
    i -=1
i = h - 2
while i >= 0:
    acq[i] += acq[i + 1]
    i -= 1
c = ac[0] + acq[0]
k = 0
# for i in xrange(h):
i = 0
while i < h:
    # for j in xrange(k,w):
        j = k
        # print i,j
        if j >= w:
            break
        if q[i] > p[j]:
            c += ac[j]
            i += 1
            # break
        else:
            c +=acq[i]
            k +=1
        # c += q[i]
print c