Buggy python submission (verdict: Time Limit Exceeded):


a,b,m = [int(x) for x in input().split()]
an = [(int(x),i+1) for i,x in enumerate(input().split())]
bn = [(int(x),i+1) for i,x in enumerate(input().split())]

an = sorted(an)
bn = sorted(bn)

discounts = {}
discount_list = []
for i in range(m):
    x,y,c = [int(x) for x in input().split()]
    if (x,y) in discounts and discounts[(x,y)] > c:
        continue
    discounts[(x,y)] = c
    discount_list.append(c)

discount_list = sorted(discount_list,reverse=True)

i = 0
j = 0
min = an[0][0]+bn[0][0]
max_discount = max(discount_list)
len_an = len(an)
len_bn = len(bn)

# print(an)
# print(bn)
# print(min)
# print(discounts)

for ai in an:
    for bj in bn:
        a_index = ai[1]
        b_index = bj[1]
        discount = 0
        if (a_index, b_index) in discounts:
            discount = discounts[(a_index, b_index)]
        total = ai[0] + bj[0] - discount
        if min > total:
            min = total

        if ai[0] + bj[0] - max_discount > min:
            break



# while True:
#     a_index = an[i][1]
#     b_index = bn[j][1]
#     discount = 0
#     if (a_index, b_index) in discounts:
#         discount = discounts[(a_index, b_index)]
#     total = an[i][0] + bn[j][0] - discount
#     if min > total:
#         min = total
#
#     if an[i][0] + bn[j][0] - max_discount > min:
#         break
#     elif i == len_an-1 and j == len_bn-1:
#         break
#     elif i == len_an-1:
#         j += 1
#     elif j == len_bn-1:
#         i += 1
#     else:
#         a_diff = an[i+1][0] - an[i][0]
#         b_diff = bn[j+1][0] - an[j][0]
#         if a_diff < b_diff:
#             i += 1
#         else:
#             j += 1

print(min)





Fixed submission:


a,b,m = [int(x) for x in input().split()]
an = [int(x) for x in input().split()]
bn = [int(x) for x in input().split()]

ans = min(an)+min(bn)
for i in range(m):
    p, q, discount = [int(x) for x in input().split()]
    a_cost = an[p-1]
    b_cost = bn[q-1]
    total = a_cost + b_cost - discount
    ans = min(total, ans)

print(ans)

Buggy python submission (verdict: Runtime Error):


a,b,m = [int(x) for x in input().split()]
an = [(int(x),i+1) for i,x in enumerate(input().split())]
bn = [(int(x),i+1) for i,x in enumerate(input().split())]

an = sorted(an)
bn = sorted(bn)

discounts = {}
discount_list = []
for i in range(m):
    x,y,c = [int(x) for x in input().split()]
    if (x,y) in discounts and discounts[(x,y)] > c:
        continue
    discounts[(x,y)] = c
    discount_list.append(c)

discount_list = sorted(discount_list,reverse=True)

i = 0
j = 0
min = an[0][0]+bn[0][0]
max_discount = max(discount_list)
len_an = len(an)
len_bn = len(bn)

# print(an)
# print(bn)
# print(min)
# print(discounts)

while True:
    a_index = an[i][1]
    b_index = bn[j][1]
    discount = 0
    if (a_index, b_index) in discounts:
        discount = discounts[(a_index, b_index)]
    total = an[i][0] + bn[j][0] - discount
    if min > total:
        min = total

    if an[i][0] + bn[j][0] - max_discount > min:
        break
    elif i == len_an-1 and j == len_bn-1:
        break
    elif i == len_an-1:
        j += 1
    elif j == len_bn-1:
        i += 1
    else:
        a_diff = an[i+1][0] - an[i][0]
        b_diff = bn[j+1][0] - an[j][0]
        if a_diff < b_diff:
            i += 1
        else:
            j += 1


print(min)





Fixed submission:


a,b,m = [int(x) for x in input().split()]
an = [int(x) for x in input().split()]
bn = [int(x) for x in input().split()]

ans = min(an)+min(bn)
for i in range(m):
    p, q, discount = [int(x) for x in input().split()]
    a_cost = an[p-1]
    b_cost = bn[q-1]
    total = a_cost + b_cost - discount
    ans = min(total, ans)

print(ans)