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.

Solution (Python):
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 _ in range(m)]

cnt=10**7
for x,y,c in XYC:
    cnt=min(cnt,A[x-1]+B[y-1]-c)
print(min(cnt,min(A)+min(B)))

Solution (Python):
import sys

import numpy as np

readline = sys.stdin.buffer.readline

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

a = np.array(list(map(int, readline().split())))
b = np.array(list(map(int, readline().split())))

XYC = [list(map(int, readline().split())) for _ in range(M)]

m = np.min(a) + np.min(b)

for x, y, c in XYC:
    x -= 1
    y -= 1
    s_c = a[x] + b[y] - c
    if s_c < m:
        m = s_c

print(m)

Solution (Python):
[A,B,M] = list(map(int,input().split()))
AL = list(map(int,input().split()))
BL = list(map(int,input().split()))
XYC = []
for i in range(M):
    XYC.append(list(map(int,input().split())))

out = []
for i in range(0,M):
    X = XYC[i][0]
    Y = XYC[i][1]
    C = XYC[i][2]
    out.append(AL[X-1]+BL[Y-1]-C)

print(min(min(out),min(AL)+min(BL)))

Solution (Python):
a, b, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = float("inf")
for _ in range(m):
    x, y, c = map(int, input().split())
    x -= 1; y -= 1
    ans = min(ans, a[x] + b[y] - c)
a.sort()
b.sort()

print(min(ans, a[0] + b[0]))

Solution (Python):
a, b, m = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
ansl = []
for i in range(m):
    x, y, c = map(int, input().split())
    ansl.append(al[x-1] + bl[y-1] - c)
ansl.append(min(al) + min(al))
print(min(ansl))

Solution (Python):

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

m_list = []

for i in range(m):
    m_list.append(list(map(int, input().split())))


a_min = 1000000000
for item in a_list:
    if item < a_min:
        a_min = item

b_min = 1000000000
for item in b_list:
    if item < b_min:
        b_min = item

val_min = a_min + b_min

for item in m_list:
    tmp = a_list[item[0]-1] + b_list[item[1]-1] - item[2]
    if tmp < val_min:
        val_min = tmp

print(val_min)

Solution (Python):
a,b,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
x = []
y = []
c = []
for i in range(m):
    val1,val2,val3 = map(int,input().split())
    x.append(val1)
    y.append(val2)
    c.append(val3)

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

for i in range(m):
    ans.append(a[x[i]-1] + b[y[i]-1] - c[i])

print(min(ans))

Solution (Python):
a,b,m=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
w=10**7
for i in range(m):
    x,y,c=map(int,input().split())
    w=min(A[x-1]+B[y-1]-c,w)
A=sorted(A)
B=sorted(B)
for i in range(min(a,b)):
    w=min(w,A[i]+B[i])
print(w)

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

ans_1 = min(a) + min(b)
ans_2 = 10 ** 9
for i in range(m):
    x, y, c = map(int, input().split())
    res = a[x - 1] + b[y - 1] - c
    ans_2 = min(ans_2, res)

print(min(ans_1, ans_2))

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

na,nb,m = inm()
a = inl()
b = inl()
mn = min(a)+min(b)
for i in range(m):
    x,y,c = inm()
    v = a[x-1]+b[y-1]-c
    if v<mn:
        mn = v
print(mn)

Solution (Python):
A, B, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
X = [list(map(int, input().split())) for i in range(M)]
m = min(A) + min(B)
for i in X:
  x, y, c = i[0] - 1, i[1] - 1, i[2]
  if A[x] + B[y] - c < m:
    m = A[x] + B[y] - c
print(m)

Solution (Python):
import sys
a, b, m = [int(x) for x in input().split()]
a_s = [int(x) for x in input().split()]
b_s = [int(x) for x in input().split()]
discounts = [[int(x) for x in input().split()] for i in range(m)]
m = min(a_s) + min(b_s)
for (x, y, c) in discounts:
    v = a_s[x - 1] + b_s[y - 1] - c
    m = min(m, v)
print(m)

Solution (Python):
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 _ in range(m)]

ans = min(a) + min(b)
for x, y, c in xyc:
    ans = min(ans, a[x-1] + b[y-1] - c)

print(ans)

Solution (Python):
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 _ in range(m)]

mn = min(A) + min(B)

for x, y, c in XYC:
    mn = min(mn, A[x - 1] + B[y - 1] - c)
print(mn)

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

Solution (Python):
A, B, M = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
m = [[int(x) for x in input().split()] for _ in range(M)]
cands = [min(a) + min(b)]
for x, y, c in m:
    cands.append(a[x - 1] + b[y - 1] - c)
print(min(cands))

Solution (Python):
a, b, m = map(int, input().split())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))
minimum = min(aList) + min(bList)

for i in range(m):
  x, y, c = map(int, input().split())
  if minimum > aList[x-1] + bList[y-1] - c:
    minimum = aList[x-1] + bList[y-1] - c
print(minimum)

Solution (Python):
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = []
y = []
c = []
for i in range(M):
    p,q,r = map(int, input().split())
    x.append(p)
    y.append(q)
    c.append(r)

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)

Solution (Python):
A,B,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
min_price = min(a)+min(b)
for _ in range(m):
  x,y,c=map(int,input().split())
  price = a[x-1]+b[y-1]-c
  if price < min_price:
    min_price = price
print(min_price)

Solution (Python):




def solve(inp):
    (A, B, M) = map(int, inp.readline().strip().split(' '))
    A = list(map(int, inp.readline().strip().split(' ')))
    B = list(map(int, inp.readline().strip().split(' ')))
    XYC = [map(int, inp.readline().strip().split(' ')) for i in range(M)]

    r = min(A) + min(B)
    for x, y, c in XYC:
        r = min(r, A[x - 1] + B[y - 1] - c)

    return str(r)


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    a_n, b_n, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 10**8
    for _ in range(m):
        x, y, c = map(int, input().split())
        x -= 1
        y -= 1
        buf = a[x] + b[y] - c
        if buf < ans:
            ans = buf
    a.sort()
    b.sort()
    if a[0] + b[0] < ans:
        ans = a[0] + b[0]
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
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 = list(map(int, input().split()))
    if a[x-1]+b[y-1]-c<ans: ans = a[x-1]+b[y-1]-c
print(ans)

Solution (Python):
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())		
	ans=min(ans,A[x-1]+B[y-1]-c)


print(ans)

Solution (Python):
a,b,m = map(int,input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
ans = min(a)+min(b)
for _ in range(m):
    x,y,c = map(int,input().split())
    x -= 1
    y -= 1
    ans = min(a[x]+b[y]-c,ans)
print(ans)

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

a_min = min(a)
b_min = min(b)

ans = a_min+b_min

for i in range(M):
    ans = min(ans,a[xyc[i][0]-1]+b[xyc[i][1]-1]-xyc[i][2])
    
print(ans)

Solution (Python):
a,b,m=map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
l=[]

for i in range(m):
  q=list(map(int,input().split()))
  l.append(al[q[0]-1]+bl[q[1]-1]-q[2])
l.append(min(al)+min(bl))
print(min(l))

Solution (Python):




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

ans = min(a_L) + min(b_L)

for _ in range(m):
    x,y,c = map(int,input().split())
    price = a_L[x-1] + b_L[y-1] - c
    ans = min(ans,price)

print(ans)

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

Solution (Python):









import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    a, b, m = list(map(int,ipti().split()))
    a_list = list(map(int,ipti().split()))
    b_list = list(map(int,ipti().split()))

    xyc = [0]* m

    for i in range(m):
        xyc[i] = list(map(int,ipti().split()))

    ans = min(a_list) + min(b_list)

    for i in range(m):
        x, y, c = xyc[i][0], xyc[i][1], xyc[i][2]
        temp = a_list[x-1] + b_list[y-1] - c
        ans = min(ans, temp)

    print(ans)



if __name__ == '__main__':
    main()

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

MIN=min(a)+min(b)

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

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

al = list(map(int,input().split()))
bl = list(map(int,input().split()))
minbl = min(bl)
min_bl = [minbl for i in range(a)]
for _ in range(m):
    x,y,c = map(int,input().split())
    min_bl[x-1] = min(min_bl[x-1], bl[y-1] - c)

ans = float("inf")
for a,b in zip(al, min_bl):
    ans = min(a+b, ans)
print(ans)

Solution (Python):
A, B, M = [int(s) for s in input().split()]
As = [int(s) for s in input().split()] + [10000000000]
Bs = [int(s) for s in input().split()] + [10000000000]

Ms = []
for _ in range(M):
    tMs = [int(s) for s in input().split()]
    Ms.append(tMs)


C = min(*As) + min(*Bs)

for x, y, c in Ms:
    C = min(As[x-1] + Bs[y-1] - c, C)

print(C)

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

























def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007




def main():
	A,B,M = inputMap()
	a = inputList()
	b = inputList()

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

	print(ans)









def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res


def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y



def H(n, r, mod=10**9+7):
	
	
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        
        
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Solution (Python):
A, B, M = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

min_cost = min(A_list) + min(B_list)
for i in range(M):
  x, y, c = map(int, input().split())
  min_cost = min(min_cost, A_list[x - 1] + B_list[y - 1] - c)

print(min_cost)

Solution (Python):
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 i in range(M)]
ans=2*10**5
for s,t,u in Z:
  d=a[s-1]+b[t-1]-u
  ans=min(ans,d)
e=min(a)+min(b)
print(min(ans,e))

Solution (Python):
Na, Nb ,M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
disc = []
ans = float('inf')
for _ in range(M):
    x,y,c = map(int, input().split())
    x -= 1
    y -= 1
    ans = min(ans, A[x] + B[y] - c)

print(min(ans, min(A) + min(B)))

Solution (Python):
import sys
from io import StringIO
import unittest

doTest=0

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    a,b,m=list(map(int, readline().strip().split()))
    ap=list(map(int, readline().strip().split()))
    bp=list(map(int, readline().strip().split()))

    minAB = min(ap) + min(bp)

    for _ in range(m):
        x,y,c=list(map(int, readline().strip().split()))
        minAB = min(ap[x-1] + bp[y-1] - c, minAB)

    print(minAB)

    return

if doTest==0:
    resolve()
    sys.exit()

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

Solution (Python):
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):
    tmp_a,tmp_b,tmp_c = map(int,input().split())
    tmp = l_a[tmp_a-1] + l_b[tmp_b-1] - tmp_c
    if tmp < ans:
        ans = tmp
    
print(ans)

Solution (Python):
A, B, M = [int(str) for str in input().strip().split()]
a = [int(str) for str in input().strip().split()]
b = [int(str) for str in input().strip().split()]
xyc = [[int(str) for str in input().strip().split()] for _ in range(M)]

min_price = min(a) + min(b)

for x, y, c in xyc:
    price = a[x - 1] + b[y - 1] - c
    min_price = min(min_price, price)

print(min_price)

Solution (Python):
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 _ in range(m)]

ans = min(A) + min(B)
for x, y, c in XYC:
  x -= 1
  y -= 1
  cand = A[x] + B[y] - c
  ans = min(ans, cand)
print(ans)

Solution (Python):











mod = int(1e9+7)
INF = 1<<29
lINF = 1<<35

def main():
    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())
        res = a[x-1] + b[y-1] - c
        ans = min(ans,res)
    print(ans)
    return

if __name__=='__main__':
    main()

Solution (Python):
a, b, m = map(int, input().split())
ax = list(map(int, input().split()))
bx = list(map(int, input().split()))

minsum = min(ax)+min(bx)

for i in range(m):
    x, y, c = map(int, input().split())
    minsum = min(ax[x-1] + bx[y-1] - c, minsum)

print(minsum)

Solution (Python):


a, b, m = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
min_cost = min(a_list) + min(b_list)
for i in range(0, m):
    ai, bi, c = map(int, input().split())
    if a_list[ai-1] + b_list[bi-1] - c < min_cost:
        min_cost = a_list[ai-1] + b_list[bi-1] - c
print(min_cost)

Solution (Python):
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())
  min_ab = min(a_list[x-1] + b_list[y-1] - c, min_ab)
print(min_ab)

Solution (Python):
import sys
input = sys.stdin.readline

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

mi = min(a) + min(b)
for _ in range(m):
    x, y, c = map(int, input().split())
    price = a[x-1]+b[y-1]-c
    if mi > price:
        mi = price

print(mi)

Solution (Python):
A,B,M = map(int,input().split())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))

min_cost = min(a_list)+min(b_list)
for _ in range(M):
    x,y,c = map(int,input().split())
    cost = a_list[x-1] + b_list[y-1] -c
    if cost<min_cost:
        min_cost = cost
print(min_cost)

Solution (Python):
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)]


min_a=min(a)
min_b=min(b)
ans=min_a+min_b

for x,y,c in xyc:
    temp=a[x-1]+b[y-1]-c
    if temp<ans:ans=temp
print(ans)

Solution (Python):
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())
    ans = min(ans , a[x-1]+b[y-1]-c)
print(ans)

Solution (Python):
def solve(a, b, m, a_list, b_list, xyc_list):
    res = min(a_list) + min(b_list)
    for x, y, c in xyc_list:
        r = a_list[x - 1] + b_list[y - 1] - c
        res = min(res, r)
    return res


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


def test():
    assert solve(2, 3, 1, [3, 3], [3, 3, 3], [[1, 2, 1]]) == 5
    assert solve(1, 1, 2, [10], [10], [[1, 1, 5], [1, 1, 10]]) == 10
    assert solve(2, 2, 1, [3, 5], [3, 5], [[2, 2, 2]]) == 6


if __name__ == "__main__":
    test()
    main()

Solution (Python):
import sys
import math
import collections
from collections import deque

a, b, m = map(int, sys.stdin.readline().split())



A = list(map(int, sys.stdin.readline().split()))
B = list(map(int, sys.stdin.readline().split()))
mina = min(A)
minb = min(B)
ans = mina + minb

for i in range(m):
    x, y ,c = map(int, sys.stdin.readline().split())
    if A[x-1] + B[y-1] - c < ans:
        ans = A[x-1] + B[y-1] - c

print(ans)

Solution (Python):
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())
  ans=min(ans,a[x-1]+b[y-1]-c)
print(ans)

Solution (Python):
def main():
    a,b,m=map(int,input().split())
    la=list(map(int,input().split()))
    lb=list(map(int,input().split()))
    ans=min(la)+min(lb)
    res=200000
    for i in range(m):
        x,y,c=map(int,input().split())
        res=min(res,la[x-1]+lb[y-1]-c)
    ans=min(ans,res)
    print(ans)
if __name__=="__main__":
    main()

Solution (Python):
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = float('inf')
X = []
Y = []
for _ in range(M):
    x, y, c = map(int, input().split())
    ans = min(a[x - 1] + b[y - 1] - c, ans)

print(min(ans, min(a) + min(b)))

Solution (Python):
from sys import stdin
def main():
	readline=stdin.readline
	A,B,M=map(int,readline().split())
	a=list(map(int,readline().split()))
	b=list(map(int,readline().split()))
	xyc=[0]*M
	for i in range(M):
		x=list(map(int,readline().split()))
		xyc[i]=x
	
	res=min(a)+min(b)
	for i in range(M):
		res=min(res,a[xyc[i][0]-1]+b[xyc[i][1]-1]-xyc[i][2])
	print(res)
if __name__=="__main__":
	main()

Solution (Python):
def main():
    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 _ in range(M):
        x,y,c = map(int,input().split(" "))
        x -= 1
        y -= 1
        ans = min(ans,A[x]+B[y]-c)
    print(ans)
if __name__ == "__main__":
    main()

Solution (Python):
a, b, m = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
xyc = []
for _ in range(m):
    xyc_ = list(map(int, input().split()))
    xyc.append(xyc_)
ans = min(al) + min(bl)
for i in range(m):
    an = xyc[i][0] - 1
    bn = xyc[i][1] - 1
    ans = min(ans, al[an] + bl[bn] - xyc[i][2])
print(ans)

Solution (Python):


def solve():
    A, B, M = list(map(int, input().split()))
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    ans = float("inf")
    for i in range(M):
        x, y, c = list(map(int, input().split()))
        tmp = As[x-1] + Bs[y-1] - c
        ans = min(ans, tmp)

    ans = min(ans, min(As)+min(Bs))

    print(ans)


if __name__ == "__main__":
    solve()

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

a = list(map(int,input().split()))
b = list(map(int,input().split()))
data = [0] * M
for i in range(M):
    x,y,c = map(int,input().split())
    x-=1
    y-=1
    data[i] = [x,y,c]

can = []

can.append(min(a)+min(b))

for i in range(M):
    can.append(a[data[i][0]]+b[data[i][1]]-data[i][2])

print(min(can))

Solution (Python):
A,B,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
p,q=10**5+1,10**5+1
for i in a:
    if i<p:
        p=i
for i in b:
    if i<q:
        q=i
r=p+q
for i in range(m):
    x,y,c=map(int,input().split())
    s=a[x-1]+b[y-1]-c
    if r>s:
        r=s
print(r)

Solution (Python):
INF = 10 ** 10
def solve(As, Bs, Ms):
    mini = min(As) + min(Bs)
    for (a, b, c) in Ms:
        cur = As[a] + Bs[b] - c
        mini = min(mini, cur)
    return mini

a, b, m = map(int, raw_input().split())
As = [INF] + map(int, raw_input().split())
Bs = [INF] + map(int, raw_input().split())
Ms = []
for i in xrange(m):
    (a, b, c) = map(int, raw_input().split())
    Ms.append((a, b, c))
print solve(As, Bs, Ms)

Solution (Python):
import sys
input = sys.stdin.readline  

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

Solution (Python):



def main():
    a, b, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    min_a = min(a)
    min_b = min(b)
    ans = min_a + min_b

    for i in range(m):
        xi, yi, ci = map(int, input().split())
        xi -= 1
        yi -= 1

        ans = min(ans, a[xi] + b[yi] - ci)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
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)]

min_a = min(a)
min_b = min(b)

set_lis = []

for x, y, c in xyc:
    money = a[x - 1] + b[y - 1] - c
    set_lis.append(money)

set_lis.sort()

print(min(min_a + min_b, set_lis[0]))

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


min_price = min(a) + min(b)


for i in range(M):
    low = a[t[i][0]-1] + b[t[i][1]-1] - t[i][2]
    if low < min_price:
        min_price = low
print(min_price)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def main():
    A,B,M = map(int,input().split())
    re = list(map(int,input().split()))
    de = list(map(int,input().split()))
    ans = min(re)+min(de)
    for _ in range(M):
        a,b,c = map(int,input().split())
        ans = min(ans,re[a-1]+de[b-1]-c)
    
    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
def mi():return map(int,input().split())
import numpy as np
A,B,M=mi()
a=np.array(list(mi()))
b=np.array(list(mi()))
minab=min(a)+min(b)

ans=float("inf")

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

Solution (Python):
a,b,m= map(int, input().split())
a1= list(map(int, input().split()))
b1= list(map(int, input().split()))
m1= [list(map(int, input().split())) for i in range(m)]

ans=min(a1)+min(b1)

for i in range(m):
    cnt=a1[m1[i][0]-1]+b1[m1[i][1]-1]-m1[i][2]
    ans=min(cnt,ans)

print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

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

A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
ans = min(A) + min(B)
query = [list(map(int, input().strip().split())) for i in range(M)]
for i, q in enumerate(query):
    x, y, m = q
    ans = min(A[x-1]+B[y-1]-m, ans)
print(ans)

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

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

aA = [int(n) for n in input().split()]
bB = [int(n) for n in input().split()]

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


min_ = min(aA[x - 1] + bB[y - 1] - c for x, y, c in xycM)

print(min(min(aA) + min(bB), min_))

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

min_ab = min(a) + min(b)

for _ in range(M):
    x, y, c = map(int, input().split())
    min_ab = min(min_ab, a[x - 1] + b[y - 1] - c)
print(min_ab)

Solution (Python):
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


def resolve():
    
    
    A, B, M = [int(x) for x in sys.stdin.readline().split()]  
    a_list = [int(x) for x in sys.stdin.readline().split()]  
    b_list = [int(x) for x in sys.stdin.readline().split()]  

    
    
    grid = [[int(x) for x in sys.stdin.readline().split()]
            for _ in range(M)]  

    logger.debug('{}'.format([]))

    _min = min(a_list) + min(b_list)

    for x, y, c in grid:
        a = a_list[x - 1]
        b = b_list[y - 1]

        t = a + b - c
        if _min > t:
            _min = t

    print(_min)


if __name__ == "__main__":
    resolve()





import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2 3 1
3 3
3 3 3
1 2 1"""
        output = """5"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """1 1 2
10
10
1 1 5
1 1 10"""
        output = """10"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """2 2 1
3 5
3 5
2 2 2"""
        output = """6"""
        self.assertIO(input, output)

Solution (Python):

''':'
docker run --rm -it -v $(cd $(dirname $0) && pwd):/vo:ro pypy:3-2.4.0 \
   /bin/bash -c "pypy3 /vo/$(basename $0) < /vo/input.txt"
exit $?
'''
import sys


def _ia(): return map(int, sys.stdin.readline().strip().split())


def main():
    a, b, m = _ia()
    ai = list(_ia())
    bi = list(_ia())
    cmin = min(ai) + min(bi)
    for _ in range(m):
        xi, yi, ci = _ia()
        ctmp = ai[xi-1] + bi[yi-1] - ci
        if cmin > ctmp:
            cmin = ctmp
    return cmin


if __name__ == "__main__":
    print(main())

Solution (Python):
aa,bb,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())
    p = a[x-1] + b[y-1] - c
    if p < ans:
        ans = p

print(ans)

Solution (Python):



import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    A, B, M = i2nn()
    a = i2nn()
    b = i2nn()
    min_price = min(a) + min(b)
    for i in range(M):
        x, y, c = i2nn()
        min_price = min(min_price, a[x-1] + b[y-1] - c)
    print(min_price)
    return

main()

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

mi = min(a) + min(b)
for k in range(m):
    x, y,  c = list(map(int, input().split()))
    score = a[x-1] + b[y-1] - c
    if score < mi:
        mi = score
print(mi)