Buggy python submission (verdict: Time Limit Exceeded):
x = raw_input()

w,h = [int(i) for i in x.split()]

p = []
for i in range(w):
    p.append(input())

q = []
for i in range(h):
    q.append(input())


cost = 0
p.sort()
q.sort()

iter = w+h


for i in range(iter):

    if(w==0):
        cost += q[0]*(w+1)
        h -= 0
        q.pop(0)

    elif(h==0):
        cost += p[0]*(h+1)
        w -= 0
        p.pop(0)

    elif(p[0]==q[0] and h>w):
        cost += p[0]*(h+1)
#        print cost,w,h
        w -= 1
        p.pop(0)

    elif(p[0]<q[0]):
        cost += p[0]*(h+1)
 #       print cost,w,h
        w -= 1
        p.pop(0)
    
    else:
        cost += q[0]*(w+1)
  #      print cost,w,h
        h -= 1
        q.pop(0)
    

print cost


Fixed submission:
x = raw_input()
w,h = [int(i) for i in x.split()]
p = []
for i in range(w):
    p.append(input())
q = []
for i in range(h):
    q.append(input())
cost = 0
p.sort()
q.sort()
iter = w+h

p_iter = 0
q_iter = 0
for i in range(iter):
    if(w==0):
        cost += q[q_iter]*(w+1)
        h -= 0
        q_iter += 1

    elif(h==0):
        cost += p[p_iter]*(h+1)
        w -= 0
        p_iter += 1

    elif(p[p_iter]==q[q_iter] and h>w):
        cost += p[p_iter]*(h+1)
        w -= 1
        p_iter += 1

    elif(p[p_iter]<q[q_iter]):
        cost += p[p_iter]*(h+1)
        w -= 1
        p_iter += 1
    
    else:
        cost += q[q_iter]*(w+1)
        h -= 1
        q_iter += 1
    
print cost

Buggy python submission (verdict: Time Limit Exceeded):
x = raw_input()

w,h = [int(i) for i in x.split()]

p = []
for i in range(w):
    p.append(input())

q = []
for i in range(h):
    q.append(input())


cost = 0


iter = w+h


for i in range(iter):

    if(len(p)==0):
        cost += min(q)*(w+1)
        h -= 0
        q.remove(min(q))

    elif(len(q)==0):
        cost += min(p)*(h+1)
        w -= 0
        p.remove(min(p))

    elif(min(p)==min(q) and h>w):
        cost += min(p)*(h+1)
        w -= 1
        p.remove(min(p))

    elif(min(p)<min(q)):
        cost += min(p)*(h+1)
 #       print cost,w,h
        w -= 1
        p.remove(min(p))
    
    else:
        cost += min(q)*(w+1)
  #      print cost,w,h
        h -= 1
        q.remove(min(q))
    

print cost

Fixed submission:
x = raw_input()
w,h = [int(i) for i in x.split()]
p = []
for i in range(w):
    p.append(input())
q = []
for i in range(h):
    q.append(input())
cost = 0
p.sort()
q.sort()
iter = w+h

p_iter = 0
q_iter = 0
for i in range(iter):
    if(w==0):
        cost += q[q_iter]*(w+1)
        h -= 0
        q_iter += 1

    elif(h==0):
        cost += p[p_iter]*(h+1)
        w -= 0
        p_iter += 1

    elif(p[p_iter]==q[q_iter] and h>w):
        cost += p[p_iter]*(h+1)
        w -= 1
        p_iter += 1

    elif(p[p_iter]<q[q_iter]):
        cost += p[p_iter]*(h+1)
        w -= 1
        p_iter += 1
    
    else:
        cost += q[q_iter]*(w+1)
        h -= 1
        q_iter += 1
    
print cost

Buggy python submission (verdict: Time Limit Exceeded):
x = raw_input()

w,h = [int(i) for i in x.split()]

p = []
for i in range(w):
    p.append(input())

q = []
for i in range(h):
    q.append(input())


cost = 0
p.sort()
q.sort()

iter = w+h


for i in range(iter):

    if(len(p)==0):
        cost += q[0]*(w+1)
        h -= 0
        q.pop(0)

    elif(len(q)==0):
        cost += p[0]*(h+1)
        w -= 0
        p.pop(0)

    elif(p[0]==q[0] and h>w):
        cost += p[0]*(h+1)
#        print cost,w,h
        w -= 1
        p.pop(0)

    elif(p[0]<q[0]):
        cost += p[0]*(h+1)
 #       print cost,w,h
        w -= 1
        p.pop(0)
    
    else:
        cost += q[0]*(w+1)
  #      print cost,w,h
        h -= 1
        q.pop(0)
    

print cost

Fixed submission:
x = raw_input()
w,h = [int(i) for i in x.split()]
p = []
for i in range(w):
    p.append(input())
q = []
for i in range(h):
    q.append(input())
cost = 0
p.sort()
q.sort()
iter = w+h

p_iter = 0
q_iter = 0
for i in range(iter):
    if(w==0):
        cost += q[q_iter]*(w+1)
        h -= 0
        q_iter += 1

    elif(h==0):
        cost += p[p_iter]*(h+1)
        w -= 0
        p_iter += 1

    elif(p[p_iter]==q[q_iter] and h>w):
        cost += p[p_iter]*(h+1)
        w -= 1
        p_iter += 1

    elif(p[p_iter]<q[q_iter]):
        cost += p[p_iter]*(h+1)
        w -= 1
        p_iter += 1
    
    else:
        cost += q[q_iter]*(w+1)
        h -= 1
        q_iter += 1
    
print cost