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:
A, B, M = map(int, input().split())

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

#print(alist, blist)

mymin = (min(alist)+min(blist))
for m in range(M):
    x, y, c = map(int, input().split())
    if alist[x-1] + blist[y-1] - c < mymin:
        mymin = alist[x-1] + blist[y-1] - c
print(mymin)

Submission status: Accepted

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

a_prices = [int(i) for i in input().split()]
b_prices = [int(i) for i in input().split()]

min_price = min(a_prices) + min(b_prices)

for _ in range(m):
    x, y, c = map(int, input().split())
    x -= 1
    y -= 1
    min_price = min(min_price, a_prices[x] + b_prices[y] - c)
print(min_price)

Submission status: Accepted

Solution code:
lena, lenb, lenm = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
m = []
m.append(min(a) + min(b))
for i in range(lenm):
    x, y, c = [int(i) for i in input().split()]
    m.append(a[x - 1] + b[y - 1] - c)

print(min(m))

Submission status: Accepted

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

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

for x in l:
  arr.append(a[x[0]-1]+b[x[1]-1]-x[2])
print(min(arr))

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

A, B, M = LI()
a = LI()
b = LI()
xyc = [[] for i in range(M)]
for i in range(M):
    x, y, c = LI()
    x -= 1
    y -= 1
    xyc[i] = [x, y, c]

ans = mod
for i in range(M):
    x, y, c = xyc[i]
    ans = min(ans, a[x] + b[y] - c)

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from math import floor

def main():
    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)
    a.sort()
    b.sort()
    ans=min(ans,a[0]+b[0])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
A, B, M = map(int, input().split())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]
xyc = [[0] * 3 for i in range(M)]
for i in range(M):
    x, y, c = map(int, input().split())
    xyc[i][0], xyc[i][1], xyc[i][2] = x, y, c
ans = min(a) + min(b)
for i in range(M):
    ans = min(ans, a[xyc[i][0]-1]+b[xyc[i][1]-1]-xyc[i][2])
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 _ in range(M)]

ans = min(a) + min(b)
for i in range(M):
    x, y, c = xyc[i]
    x -= 1
    y -= 1
    cost = a[x] + b[y] - c
    ans = min(ans, cost)
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()))
li = []
val = []
c = sorted(A)
d = sorted(B)
minv = c[0]+d[0]
for i in range(m):
    x,y,c = map(int,input().split())
    li.append([x,y,c])
    val.append(A[x-1]+B[y-1]-c)
ans = min(min(val),minv)
print(ans)

Submission status: Accepted

Solution code:
A, B, M = map(int, input().rstrip().split(' '))
a = list(map(int, input().rstrip().split(' ')))
b = list(map(int, input().rstrip().split(' ')))
coupon = []

for i in range(M):
  x, y, c = map(int, input().rstrip().split(' '))
  temp = a[x-1]+b[y-1]-c
  coupon.append(temp)

min_price = min(coupon)

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

ans = min(a) + min(b)

for x, y, c in xyc:
    d = a[x-1] + b[y-1] - c
    if ans > d:
        ans = d

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

saisho1 = min(a) + min(b)

kouho = [0 for i in range(M)]

for i in range(M):
  kouho[i] = a[xyc[i][0]-1] + b[xyc[i][1]-1] - xyc[i][2]

saisho2 = min(kouho)

print(min(saisho1, saisho2))

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

Submission status: Accepted

Solution code:
A, B ,M =map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = [list(map(int, input().split())) for k in range(M)]
ans = min(a) + min(b)
for k in range(M):
    cos = a[m[k][0]-1] + b[m[k][1]-1] - m[k][2]
    if ans > cos:
        ans = cos

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
a,b,m = map(int,input().split())
As = list(map(int,input().split()))
Bs = list(map(int,input().split()))
amin = min(As)
bmin = min(Bs)

xycs = []
comb = []
for i in range(m):
    x, y, c = map(int, input().split())
    comb.append(As[x-1] + Bs[y-1] - c)

print(min(amin+bmin, min(comb)))

Submission status: Accepted

Solution code:
A, B, m = list(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)]

anses = [min(a) + min(b)]

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

print(min(anses))

Submission status: Accepted

Solution code:
a,b,m=map(int, input().split())
al=list(map(int, input().split()))
bl=list(map(int, input().split()))
tmp=[]
tmp.append(min(al)+min(bl))
for i in range(m):
    x,y,c=map(int, input().split())
    tmp.append(al[x-1]+bl[y-1]-c)
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()))
xyc = [list(map(int, input().split())) for _ in range(M)]

m = min(a) + min(b)

M = float('inf')
for x,y,c in xyc:
    x -= 1
    y -= 1
    tmp = a[x] + b[y] - c
    M = min(M,tmp)

if m>M:
    print(M)
else:
    print(m)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
a,b,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a_idx = a.index(min(a))
b_idx = b.index(min(b))
#print(a_idx,b_idx)

ans = min(a)+min(b)
for i in range(m):
    x,y,c = map(int,input().split())
    if x-1 != a_idx or y-1 != b_idx:
        ans = min(ans,a[x-1]+b[y-1]-c)
    if x-1 == a_idx and y-1 == b_idx:
        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()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = min(a)+min(b)  # 通常価格の最小値


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

print(ans)

Submission status: Accepted

Solution code:
import sys

A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
IN = sys.stdin.readlines()

ans = min(a) + min(b)

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

print(ans)

Submission status: Accepted

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

if A > 1:
    F = [int(i) for i in input().split()]
else:
    F = [int(input())]
if B > 1:
    D = [int(i) for i in input().split()]
else:
    D = [int(input())]


i = F.index(min(F))
j = D.index(min(D))
price = F[i] + D[j]

for i in range(M):
    xi, yi, ci = map(int, input().split())
    price_iter = F[int(xi)-1] + D[int(yi)-1] - ci
    if price > price_iter:
        i = xi
        j = yi
        price = price_iter

print(price)

Submission status: Accepted

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

aas = input().split()
aas = [int(a) for a in aas]
as2 = sorted(aas)

bbs = input().split()
bbs = [int(b) for b in bbs]
bs2 = sorted(bbs)
mini = bs2[0] + as2[0]
xycs = []
for i in range(M):
  xyc = input().split()
  xycs.append([int(xyc[0]), int(xyc[1]), int(xyc[2])])
  
for xyc in xycs:
  a = aas[xyc[0]-1]
  b = bbs[xyc[1]-1]
  c = xyc[2]
  if a + b - c < mini:
    mini = a+b-c
print(mini)

Submission status: Accepted

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

ans = mina + minb

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

print(ans)

Submission status: Accepted

Solution code:
a,b,m = map(int,input().split())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
z = []
for _ in range(m):
    z.append(list(map(int,input().split())))
ans = min(x)+min(y)
for d in z:
    tmp = x[d[0]-1]+y[d[1]-1]-d[2]
    ans = min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
a, b, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = [0] * m
y = [0] * m
c = [0] * m
minval = int(1e10)
for i in range(m):
    x[i], y[i], c[i] = map(int, input().split())
    val = a[x[i]-1] + b[y[i]-1] - c[i]
    if minval > val:
        minval = val
print(min(min(a) + min(b), minval))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_map_as_int(H):
    '''
    #→1,.→0として読み込む
    '''
    ret = []
    for _ in range(H):
        ret.append([1 if s == '#' else 0 for s in read()[:-1]])
        # 内包表記はpypyでは若干遅いことに注意
        # #numpy使うだろうからこれを残しておくけど
    return ret


# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from fractions import gcd


def lcm(a, b):
    # 最小公約数
    g = gcd(a, b)
    return a * b // g


A, B, M = read_ints()
a = read_ints()
b = read_ints()

ans = 10 ** 9
for _ in range(M):
    x, y, c = read_ints()
    nedan = a[x - 1] + b[y - 1] - c
    ans = min(ans, nedan)

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

Submission status: Accepted

Solution code:
inf = 10 ** 19    # 1,000,000,000,000,000,000

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

    ans = inf

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

    a_min = min(a_list)
    b_min = min(b_list)
        
    ans = min(ans, a_min+b_min)

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

Submission status: Accepted

Solution code:
# coding: utf-8

a, b, m = map(int, raw_input().split())
al = map(int, raw_input().split())
bl = map(int, raw_input().split())
mls = [map(int, raw_input().split()) for _ in xrange(m)]

minp = min(al) + min(bl)

for xi, yi, ci in mls:
    p = al[xi - 1] + bl[yi - 1] - ci
    minp = min(minp, p)

print minp

Submission status: Accepted

Solution code:
import numpy as np

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

an = np.asarray(input().split(), dtype=np.uint32)

bn = np.asarray(input().split(), dtype=np.uint32)

minimum = np.min(an) + np.min(bn)

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

print(minimum)

Submission status: Accepted

Solution code:
na, nb, nm = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a_min, b_min = min(A), min(B)
min_tmp = a_min + b_min

aibjc = []
for _ in range(nm):
  ai, bj, c = list(map(int, input().split()))
  min_tmp = min(min_tmp, A[ai-1]+B[bj-1]-c)

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

print(min([min(a)+min(b),min(xyc)]))

Submission status: Accepted

Solution code:
a,b,m=map(int,input().split())
als=list(map(int,input().split()))
bls=list(map(int,input().split()))
mls=[list(map(int,input().split())) for i in range(m)]

ans=min(als)+min(bls)
for i in mls:
    aidx = i[0]-1
    bidx = i[1]-1
    cost = als[aidx]+bls[bidx]-i[2]
    if cost < ans:
        ans = cost
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_a = min(a)
min_b = min(b)

min_ab = min_a + min_b

min_wari = 10000000

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

if min_ab > min_wari:
    print(min_wari)
else:
    print(min_ab)

Submission status: Accepted

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

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

Submission status: Accepted

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

cand1 = min(A) + min(B) # Without qupon
A_with_qupons = A[:]
B_with_qupons = B[:]
prices_with_qpons = [A[q[0]-1] + B[q[1]-1] - q[2] for q in qupons]
cand2 = min(prices_with_qpons) # With qupon
print(min([cand1, cand2]))

Submission status: Accepted

Solution code:
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    A, B, M = lr()
    a = lr()
    b = lr()
    ans = 10**6
    for i in range(M):
        x, y, c = lr()
        ans = min(ans, a[x-1]+b[y-1]-c)
    ans = min(ans, sorted(a)[0]+sorted(b)[0])
    print(ans)
resolve()

Submission status: Accepted

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

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

minimum = min(a) + min(b)

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

Submission status: Accepted

Solution code:
def main():
    A, B, M = map(int, input().split(' '))
    a_list = list(map(int, input().split(' ')))
    b_list = list(map(int, input().split(' ')))
    result = min(a_list) + min(b_list)
    for _ in range(M):
        x, y, c = map(int, input().split(' '))
        if result > a_list[x - 1] + b_list[y - 1] - c:
            result = a_list[x - 1] + b_list[y - 1] - c
    print(result)


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

Submission status: Accepted

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

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()]
mina, minb = min(a), min(b)
minv = mina+minb
for _ in range(M):
    x, y, c = [int(i) for i in input().split()]
    x -= 1
    y -= 1
    minv = min(minv, a[x]+b[y]-c)


print(minv)

Submission status: Accepted

Solution code:
a,b,m = map(int,input().split())        
a1 = list(map(int,input().split()))
b1 = list(map(int,input().split()))
ab = []
for _ in range(m):
    t = list(map(int,input().split()))
    ab.append(t)

ans = min(a1)+min(b1)

for i in ab:
    #print(a1[i[0]-1]+b1[i[1]-1]-i[2])
    ans = min(ans, a1[i[0]-1]+b1[i[1]-1]-i[2])
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations
    #from itertools import accumulate, product
    from bisect import bisect_left,bisect_right
    from math import floor, ceil
    #from operator import itemgetter

    #mod = 1000000007

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a,b,m = map(int,input().split())
a_L = list(map(int,input().split()))
b_L = list(map(int,input().split()))
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)

Submission status: Accepted

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

ia = [int(s) for s in input().split()]
ib = [int(s) for s in input().split()]

mina = min(ia)
minb = min(ib)
mv = mina + minb

for x in range(m):
  vouch = [int(s) for s in input().split()]
  if mv >= ia[vouch[0]-1] + ib[vouch[1]-1] - vouch[2]:
    mv = ia[vouch[0]-1] + ib[vouch[1]-1] - vouch[2]

print(mv)

Submission status: Accepted

Solution code:
def solve():
    A, B, M = [int(x) for x in input().split(" ")]
    Ai = [int(x) for x in input().split(" ")]
    Bi = [int(x) for x in input().split(" ")]

    min_val = min(Ai) + min(Bi)

    for _ in range(M):
        x, y, c = input().split(" ")
        x = int(x) - 1
        y = int(y) - 1
        c = int(c)
        min_val = min(Ai[x] + Bi[y] - c, min_val)
    print(min_val)


solve()

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
F = list(map(int,input().split()))
F.insert(0,10**6)
G = list(map(int,input().split()))
G.insert(0,10**6)
cmin = 10**6
for _ in range(M):
    x,y,c = map(int,input().split())
    cnt = F[x]+G[y]-c
    cmin = min(cmin,cnt)
F = sorted(F)
G = sorted(G)
cmin = min(cmin,F[0]+G[0])
print(cmin)

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
Ali = list(map(int,input().split()))
Bli = list(map(int,input().split()))
C=[]
while True:
    try:
        C.append(list(map(int,input().split())))

    except:
        break;

notuse = min(Ali) + min(Bli)
minuse =10000000
for i in range(M):
    minuse = min(Ali[C[i][0]-1] +Bli[C[i][1]-1]-C[i][2],minuse)
print(min(minuse,notuse))

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
ai = list(map(int,input().split()))
bi = list(map(int,input().split()))
amin = min(ai)
bmin = min(bi)
ans = amin+bmin
for num in range(M):
    xi,yi,ci=map(int,input().split())
    a = ai[xi-1]
    b = bi[yi-1]
    new = a+b-ci
    if ans>new:
        ans=new
print(ans)

Submission status: Accepted

Solution code:
# Code for B - Nice Shopping
# Use input() to fetch data from STDIN
[A, B, M] = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

x = [0 for i in range(M)]
y = [0 for i in range(M)]
c = [0 for i in range(M)]
for i in range(M):
    [x[i], y[i], c[i]] = map(int, input().split())

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

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

dis = [[int(i) for i in input().split(' ')] for j in range(M)]

ls = [min(a)+min(b)]

for d in dis:
  ans = a[d[0]-1] + b[d[1]-1] - d[2]
  ls.append(ans)
  
print(min(ls))

Submission status: Accepted

Solution code:
def main():
    _, _, m_num = map(int, input().split())
    a = [int(s) for s in input().split()]
    b = [int(s) for s in input().split()]

    min_price = min(a) + min(b)

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

main()

Submission status: Accepted

Solution code:
A,B,M=map(int,input().split())
C=list(map(int,input().split()))
D=list(map(int,input().split()))
a=10**5
b=10**5
for i in range(A):
    if(C[i]<a):
        a=C[i]
for i in range(B):
    if(D[i]<b):
        b=D[i]
k=a+b
for i in range(M):
    x,y,c=map(int,input().split())
    if((C[x-1]+D[y-1]-c)<k):
        k=C[x-1]+D[y-1]-c
print(k)

Submission status: Accepted

Solution code:
#python3
INF = int(1e6)
def main():
    a, b, m = map(int, input().split())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    cp = []
    ans1 = INF
    for _ in range(m):
        x, y, c = map(int, input().split())
        x -= 1
        y -= 1
        ans1 = min(a[x]+b[y]-c, ans1)
    a_cost = min(a)
    b_cost = min(b)
    ans2 = a_cost + b_cost
    ans = min(ans1, ans2) 
    print(ans)
main()

Submission status: Accepted

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

A,B,M = map(int,input().split(' '))
l_a = list(map(int, input().split()))
l_b = list(map(int, input().split()))
l_c = [list(map(int,input().split())) for _ in range(M)]

min_a = min(l_a)
min_b = min(l_b)

MIN = min_a+min_b

for i in range(M):
    k = l_c[i]
    x = k[0]
    y = k[1]
    c = k[2]
    m = l_a[x-1]+l_b[y-1]-c
    MIN = min(MIN,m)
print(MIN)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
import sys
sys.setrecursionlimit(200000000)


# my functions here!

#入力

def pin(type=int):
    return map(type,input().rstrip().split())

def resolve():
    A,B,M=pin()
    a=list(pin())
    b=list(pin())
    ans=min(a)+min(b)
    
    for i in range(M):
       x,y,z=pin()
       ans=min(ans,a[x-1]+b[y-1]-z)
    print(ans)
    #print([["NA","YYMM"],["MMYY","AMBIGUOUS"]][cMMYY][cYYMM])
#if __name__=="__main__":resolve()

"""
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
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)
if __name__=="__main__":resolve()

Submission status: Accepted

Solution code:
A_ref, B_range, M_waribiki = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

price = []
mini = min(A) + min(B)

price.append(mini)

for i in range(M_waribiki):
    x, y, redu = map(int, input().split())
    x -= 1
    y -= 1
    
    summ = A[x] + B[y] - redu
    
    price.append(summ)

print(min(price))

Submission status: Accepted

Solution code:
d = input().split(" ")
la = int(d[0])
lb = int(d[1])
lm = int(d[2])
a = input().split(" ")
mina = 100000
for i in range(la):
	a[i] = int(a[i])
	if mina > a[i]:
		mina = a[i]
b = input().split(" ")
minb = 100000
for i in range(lb):
	b[i] = int(b[i])
	if minb > b[i]:
		minb = b[i]
minab = mina + minb
for i in range(lm):
	d = input().split(" ")
	c = a[int(d[0])-1] + b[int(d[1])-1] - int(d[2])
	if c < minab:
		minab = c
print(minab)

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)]
ans = min(a)+min(b)
for i in range(m):
    price_a = a[xyc[i][0]-1]
    price_b = b[xyc[i][1]-1]
    c = xyc[i][2]
    ans = min(ans,price_a+price_b-c)
print(ans)

Submission status: Accepted

Solution code:

def main():
    a,b,m = map(int,input().split())
    reizouko = list(map(int,input().split()))
    mn_reizouko = min(reizouko)
    renji = list(map(int,input().split()))
    mn_renji = min(renji)
    mn = 20 ** 5 + 1
    for i in range(m):
        x,y,c = map(int,input().split())
        mn = min(mn,reizouko[x-1]+renji[y-1]-c)
    mn = min(mn,mn_reizouko+mn_renji)

    print(mn)




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) 
for i in range(m) :
    X = list(map(int,input().split()))
    y = A[X[0]-1] + B[X[1]-1] - X[2]
    if y <= ans :
        ans = y
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 _ in range(M)]

ans = min(a) + min(b)

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

print(ans)

Submission status: Accepted

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

a_m = min(al)
b_m = min(bl)
ans = a_m + b_m
for  _  in range(m):
  x, y, c = map(int, input().split())
  t_ans = al[x-1] + bl[y-1] -c
  if t_ans < ans:
    ans = t_ans
print(ans)

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
lis = []
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:
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(" ")]

min=min(a)+min(b)

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

print(min)