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:
# import bisect
# from collections import Counter, defaultdict, deque, namedtuple
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

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

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

    xyc = [0]* m

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

    ans = min(a_list) + min(b_list)

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

    print(ans)



if __name__ == '__main__':
    main()

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=min(a)+min(b)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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


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

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

print(C)

Submission status: Accepted

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

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

print(min_cost)

Submission status: Accepted

Solution code:
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
Z=[list(map(int,input().split())) for i in range(M)]
ans=2*10**5
for s,t,u in Z:
  d=a[s-1]+b[t-1]-u
  ans=min(ans,d)
e=min(a)+min(b)
print(min(ans,e))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

doTest=0

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

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

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

    minAB = min(ap) + min(bp)

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

    print(minAB)

    return

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

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

Submission status: Accepted

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

ans = min(l_a)+min(l_b)

for i in range(m):
    tmp_a,tmp_b,tmp_c = map(int,input().split())
    tmp = l_a[tmp_a-1] + l_b[tmp_b-1] - tmp_c
    if tmp < ans:
        ans = tmp
    
print(ans)

Submission status: Accepted

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

min_price = min(a) + min(b)

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

print(min_price)

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

Submission status: Accepted

Solution code:
# import sys
# import math
# import decimal
# import queue
# import bisect
# import heapq
# import time
# import itertools
# import collections # queue
# from fractions import Fraction

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

def main():
    a,b,m = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    ans = min(a) + min(b)
    for i in range(m):
        x,y,c = map(int,input().split())
        res = a[x-1] + b[y-1] - c
        ans = min(ans,res)
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Accepted

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

minsum = min(ax)+min(bx)

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

print(minsum)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
a, b, m = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
min_cost = min(a_list) + min(b_list)
for i in range(0, m):
    ai, bi, c = map(int, input().split())
    if a_list[ai-1] + b_list[bi-1] - c < min_cost:
        min_cost = a_list[ai-1] + b_list[bi-1] - c
print(min_cost)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

print(mi)

Submission status: Accepted

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

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

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)]
#x番目の冷蔵庫、y番目のレンジを買うとc円割引される

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

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

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:
def solve(a, b, m, a_list, b_list, xyc_list):
    res = min(a_list) + min(b_list)
    for x, y, c in xyc_list:
        r = a_list[x - 1] + b_list[y - 1] - c
        res = min(res, r)
    return res


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


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


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

Submission status: Accepted

Solution code:
import sys
import math
import collections
from collections import deque
#n = int(input())
a, b, m = map(int, sys.stdin.readline().split())
#s = input()
#x = sys.stdin.readline().split()

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

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

print(ans)

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

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')
X = []
Y = []
for _ in range(M):
    x, y, c = map(int, input().split())
    ans = min(a[x - 1] + b[y - 1] - c, ans)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

can = []

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

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

print(min(can))

Submission status: Accepted

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

Submission status: Accepted

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

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

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 = 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:
# -*- coding: utf-8 -*-


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

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

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

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

set_lis = []

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

set_lis.sort()

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

Submission status: Accepted

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


min_price = min(a) + min(b)

#チケット処理
for i in range(M):
    low = a[t[i][0]-1] + b[t[i][1]-1] - t[i][2]
    if low < min_price:
        min_price = low
print(min_price)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans=float("inf")

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

Submission status: Accepted

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

ans=min(a1)+min(b1)

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

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

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

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

Submission status: Accepted

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

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

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


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

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

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_ab = min(a) + min(b)

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)

import math

import logging

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


def resolve():
    # S = [x for x in sys.stdin.readline().split()][0]  # 文字列 一つ
    # N = [int(x) for x in sys.stdin.readline().split()][0]  # int 一つ
    A, B, M = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    a_list = [int(x) for x in sys.stdin.readline().split()]  # 複数int
    b_list = [int(x) for x in sys.stdin.readline().split()]  # 複数int

    # grid = [list(sys.stdin.readline().split()[0]) for _ in range(N)]  # 文字列grid
    # v_list = [int(sys.stdin.readline().split()[0]) for _ in range(N)]
    grid = [[int(x) for x in sys.stdin.readline().split()]
            for _ in range(M)]  # int grid

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

    _min = min(a_list) + min(b_list)

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

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

    print(_min)


if __name__ == "__main__":
    resolve()

# AtCoder Unit Test で自動生成できる, 最後のunittest.main は消す
# python -m unittest template/template.py で実行できる
# pypy3 -m unittest template/template.py で実行できる

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)

Submission status: Accepted

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


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


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


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

Submission status: Accepted

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

ans = min(a) + min(b)

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

print(ans)

Submission status: Accepted

Solution code:
#!python3.4
# -*- coding: utf-8 -*-
# hitachi2020/hitachi2020_b
import sys

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

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

main()

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

AS = list(map(int, input().split()))
BS = list(map(int, input().split()))

xyc = [[int(x) for x in input().split()] for _ in range(M)]

ans = min(AS) + min(BS)

for x, y, c in xyc:
    r = AS[x - 1] + BS[y - 1] - c
    ans = min(ans, r)

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

P = [[int(i) for i in input().split()] for _ in range(M)]

ans = min(a) + min(b)

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

cnt = min(A)+min(B)
for i in range(m):
    x, y, d = map(int, input().split())
    cnt = min(cnt, A[x-1] + B[y-1] - d)
print(cnt)

Submission status: Accepted

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = list(map(int, input().split()))
    #a = [input().split() for i in range(n)]
    #a = [int(input()) for i in range(n)]
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0

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

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


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, y, c = map(int, input().split())

    price = a[x-1] + b[y-1] - c

    ans = min(price, ans)

print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    from sys import stdin
    input = stdin.readline

    a, b, m = map(int, input().split())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    min_ab = min(aa) + min(bb)
    for _ in range(m):
        x, y, c = map(int, input().split())
        now = aa[x-1] + bb[y-1] - c
        if min_ab > now:
            min_ab = now
    print(min_ab)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a,b,m=map(int,input().split())
la =list(map(int,input().split()))
lb =list(map(int,input().split()))
ans = min(la)+min(lb)
for i in range(m):
  aa,bb,cc =map(int,input().split())
  ans =min(ans,la[aa-1]+lb[bb-1]-cc)
print(ans)

Submission status: Accepted

Solution code:
a, b, m = [int(v) for v in input().split()]
alist = [int(v) for v in input().split()]
blist = [int(v) for v in input().split()]
mlist = [tuple(int(v) for v in input().split()) for _ in range(m)]
mlist = [(ai - 1, bi - 1, c) for ai, bi, c in mlist]

cost = min(alist) + min(blist)
for x, y, c in mlist:
    lc = alist[x] + blist[y] - c
    if lc < cost:
        cost = lc

print(cost)

Submission status: Accepted

Solution code:
a, b, m = map(int, input().split())
data = list(map(int, input().split()))
datb = list(map(int, input().split()))
res = 999999999999
for i in range(m):
    x, y, c = map(int, input().split())
    t = data[x - 1] + datb [y - 1] - c
    res = min(res, t)
data.sort()
datb.sort()
res = min(res, data[0] + datb[0])
print(res)

Submission status: Accepted

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

ans=[]

for i in range(m):
    x,y,c=map(int,input().split())
    x-=1
    y-=1
    ans.append(a[x]+b[y]-c)
ans.append(min(a)+min(b))
print(min(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 = float('inf')

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

nouse = min(a) + min(b)
ans = []
ans.append(nouse)
for x in X:
    re, de, c = x
    ans.append(a[re-1] + b[de-1] - c)

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))
ans = min(a_li) + min(b_li)
for i in range(M):
    x,y,c = map(int,input().split())
    ans = min(ans, a_li[x-1] + b_li[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()))
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:
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
A, B, M = [int(x) for x in input().strip().split()]
a = [int(x) for x in input().strip().split()]
b = [int(x) for x in input().strip().split()]
ans = min(a) + min(b)
for m in range(M):
    x, y, c = [int(x) for x in input().strip().split()]
    ans = min(ans, a[x-1]+b[y-1]-c)

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**9)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

a,b,m = na()

aa = na()
bb = na()

ans = min(aa)+min(bb)

for i in range(m):
    x,y,c = na()
    ans = min(ans,aa[x-1]+bb[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, Y, C = [], [], []
for _ in range(m):
    x, y, c = map(int, input().split())
    X.append(x)
    Y.append(y)
    C.append(c)
 
ans = min(A) + min(B)
for i in range(m):
    tot = A[X[i]-1] +B[Y[i]-1] - C[i]
    ans = min(tot, ans)
print(ans)

Submission status: Accepted

Solution code:
a,b,m = map(int,input().split())
l = list(map(int,input().split()))
d = list(map(int,input().split()))
ans = min(l)+min(d)
for i in range(m):
  x,y,c = map(int,input().split())
  p = l[x-1]+d[y-1]-c
  if p < ans:
    ans = p
print(ans)

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

Submission status: Accepted

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

ans = min(a) + min(b)

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    a,b,m = map(int, input().split())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))
    xycs = [list(map(int, input().split())) for i in range(m)]

    #min(min(As) + min(Bs), nin)
    nin = 10**9
    for i in range(m):
        if As[xycs[i][0]-1] + Bs[xycs[i][1]-1] - xycs[i][2] <= nin:
            nin = As[xycs[i][0]-1] + Bs[xycs[i][1]-1] - xycs[i][2]
    
    print(min(min(As) + min(Bs), nin))
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

A, B, M = lr()
R = lr()
D = lr()
answer = min(R) + min(D)
for _ in range(M):
    x, y, c = lr()
    x -= 1; y -= 1
    temp = R[x] + D[y] - c
    if temp < answer:
        answer = temp

print(answer)