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: Time Limit Exceeded

Solution code:
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
List=[]
for i in range(M):
    List.append(list(map(int,input().split())))
if M==1:
    print(min(a[List[0][0]-1]+b[List[0][1]-1]-List[0][2],min(a)+min(b)))
else:
    LIST=[]
    for i in range(1,M+1):
        LIST.append(str(min(a[List[i-1][0]-1]+b[List[i-1][1]-1]-List[i-1][2],min(a)+min(b))))
    print(min(LIST))

Submission status: Time Limit Exceeded

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]
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: Time Limit Exceeded

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())

min = 99999999

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

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


print(min)

Submission status: Time Limit Exceeded

Solution code:
a, b, m = input().split()
a, b, m = int(a), int(b), int(m)
a_values = input().split()
b_values = input().split()
m_values = {}
for i in range(m):
    m_values[i] = input().split()

min = int(a_values[int(m_values[0][0])-1]) + int(b_values[int(m_values[0][1])-1]) - int(m_values[0][2])
for i in range(1, m):
    now = int(a_values[int(m_values[i][0])-1]) + int(b_values[int(m_values[i][1])-1]) - int(m_values[i][2])
    if min > now:
        min = now
for i in a_values:
    for j in b_values:
        now = int(i) + int(j)
        if min > now:
            min = now
print(min)

Submission status: Time Limit Exceeded

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

m = []

for i in range(M):
    x, y, z = map(int, input().split())
    if min(a) + min(b) <= z or max(a) + max(b) >= z:
        m.append([x, y, z])

m.sort(key = lambda x: x[2], reverse=True)


for j in m:
    if a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] >= 0 and a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] < min(a) + min(b):
        print(a[m[0][0]-1] + b[m[0][1]-1] - m[0][2])
        exit()

print(min(a) + min(b))

Submission status: Time Limit Exceeded

Solution code:
abm = input('')
a = int(abm.split(' ')[0])
b = int(abm.split(' ')[1])
m = int(abm.split(' ')[2])
wdic = {}
pdic = {}
clist = input('').split(' ')
dlist = input('').split(' ')

# 割引辞書の作成
max_w = 0
for i in range(0,m):
  win = input('').split(' ')
  wc = win[0]
  wd = win[1]
  wkey = wc + "-" + wd # 割引キー
  wp = int(win[2]) # 割引価格
  if wkey in wdic.keys():
    if wp > wdic[wkey]:
      wdic[wkey] = wp
  else:
    wdic[wkey] = wp

# 価格表組み合わせの作成
for i in range(0,a):
  for j in range(0,b):
    key = str(i+1) + "-" + str(j+1)
    val = int(clist[i]) + int(dlist[j])
    w = 0
    if key in wdic.keys():
	    w = wdic[key]
    pdic[key] = val - w

# 割引後価格でソート
pdic2 = sorted(pdic.items(), key=lambda x:x[1])

# ソート結果の1件目の値が最安値
print(pdic2[0][1])

Submission status: Time Limit Exceeded

Solution code:
A, B, M = map(int, input().split())
Ma = [int(_) for _ in input().split()]
Mb = [int(_) for _ in input().split()]
Mm = [[int(_) for _ in input().split()] for __ in range(M)]

r = 10 ** 5
for i in range(A):
  for j in range(B):
    c = 0
    for x, y, z in Mm:
      if i == (x - 1) and j == (y - 1):
        c = max(c, z)
    r = min(r, Ma[i] + Mb[j] - c)
print(r)

Submission status: Time Limit Exceeded

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

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

x = list()
y = list()
c = list()
for i in range(M):
    xi, yi, ci = map(int,input().split())
    x.append(xi)
    y.append(yi)
    c.append(ci)

p = list()

for i in range(A):
    for j in range(B):
        pij = a[i] + b[j]
        p.append(pij)
        
for i in range(M):
    pi = a[x[i]-1] + b[y[i]-1] - c[i]
    p.append(pi)

print(min(p))

Submission status: Time Limit Exceeded

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 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: Time Limit Exceeded

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

    grid = []
    for i in range(M):
        array = list(map(int, input().strip().split()))
        grid.append(array)

    totals = []
    res = 10000 + 10000
    for i in range(A):
        total = []
        for j in range(B):
            value = a[i]+b[j]
            total.append(value)
            if value < res:
                res = value
        
        totals.append(total)

    for m in grid:
        if m[0]-1 < A and m[1]-1 < B:
            tmp = totals[m[0]-1][m[1]-1] - m[2]
    
            if tmp < res:
                res = tmp

    print(res)
    
resolve()

Submission status: Time Limit Exceeded

Solution code:
A,B,M=map(int,input().split())
vecA=list(map(int,input().split()))
vecB=list(map(int,input().split()))
matX=[input().split() for k in range(M)]
price = 10**6
for i in range(0,M):
    pricecom = int(vecA[int(matX[i][0]) -1]) + int(vecB[int(matX[i][1]) -1]) - int(matX[i][2])
    if pricecom<= price:
        price=pricecom
    else:
        pass
for m in range(A):
    for n in range (B):
        price2=int(vecA[m]) + int(vecB[n])
        if price2<=price:
            price=price2
print(price)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
A,B,M = map(int,input().rstrip().split())
a = list(map(int,input().rstrip().split()))
b = list(map(int,input().rstrip().split()))

suml = [[0 for _ in range(len(b))] for _ in range(len(a))]

for i in range(len(a)):
  for j in range(len(b)):
    suml[i][j] = a[i]+b[j]



sumk = [[0 for _ in range(len(b))] for _ in range(len(a))]

for k in range(M):
  i,j,c = list(map(int,input().rstrip().split()))
  if sumk[i-1][j-1] != 0:
    sumk[i-1][j-1] = max(sumk[i-1][j-1],c)
  else:
    sumk[i-1][j-1] = c

x = np.array(suml)
y = np.array(sumk)

z = x - y

print(np.amin(z))

Submission status: Time Limit Exceeded

Solution code:
abm = input('')
a = int(abm.split(' ')[0])
b = int(abm.split(' ')[1])
m = int(abm.split(' ')[2])
wdic = {}
pdic = {}
clist = input('').split(' ')
dlist = input('').split(' ')

# 割引辞書の作成
max_w = 0
for i in range(0,m):
  win = input('').split(' ')
  wc = win[0]
  wd = win[1]
  wkey = wc + "-" + wd
  wp = int(win[2])
  if wkey in wdic.keys():
    if wp > wdic[wkey]:
      wdic[wkey] = wp
  else:
    wdic[wkey] = wp
  if wp > max_w:
    max_w = wp

# 価格表組み合わせの作成
min_price = -1
for i in range(0,a):
  for j in range(0,b):
    key = str(i+1) + "-" + str(j+1)
    val = int(clist[i]) + int(dlist[j])
    w = 0
    if key in wdic.keys():
	    w = wdic[key]
    pdic[key] = val - w

pdic2 = sorted(pdic.items(), key=lambda x:x[1])

print(pdic2[0][1])

Submission status: Time Limit Exceeded

Solution code:
A, B, M = map(int,input().split())
val_m = [[0] * 3 for i in range(M)]

val_a = list(map(int, input().split()))
val_b = list(map(int, input().split()))

for i in range(M):
  val_m[i][0], val_m[i][1], val_m[i][2] = map(int, input().split())

dis_val = []
for i in range(M):
  dis_val.append(val_a[val_m[i][0]-1] + val_b[val_m[i][1]-1] - val_m[i][2])


val_a = list(set(val_a))
val_b = list(set(val_b))
sum_val = []
for i in range(len(val_a)):
  for j in range(len(val_b)):
    sum_val.append(val_a[i] + val_b[j])


sum_val.extend(dis_val)
sum_val.sort()
print(sum_val[0])

Submission status: Time Limit Exceeded

Solution code:
p = []
list1 = input()
a = input()
b = input()
list_1 = list(map(int, list1.split(" ")))
alist = list(map(int, a.split(" ")))
blist = list(map(int, b.split(" ")))
A = list_1[0]
B = list_1[1]
M = list_1[2]

for i in range(M):
  l = input()
  xyc = list(map(int, l.split(" ")))
  x = xyc[0]-1
  y = xyc[1]-1
  c = xyc[2]
  for i in range(A):
    for j in range(B):
      if x == i and y == j:
        p.append(alist[i]+blist[j]-c)
for i in range(A):
  for j in range(B):
    p.append(alist[i]+blist[j])
print(min(p))

Submission status: Time Limit Exceeded

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
d = [0] * m
c = [[0 for i in range(len(b))] for j in range(len(a))]
# print(c)


for i in range(m):
  x[i], y[i], d[i] = map(int, input().split())
  if c[x[i] - 1][y[i] - 1] > 0:
    c[x[i] - 1][y[i] - 1] = max(c[x[i] - 1][y[i] - 1], d[i])
  else:
    c[x[i] - 1][y[i] - 1] = d[i]

ans = float('inf')

for i in range(len(a)):
  for j in range(len(b)):
    cost = a[i] + b[j] - c[i][j]
    if ans > cost:
      ans = cost

print(ans)

Submission status: Time Limit Exceeded

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 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: Time Limit Exceeded

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

m = []

for i in range(M):
    x, y, z = map(int, input().split())
    m.append([x, y, z])

m.sort(key = lambda x: x[2], reverse=True)

for j in m[:min(len(a),len(b))+1]:
    if a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] >= 0 and a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] < min(a) + min(b):
        print(a[m[0][0]-1] + b[m[0][1]-1] - m[0][2])
        exit()

print(min(a) + min(b))

Submission status: Time Limit Exceeded

Solution code:
a, b, m = map(int, input().split())
ax = list(map(int, input().split()))
bx = list(map(int, input().split()))
x = []
for i in range(m):
    _x = map(int, input().split())
    x.append(list(_x))

min_sum = 99999999
for i in range(a):
    for j in range(b):
        sum = ax[i] + bx[j]
        for k in x:
            sum2 = sum
            if k[0] == i+1 and k[1] == j+1:
                sum2 = sum2 - k[2]
            if sum2 < min_sum:
                min_sum = sum2

print("{}".format(min_sum))

Submission status: Time Limit Exceeded

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

disc = [list(map(int, input().split())) for i in range(m)]

ans = []
from collections import deque
ans = deque(ans)

ans1 = [x+y for x in a1 for y in b1]

for i in range(m):
    ans.append( a1[disc[i][0]-1] + b1[disc[i][1]-1] - disc[i][2] )

ans = list(ans)
ans = ans + ans1
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

input = sys.stdin.readline

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

microwave = list(map(int, input().split()))

refrigerator = list(map(int, input().split()))

dis_dict = {}
for ii in range(m):
    an, bn, mn = map(int, input().split())
    key = str(an).zfill(5) + str(bn).zfill(5)
    if key in dis_dict:
        if dis_dict[key] < mn:
            dis_dict[key] = mn
    else:
        dis_dict[key] = mn
money = 99999999999
for i, ai in enumerate(microwave, start=1):
    for j, bi in enumerate(refrigerator, start=1):
        mi = ai + bi
        key_ij = str(i).zfill(5) + str(j).zfill(5)
        dis_max = dis_dict.get(key_ij, 0)
#         for dis in Discount:
#             if (dis[0] == i+1) and (dis[1] == j+1):
#                 if dis[2] > dis_max:
#                     dis_max = dis[2]
        mi -= dis_max
        if money > mi:
            money = mi
print(money)

Submission status: Time Limit Exceeded

Solution code:
abm = input('')
a = int(abm.split(' ')[0])
b = int(abm.split(' ')[1])
m = int(abm.split(' ')[2])
wdic = {}
pdic = {}
clist = input('').split(' ')
dlist = input('').split(' ')

max_w = 0
for i in range(0,m):
  win = input('').split(' ')
  wc = win[0]
  wd = win[1]
  wkey = wc + "-" + wd
  wp = int(win[2])
  if wkey in wdic.keys():
    if wp > wdic[wkey]:
      wdic[wkey] = wp
  else:
    wdic[wkey] = wp
  if wp > max_w:
    max_w = wp

for i in range(0,a):
  for j in range(0,b):
    key = str(i+1) + "-" + str(j+1)
    val = int(clist[i]) + int(dlist[j])
    w = 0
    if key in wdic.keys():
	    w = wdic[key]
    pdic[key] = val - w
    
min_price = -1

pdic2 = sorted(pdic.items(), key=lambda x:x[1],reverse=True)

for kp in pdic2:
  key = kp[0]
  price = kp[1]
  if min_price == -1:
    min_price = price
  elif price < min_price:
    min_price = price     
  if price > (min_price + max_w):
    break

print(min_price)

Submission status: Time Limit Exceeded

Solution code:
A, B, M = map(int,input().split())
val_m = [[0] * 3 for i in range(M)]

val_a = list(map(int, input().split()))
val_b = list(map(int, input().split()))

for i in range(M):
  val_m[i][0], val_m[i][1], val_m[i][2] = map(int, input().split())

sum_val = []
for i in range(A):
  for j in range(B):
    sum_val.append(val_a[i] + val_b[j])
    
dis_val = []
for i in range(M):
  dis_val.append(val_a[val_m[i][0]-1] + val_b[val_m[i][1]-1] - val_m[i][2])

sum_val.extend(dis_val)
sum_val.sort()
print(sum_val[0])

Submission status: Time Limit Exceeded

Solution code:
#読み込み
list_num = input().split(" ") #A,B,M
list_ref = input().split(" ") #冷蔵庫の値段
list_ren = input().split(" ") #レンジの値段
list2 = [] #割引できるやつの組み
for i in range(int(list_num[2])):
    list_c = input().split(" ")
    list2.append(list_c)
A = int(list_num[0])
B = int(list_num[1])
M = int(list_num[2])

#割引なしで総当たり
list_price = []
for i in range(A):
    for j in range(B):
        list_price.append(int(list_ref[i]) + int(list_ren[j]))

#割引きを総当たり
for k in range(M):
    price = int(list_ref[int(list2[k][0])-1]) + int(list_ren[int(list2[k][1])-1]) - int(int(list2[k][2])-1) - 1
    list_price.append(price)


print(min(list_price))

Submission status: Time Limit Exceeded

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=int(a[0]+b[0])
for l in range(A):
    for m in range(B):
        x=int(a[l]+b[m])
        if comp2>x:
            comp2=x
                  
print(min(comp1,comp2))

Submission status: Time Limit Exceeded

Solution code:
num = list(map(int, input().split()))
A = num[0]
B = num[1]
M = num[2]
a = list(map(int, input().split()))
b = list(map(int, input().split()))

min = 1000000000

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

for i in range(M):
    c = list(map(int, input().split()))
    sum = a[c[0]-1] + b[c[1]-1] - c[2]
    if sum < min:
        min = sum

print(min)

Submission status: Time Limit Exceeded

Solution code:
import sys
 
lines = sys.stdin.readlines()
num_a ,num_b, num_m = [int(v) for v in lines[0].split()]
# ----
 
a_list = [int(v) for v in lines[1].split()[:num_a+1]]
b_list = [int(v) for v in lines[2].split()[:num_b+1]]
 
# print(a_list)
# print(b_list)
 
result = max(a_list) + max(b_list)
ignores = set()
for c in lines[3:num_m+3]:
    a_i, b_i, discount = map(int, c.split())
    try:
        ignores.add((int(a_i), int(b_i)))
        
        price = a_list[a_i-1] + b_list[b_i-1] - discount
        
        if price < result:
            result = price
    except IndexError:
        print('------')
        print(c)
        pass
 
# ---
# result = max(a_list) + max(b_list)
for i, a in enumerate(a_list):
    for j, b in enumerate(b_list):
        if (i+1, j+1) not in ignores:
            price = a + b
            result = price if price < result else result
            
print(result)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np

# input = sys.stdin.readline

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

microwave = list(map(int, input().split()))

refrigerator = list(map(int, input().split()))

dis_dict = {}
for ii in range(m):
    an, bn, mn = map(int, input().split())
    key = str(an).zfill(5) + str(bn).zfill(5)
    if key in dis_dict:
        if dis_dict[key] < mn:
            dis_dict[key] = mn
    else:
        dis_dict[key] = mn
money = 99999999999
for i, ai in enumerate(microwave, start=1):
    for j, bi in enumerate(refrigerator, start=1):
        mi = ai + bi
        key_ij = str(i).zfill(5) + str(j).zfill(5)
        dis_max = dis_dict.get(key_ij, 0)
#         for dis in Discount:
#             if (dis[0] == i+1) and (dis[1] == j+1):
#                 if dis[2] > dis_max:
#                     dis_max = dis[2]
        mi -= dis_max
        if money > mi:
            money = mi
print(money)

Submission status: Time Limit Exceeded

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

m = []

for i in range(M):
    x, y, z = map(int, input().split())
    m.append([x, y, z])

m.sort(key = lambda x: x[2], reverse=True)

for j in list(map(list, set(map(tuple, m)))):
    if a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] >= 0 and a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] < min(a) + min(b):
        print(a[m[0][0]-1] + b[m[0][1]-1] - m[0][2])
        exit()

print(min(a) + min(b))

Submission status: Time Limit Exceeded

Solution code:
a,b,m=map(int, input().split())
rei=list(map(int, input().split())) 
den=list(map(int, input().split())) 
ans = []
cou=[list(map(int, input().split())) for i in range(m)]
[ans.append(rei[cou[i][0]-1]+den[cou[i][1]-1]-cou[i][2]) for i in range(m)]
[[ans.append(rei[j]+den[i]) for i in range(b)] for j in range(a)]
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
abm = input('')
a = int(abm.split(' ')[0])
b = int(abm.split(' ')[1])
m = int(abm.split(' ')[2])
wdic = {}
pdic = {}
clist = input('').split(' ')
dlist = input('').split(' ')

for i in range(0,a):
  for j in range(0,b):
    key = str(i+1) + "-" + str(j+1)
    val = int(clist[i]) + int(dlist[j])
    pdic[key] = val

max_w = 0
for i in range(0,m):
  win = input('').split(' ')
  wc = win[0]
  wd = win[1]
  wkey = wc + "-" + wd
  wp = int(win[2])
  if wkey in wdic.keys():
    if wp > wdic[wkey]:
      wdic[wkey] = wp
  else:
    wdic[wkey] = wp
  if wp > max_w:
    max_w = wp

min_price = -1

pdic2 = sorted(pdic.items(), key=lambda x:x[1],reverse=True)

for kp in pdic2:
  key = kp[0]
  price = kp[1]
  w = 0
  if key in wdic.keys():
     w = wdic[wkey]
  total = price - w
  if min_price == -1:
    min_price = total
  elif total < min_price:
    min_price = total     
  if price > (min_price + max_w):
    break

print(min_price)

Submission status: Time Limit Exceeded

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



a=input().split()
b=input().split()
m=[]
for i in range(M):
    m.append(input().split())

counta=0
countb=0
nummin=int(a[0])+int(b[0])
for i in m:

    x=int(i[0])-1
    y=int(i[1])-1
    c=int(i[2])
    num=int(a[x])+int(b[y])-c

    if num<nummin:
        nummin=num

for i in a:
    i=int(i)
    for j in b:
        j=int(j)
        if (i+j)<nummin:
            nummin=i+j
print(nummin)

Submission status: Time Limit Exceeded

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 = []
    for i in range(M):
        x, y, c = map(int, input().split())
        xyc.append([x, y, c])

    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: Time Limit Exceeded

Solution code:
abm = input('')
a = int(abm.split(' ')[0])
b = int(abm.split(' ')[1])
m = int(abm.split(' ')[2])
wdic = {}
pdic = {}
clist = input('').split(' ')
dlist = input('').split(' ')

for i in range(0,a):
  for j in range(0,b):
    key = str(i+1) + "-" + str(j+1)
    val = int(clist[i]) + int(dlist[j])
    pdic[key] = val

max_w = 0
for i in range(0,m):
  win = input('').split(' ')
  wc = win[0]
  wd = win[1]
  wkey = wc + "-" + wd
  wp = int(win[2])
  if wkey in wdic.keys():
    if wp > wdic[wkey]:
      wdic[wkey] = wp
  else:
    wdic[wkey] = wp
  if wp > max_w:
    max_w = wp

min_price = -1

pdic2 = sorted(pdic.items(), key=lambda x:x[1])

for kp in pdic2:
  key = kp[0]
  price = kp[1]
  w = 0
  if key in wdic.keys():
     w = wdic[wkey]
  total = price - w
  if min_price == -1:
    min_price = total
  elif total < min_price:
    min_price = total     
  if price > (min_price + max_w):
    break

print(min_price)

Submission status: Time Limit Exceeded

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)]

list = []

for i in range(M):
  if min(a) + min(b) > a[d[i][0] - 1] + b[d[i][1] - 1] - d[i][2]:
    list.append(a[d[i][0] - 1] + b[d[i][1] - 1] - d[i][2])

if len(list) != 0:
  print(min(list))
else:
  print(min(a) + min(b))

Submission status: Time Limit Exceeded

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 = []
    for i in range(M):
        x, y, c = map(int, input().split())
        xyc.append([x, y, c])

    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)
    for j in xyc:
        p.append(A[j[0]-1] + B[j[1]-1] - j[2])
    ret = min(p)    
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
a,b,m=map(int, input().split())
rei=list(map(int, input().split())) 
den=list(map(int, input().split())) 
cou = [0] * m
ans = []
cou=[list(map(int, input().split())) for i in range(m)]
[ans.append(rei[cou[i][0]-1]+den[cou[i][1]-1]-cou[i][2]) for i in range(m)]
[[ans.append(rei[j]+den[i]) for i in range(b)] for j in range(a)]
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
import itertools


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
A_B = list(itertools.product(range(1, A + 1), range(1, B + 1)))
A_B_sum = list(map(lambda x: x[0] + x[1], (itertools.product(a, b))))
for i in range(len(A_B)):
    # print(str(A_B[i][0]) + "&" + str(A_B[i][1]))
    if str(A_B[i][0]) + "&" + str(A_B[i][1]) in m.keys():
        # print(str(A_B[i][0]) + "&" + str(A_B[i][1]))
        A_B_sum[i] -= m[str(A_B[i][0]) + "&" + str(A_B[i][1])]
print(min(A_B_sum))

Submission status: Time Limit Exceeded

Solution code:
a, b ,c = map(int,input().split(" "))
ap =[]
ap = list(map(int,input().split(" ")))
bp =[]
bp = list(map(int,input().split(" ")))
lowest = 10000000
for i in ap:
    for j in bp:
        lowest = min(lowest,i+j)
for _ in range(c):
    x, y ,z = map(int,input().split(" "))
    dis = ap[x-1] + bp[y-1] - z
    lowest = min(lowest,dis)
print(lowest)

Submission status: Time Limit Exceeded

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=[0]*M

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

comp2=int(a[0]+b[0])
for l in range(A):
    for m in range(B):
        x=int(a[l]+b[m])
        if comp2>=x:
            comp2=x
          
comp3=min(comp1)         
print(min(comp3,comp2))

Submission status: Time Limit Exceeded

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

m = []

for i in range(M):
    x, y, z = map(int, input().split())
    m.append([x, y, z])

m.sort(key = lambda x: x[2], reverse=True)

for j in m:
    if a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] >= 0 and a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] < min(a) + min(b):
        print(a[m[0][0]-1] + b[m[0][1]-1] - m[0][2])
        exit()

print(min(a) + min(b))

Submission status: Time Limit Exceeded

Solution code:
a,b,m=map(int, input().split())
rei=list(map(int, input().split())) 
den=list(map(int, input().split())) 
cou = [0] * m
ans = []
for i in range(m):
	cou[i]=list(map(int, input().split()))
	ans.append(rei[cou[i][0]-1]+den[cou[i][1]-1]-cou[i][2])
[[ans.append(rei[j]+den[i]) for i in range(b)] for j in range(a)]
print(min(ans))

Submission status: Time Limit Exceeded

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 = []
    for i in range(M):
        x, y, c = map(int, input().split())
        xyc.append([x, y, c])

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

Submission status: Time Limit Exceeded

Solution code:
import itertools

a,b,m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
P = [list(map(int,input().split())) for i in range(m)]
x = []
y = []
#普通に足算する
p = itertools.product(A, B)
for v in p:
    x.append(sum(v))
#min出す
xx = min(x)    
#割引券使う
for i in range(m):
  y.append(A[P[i][0]-1] + B[P[i][1]-1] - P[i][2])
#min出す
yy = min(y)
print(min(xx,yy))

Submission status: Time Limit Exceeded

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

min = 1000000000

for i in range(num[0]):
    for j in range(num[1]):
        sum = a[i] + b[j]
        if sum < min:
            min = sum

for i in range(num[2]):
    c = list(map(int, input().split()))
    sum = a[c[0]-1] + b[c[1]-1] - c[2]
    if sum < min:
        min = sum

print(min)

Submission status: Time Limit Exceeded

Solution code:
a,b,m=map(int, input().split())
rei=list(map(int, input().split())) 
den=list(map(int, input().split())) 
cou = [0] * m
ans = []
for i in range(m):
	cou[i]=list(map(int, input().split()))
	ans.append(rei[cou[i][0]-1]+den[cou[i][1]-1]-cou[i][2])
for i in range(b):
	for j in range(a):
		ans.append(rei[j]+den[i])
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
A , B , M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = [i+j for i in a for j in b]
for i in range(M):
    q = list(map(int, input().split()))
    x = a[q[0]-1] + b[q[1]-1] - q[2]
    ans.append(x)
print(min(ans))

Submission status: Time Limit Exceeded

Solution code:
abm = input('')
a = int(abm.split(' ')[0])
b = int(abm.split(' ')[1])
m = int(abm.split(' ')[2])
clist = []
dlist = []
wdic = {}
pdic = {}
clist = input('').split(' ')
dlist = input('').split(' ')

for i in range(0,a):
  for j in range(0,b):
    key = str(i+1) + "-" + str(j+1)
    val = int(clist[i]) + int(dlist[j])
    pdic[key] = val

for i in range(0,m):
  win = input('').split(' ')
  wc = win[0]
  wd = win[1]
  wkey = wc + "-" + wd
  wp = int(win[2])
  if wkey in wdic.keys():
    if wp > wdic[wkey]:
      wdic[wkey] = wp
  else:
    wdic[wkey] = wp

min_price = -1

for key,price in pdic.items():
  w = 0
  if key in wdic.keys():
     w = wdic[wkey]
  total = price - w
  if min_price == -1:
    min_price = total
  elif total < min_price:
    min_price = total      

print(min_price)

Submission status: Time Limit Exceeded

Solution code:
m=list(map(int, input().split()))
rei=list(map(int, input().split())) 
den=list(map(int, input().split())) 
cou = [0] * m[2]
ans = []
for i in range(m[2]):
	cou[i]=list(map(int, input().split()))
	ans.append(rei[cou[i][0]-1]+den[cou[i][1]-1]-cou[i][2])
for i in range(len(den)):
	for j in range(len(rei)):
		ans.append(rei[j]+den[i])
print(min(ans))