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:
def main():

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

    a_min, b_min = min(a), min(b)
    val_min = a_min + b_min

    for i in range(M):
        val = a[dis[i][0] - 1] + b[dis[i][1] - 1] - dis[i][2]
        if val < val_min:
            val_min = val

    print(val_min)


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()))
xyc = [list(map(int,input().split())) for _ in range(m)]

price = min(A)+min(B)
for x,y,c in xyc:
    k = A[x-1] + B[y-1] - c 
    if k < price:
        price = k
        
print(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()))
xyc = [list(map(int,input().split())) for i in range(M)]

# print(a,b,xyc)
ans = min(a)+min(b)
for i in range(M):
    x = a[xyc[i][0]-1]
    y = b[xyc[i][1]-1]
    nedan = x + y - xyc[i][2]
    if nedan < ans:
        ans = nedan
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque


def input():
    return sys.stdin.readline()[:-1]


def solve():
    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)
    memo = [0]*(M+1)
    memo[0] = a_min + b_min
    for m in range(M):
        x, y, c = map(int, input().split())
        memo[m+1] = a[x-1]+b[y-1]-c
    print(min(memo))


if __name__ == "__main__":
    solve()

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()))
l_x = []
for i in range(m):
  l_x.append(list(map(int,input().split())))

#何も持っていないときの最小値
sonomama = min(l_a)+min(l_b)

#割引券を使ったときの最小値
l_warine = [sonomama]
for i in l_x:
  l_warine.append( l_a[ i[0]-1 ] + l_b[ i[1]-1 ] -i[2] )
print(min(l_warine))

Submission status: Accepted

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

Submission status: Accepted

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

an=min(ali)+min(bli)

for i in range(m):
    x,y,c=map(int,input().split())
    if an>((ali[x-1]+bli[y-1])-c):
        an=((ali[x-1]+bli[y-1])-c)

print(an)

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

mini = min(a_list) + min(b_list)

c_list = []

for i in range(m):
  a_1, b_1, m_1 = map(int, input().split())
  c_list.append(a_list[a_1 - 1] + b_list[b_1 - 1] - m_1)

c_list.append(mini)

ans = min(c_list)

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 set(range(M)):
    x,y,c = map(int, input().split())
    ans = min(ans,a[x-1]+b[y-1]-c)
print(ans)

Submission status: Accepted

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

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



min_price = min(refrigerator) + min(microwave)

for i in range(m):
    x, y, c = list(map(int, input().split()))
    min_price = min(min_price, refrigerator[x-1]+microwave[y-1]-c)
    
print(min_price)

Submission status: Accepted

Solution code:
A,B,M = list(map(int,input().split()))
listA = list(map(int,input().split()))
listB = list(map(int,input().split()))
listC = set([])
for i in range(M):
    M = list(map(int,input().split()))
    listC.add(listA[M[0]-1]+listB[M[1]-1]-M[2])
D = min(listC)
E = min(listA) + min(listB)
print(min(D,E))

Submission status: Accepted

Solution code:
a,b,m = map(int,input().split())
aprice = list(map(int,input().split()))
bprice = list(map(int,input().split()))
waribiki = [list(map(int, input().split())) for i in range(m)]
minprice = min(aprice)+min(bprice)
for i in range(len(waribiki)):
         tmp = aprice[waribiki[i][0]-1]+bprice[waribiki[i][1]-1]-waribiki[i][2]
         if minprice > tmp:
                  minprice = tmp
print(minprice)

Submission status: Accepted

Solution code:
import numpy as np
import fractions

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 k in range(m):
    i,j,x=map(int, input().split())
    ans=min(ans,A[i-1]+B[j-1]-x)
if(ans<0):ans=0
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 = []
y = []
c = []

for _ in range(M):
    tmp = list(map(int,input().split()))
    x.append(tmp[0])
    y.append(tmp[1])
    c.append(tmp[2])

ans = min(a) + min(b)

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

print(ans)

Submission status: Accepted

Solution code:
f = lambda:map(int,input().split())
a,b,m = f()
la = list(f())
lb = list(f())
lx,ly,lc = [],[],[]
a = min(la)+min(lb)
for _ in range(m):
  x,y,c = f()
  a = min(a,la[x-1]+lb[y-1]-c)
print(a)

Submission status: Accepted

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

al = list(map(int, input().split()))
bl = list(map(int, input().split()))
xl = list(list(map(int, input().split())) for _ in range(m))

cost = min(al) +min(bl)
for i in range(m):
    ai = xl[i][0]
    bi = xl[i][1]
    ci = xl[i][2]
    if cost > (al[ai-1] + bl[bi-1] - ci):
        cost = (al[ai-1] + bl[bi-1] - ci)

print(cost)

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 _ in range(m):
    x, y, c = map(int, input().split())
    ans = min(ans, A[x-1] + B[y-1] - c)

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

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



print(ans)

Submission status: Accepted

Solution code:
A,B,M = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = []
for _ in range(M):
    tmp = [int(x) for x in input().split()]
    c.append(tmp)

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from pprint import pprint

A, B, M = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))
b = list(map(int, sys.stdin.readline().strip().split()))

ans = min(a) + min(b)
for i in range(M):
    x, y, c = map(int, sys.stdin.readline().strip().split())
    ans = min(ans, a[x-1] + b[y-1] - c)

print(ans)

Submission status: Accepted

Solution code:
A, B, M = [int(s) for s in input().split()]
Alist = [int(s) for s in input().split()]
Blist = [int(s) for s in input().split()]
minimum = min(Alist) + min(Blist)
for i in range(M):
  xi, yi, c = [int(s) for s in input().split()]
  xi = xi-1
  yi = yi-1
  attempt = Alist[xi] + Blist[yi] - c
  if minimum >= attempt:
    minimum = attempt
print(minimum)

Submission status: Accepted

Solution code:
a,b,m=map(int,input().split())
fri=list(map(int,input().split()))
mic=list(map(int,input().split()))
wari=[list(map(int,input().split())) for i in range(m)]
mi=min(fri)+min(mic)
for i in range(m):
    if fri[wari[i][0]-1]+mic[wari[i][1]-1]-wari[i][2]<mi:
        mi=fri[wari[i][0]-1]+mic[wari[i][1]-1]-wari[i][2]
print(mi)

Submission status: Accepted

Solution code:
a,b,m = [int(x) for x in input().split()]
lia = [int(x) for x in input().split()]
lib = [int(x) for x in input().split()]
wari = list()
for _ in range(m):
    wari.append([int(x) for x in input().split()])
ans = 10**20
for i in wari:
    s = lia[i[0]-1]+lib[i[1]-1]-i[2]
    ans = min(ans,s)
x = min(lia)+min(lib)
ans = min(ans,x)
print(ans)

Submission status: Accepted

Solution code:
A_num,B_num,M_num = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Waribiki = []
for _ in range(M_num):
    Waribiki.append(list(map(int,input().split())))
# 何も割引券を使わないときの最安値
min_price = min(A) + min(B)
# 割引券で安くなったときの値段を考える
for w in Waribiki:
    x = w[0]
    y = w[1]
    c = w[2]
    min_price = min(min_price, A[x-1]+B[y-1]-c)
print(min_price)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
As = [i for i in map(int, input().split())]
Bs = [i for i in map(int, input().split())]
out = min(As) + min(Bs)
for _ in range(M):
  x, y, c = map(int, input().split())
  out = min(out, As[x-1]+Bs[y-1]-c)
print(out)

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

result = min(a) + min(b)
for i in range(M):
    x,y,c = xyc[i]
    cost = a[x-1] + b[y-1] - c
    result = min(result, cost)
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()))
info=[]
for i in range(m):
    x,y,c=map(int,input().split())
    info.append([x-1,y-1,c])
ans=min(a)+min(b)
for i in range(m):
    p=a[info[i][0]]+b[info[i][1]]-info[i][2]
    if p<ans:
        ans=p
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()))

min_tmp = min(a)+min(b)

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

print(min_tmp)

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
d = [[0]*3 for i in range(M)]
for i in range (M):
    d[i][0],d[i][1],d[i][2] = map(int,input().split())

now_min = min(a)+min(b)
mim = 10**9
for i in range(M):
    if a[d[i][0]-1] + b[d[i][1]-1] - d[i][2] <= mim:
        mim = a[d[i][0]-1] + b[d[i][1]-1] - d[i][2]

print(min(now_min,mim))

Submission status: Accepted

Solution code:
a, b, m = map(int, input().split())
q = list(map(int, input().split()))
w = list(map(int, input().split()))
ans = min(q)+min(w)
for i in range(m):
  x, y, c = map(int, input().split())
  ans = min(q[x-1]+w[y-1]-c, ans)
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()))
s = []
for i in range(m):
    s.append(list(map(int,input().split())))


ans = [min(alist)+min(blist)]

for i in range(m):
    x = s[i][0]
    y = s[i][1]
    c = s[i][2]
    p = alist[x-1] + blist[y-1] - c
    ans.append(p)

print(min(ans))

Submission status: Accepted

Solution code:
a,b,m= list(map(int, input().split()))
ia = list(map(int, input().split()))
ib= list(map(int, input().split()))
z=min(ia)+min(ib)
for j in range(m):
    k= list(map(int, input().split()))
    bn=ia[(k[0]-1)]+ib[(k[1]-1)]-k[2]
    if z>bn:
        z=bn
print(z)

Submission status: Accepted

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

minprice = min(a)+min(b)
for i in range(M):
    p = a[c[i][0]-1]+b[c[i][1]-1]-c[i][2]
    minprice = min(p, minprice)

print (minprice)

Submission status: Accepted

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

import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def fft(A, B):
    """ 
    高速フーリエ変換(FFT)
        A：出現回数をカウントしたリスト
        B：出現回数をカウントしたリスト
    """
    import numpy as np
    from numpy.fft import rfft, irfft

    # 出現数カウント
    MAXA = max(A)
    MAXB = max(B)
    C1 = [0] * (MAXA+1)
    C2 = [0] * (MAXB+1)
    for a in A:
        C1[a] += 1
    for b in B:
        C2[b] += 1
    # max(A)+max(B)より大きい2冪
    L = 1
    k = 0
    while L <= MAXA + MAXB:
        k += 1
        L = 2**k
    # FFT
    res = irfft(rfft(C1, L) * rfft(C2, L), L)
    # 四捨五入して整数に
    res = np.rint(res).astype(np.int64)
    return res

N, M, L = MAP()
A = LIST()
B = LIST()

res = list(fft(A, B))

mn = INF
for i, a in enumerate(res):
    if a != 0:
        mn = i
        break

for _ in range(L):
    x, y, c = MAP()
    x -= 1; y -= 1
    mn = min(mn, A[x] + B[y] - c)
print(mn)

Submission status: Accepted

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

def ri():
    return int(input())

def rl():
    return list(input().split())

def rli():
    return list(map(int, input().split()))

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

#xyc=[]
xymin=min(ai)+min(bi)

for i in range(m):
#    xyc.append(list(map(int, input().split()))
    x,y,c=map(int, input().split())
    xy=ai[x-1]+bi[y-1]-c
    if xymin>xy:
        xymin=xy

print(xymin)

Submission status: Accepted

Solution code:
a, b, m = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
XYZ = []
for i in range(m):
    XYZ.append(list(map(int,input().split())))
ans = 10**5+10**5
for i in range(m):
    a_num = XYZ[i][0]
    b_num = XYZ[i][1]
    if A[a_num-1]+B[b_num-1]-XYZ[i][2] < ans:
        ans = A[a_num-1]+B[b_num-1]-XYZ[i][2]
minAB = min(A) + min(B)
if ans < minAB:
    print(ans)
else:
    print(minAB)

Submission status: Accepted

Solution code:
a, b, m  = [int(x) for x in input().split()]
a_l = [int(x) for x in input().split()]
b_l = [int(x) for x in input().split()]
ans = min(a_l) + min(b_l)
for mm in range(m):
    xm, ym, cm = [int(x) for x in input().split()]
    anss = a_l[xm - 1] + b_l[ym - 1] - cm
    if anss < ans:
        ans = anss
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()))
prices = []
prices.append(min(a)+min(b))
for i in range(m):
    ticket = list(map(int, input().split()))
    prices.append(a[ticket[0]-1]+b[ticket[1]-1]-ticket[2])
print(min(prices))

Submission status: Accepted

Solution code:
import sys
import math
def i():return int(sys.stdin.readline().replace("\n",""))
def i2():return map(int,sys.stdin.readline().replace("\n","").split())
def s():return str(sys.stdin.readline().replace("\n",""))
def l():return list(sys.stdin.readline().replace("\n",""))
def intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def lx():return list(map(lambda x:int(x)*-1,sys.stdin.readline().replace("\n","").split()))
def t():return tuple(map(int,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":pass
na, nb, nm = i2()
a = intl()
b = intl()
mp = min(a)+min(b)
for i in range(nm):
    x , y , c = i2()
    p = a[x-1]+b[y-1]-c
    if (p) < mp:
        mp = p
print(mp)

Submission status: Accepted

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

mn=min(al)+min(bl)

for i in range(m):
    price=0
    x,y,c=map(int,input().split())
    price=al[x-1]+bl[y-1]-c
    if mn>price:
        mn=price
print(mn)

Submission status: Accepted

Solution code:
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 = 2*10**5

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

Submission status: Accepted

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

    aryA = [int(v) for v in input().split()]
    aryB = [int(v) for v in input().split()]

    minPrice = min(aryA) + min(aryB)

    for _ in range(M):
        x, y, c = map(int, input().split())

        x -= 1
        y -= 1

        minPrice = min(minPrice, aryA[x] + aryB[y] - c)

    print(minPrice)


main()

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
prices_A = list(map(int, input().split()))
prices_B = list(map(int, input().split()))
coupons = [tuple(map(int, input().split())) for _ in range(M)]
possible_purchases = []
for coupon in coupons:
    combination_price = prices_A[coupon[0]-1] + prices_B[coupon[1]-1] - coupon[2]
    possible_purchases.append(combination_price)
possible_purchases.append(min(prices_A)+min(prices_B))
print(min(possible_purchases))

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

cost = min(a) + min(b)
for i in range(m):
    ai, bi, c = map(int, input().split())
    cost = min(cost, a[ai-1] + b[bi-1] - c)

print(cost)

Submission status: Accepted

Solution code:
# hitachi2020 B

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 i in range(m):
    x, y, c = map(int, input().split())
    
    if A[x-1] + B[y-1] - c < min_price:
        min_price = A[x-1] + B[y-1] - c

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

ans = min(a) + min(b)

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

print(ans)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    a, b, m = map(int, input().split())

    refrigerator = list(map(int, input().split()))
    microwave = list(map(int, input().split()))
    tickets = list()
    for i in range(m):
        tickets.append(list(map(int, input().split())))

    ans = min(refrigerator) + min(microwave)

    for i in range(m):
        t = tickets[i]
        tmp = refrigerator[t[0]-1] + microwave[t[1]-1] - t[2]
        if tmp < ans:
            ans = tmp

    print(ans)

Submission status: Accepted

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

def solve(nA: int, nB: int, M: int, A: "List[int]", B: "List[int]", x: "List[int]", y: "List[int]", c: "List[int]"):

    cheapest = -1 


    for m in range(M):
        price = A[x[m]-1] + B[y[m]-1] - c[m] 
        if cheapest == -1 or cheapest >= price:
            cheapest = price

    if cheapest >= min(A)+min(B):
        cheapest = min(A)+min(B)

    print(cheapest)
    
    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:
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

a,b,m = mi()
av = li()
bv = li()

avs = av.copy()
bvs = bv.copy()
avs.sort()
bvs.sort()

ans = avs[0] + bvs[0]

for i in range(m):
    q = li()
    tmp = av[q[0]-1] + bv[q[1]-1] - q[2]
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
A, B, m = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
ans = min(a[1:]) + min(b[1:])
for _ in range(m):
    x, y, c = map(int, input().split())
    ans = min(ans, a[x] + b[y] - c)
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())
    if A[x-1]+B[y-1]-c <ans:
        ans=A[x-1]+B[y-1]-c 
print(ans)

Submission status: Accepted

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

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

alist = list(map(int, input().split()))
blist = list(map(int, input().split()))

ans = min(alist) + min(blist)

for i in range(m):
    x, y, c = map(int, input().split())
    x -= 1  # リストは0番目から始まるため、合わせる
    y -= 1  # リストは0番目から始まるため、合わせる
    if alist[x] + blist[y] - c < ans:
        ans = alist[x] + blist[y] - c
    else:
        continue

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def print_matrix(mat):
    for i in range(len(mat)):
        print(*mat)


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YNL = {False: 'No', True: 'Yes'}
YNU = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    A, B, M = MI()
    a = LI()
    b = LI()
    x = [0] * M
    y = [0] * M
    c = [0] * M
    ans = 10**9
    # min_x_i = 10**9
    # min_y_i = 10**9
    # min_x = 10**9
    # min_y = 10**9

    for i in range(M):
        x[i], y[i], c[i] = MI()

    for i in range(M):
        A = a[x[i]-1]
        B = b[y[i]-1]
        ans = min(ans, A + B - c[i])
        # if min_x > x[i]:
        #     min_x = x[i]
        #     min_x_i = i

        # if min_y > y[i]:
        #     min_y = y[i]
        #     min_y_i = i

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

    print(ans)


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()))
ans = min(a) + min(b)
# print(ans)
for i in range(M):
  x, y, c = map(int, input().split())
  discount = a[(x-1)] + b[(y-1)] - c
  if (discount < ans):
    ans = discount
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
a,b,m  = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]
M = []
for i in range(m):
    M.append([int(x) for x in stdin.readline().rstrip().split()])
t = []
for i in range(m):
    t.append(A[M[i][0]-1] + B[M[i][1]-1] - M[i][2])
t = min(t)
k = min(A) + min(B)
print(min(t,k))

Submission status: Accepted

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

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]"):
    # sort_a = sorted(enumerate(a),key=lambda x: x[1])
    MINB = min(b)
    # print(sort_a)
    waribiki = [[] for _ in range(A)]
    for i in range(M):
        waribiki[x[i]-1].append((y[i]-1,c[i]))
    
    answer = 10**18

    for i in range(A):
        aa = a[i]
        for b_idx,cc in waribiki[i]:
            answer = min(aa+b[b_idx]-cc,answer)
        
        answer = min(aa+MINB,answer)
    print(answer)
    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))  # 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 = min(a) + min(b)

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

print(c)

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

ans = 10**9
for i in range(M):
    x,y,c = [int(i) for i in input().split()]
    if ans > a[x-1] + b[y-1] - c:
        ans = a[x-1] + b[y-1] - c
if ans > min(a) + min(b):
    ans = min(a) + min(b)
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()))
XYC = [list(map(int,input().split())) for i in range(m)]

kouho = (min(A)+min(B))
for i in range(m):
    kouho = min(kouho,A[XYC[i][0]-1] + B[XYC[i][1]-1] - XYC[i][2])

print(kouho)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a,b,m = map(int,input().split())
ai = list(map(int,input().split()))
bi = list(map(int,input().split()))
cheap = min(ai)+min(bi)
for i in range(m):
    x,y,c = map(int,input().split())
    cheap = min(ai[x-1]+bi[y-1]-c,cheap)
print(cheap)

Submission status: Accepted

Solution code:
def main():
    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()]

    ans = min(A) + min(B)
    for mi in range(m):
        x, y, c = [int(e) for e in input().split()]
        ans = min(A[x - 1] + B[y - 1] - c, ans)

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

result = 10 ** 10
DisList = []
for i in range(M):
    tmpList = list(map(int, input().split()))
    if result > AList[tmpList[0] - 1] + BList[tmpList[1] - 1] - tmpList[2]:
        result = AList[tmpList[0] - 1] + BList[tmpList[1] - 1] - tmpList[2]

AList.sort()
BList.sort()

if result > AList[0] + BList[0]:
    result = AList[0] + BList[0]

print(result)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
Ms = [input().split() for _ in range(M)]


ans = min(As) + min(Bs)
for m in Ms:
  a = int(m[0])-1
  b = int(m[1])-1
  rate = int(m[2])
  price = int(As[a])+int(Bs[b])-rate
  if price < ans:
    ans = price
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()))

min = min(al) + min(bl)
for i in range(m):
    x, y, c = map(int, input().split())
    onedan = al[x-1] + bl[y-1] - c
    if min > onedan:
        min = onedan

print(min)

Submission status: Accepted

Solution code:
def main():
	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 _ in range(m):
		x,y,c = map(int,input().split())
		min_cost = min(min_cost, a[x-1]+b[y-1]-c)
	print(min_cost)

main()