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: Accepted

Solution code:
#!/usr/bin/env python

def main():
    a, b, m = map(int, input().split())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    c_list = [list(map(int, input().split())) for _ in range(m)]

    cost = min(a_list) + min(b_list)

    for i in range(m):
        discount = a_list[c_list[i][0] - 1] + b_list[c_list[i][1] - 1] - c_list[i][2]
        if cost > discount:
            cost = discount

    print(cost)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b,m=map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
buf=min(A)+min(B)
for i in range(m):
    x,y,c=map(int, input().split())
    cp=A[x-1]+B[y-1]-c
    if buf>=cp:
        buf=cp
print(buf)

Submission status: Accepted

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()]
x = []
y = []
c = []
for i in range(0, M):
  xi, yi, ci = map(int, input().split())
  x.append(xi)
  y.append(yi)
  c.append(ci)
#print (a)

min_price_wo_waribiki = min(a) + min(b)
#print(min_price_wo_waribiki)

min_price_w_waribiki = min([a[x[i]-1] + b[y[i]-1] - c[i] for i in range(0, M)])
#print(min_price_w_waribiki)

min_price = min(min_price_wo_waribiki, min_price_w_waribiki)
print(min_price)

Submission status: Accepted

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

for i in range(M):
  c1, c2, c3 = [int(j) for j in input().split()]
  m =  a[c1-1] + b[c2-1] - c3
  if mi > m:
    mi = m

print(mi)

Submission status: Accepted

Solution code:
aa, bb, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = []
y = []
c = []
for i in range(m):
  x1, y1, c1 = list(map(int,input().split()))
  x.append(x1)
  y.append(y1)
  c.append(c1)

price = []
  
for i in range(m):
  price.append(a[x[i] - 1] + b[y[i] - 1] - c[i])
  
  
  
print(min(min(price), min(a) + min(b)))

Submission status: Accepted

Solution code:
a,b,m=map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
tickets=list()
for i in range(m):
    tickets.append(list(map(int,input().split())))

price_min=float("INF")
for ticket in tickets:
    price = A[ticket[0]-1] + B[ticket[1]-1] - ticket[2]
    price_min = price if price_min>price else price_min

# 割引券を使わない場合
no_ticket=min(A)+min(B)
if price_min > no_ticket: price_min=no_ticket
print(price_min)

Submission status: Accepted

Solution code:
n_a, n_b, n_m=map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
total = min(a) + min(b)
for i in range(n_m):
    x, y, c = list(map(int,input().split()))
    _total = a[x - 1] + b[y - 1] - c
    if _total < total:
        total = _total
print(total)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
min = sorted(a)[0]+sorted(b)[0]
for i in range(M):
    x, y, c = map(int, input().split())
    d = a[x-1]+b[y-1]-c
    if min > d:
        min = d
print(min)

Submission status: Accepted

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

ans = min(l_a) + min(l_b)
for i in range(m):
    x, y, c = map(int, input().split())
    o_sum = l_a[x-1] + l_b[y-1] - c
    ans = min(o_sum, ans)

print(ans)

Submission status: Accepted

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

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

w = {}
m = min(a) + min(b)

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

print(m)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
A, B, M = map(int, input().split())
a_costs = list(map(int, input().split()))
b_costs = list(map(int, input().split()))
m_discounts = []
for m in range(M):
    tmp = list(map(int, input().split()))
    # m_discounts[[tmp[0], tmp[1]]] = tmp[2]
    m_discounts.append(tmp)
ans = min(a_costs) + min(b_costs)
# no_doscount_min = min(a_costs) + min(b_costs) - discount

for m in m_discounts:
    tmp_ans = a_costs[m[0]-1] + b_costs[m[1]-1] - m[2]
    if (ans > tmp_ans):
        ans = tmp_ans
print(ans)

Submission status: Accepted

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

ans = min(a)+min(b)

for i in range(m):
    ans = min(ans,(a[x[i]-1] + b[y[i]-1]) - c[i])
print(ans)

Submission status: Accepted

Solution code:
A, B, M = map(int,input().split())
aArray = list(map(int,input().split()))
bArray = list(map(int,input().split()))
cArray = [[]] * M
for i in range(M):
    cArray[i] = (list(map(int,input().split())))
minV = min(aArray) + min(bArray)
for c in cArray:
    minV = min([minV, (aArray[c[0] - 1] + bArray[c[1] - 1] - c[2])])
print(minV)

Submission status: Accepted

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

a = [int(i) for i in input().split(" ")]
b = [int(i) for i in input().split(" ")]
C = []

for i in range(M):
  c = [int(i) for i in input().split(" ")]
  C.append(c)

m = min(a) + min(b)

for c in C:
  cost = a[c[0] - 1] + b[c[1] - 1] - c[2]
  if m >= cost:
    m = cost

print(m)

Submission status: Accepted

Solution code:
na,nb,M=map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [[0 for i in range(3)] for j in range(M)]
#C = [[0]*3]*M
s=min(A)+min(B)
t=0

#print(A)
#print(B)

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

#print(C)

for j in range(M):
	#print(j)
	t=A[C[j][0]-1]+B[C[j][1]-1]-C[j][2]
	if t<s:
		s=t

print(s)

Submission status: Accepted

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

min_ab = min(a_list) + min(b_list)
for i in range(m):
  x, y, c = map(int, input().split())
  if a < x or b < y:
    continue
  min_ab = min(a_list[x-1] + b_list[y-1] - c, min_ab)

print(min_ab)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
XYC = [input().split() for _ in range(M)]
ans = min(a) + min(b)
for x, y, c in XYC:
  discount_price = a[int(x) - 1] + b[int(y) - 1] - int(c)
  ans = min(ans, discount_price)
print(ans)

Submission status: Accepted

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

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

ans = min(a) + min(b)

for i in range(M):
  x, y, c = map(int, input().split())
  exp = a[x-1] + b[y-1] - c
  if ans > exp:
    ans = exp
    
print(ans)

Submission status: Accepted

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

print(value)

Submission status: Accepted

Solution code:
ABM = input().split()
A = int(ABM[0])
B = int(ABM[1])
M = int(ABM[2])

A_list = []
tmp = input().split()
for i in range(A):
  A_list.append(int(tmp[i]))
  
B_list = []
tmp = input().split()
for i in range(B):
  B_list.append(int(tmp[i]))
  
  
min_ = min(A_list)+min(B_list)

  
for i in range(M):
  tmp = input().split()
  tmp_goukei = A_list[int(tmp[0])-1]+B_list[int(tmp[1])-1]-int(tmp[2])
  if tmp_goukei < min_:
    min_ = tmp_goukei

print(min_)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
ticket = []
for i in range(M):
    ticket.append(list(map(int, input().split())))
result = min(arr1) + min(arr2)
for i in ticket:
    x1 = i[0] - 1
    x2 = i[1] - 1
    temp = arr1[x1] + arr2[x2] - i[2]
    result = min(result, temp)
print(result)

Submission status: Accepted

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

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

tickets = []
for _ in range(m):
  tickets.append(map(int, input().split()))
  
m = min(A) + min(B)

for x, y, c in tickets:
  tmp_m = A[x-1] + B[y-1] - c
  if tmp_m < m:
    m = tmp_m
    
print(m)

Submission status: Accepted

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

a = input()
a = a.split(' ')
b = input()
b = b.split(' ')

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

for i in list(range(int(M))):
    tmp = input()
    tmp = tmp.split(' ')

    x.append(int(tmp[0]))
    y.append(int(tmp[1]))
    c.append(int(tmp[2]))

ans = []
tmp = int(min(a)) + int(min(b))
ans.append(tmp)

for i in list(range(int(M))):
    tmp = int(a[x[i]-1]) + int(b[y[i]-1]) - c[i]
    ans.append(tmp)

ans.sort()
print(ans[0])

Submission status: Accepted

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

minPrice = min(a) + min(b) #割引額を考慮しない最小金額
price = 0

for x in range(len(c)):
    price = a[c[x][0]-1] + b[c[x][1]-1] - c[x][2]
    if minPrice > price:
        minPrice = price
    price = 0

print(minPrice)

Submission status: Accepted

Solution code:
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
L=[list(map(int,input().split())) for i in range(M)]
MA=min(a)
MB=min(b)
MC=MA+MB
for i in range(M):
  if a[L[i][0]-1]+b[L[i][1]-1]-L[i][2]<MC:
    MC=a[L[i][0]-1]+b[L[i][1]-1]-L[i][2]
print(MC)

Submission status: Accepted

Solution code:
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
dis=[]
for i in range(M):
    d=list(map(int,input().split()))
    dis.append(d)
mn=min(a)+min(b)
for x,y,c in dis:
    r1=a[x-1]+b[y-1]-c
    if(r1<mn):
        mn=r1
print(mn)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
LA = [int(a) for a in input().split()]
LB = [int(b) for b in input().split()]
T = [[int(t) for t in input().split()] for _  in range(M)]
C = 10 ** 9
for i in range(M):
    C = min(LA[T[i][0]-1]+LB[T[i][1]-1]-T[i][2], C)
print(min(C, min(LA)+min(LB)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(A: int, B: int, M: int, a: "List[int]", b: "List[int]", x: "List[int]", y: "List[int]", c: "List[int]"):
    result = min(a)+min(b)
    for i in range(M):
        result = min(result,a[x[i]-1]+b[y[i]-1]-c[i])
    print(max(0,result))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(A)]  # type: "List[int]"
    b = [int(next(tokens)) for _ in range(B)]  # type: "List[int]"
    x = [int()] * (M)  # type: "List[int]"
    y = [int()] * (M)  # type: "List[int]"
    c = [int()] * (M)  # type: "List[int]"
    for i in range(M):
        x[i] = int(next(tokens))
        y[i] = int(next(tokens))
        c[i] = int(next(tokens))
    solve(A, B, M, a, b, x, y, c)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a, b, m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = sorted(A)
D = sorted(B)
l = C[0] + D[0]
for i in range(m):
  x, y, c = map(int, input().split())
  k = A[x-1] + B[y-1] -c
  if l > k:
    l = k
print(l)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

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

xn = []
yn = []
cn = []
for i in range(m):
    x, y, c = map(int, input().split())
    xn.append(x)
    yn.append(y)
    cn.append(c)

# print()
# print(a, b, m)
# print(price_a)
# print(price_b)
# print(xn, yn, cn)

# find min with no discount
min_a = min(price_a)
min_b = min(price_b)
min_s = min_a + min_b

# find smaller min of discounts than no discount
min_d_s = min_s
for i in range(m):
    # print(i, xn[i], yn[i], cn[i])
    s = price_a[xn[i] - 1] + price_b[yn[i] - 1] - cn[i]
    min_d_s = min(min_d_s, s)

print(min_d_s)

Submission status: Accepted

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

    a = [0] * A
    b = [0] * B

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

    min_price = min(a) + min(b)

    for _ in range(M):
        ticket = list(map(int,input().split()))
        price = a[ticket[0]-1] + b[ticket[1]-1] - ticket[2]

        min_price = min(min_price,price)

    print(min_price)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

d=[0]*M
for i in range(0,M):
    d[i] = a[x[i]-1] + b[y[i]-1] - c[i]
a.sort()
b.sort()
d.sort()
print(min(a[0]+b[0],d[0]))

Submission status: Accepted

Solution code:
s = input().split()
a, b, m = int(s[0]), int(s[1]), int(s[2])

a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]

ans = min(a_list) + min(b_list)

for i in range(m):
    ss = input().split()
    temp = a_list[int(ss[0])-1] + b_list[int(ss[1])-1] - int(ss[2])
    if ans > temp:
        ans = temp

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
m = [list(map(int, input().split())) for _ in range(M)]

ans = min(a) + min(b)
for x in m:
    ans = min(ans, a[x[0] - 1] + b[x[1] - 1] - x[2])

print(ans)

Submission status: Accepted

Solution code:
ABM = list(map(int, input().split()))
refrigerator = list(map(int, input().split()))
microwave = list(map(int, input().split()))

commonMin = min(refrigerator) + min(microwave)

discount = []

for i in range(ABM[2]):
    discount.append(list(map(int, input().split())))

min = 200000
tmp = 0
for i in range(len(discount)):
    dis = discount[i]
    tmp = refrigerator[dis[0] - 1] + microwave[dis[1] - 1] - dis[2]
    if (tmp < min):
        min = tmp

if (commonMin < min):
    print(commonMin)
else:
    print(min)

Submission status: Accepted

Solution code:
inputs = input().split()
A = int(inputs[0])
B = int(inputs[1])
M = int(inputs[2])
a = []
b = []
x = []
y = []
c = []
inputs = input().split()
for i in range(A):
    a.append(int(inputs[i]))
inputs = input().split()
for j in range(B):
    b.append(int(inputs[j]))

for i in range(M):
    inputs = input().split()
    x.append(int(inputs[0]))
    y.append(int(inputs[1]))
    c.append(int(inputs[2]))
price = min(a) + min(b)
for i in range(M):
    if(a[x[i]-1] + b[y[i]-1] - c[i] < price):
        price = a[x[i]-1] + b[y[i]-1] -  c[i]
print(price)

Submission status: Accepted

Solution code:
n = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cupon = [list(map(int,input().split())) for i in range(n[2])]
#クーポンなし
nasi = min(a) + min(b)
mai = [a[i[0] -1] + b[i[1] -1] - i[2] for i in cupon]
m = min(mai)
if nasi < m:
  print(nasi)
else:
  print(m)

Submission status: Accepted

Solution code:
from collections import deque

(A, B, M) = list(map(int, input().split()))
A_Price = list(map(int, input().split()))
B_Price = list(map(int, input().split()))
Coupon = [list(map(int, input().split())) for _ in range(M)]

tmp_min_price = min(A_Price) + min(B_Price)

discounted_Prices = deque()
for i in range(M):
    discounted_Prices.append(A_Price[Coupon[i][0] - 1] \
                             + B_Price[Coupon[i][1] - 1] \
                             - Coupon[i][2])

min_discounted_price = min(discounted_Prices)
print(min(min_discounted_price, tmp_min_price))

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    a, b, m = map(int, input().split())
    refrigerators = list(map(int, input().split()))
    microwaves = list(map(int, input().split()))
    tickets = [list(map(int, input().split())) for _ in range(m)]

    x_ret = min(refrigerators)
    y_ret = min(microwaves)
    ret = x_ret + y_ret
    for x, y, c in tickets:
        p = refrigerators[x - 1] + microwaves[y - 1] - c
        ret = min(ret, p)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(M):
    q=input().split()
    q=[int(p)for p in q]
    minv=min(minv,a[q[0]-1]+b[q[1]-1]-q[2])
    
print(minv)

Submission status: Accepted

Solution code:
a, b, m = map(int,input().split())
a_l = list(map(int,input().split()))
b_l = list(map(int,input().split()))
m_l = []
for i in range(m):
    m_l.append(list((map(int,input().split()))))

m_l.sort(key=lambda x: x[2])
a_l_s = sorted(a_l)
b_l_s = sorted(b_l)
ans = a_l_s[0] + b_l_s[0]

for j in range(m):
    price = a_l[m_l[j][0]-1] + b_l[m_l[j][1]-1] - m_l[j][2]
    if price < ans:
        ans = price

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

A,B,M = inpl()
aa = inpl()
bb = inpl()

ans = min(aa) + min(bb)

for _ in range(M):
    x,y,c = inpl()
    tmp = aa[x-1] + bb[y-1] - c
    ans = min(ans,tmp)


print(ans)

Submission status: Accepted

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

tmp=99999999

for i in range(m):
    x,y,c=[int(i) for i in input().split()]
    cost = al[x-1] + bl[y-1] - c
    #各割引券を使った時の金額
    tmp=min(tmp,cost)

k=(min(al)+min(bl))
#割引券を使わなかった時の最小値
print(min(k,tmp))

Submission status: Accepted

Solution code:
a,b,m=map(int,input().split())
*a,=map(int,input().split())
*b,=map(int,input().split())
ans=10**9
for i in range(m):
    x,y,c=map(int,input().split())
    if len(a)>=x and len(b)>=y:
        res=a[x-1]+b[y-1]-c
        ans=min(ans,res)
l=min(a)+min(b)
print(min(ans,l))

Submission status: Accepted

Solution code:
import sys; sys.setrecursionlimit(2147483647); input = sys.stdin.readline
from math import floor, ceil, sqrt, factorial, log
from collections import Counter, defaultdict, deque
from operator import itemgetter
INF = float('inf'); MOD = 10**9+7
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(MI())
def LIR(n): return [LI() for i in range(n)]
def IS(): return input().rstrip()

def main():
    A, B, M = MI()
    a = LI()
    b = LI()
    xyc = LIR(M)
    
    m = min([a[i[0]-1] + b[i[1]-1] - i[2] for i in xyc])
    print(min([m, min(a)+min(b)]))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(M):
    x, y, c = input().split(" ")
    x, y, c = int(x), int(y), int(c)
    if (min_price > a[x - 1] + b[y - 1] - c):
        min_price = a[x - 1] + b[y - 1] - c

print(min_price)

Submission status: Accepted

Solution code:
A, B, M = [int(_) for _ in input().split()]
a = [int(__) for __ in input().split()]
b = [int(__) for __ in input().split()]
# 一番安い金額を足しておく。
MIN = min(a) + min(b)
sum = 0
for _ in range(M):
    x, y ,c = [int(__) for __ in input().split()]
    tmp = a[x-1] + b[y-1] - c
    if tmp < MIN:
        MIN = tmp
print(MIN)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
a_price = list(map(int, input().split()))
b_price = list(map(int, input().split()))
coupon = [list(map(int, input().split())) for i in range(M)]



min = min(a_price) + min(b_price)


for cpn in coupon:
    idx_a = cpn[0]-1
    idx_b = cpn[1]-1
    total = a_price[idx_a] + b_price[idx_b] - cpn[2]
    if min > total:
        min = total

print(min)

Submission status: Accepted

Solution code:
a,b,m=map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
t=[[0,0,0] for i in range(m)]
for i in range(m):
    a, b,c = map(int, input().split())
    t[i][0]=a
    t[i][1]=b
    t[i][2]=c
k=min(A)+min(B)
for i in range(m):
    if A[t[i][0]-1]+B[t[i][1]-1]-t[i][2]<k:
        k=A[t[i][0]-1]+B[t[i][1]-1]-t[i][2]
print(k)

Submission status: Accepted

Solution code:
def solve():
    A, B, M = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    X = []
    Y = []
    for _ in range(M):
        x, y, c = list(map(int, input().split()))
        X.append([x-1, y-1, c])
        Y.append(a[x-1] + b[y-1] - c)

    normal = min(a) + min(b)
    return min(normal, min(Y))

print(solve())

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xyc = [list(map(int, input().split())) for i in range(M)]
sum = min(a) + min(b)
for i in range(M):
    calc = a[xyc[i][0] - 1] + b[xyc[i][1] - 1] - xyc[i][2]
    sum = min(sum, calc)
print(sum)

Submission status: Accepted

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

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

ticket = []

while M>0:
    ticket.append(tuple(map(int, input().split())))
    M -= 1

lowest = min(a)+min(b)

for ele in ticket:
    t = a[ele[0]-1] + b[ele[1]-1] - ele[2]
    if t < lowest:
        lowest = t
print(lowest)

Submission status: Accepted

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

A, B, M = MAP()
A_L = LIST()
B_L = LIST()

M_L = []
for i in range(M):
    tmp = LIST()
    M_L.append(tmp)

A_L_min = min(A_L)
B_L_min = min(B_L)
A_B_min_sum = A_L_min + B_L_min

ans = A_B_min_sum
for m in M_L:
    tmp = A_L[m[0]-1] + B_L[m[1]-1] - m[2]
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
abm=list(map(int, input().split())) 
a=list(map(int, input().split())) 
b=list(map(int, input().split())) 
answer=min(a)+min(b)
for i in range(abm[2]):
    xyc=list(map(int, input().split())) 
    answer00=a[xyc[0]-1]+b[xyc[1]-1]-xyc[2]
    if answer00<answer:
        answer=answer00
print(answer)

Submission status: Accepted

Solution code:
a,b,m = map(int, input().split())
lst_A = list(map(int, input().split()))
lst_B = list(map(int, input().split()))
lst_x = [list(map(int, input().split())) for _ in range(m)]

cheap_price = float("inf")
for i in lst_x:
    price = lst_A[i[0]-1] + lst_B[i[1]-1] - i[2]
    if price < cheap_price:
        cheap_price = price

price_wa = min(lst_A) + min(lst_B)

if cheap_price <= price_wa:
    print(cheap_price)
elif cheap_price > price_wa:
    print(price_wa)

Submission status: Accepted

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

# cheapest
cheap = a_s[0] + b_s[0]

for i in range(M):

    # discount
    x, y, c = map(int, input().split())
    dis_cheap = a[x - 1] + b[y - 1] - c

    if cheap > dis_cheap:
        cheap = dis_cheap

print(cheap)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.buffer.readline

def inn(func=int):
    return list(map(func,input().split()))

def ins():
    return input().split()

an,bn,m = inn()
a = inn()
b = inn()

m_a = min(a)
m_b = min(b)
min_n = min(a)+min(b)

coupon = {}
for i in range(m):
    xi,yi,ci = inn()
    xi = xi-1
    yi = yi-1
    sum_vou = a[xi]+b[yi]-ci
    if sum_vou < min_n:
        min_n = sum_vou

print(min_n)


'''
for i in range(m):
    xi,yi,ci = inn()
    xi = xi-1
    yi = yi-1
    if not (xi,yi) in coupon.keys():
        coupon[(xi,yi)] = ci
    else:
        if coupon[(xi,yi)] < ci:
            coupon[(xi,yi)] = ci
'''

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
ABM= list(map(int,input().split()))
a =list(map(int,input().split()))
b =list(map(int,input().split()))

list1 =[]
for n in range(ABM[2]):
    list1.append(list(map(int,input().split())))

amin = min(a)
bmin = min(b)

min1 = amin + bmin

minlist = []

for i in range(ABM[2]):
    minlist.append(a[list1[i][0]-1] + b[list1[i][1]-1] - list1[i][2])

# print(minlist)
min2 = min(minlist)
# print(min2)

if(min1 > min2):
    min1 = min2

print(min1)

Submission status: Accepted

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):
	_x, _y, _c = map(int, input().split())
	x.append(_x-1)
	y.append(_y-1)
	c.append(_c)


# no discount
price_no_discount = min(a) + min(b)

# with discount
price_with_discount = list()
for i in range(M):
	price_with_discount.append(a[x[i]] + b[y[i]] - c[i])

price_with_discount = min(price_with_discount)

if price_no_discount < price_with_discount:
	print(price_no_discount)
else:
	print(price_with_discount)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_min = min(a)
b_min = min(b)
total_min = a_min + b_min 
for i in range(M):
    x, y, c = map(int, input().split())
    total = a[x-1] + b[y-1] - c
    if total < total_min:
        total_min = total
print(total_min)

Submission status: Accepted

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 = min(a)+min(b)
for i in range(M):
    ans=min(a[x[i]-1]+b[y[i]-1]-c[i],ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b,m=map(int,input().split())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))
x=[]
y=[]
c=[]
for _ in range(m):
    x1,y1,c1=map(int,input().split())
    x.append(x1)
    y.append(y1)
    c.append(c1)
#print(alist,blist,x,y,c)
ans=300000
amin=min(alist)
bmin=min(blist)
if(ans>(amin+bmin)):
    ans=amin+bmin
else:
    pass

for i in range(m):
    cost=alist[x[i]-1]+blist[y[i]-1]-c[i]
    if(ans>cost):
        ans=cost
    else:
        continue
print(ans)

Submission status: Accepted

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

cost = []
for i in range(m):
  x,y,c = map(int,input().split())
  cost.append(a[x-1]+b[y-1]-c)
a.sort()
b.sort()
res = a[0] + b[0]
for i in cost:
  res = min(i,res)
print(res)

Submission status: Accepted

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

    aa = sorted(a)
    bb = sorted(b)
    ans = aa[0] + bb[0]

    for i in range(M):
        xx, yy, cc = map(int, input().split())
        tmp = a[xx - 1] + b[yy - 1] - cc
        ans = min(ans, tmp)

    print(ans)


resolve()

Submission status: Accepted

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

minv = (10**5)*2
for _ in range(M):
    x, y, c = map(int, input().split())
    x = x - 1
    y = y - 1
    v = (a[x] + b[y]) - c
    if v < minv:
        minv = v

if min(a)+min(b) < minv:
    minv = min(a)+min(b)

print(minv)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = min(a) + min(b)
count = []
for m in range(M):
    x, y, c = map(int, input().split())
    price = a[x - 1] + b[y - 1] - c
    count.append(price)

if min(count) <= ans:
    print(min(count))
else:
    print(ans)