Problem:


Score : 200 points

Problem Statement

You are visiting a large electronics store to buy a refrigerator and a microwave.

The store sells A kinds of refrigerators and B kinds of microwaves. The i-th refrigerator ( 1 \le i \le A ) is sold at a_i yen (the currency of Japan), and the j-th microwave ( 1 \le j \le B ) is sold at b_j yen.

You have M discount tickets. With the i-th ticket ( 1 \le i \le M ), you can get a discount of c_i yen from the total price when buying the x_i-th refrigerator and the y_i-th microwave together. Only one ticket can be used at a time.

You are planning to buy one refrigerator and one microwave. Find the minimum amount of money required.

Constraints

All values in input are integers.

 1 \le A \le 10^5 

 1 \le B \le 10^5 

 1 \le M \le 10^5 

 1 \le a_i , b_i , c_i \le 10^5 

 1 \le x_i \le A 

 1 \le y_i \le B 

 c_i \le a_{x_i} + b_{y_i} 

Input

Input is given from Standard Input in the following format:

A B M
a_1 a_2 ... a_A
b_1 b_2 ... b_B
x_1 y_1 c_1
\vdots
x_M y_M c_M

Output

Print the answer.

Sample Input 1

2 3 1
3 3
3 3 3
1 2 1

Sample Output 1

5

With the ticket, you can get the 1-st refrigerator and the 2-nd microwave for 3+3-1=5 yen.

Sample Input 2

1 1 2
10
10
1 1 5
1 1 10

Sample Output 2

10

Note that you cannot use more than one ticket at a time.

Sample Input 3

2 2 1
3 5
3 5
2 2 2

Sample Output 3

6

You can get the 1-st refrigerator and the 1-st microwave for 6 yen, which is the minimum amount to pay in this case.
Note that using a ticket is optional.



Sample input:
2 3 1
3 3
3 3 3
1 2 1


Sample output:
5

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
m1 = min(A) + min(B)
m2 = 100000
for i in range(m):
    l = list(map(int, input().split(' ')))
    lp = A[l[0] - 1] + B[l[1] - 1] - l[2]
    if lp < m2:
        m2 = lp        
print(m1 if m1 < m2 else m2)

Submission status: Wrong Answer

Solution code:
[A, B, M] = input().split()
A = int(A)
B = int(B)
M = int(M)

a = list(map(int, input().split()))
b = list(map(int, input().split()))

cut_map = [[0*i for i in range(B)] for j in range(A)]
for i in range(M):
    [inx, iny, inc] = list(map(int, input().split()))
    if cut_map[inx-1][iny-1] > -inc:
        cut_map[inx-1][iny-1] = -inc

for i in range(A):
    for j in range(B):
        cut_map[i][j] = a[i] + b[j]

print(min(min(cut_map)))

Submission status: Wrong Answer

Solution code:
A, B, M = input().split()
A = int(A)
B = int(B)
M = int(M)
listA = [int(i) for i in input().split()]
listB = [int(i) for i in input().split()]
table = [[int(i) for i in input().split()]for l in range(M)]
li = []
for k in range(A):
    for j in range(B):
        if j <= M:
            waribiki = table[j - 1]
        else:
            waribiki = [0,0,0]
        plice = int(listA[k] + listB[j] - waribiki[2])
        li.append(plice)
print(li)
li.sort()
print(li[0])

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [[x, y, c]]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    # print(A)
    # print(B)
    # print(xyc)

    p_min = min(A) + min(B)
    ret = min(A) + min(B)
    for j in xyc:
        if p_min >= (A[j[0]-1] + B[j[1]-1]):
            ret = A[j[0]-1] + B[j[1]-1] - j[2]
        else:
            continue
    print(ret)

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [[x, y, c]]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    print(A)
    print(B)
    print(xyc)

    p_min = min(A) + min(B)
    ret = min(A) + min(B)
    for j in xyc:
        if p_min >= (A[j[0]-1] + B[j[1]-1]):
            ret = A[j[0]-1] + B[j[1]-1] - j[2]
        else:
            continue
    print(ret)

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())

a_A = list(map(int, input().split()))
b_B = list(map(int, input().split()))

mini = min(a_A) + min(b_B)

x = []
y = []
c = []

for i in range(m):
    x_t, y_t, c_t = map(int, input().split())
    x.append(x_t)
    y.append(y_t)
    c.append(c_t)

# print(a_A[1])

minwork = 10 ** 5

for i in range(m):
    # print(i)
    x_i = int(x[i])
    # print(x_i)
    # print(a_A[x_i])
    y_i = y[i]
    c_i = c[i]
    # print("a")
    # print(int(a_A[x_i-1]))
    # print(int(b_B[y_i-1]))
    # minwork = a_A[x_i - 1] + b_B[y_i - 1] - c[c_i]
    # print("yahoo")
    # print(minwork)
    # print(c_i)
    # print(int(c[c_i-1]))
    # print("b")
    if minwork >= a_A[x_i - 1] + b_B[y_i - 1]- int(c_i):
        minwork = a_A[x_i - 1] + b_B[y_i - 1]- int(c_i)
        # print("a")
    # print(i)

if minwork < mini:
    print(minwork)
else:
    print(mini)

Submission status: Wrong Answer

Solution code:
n_freges, n_micros, M = [int(x) for x in input().split()]
frege_prices = [int(x) for x in input().split()]
micro_prices = [int(x) for x in input().split()]
discounts = {}
for i in range(M):
    x, y, c = [int(x) for x in input().split()]
    prev_c = discounts.get((x, y), 0)
    if c >= prev_c:
        discounts[x - 1, y - 1] = c
        discounts[y - 1, x - 1] = c

cost = 2 * (10 ** 5) + 1
for i in range(n_freges):
    for j in range(n_micros):
        if frege_prices[i] + micro_prices[j] - discounts.get((i, j), 0) < cost:
            cost = frege_prices[i] + micro_prices[j] - discounts.get((i, j), 0)

print(cost)

Submission status: Wrong Answer

Solution code:
s = input().split(' ')
n = 0
if int(s[0]) > 0:
    n += 1
if int(s[1]) > 0:
    n += 1
if int(s[2]) > 0:
    n += int(s[2])
coupon = []
for times in range(1, n+1):
    if times == 1:
        ref = input().split(' ')
    if times == 2:
        micro = input().split(' ')
    if times > 2:
        coupon.append(input().split(' '))
price = int(ref[0]) + int(micro[0])
minPrice = price
for discount in coupon:
    price = int(ref[int(discount[0])-1]) + int(micro[int(discount[1])-1]) - int(discount[2])
    minPrice = min(minPrice, price)
print(minPrice)

Submission status: Wrong Answer

Solution code:
s = input().split(' ')
n = 0
if int(s[0]) > 0:
    n += 1
if int(s[1]) > 0:
    n += 1
if int(s[2]) > 0:
    n += int(s[2])
coupon = []
for times in range(1, n+1):
    if times == 1:
        ref = input().split(' ')
    if times == 2:
        micro = input().split(' ')
    if times > 2:
        coupon.append(input().split(' '))
price = int(ref[0]) + int(micro[0])
minPrice = price
for discount in coupon:
    price = int(ref[int(discount[0])-1]) + int(micro[int(discount[1])-1]) - int(discount[2])
    minPrice = min(minPrice, price)
print("$$$", minPrice)

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [x, y, c]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    # print(A)
    # print(B)
    # print(xyc)

    p_min = min(A) + min(B)
    ret = min(A) + min(B)
    for j in xyc:
        if j is not list:
            if p_min >= (A[xyc[0]-1] + B[xyc[1]-1]):
                ret = A[xyc[0]-1] + B[xyc[1]-1] - xyc[2]
                break
            else:
                break
        elif p_min >= (A[j[0]-1] + B[j[1]-1]):
            ret = A[j[0]-1] + B[j[1]-1] - j[2]
        else:
            continue
    print(ret)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
list_a = []
list_b = []
list_x = []
list_y = []
list_c = []
sum = []

a = input()
list_a = a.split()

b = input()
list_b = b.split()

for i in range(0, M):
    x, y, c = map(int, input().split())
    list_x.append(x)
    list_y.append(y)
    list_c.append(c)

list_a = sorted(list_a)
list_b = sorted(list_b)

sum_1 = int(list_a[0]) + int(list_b[0])
sum.append(sum_1)

for k in range(0, M):
    x_1 = int(list_x[k])
    y_1 = int(list_y[k])
    sum_2 = int(list_a[x_1-1]) + int(list_b[y_1-1]) - list_c[k]
    sum.append(sum_2)

print(min(sum))

Submission status: Wrong Answer

Solution code:
data = input().split(" ")
A = input().split(" ")
B = input().split(" ")
total = 0
a = 0
b = 0
j = 0
for i in data:
    data[j] = int(i)
    j += 1
j = 0
for i in A:
    A[j] = int(i)
    j += 1
j = 0
for i in B:
    B[j] = int(i)
    j += 1


for i in range(data[2]):
    data1 = input().split()
    j = 0
    for i in data1:
        data1[j] = int(i)
        j += 1
    data2 = A[data1[0]-1] + B[data1[1]-1] - data1[2]
    if total > data2:
        total = data2

for i in A:
    if a > i:
        a = i
for i in B:
    if b > i:
        b = i

if total > a+b:
    total = a+b

print(total)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = {}
for _ in range(M):
    x, y, z = map(int, input().split())
    key = str(x) + "&" + str(y)
    if key in m.keys():
        if m[key] < z:
            m[key] = z
    else:
        m[key] = z
print(0)

Submission status: Wrong Answer

Solution code:
data = input().split(" ")
A = input().split(" ")
B = input().split(" ")
total = 100000
a = 100000
b = 100000
j = 0
for i in data:
    data[j] = int(i)
    j += 1
j = 0
for i in A:
    A[j] = int(i)
    j += 1
j = 0
for i in B:
    B[j] = int(i)
    j += 1


for i in range(data[2]):
    data1 = input().split()
    j = 0
    for i in data1:
        data1[j] = int(i)
        j += 1
    data2 = A[data1[0]-1] + B[data1[1]-1] - data1[2]
    print(data2)
    if total > data2:
        total = data2

for i in A:
    if a > i:
        a = i
for i in B:
    if b > i:
        b = i

if total > a+b:
    total = a+b

print(total)

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [x, y, c]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    # print(A)
    # print(B)
    # print(xyc)

    p_min = min(A) + min(B)
    for j in xyc:
        if j is not list:
            if p_min >= (A[xyc[0]-1] + B[xyc[1]-1] - xyc[2]):
                p_min = A[xyc[0]-1] + B[xyc[1]-1] - xyc[2]
                break
            else:
                break
        elif p_min >= (A[j[0]-1] + B[j[1]-1] - j[2]):
            p_min = A[j[0]-1] + B[j[1]-1] - j[2]
        else:
            continue
    print(p_min)

Submission status: Wrong Answer

Solution code:
import numpy as np

A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

L = []



waribiki = [tuple(map(int,input().split())) for _ in range(M)]

for x,y,m in waribiki:
  d = a[x-1]+b[y-1]-m
  L.append(d)

aa = np.array(a)

for i in range(B):
  ans = aa + b[i]
  l = np.array(L)
  kotae = np.append(l,ans)
  
print(min(kotae))

Submission status: Wrong Answer

Solution code:
A,B,M  = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = [0] * M
x = [0] * M
y = [0] * M
for i in range(M):
    t[i], x[i], y[i] = map(int, input().split())

p=int()
q=int()
p=a[0]+b[0]
number1=0
number2=0
for i in a:
    number1+=1
    for s in b: 
        number2+=1
        for m in range(M):
            if number1==t[m]:
                if number2==x[m]:
                    r=int(y[m])
                else:
                    r=0
            else :
                r=0
            q=i+s-r
            if q<p:
                p=q
print(p)

Submission status: Wrong Answer

Solution code:
A,B,M = map(int, input().split())

la = list(map(int, input().split()))
lb = list(map(int, input().split()))

d = []
for i in range(M):
  d.append(list(map(int, input().split())))
           
for i in range(M):
  d[i] = la[(d[i][0]-1)] + lb[(d[i][1]-1)] - d[i][2]

print(min(d))

Submission status: Wrong Answer

Solution code:
A,B, M = map(int,input().split())

A_list = list(map(int,input().split()))
B_list = list(map(int,input().split()))

x_list = []
y_list = []
c_list = []



#print(A,B,M)
#print(A_list)
#print(B_list)

min = 10000000
sum = 0
min_waribiki = 1000000
sum_waribiki = 0
for i in range(M):
    x,y,c= list(map(int,input().split()))
    #print(x,y,c)

    x_list.append(x)
    y_list.append(y)
    c_list.append(c)
    sum_waribiki = A_list[x-1] + B_list[y-1] - c
    if sum_waribiki < min_waribiki:
        min_waribiki = sum_waribiki

for a in range(A):
    for b in range(B):
        sum = A_list[a] + B_list[b]

        if sum < min:
            min = sum

if min_waribiki <= min:
    print(sum_waribiki)
else:
    print(min)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = {}
for _ in range(M):
    x, y, z = map(int, input().split())
    key = str(x) + "&" + str(y)
    if key in m.keys():
        if m[key] < z:
            m[key] = z
    else:
        m[key] = z
dp = [[10**18 for i in range(B + 1)] for j in range(A + 1)]
for i in range(A):
    for j in range(B):
        c = m[str(i + 1) + "&" + str(j + 1)] if str(i + 1) + \
            "&" + str(j + 1) in m.keys() else 0
        dp[i + 1][j + 1] = min(dp[i][j], dp[i + 1][j],
                               dp[i][j + 1], a[i] + b[j]) - c
print(dp[A][B])

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

x = [0] * M
y = [0] * M
c = [0] * M

min = 99999999

for i in range(M):
    x[i], y[i], c[i] = map(int, input().split())
    cost = a[x[i] - 1] + b[y[i] - 1] - c[i]
    if cost < min:
        min = cost

min = 99999999

for i in range(A):
    for j in range(B):
        cost = a[i] + b[j]
        if cost < min:
            min = cost

print(min)

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [x, y, c]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    # print(A)
    # print(B)
    # print(xyc)

    p_min = min(A) + min(B)
    for j in xyc:
        if j is not list:
            if p_min >= (A[xyc[0]-1] + B[xyc[1]-1] - xyc[2]):
                p_min = A[xyc[0]-1] + B[xyc[1]-1] - xyc[2]
                break
        elif p_min >= (A[j[0]-1] + B[j[1]-1] - j[2]):
            p_min = A[j[0]-1] + B[j[1]-1] - j[2]
    print(p_min)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
price1 = min(a) + min(b)

for _ in range(M):
  x, y, c = map(int,input().split())
  price2 = a[x-1] + a[y-2] - c
  if price1 > price2:
    price1 = price2
print(price1)

Submission status: Wrong Answer

Solution code:
[A, B, M] = input().split()
A = int(A)
B = int(B)
M = int(M)

a = list(map(int, input().split()))
b = list(map(int, input().split()))

x = []
y = []
c = []
for i in range(M):
    [inx, iny, inc] = list(map(int, input().split()))
    x.append(inx)
    y.append(iny)
    c.append(inc)

cost_map = [[0*i for i in range(B)] for j in range(A)]

for i in range(A):
    for j in range(B):
        cost_map[i][j] = a[i] + b[j]

for i in range(M):
    cost_map[x[i]-1][y[i]-1] = cost_map[x[i]-1][y[i]-1] - c[i]

import numpy as np
ex1 = np.array(cost_map)
print(np.min(ex1))

Submission status: Wrong Answer

Solution code:
A,B,M = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
S = P
T = Q
S.sort()
T.sort()
minimum = (S[0]+T[0])
K = 10**9
for i in range(M):
    x,y,c = map(int,input().split())
    S = P[x-1]+Q[y-1]-c
    if S<K:
      X = S
if minimum<K:
    print(X)
else:
    print(minimum)

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())
priceOfMicrowaves = list(map(int, input().split()))
priceOfRefrigerators = list(map(int, input().split()))
priceTable = [list(map(int,input().split())) for i in range(m)]
cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
for price in priceTable:
    if priceOfMicrowaves[price[0]-1]-price[2] >= cheepest:
        continue
    elif priceOfRefrigerators[price[1]-1]-price[2] >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
        cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
min(priceOfMicrowaves) + min(priceOfRefrigerators)
print(cheepest)

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())
priceOfMicrowaves = list(map(int, input().split()))
priceOfRefrigerators = list(map(int, input().split()))
priceTable = [list(map(int,input().split())) for i in range(m)]
cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
for price in priceTable:
    if priceOfMicrowaves[price[0]-1]-price[2] >= cheepest:
        continue
    elif priceOfRefrigerators[price[1]-1]-price[2] >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
        cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
if cheepest > 0:
    min(priceOfMicrowaves) + min(priceOfRefrigerators)
print(cheepest)

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())
priceOfMicrowaves = list(map(int, input().split()))
priceOfRefrigerators = list(map(int, input().split()))
priceTable = [list(map(int,input().split())) for i in range(m)]
cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
for price in priceTable:
    if priceOfMicrowaves[price[0]-1]-price[2] >= cheepest:
        continue
    elif priceOfRefrigerators[price[1]-1]-price[2] >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
        cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
if cheepest > 1:
    min(priceOfMicrowaves) + min(priceOfRefrigerators)
print(cheepest)

Submission status: Wrong Answer

Solution code:
data = input().split(" ")
A = input().split(" ")
B = input().split(" ")
total = 100000
a = 100000
b = 100000
j = 0
for i in data:
    data[j] = int(i)
    j += 1
j = 0
for i in A:
    A[j] = int(i)
    j += 1
j = 0
for i in B:
    B[j] = int(i)
    j += 1


for i in range(data[2]):
    data1 = input().split()
    j = 0
    for i in data1:
        data1[j] = int(i)
        j += 1
    data2 = A[data1[0]-1] + B[data[1]-1] - data1[2]
    if total > data2:
        total = data2

for i in A:
    if a > i:
        a = i
for i in B:
    if b > i:
        b = i

if total > a+b:
    total = a+b

print(total)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
# items
a = list(map(int, input().split()))
b = list(map(int, input().split()))
# discount
d = [list(map(int,input().split())) for i in range(M)]
max = 0
max_i = 0
hoge = 0
for i in range(M):
  if d[i][0] == 1 + a.index(min(a)) and d[i][1] == 1 + b.index(min(b)):
    hoge = i + 1
  if d[i][2] > max:
    max = d[i][2]
    max_i = i

min1 = min(a) + min(b)
min2 = a[d[max_i][0] - 1] + b[d[max_i][1] - 1] - d[max_i][2]
min3 = min(a) + min(b) - d[hoge - 1][2]
print(min(min1,min2,min3))

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())
priceOfMicrowaves = list(map(int, input().split()))
priceOfRefrigerators = list(map(int, input().split()))
priceTable = [list(map(int,input().split())) for i in range(m)]
cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
            
for price in priceTable:
    if price[2] + 100 >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]-price[2] >= cheepest:
        continue
    elif priceOfRefrigerators[price[1]-1]-price[2] >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
        cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
for p in priceOfMicrowaves:
    if p >= cheepest:
        continue
    for q in priceOfRefrigerators:
        if q >= cheepest:
            continue
        if p + q < cheepest:
            cheepest = p + q
print(cheepest)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
z = [list(map(int, input().split())) for _ in range(M)]

C = min(A, B)
mem = []
for i in range(C):
    mem.append(a[i] + b[i])

for x, y, c in z:
    mem.append(a[x - 1] + b[y - 1] - c)

print(min(mem))

Submission status: Wrong Answer

Solution code:


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 = {}
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

i = 0
j = 0
min = an[0][0]+bn[0][0]

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

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [x, y, c]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    # print(A)
    # print(B)
    # print(xyc)

    p_min = min(A) + min(B)
    for j in [xyc]:
        if p_min >= (A[j[0]-1] + B[j[1]-1] - j[2]):
            p_min = A[j[0]-1] + B[j[1]-1] - j[2]
    print(p_min)

Submission status: Wrong Answer

Solution code:
A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
x = [0]*M
y = [0]*M
c = [0]*M
for i in range(M):
  x[i],y[i],c[i] = map(int,input().split())
ans = 10**10
ma = max(c)
#ind = c.index(ma)
am = min(a)
bm = min(b)
mae = am + bm #ここまで
#indxy = a[x[ind]-1] + b[y[ind]-1] - ma

for i in range(M):
  mai = a[x[i]-1] + b[y[i] -1] - c[i]
  if mai < ans:
    ans = mai
print(ans)

Submission status: Wrong Answer

Solution code:
import re
index = re.search(r'(hi)+(.*?)', input())
if index:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())
a_price = list(map(int, input().split()))
b_price = list(map(int, input().split()))
m_prices = []
for i in range(m):
  m_prices.append(list(map(int, input().split())))

ref = min(a_price)
mi = min(b_price)
c = []
for i in range(m):
  temp = dict.fromkeys(m_prices[i])
  if len(temp) != 1:
  	c.append(m_prices[i][2])

if len(c) != 0:
  print(ref + mi - max(c))
else:
  print(ref + mi)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
z = [list(map(int, input().split())) for _ in range(M)]

C = min(A, B)
mem = []
for i, j in zip(a, b):
    mem.append(i + j)

for x, y, c in z:
    mem.append(a[x - 1] + b[y - 1] - c)

print(min(mem))

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())
priceOfMicrowaves = list(map(int, input().split()))
priceOfRefrigerators = list(map(int, input().split()))
priceTable = [list(map(int,input().split())) for i in range(m)]
cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
for p in priceOfMicrowaves:
    if p >= cheepest:
        continue
    for q in priceOfRefrigerators:
        if q >= cheepest:
            continue
        if p + q < cheepest:
            cheepest = p + q
for price in priceTable:
    if price[2] + 100 >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]-price[2] >= cheepest:
        continue
    elif priceOfRefrigerators[price[1]-1]-price[2] >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
        cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
print(cheepest)

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [x, y, c]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    # print(A)
    # print(B)
    # print(xyc)

    A_set = list(set(A))
    B_set = list(set(B))
    p = []
    p_min  = 999
    for a, b in itertools.product(A_set, B_set):
        if p_min >= (a + b):
            p_min = a + b
    for j in [xyc]:
        if p_min >= (A[j[0]-1] + B[j[1]-1] - j[2]):
            p_min = A[j[0]-1] + B[j[1]-1] - j[2]
    print(p_min)

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split())
priceOfMicrowaves = list(map(int, input().split()))
priceOfRefrigerators = list(map(int, input().split()))
priceTable = [list(map(int,input().split())) for i in range(m)]
cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
for p in priceOfMicrowaves:
    if p >= cheepest:
        continue
    for q in priceOfRefrigerators:
        if q >= cheepest:
            continue
        if p + q < cheepest:
            cheepest = p + q
for price in priceTable:
    if price[2] >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]-price[2] >= cheepest:
        continue
    elif priceOfRefrigerators[price[1]-1]-price[2] >= cheepest:
        continue
    elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
        cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
print(cheepest)

Submission status: Wrong Answer

Solution code:
A,B,M = map(int, input().split())

a=list(map(int, input().split()))
b=list(map(int, input().split()))
dis=[list(map(int,input().split()))  for l in range(M)]


comp1=a[dis[0][0]-1]+a[dis[0][0]-1]-dis[0][2]
for l in range(M):
    y=a[dis[l][0]-1]+b[(dis[l][1])-1]-dis[l][2]
    if comp1>y:
        comp1=y

comp2=min(a)+min(b)
                  
print(min(comp1,comp2))

Submission status: Wrong Answer

Solution code:
A,B,M = map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
s = [input().split() for i in range(M)]

mini = min(a) + min(b)
credit = []

for i in s:
    credit.append(int(i[-1]))

Credit = max(credit)

a_s,b_s,c_s = s[credit.index(Credit)-1]

if mini <= a[int(a_s)-1] +b[int(b_s)-1] -int(Credit):
    print(mini)
else:
    print(a[int(a_s)-1] +b[int(b_s)-1] -int(Credit))

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [x, y, c]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    # print(A)
    # print(B)
    # print(xyc)

    A_set = list(set(A))
    B_set = list(set(B))
    p = []
    for a, b in itertools.product(A_set, B_set):
        p.append(a + b)
    p = [min(p)]
    for j in [xyc]:
        p.append(A[j[0]-1] + B[j[1]-1] - j[2])
    ret = min(p)    
    print(ret)

Submission status: Wrong Answer

Solution code:
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
ad=bd={}
for i in range(len(a)):ad[i]=a[i]
b=list(map(int,input().split()))
for i in range(len(b)):bd[i]=b[i]
m=min(a)+min(b)
for _ in range(M):
    x,y,c=map(int,input().split())
    s=ad[x-1]+bd[y-1]-c
    m=min(m,s)
print(m)

Submission status: Wrong Answer

Solution code:
a, b, m = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
M = []
lp = 0
id = 0
for i in range(m):
    l = list(map(int, input().split(' ')))
    M.append(l)
    if lp < l[2]:
        lp = l[2]
        id = i
m1 = min(A) + min(B)
m2 = A[M[id][0] - 1] + B[M[id][1] - 1] - M[id][2]
print(m1 if m1 < m2 else m2)

Submission status: Wrong Answer

Solution code:
inp = input()
A=int(inp[0])
B=int(inp[2])
M=int(inp[4])
a=input().split(" ")
b=input().split(" ")

s=[0]*(M+(1))
m=[[0]*3]*M
i=0
for x in m:
    x=input().split(" ")
    s[i]=int(a[int(x[0])-1])+int(b[int(x[1])-1])-int(x[2])
    i+=1

s[i]=int(min(a))+int(min(b))

print(min(s))

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    pc_max = 0
    for i in range(M):
        x, y, c = map(int, input().split())
        if c > pc_max:
            xyc = [x, y, c]
        elif c == pc_max:
            xyc.append([x, y, c])
        else:
            continue
    print(A)
    print(B)
    print(xyc)

    A_set = list(set(A))
    B_set = list(set(B))
    p = []
    for a, b in itertools.product(A_set, B_set):
        p.append(a + b)
    p = [min(p)]
    for j in [xyc]:
        p.append(A[j[0]-1] + B[j[1]-1] - j[2])
    ret = min(p)    
    print(ret)

Submission status: Wrong Answer

Solution code:
A,B,M  = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = [0] * M
x = [0] * M
y = [0] * M
for i in range(M):
    t[i], x[i], y[i] = map(int, input().split())

p=int()
q=int()
p=a[0]+b[0]
number1=0
number2=0
for i in a:
    number1+=1
    for s in b: 
        number2+=1
        if number1 in t:
            if number2 in x:
                r=int(y[x.index(number2)])
            else:
                    r=0
        else :
            r=0
        q=i+s-r
        if q<p:
            p=q
print(p)

Submission status: Wrong Answer

Solution code:
A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
x = [0]*M
y = [0]*M
c = [0]*M
for i in range(M):
  x[i],y[i],c[i] = map(int,input().split())
ma = max(c)
ind = c.index(ma)
am = min(a)
bm = min(b)
mae = am + bm #ここまで
indxy = a[x[ind]-1] + b[y[ind]-1] - ma


if mae < indxy:
  print(mae)
else:
  print(indxy)