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

min_cost = min(a) + min(b)

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

  if min_cost > cost:
    min_cost = cost

print(min_cost)

Solution (Python):
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue


Mod = 1000000007
import sys
sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

def main(): 
    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)]
    cost = float("inf")
    for i in range(m):
        cost = min(cost,a[xyc[i][0]-1]+b[xyc[i][1]-1]-xyc[i][2])
    print(min(cost,min(a)+min(b)))

if __name__ == "__main__":
    main()

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

minPrice = sorted(aList)[0] + sorted(bList)[0]

for indx in range(M):
    x, y, c = map(int, input().split())
    if minPrice >= (aList[x-1] + bList[y-1] - c):
        minPrice = (aList[x-1] + bList[y-1] - c)

print(minPrice)

Solution (Python):
import sys
import os


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    A, B, M = list(map(int, sys.stdin.readline().split()))
    a = list(map(int, sys.stdin.readline().split()))
    b = list(map(int, sys.stdin.readline().split()))
    c = [list(map(int, sys.stdin.readline().split())) for _ in range(M)]

    ret = float('inf')
    for w in c:
        tmp = a[w[0] - 1] + b[w[1] - 1] - w[2]
        if ret > tmp:
            ret = tmp

    if ret > min(a) + min(b):
        ret = min(a) + min(b)

    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
AC, BC, MC = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
M = [tuple(map(int, input().split())) for _ in range(MC)]

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

Solution (Python):

import heapq

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

price = min(a) + min(b)

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

print(price)

Solution (Python):
a, b, m = map(int, input().split())
a1 = list(map(int, input().split()))
b1 = list(map(int, input().split()))
c = [list(map(int, input().split())) for l in range(m)]
l = [min(a1) + min(b1)]
for i in c:
    l.append(a1[i[0] - 1] + b1[i[1] - 1] - i[2])
print(min(l))

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

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

aa = list(map(int, input().split()))
bb = list(map(int, input().split()))

mm = list()
for ii in range(0,m):
    mm.append(list(map(int, input().split())))

prices = list()

prices.append(int(min(aa)) + int(min(bb)))

for ml in mm:
    prices.append(aa[ml[0]-1] + bb[ml[1]-1] - ml[2])

print(min(prices))

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

min_cost = 10 ** 6

for i in range(M):
  x,y,c = [int(s) for s in input().split()]
  if min_cost > a[x-1] + b[y-1] - c:
    min_cost = a[x-1] + b[y-1] - c
    
    
min_cost2 = min(a) + min(b)
print(min_cost) if min_cost < min_cost2 else print(min_cost2)

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

a_i = list(map(int,input().split()))
b_j = list(map(int,input().split()))

x = []
for i in range(m):
    x_x = list(map(int,input().split()))
    x.append(x_x)
    continue

ans = 1000000000000
for i in x:
    s = a_i[i[0]-1] + b_j[i[1]-1] - i[2]
    if ans > s:
        ans = s

a_b = min(a_i) + min(b_j)

if a_b < ans:
    print(a_b)
else:
    print(ans)

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

lisa = [0] * a
lisb = [0] * b
lism = [[0, 0, 0]] * m 
lisab = input().split()
lisbc = input().split()
lisa = [int(i) for i in lisab]
lisb = [int(i) for i in lisbc]


minval = min(lisa) + min(lisb)

for i in range(m):
    [aa, bb, cc] = input().split()
    if (lisa[int(aa)-1] + lisb[int(bb)-1] - int(cc) < minval):
        minval = lisa[int(aa)-1 ] + lisb[int(bb)-1] - int(cc)
print(minval)

Solution (Python):
A,B,M= map(int,input().split())
A_list = list(map(int,input().split()))
B_list = list(map(int,input().split()))
coupon = []
for _ in range(M):
  x,y,d = map(int,input().split())
  coupon.append([x,y,d])
mini = min(A_list) + min(B_list)
Answer = mini

for i in range(M):
  discount = A_list[coupon[i][0]-1] + B_list[coupon[i][1]-1] - coupon[i][2]
  if discount < Answer:
    Answer = discount
print(Answer)

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):
    data=list(map(int,input().split()))
    ans=min(ans,A[data[0]-1]+B[data[1]-1]-data[2])

print(ans)

Solution (Python):

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]"):
    u=[]
    for i in range(M):
        u.append(a[x[i]-1]+b[y[i]-1]-c[i])
    o=min(u)
    if min(a)+min(b)<o:
        print(min(a)+min(b))
    else:
        print(o)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    M = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(A)]  
    b = [int(next(tokens)) for _ in range(B)]  
    x = [int()] * (M)  
    y = [int()] * (M)  
    c = [int()] * (M)  
    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()

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

print(ans)

Solution (Python):
s=list(map(int,input().split()))
t=list(map(int,input().split()))
u=list(map(int,input().split()))
v=[list(map(int,input().split())) for i in range(s[2])]
a=10**6
d=0
e=0
for i in range(s[2]):
  d=v[i][0]-1
  e=v[i][1]-1
  b=t[d]+u[e]-v[i][2]
  a=min(a,b)
f=min(t)
e=min(u)+f
a=min(a,e)
print(a)

Solution (Python):
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
mi = 10**6
for i in range(M):
  x, y, c = map(int, input().split())
  tmp = a[x-1] + b[y-1] - c
  if mi > tmp:
    mi = tmp
a.sort()
b.sort()
tmp = a[0] + b[0]
if mi > tmp:
  mi = tmp
print(mi)

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

MIN = min(a) + min(b)

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

Solution (Python):
import sys
f=lambda:map(int,sys.stdin.readline().split())
_,_,n=f()
a,b=list(f()),list(f())
m=min(a)+min(b)
for _ in range(n): x,y,c=f(); m=min(m,a[x-1]+b[y-1]-c)
print(m)

Solution (Python):
sum = []
A,B,M = map(int, input().split())
a_l = [int(x) for x in input().split()]
b_l = [int(x) for x in input().split()]

sum.append(min(a_l) + min(b_l))

for m in range(M):
    coupon = []
    coupon = [int(x) for x in input().split()]

    sum.append(a_l[coupon[0]-1] + b_l[coupon[1]-1] - coupon[2])

print(min(sum))

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

m_arr = []

for _ in range(m):
    m_arr.append(list(input().split()))

temp_min = int(min(a_arr)) + int(min(b_arr))

for xm, ym, cm in m_arr:
    wari = int(a_arr[int(xm)-1]) + int(b_arr[int(ym)-1]) - int(cm)

    if wari < temp_min:
        temp_min = wari


print(temp_min)

Solution (Python):
a, b, m = map(int, input().split())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
xycs = [tuple(map(int, input().split())) for _ in range(m)]
ans = min(arr) + min(brr)

for elem in xycs:
    x = elem[0]
    y = elem[1]
    c = elem[2]
    ans = min((arr[x - 1] + brr[y - 1] - c), ans)

print(ans)

Solution (Python):
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
coupon = {}
values = []
for i in range(M):
  x,y,c = map(int,input().split())
  values.append(a[x-1]+b[y-1]-c)
Amin = min(a)
Bmin = min(b)
values.append(Amin+Bmin)
print(min(values))

Solution (Python):
a, b, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = min(a)
bb = min(b)
aabb = aa+bb
ab = [a+b for a, b in zip(a, b)]
ans = []
for i in ab:
    ans.append(i)

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

ans.append(aabb)
print(min(ans))

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

aaa = []
bbb = []
ccc = []
for i in range(m):
  e = list(map(int, input().split()))
  aaa.append(e[0])
  bbb.append(e[1])
  ccc.append(e[2])
  
cost = []
for i,j,k in zip(aaa, bbb, ccc):
  u = aa[i-1] + bb[j-1] - k
  cost.append(u)
  
aa.sort()
bb.sort()
minn = aa[0] + bb[0]
cost.append(minn)

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

Solution (Python):
(_, _, M) = [int(inp) for inp in input().split()]

A = [int(inp) for inp in input().split()]
B = [int(inp) for inp in input().split()]

minA = min(A)
minB = min(B)

min = minA+minB

for m in range(0, M):
    (aindex, bindex, waribiki) = [int(inp) for inp in input().split()]
    newmin = A[aindex-1]+B[bindex-1] -waribiki
    if (newmin < min):
        min=newmin

print(min)

Solution (Python):
a,b,m= [int(x) for x in input().split()]
alist = [int(x) for x in input().split()]
blist = [int(x) for x in input().split()]
w = []
for i in range(m):
  w.append([int(x) for x in input().split()])
kane = min(alist) + min(blist)
for i in range(m):
  money = alist[w[i][0]-1] + blist[w[i][1]-1]-w[i][2]
  if money < kane:
    kane = money
print(kane)

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())
  k=A[x-1]+B[y-1]-c
  if k<ans:
    ans=k
print(ans)

Solution (Python):


import sys
import math
import itertools

def main():
    A, B, M = map(int, sys.stdin.readline().split())
    As = list(map(int, sys.stdin.readline().split()))
    Bs = list(map(int, sys.stdin.readline().split()))
    Xs, Ys, Cs = map(list, zip(*(tuple(map(int, sys.stdin.readline().split())) for _ in range(M))))

    
    nocoupon = min(As) + min(Bs)

    
    coupon = min(As[Xs[i] - 1] + Bs[Ys[i] - 1] - Cs[i] for i in range(M))

    print(min(nocoupon, coupon))

if __name__ == '__main__':
    main()

Solution (Python):
line1 = input()
a, b, m = line1.split(" ")
line2 = input()
a_list = line2.split(" ")
a_list = [int(a) for a in a_list]
line3 = input()
b_list = line3.split(" ")
b_list = [int(b) for b in b_list]

min_val = min(a_list) + min(b_list)

for i in range(int(m)):
    line = input()
    x, y, c = line.split(" ")
    x = int(x)
    y = int(y)
    c = int(c)
    if min_val > a_list[x-1] + b_list[y-1] - c:
        min_val = a_list[x-1] + b_list[y-1] - c
print(min_val)

Solution (Python):
def main():
    A,B,M = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    
    c = []

    for i in range(M):
        cc = list(map(int,input().split()))
        c.append(cc)
    a_min = min(a)
    b_min = min(b)
    ans = a_min + b_min
    for i , c_i in enumerate(c):
        tmp = a[c_i[0]-1] + b[c_i[1]-1] - c_i[2]
        ans = min(ans,tmp)
    
    
    
    
    
    
    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()))
S=[input().split() for l in range(M)]

List = []
for i in range(M):
    x = a[int(S[i][0])-1]+b[int(S[i][1])-1]-int(S[i][2])
    List.append(x)
    
Min = List[0]
for i in range(1,M):
    if Min > List[i]:
        Min = List[i]
        
mina = a[0]
for i in range(A):
    if mina > a[i]:
        mina = a[i]
        
minb = b[0]
for i in range(B):
    if minb > b[i]:
        minb = b[i]
        
if Min >= mina+minb:
    print(mina+minb)
else:
    print(Min)

Solution (Python):
a,b,m=map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
minimum=200000
x=[0]*m
y=[0]*m
c=[0]*m
for i in range(m):
    x[i],y[i],c[i]=map(int,input().split())
    sum=A[x[i]-1]+B[y[i]-1]-c[i]
    if sum<minimum:
        minimum=sum
sum=min(A)+min(B)
if sum<minimum:
    print(sum)
else:
    print(minimum)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def main():
	a, b , m = IN()
	al=list(IN())
	bl=list(IN())
	mm=min(al)+min(bl)
	for _ in range(m):
		x,y,c=IN()
		cc = al[x-1]+bl[y-1]-c
		mm = min(mm,cc)
	return mm
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
A, B, M = map(int, input().split())
Ax = list(map(int, input().split()))
Bx = list(map(int, input().split()))
listM = []
for i in range(M):
    listM.append(list(map(int, input().split())))

Min = Ax[listM[i][0] - 1] + Bx[listM[i][1] - 1] - listM[i][2]
min0 = min(Ax) + min(Bx)

for i in range(M):
    if Ax[listM[i][0] - 1] + Bx[listM[i][1] - 1] - listM[i][2] < Min:
        Min = Ax[listM[i][0] - 1] + Bx[listM[i][1] - 1] - listM[i][2]
if min0 < Min:
    print(min0)
else:
    print(Min)

Solution (Python):
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
xyc=[0]*M
for i in range(M):
  x[i],y[i],c[i] = map(int,input().split())
  xyc[i]=a[x[i]-1]+b[y[i]-1]-c[i]
  
ans1=min(xyc)
ans2=min(a)+min(b)

print(min(ans1,ans2))

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

price_min = min(a) + min(b)
for i in range(h[2]):
  c = list(map(int,input().split()))
  price = a[c[0]-1] + b[c[1]-1] - c[2]
  if price_min > price:
    price_min = price
    
print(price_min)

Solution (Python):
a, b, m = map(int, input().split())
list_a = list(map(int, input().split()))
list_b = list(map(int, input().split()))
li = []
for i in range(m):
  li.append(list(map(int, input().split())))
wa_1 = min(list_a) + min(list_b)
wa_2 = 10000000000
for i in range(m):
  wa_2 = min(wa_2, list_a[li[i][0]-1]+list_b[li[i][1]-1]-li[i][2])
print(min(wa_1, wa_2))

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

Solution (Python):
val = input().split(' ')
a = int(val[0])
b = int(val[1])
m = int(val[2])

alist = [int(a) for a in input().split(' ')]
blist = [int(b) for b in input().split(' ')]

min_price = sorted(alist)[0] + sorted(blist)[0]

for i in range(m):
    discount = [int(m) for m in input().split(' ')]
    price = alist[discount[0] - 1] + blist[discount[1] - 1] - discount[2]
    if price < min_price:
        min_price = price

print(min_price)

Solution (Python):
a_num, b_num, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xyc = [input().split() for i in range(m)]

ans = min(a) + min(b)
for i in range(m):
  p = int(xyc[i][0])
  q = int(xyc[i][1])
  r = int(xyc[i][2])
  if a[p-1] + b[q-1] - r < ans:
    ans = a[p-1] + b[q-1] - r

print(ans)

Solution (Python):
import collections
A, B, M = [int(item) for item in input().split()]
A_list = [int(item) for item in input().split()]
B_list = [int(item) for item in input().split()]
M_list = [[int(item) for item in input().split()] for _ in range(M)]

res_list = collections.deque()

for a, b, c in M_list:
    res_list.append(A_list[a-1] + B_list[b-1] - c)

res_list.append(min(A_list) + min(B_list))

print(min(res_list))

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

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

print(ans)

Solution (Python):

import sys

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

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

print(minSum)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians

from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    A, B, M = MAP()
    a = LIST()
    b = LIST()
    
    ans = 10000000
    for i in range(M):
        x, y, c = MAP()
        ans = min(ans, a[x-1] + b[y-1] - c) 

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


if __name__ == '__main__':
    main()

Solution (Python):

def main():
    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()]
    
    p = []
    for i in range(M):
        x, y, c = [int(i) for i in input().split()]
        p.append(a[x - 1] + b[y - 1] - c)
    
    print(min([min(p), min(a) + min(b)]))

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 _ in range(M):
  l=list(map(int,input().split()))
  min_=min(min_,a[l[0]-1]+b[l[1]-1]-l[2])
print(min_)

Solution (Python):


import sys
 
stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
 
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()

A, B, M = na()
a = na()
b = na()
xyc = nan(M)
ans = min(a) + min(b)
for i in range(M):
    x, y, c = xyc[i]
    x -= 1
    y -= 1
    ans = min(ans, a[x] + b[y] - c)

print(ans)

Solution (Python):
na,nb,m = map(int,input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
D = [[int(x) for x in input().split()] for _ in range(m)]

best = min(A) + min(B)
for x,y,c in D:
    cand =  A[x-1] + B[y-1] - c
    if cand < best:
        best = cand
print(best)

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

ans = float('Inf')
ans = min(a) + min(b)

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

Solution (Python):

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

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

        if answer > candidate:
            answer = candidate

    print(answer)



if __name__ == "__main__":
    b()

Solution (Python):
A, B, M = [int(v) for v in input().strip().split(" ")]
a_list = [int(v) for v in input().strip().split(" ")]
b_list = [int(v) for v in input().strip().split(" ")]
m_list = [
    [int(v) for v in input().strip().split(" ")]
    for _ in range(M)
]

c = [min(a_list) + min(b_list)]
for m in m_list:
    c.append(a_list[m[0]-1]+b_list[m[1]-1]-m[2])

print(min(c))

Solution (Python):

a,b,m=map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
amin=min(al)
bmin=min(bl)
minp=amin+bmin
for i in range(m):
    num=list(map(int,input().split()))
    price=al[num[0]-1]+bl[num[1]-1]-num[2]
    minp=min(price,minp)
print(minp)

Solution (Python):
a,b,m = map(int,input().split())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
x_y_c_list=[list(map(int,input().split())) for _ in range(m)]

ans = min(a_list) + min(b_list)

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

print(ans)

Solution (Python):

import sys
def input(): return sys.stdin.readline().rstrip()
na, nb, nm = map(int, input().split())
A = [10**5] + list(map(int, input().split()))
B = [10**5] + list(map(int, input().split()))
coupon = [list(map(int, input().split())) for _ in range(nm)]

min_price = 2 * 10**5
for i in range(len(coupon)):
    price = A[coupon[i][0]] + B[coupon[i][1]] - coupon[i][2]
    if min_price > price: min_price = price
no_coupon = min(A)+min(B)
print(min_price if min_price < no_coupon else no_coupon)

Solution (Python):
A, B, M = map(int, input().split())
As = [int(s) for s in input().split()]
Bs = [int(s) for s in input().split()]
Ms = []
for i in range(M):
    Ms.append([int(s) for s in input().split()])





minPrice = min(As) + min(Bs) 

for m in Ms:
    a, b, d = m
    price = As[a-1] + Bs[b-1] - d 
    
    minPrice = min(minPrice, price)

print(minPrice)

Solution (Python):
"""
B - Nice Shopping
"""
a_list = []
b_list = []

a, b, m = map(int, input().split(' '))
a_list = [int(i) for i in input().split(' ')]
b_list = [int(i) for i in input().split(' ')]
m_list = []
for i in range(m):
    m_list.append([int(i) for i in input().split(' ')])


a_min = min(a_list)
b_min = min(b_list)
ode = a_min + b_min

for indirim in m_list:
    yeni_ode = a_list[indirim[0]-1] + b_list[indirim[1]-1] -indirim[2]
    if yeni_ode < ode:
        ode = yeni_ode

print(ode)

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

cost=[]
for i in range(M):
    cost.append(a[L[i][0]-1]+b[L[i][1]-1]-L[i][2])

cost.append(min(a)+min(b))
print(min(cost))

Solution (Python):
ALEN, BLEN, M = map(int, input().split())
A = [int(n) for n in input().split()]
B = [int(n) for n in input().split()]

price = float('inf')

for i in range(M):
	ai, bi, c = map(int, input().split())
	waribiki = A[ai-1] + B[bi-1] - c
	if waribiki < price:
		price = waribiki

nowaribiki = min(A) + min(B)

if nowaribiki < price:
	price = nowaribiki

print(price)

Solution (Python):
def resolve():
    A, B, M = map(int, input().split())  
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    x_list = []

    for i in range(M):
        x_list.append(list(map(int, input().split())))

    min_prace = min(a_list) + min(b_list)

    for i_list in x_list:
        tmp_prace = a_list[i_list[0]-1] + b_list[i_list[1]-1] - i_list[2]
        if tmp_prace < min_prace:
            min_prace = tmp_prace

    print(min_prace)

if __name__ == "__main__":
    resolve()

Solution (Python):

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

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

    print(min_)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  
    B = int(next(tokens))  
    M = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(A)]  
    b = [int(next(tokens)) for _ in range(B)]  
    x = [int()] * (M)  
    y = [int()] * (M)  
    c = [int()] * (M)  
    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()

Solution (Python):
import sys

numbers=input().rstrip().split(" ")
A=int(numbers[0])
B=int(numbers[1])
M=int(numbers[2])

ref=input().rstrip().split(" ")
mic=input().rstrip().split(" ")

koopons=[]
for line in sys.stdin.readlines():
    koopons.append(line.rstrip().split(" "))

'''print(A)
print(B)
print(M)
print(ref)
print(mic)
print(koopons)'''

prices=[]
for koopon in koopons:
    price=int(ref[int(koopon[0])-1])+int(mic[int(koopon[1])-1])-int(koopon[2])
    prices.append(price)

lowest2=sorted(prices)[0]
refs=sorted(ref)
mics=sorted(mic)
lowest1=int(refs[0])+int(mics[0])


if lowest1>=lowest2:
    print(lowest2)
else:
    print(lowest1)

Solution (Python):

import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)

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

Alist = list(map(int, readline().split()))
Blist = list(map(int, readline().split()))

min_a = min(Alist)
min_b = min(Blist)
answer = min_a + min_b

for _ in range(0, M):
    x, y, c = map(int, readline().split())
    tmp = Alist[x-1] + Blist[y-1] - c
    answer = min(answer, tmp)

if answer > 0:
    print(answer)
else:
    print(0)

Solution (Python):
import itertools

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

kumiawase_kingaku = min(a_list) + min(b_list)

for x, y, c in m_list:
    reizouko = a_list[x-1]
    denshirenji = b_list[y-1]
    kingaku = reizouko + denshirenji - c
    if kingaku < kumiawase_kingaku:
        kumiawase_kingaku = kingaku

print(kumiawase_kingaku)

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

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

min_cost = min_a + min_b

for i in range(M):
    kari = a[xyc[i][0]-1] + b[xyc[i][1]-1] -xyc[i][2]
    if kari < min_cost:
        min_cost = kari
        
print(min_cost)

Solution (Python):
A, B, M = list(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):
    tmpx, tmpy, tmpc = list(map(int, input().split()))
    val = a[tmpx-1]+b[tmpy-1]-tmpc
    if ans > val:
        ans = val

print(ans)

Solution (Python):
import sys
l = sys.stdin.read().split('\n')[:-1]
p = [list(map(int, i.split())) for i in l[1:3]]
ans = (p[0][d[0] - 1] + p[1][d[1] - 1] - d[2] for d in (list(map(int, i.split())) for i in l[3:]))
print(min(min(ans), min(p[0]) + min(p[1])))