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: Time Limit Exceeded

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

ans = 10**6
for i 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

for j in range(len(a)):
    for k in range(len(b)):
        if(a[j]+b[k] < ans):
            ans = a[j]+b[k]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

A, B, M = [int(x) for x in sys.stdin.readline().strip().split(" ")]
list_a = [int(x) for x in sys.stdin.readline().strip().split(" ")]
list_b = [int(x) for x in sys.stdin.readline().strip().split(" ")]
dict_c = {}
for i in range(M):
    x, y, c = [int(x) for x in sys.stdin.readline().strip().split(" ")]
    if x not in dict_c:
        dict_c[x] = {}
    dict_c[x][y] = max(dict_c[x].get(y, 0), c)

s = 1e5
for i, a in enumerate(list_a):
    for j, b in enumerate(list_b):
        t = a + b - dict_c.get(i + 1, {}).get(j + 1, 0)
        if s > t:
            s = t
print(s)

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, Counter, deque

NA, NB, NM = list(map(int, input().split()))

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

coupons = defaultdict(list)
for i in range(NM):
    x, y, c = list(map(int, input().split()))
    coupons[(x, y)].append(c)

best = float('inf')
for i, a in enumerate(A):
    for j, b in enumerate(B):
        cost = a + b
        if (i + 1, j + 1) in coupons:
            cost -= max(coupons[(i + 1, j + 1)])
        best = min(cost, best)
print(best)

Submission status: Time Limit Exceeded

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

a, b, m = MAP()
A = LIST()
B = LIST()
L = [LIST() for i in range(m)]

ans = []
for i in range(a):
    for j in range(b):
        ans.append(A[i] + B[i])

for x, y, c in L:
    ans.append(A[x-1] + B[y-1] - c)
print(min(ans))



exit()
s = input()
m= "hi"
# for i in range():
l = len(s) // 2
if l * m == s:
    print("Yes")
else:
    print("No")

# if s // 2

Submission status: Accepted

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

# cheapest price without using discount ticket
min_price = min(a) + min(b)

for i in range(m):
    arr = list(map(int, input().split()))
    discounted_price = a[arr[0] - 1] + b[arr[1] - 1] - arr[2]
    min_price = min(min_price, discounted_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()))
ans=10**6
for i in range(m):
  x,y,c=map(int,input().split())
  ans=min(ans,a[x-1]+b[y-1]-c)
print(min(ans,min(a)+min(b)))

Submission status: Accepted

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

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]"):
    ans = min(a) + min(b)
    for i in range(M):
        xi = x[i]
        yi = y[i]
        ci = c[i]
        t = a[xi-1] + b[yi-1] - ci
        if ans >= t:
            ans = t
    print(ans)
    return


# Generated by 1.1.7.1 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:
A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
price_list = []

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


a.sort()
b.sort()
price_list.append(a[0]+b[0])
min_price = price_list[0]

for n in price_list:
  if min_price >= n:
    min_price = n
  elif min_price < n:
    pass


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 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())
a_list = [int(x.strip()) for x in input().split()]
b_list = [int(x.strip()) for x in input().split()]
ans = min(a_list)+min(b_list)
for i in range(m):
  ai,bi,ci = map(int,input().split())
  ch = a_list[ai-1]+b_list[bi-1]-ci
  if ch < ans:
    ans = ch
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()))

mini = min(a) + min(b)
for i in range(M):
    x,y,c = map(int,input().split())

    rei = a[x-1]
    den = b[y-1]
    coop = c

    if rei + den - coop < mini:
        mini = rei + den - coop

print(mini)

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,Z = map(int,input().split())
    x.append(X)
    y.append(Y)
    c.append(Z)

mini = min(a) + min(b)
for i in range(M):

    rei = a[x[i]-1]
    den = b[y[i]-1]
    coop = c[i]

    if rei + den - coop < mini:
        mini = rei + den - coop

print(mini)

Submission status: Accepted

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

cst = min(ai) + min(bi)

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

print(cst)

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

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

    ans = 200001

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

    amin = min(a)
    bmin = min(b)

    if ans > amin + bmin:
        ans = amin + bmin

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
from numpy import array
input = lambda: stdin.readline().rstrip()
na, nb, m = map(int, input().split())
a = array(list(map(int, input().split())))
b = array(list(map(int, input().split())))
ans = min(a) + min(b)
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:
from sys import stdin
input = lambda: stdin.readline().rstrip()
na, nb, 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())
    x -= 1
    y -= 1
    ans = min(ans, a[x] + b[y] - c)
print(ans)

Submission status: Accepted

Solution code:
na, nb, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
min_a = min_b = 10**9
for i in range(na):
    min_a = min(min_a, a[i])
for i in range(nb):
    min_b = min(min_b, b[i])
ans = min_a + min_b
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:
from sys import stdin
input = lambda: stdin.readline().rstrip()
na, nb, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
min_a = min_b = 10**9
for i in range(na):
    min_a = min(min_a, a[i])
for i in range(nb):
    min_b = min(min_b, b[i])
ans = min_a + min_b
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())
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):
  xi,yi,ci=map(int,input().split())
  ans.append(l_a[xi-1]+l_b[yi-1]-ci)
print(min(ans))

Submission status: Accepted

Solution code:
from sys import exit
import math
import collections
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

A,B,M = mi()
a = li()
b = li()
xyc = [li() for i in range(M)]

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

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline
import heapq

def main():
    A=[]
    B=[]
    M=[]
    values=[]
    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 _ in range(m)]

# #割引した組み合わせ
    for m in M:
        values.append(A[m[0]-1]+B[m[1]-1]-m[2])

    heapq.heapify(A)
    heapq.heapify(B)

#最も安い組み合わせ
    cheap=heapq.heappop(A)+heapq.heappop(B)

    print(min(cheap,min(values)))

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import copy
l = [int(c) for c in input().split()]
A=l[0]
B=l[1]
M=l[2]
a=[int(c) for c in input().split()]
b=[int(c) for c in input().split()]
H = [list(map(int,input().split())) for c in range(M)]

a_sorted = sorted(a)
b_sorted = sorted(b)

MinPrice = a_sorted[0]+b_sorted[0]

for i in range(M):
    if MinPrice > a[H[i][0]-1]+b[H[i][1]-1]-H[i][2]:
        MinPrice = a[H[i][0]-1]+b[H[i][1]-1]-H[i][2]

print(MinPrice)

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

ans = min(a_l) + min(b_l)
for x,y,c in x_y_c:
   ans = min(a_l[x-1]+b_l[y-1]-c, ans)
print(ans)

Submission status: Accepted

Solution code:

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

# X_i番目の冷蔵庫とY_i番目の電子レンジを同時に買うと支払い増額がC_i安くなる
X_Y_C = [list(map(int, input().split())) for _ in range(M)]

# 割引を使わない場合の最小値
ans = min(ref_p) + min(mic_p)
for x, y, c in X_Y_C:
    temp = ref_p[x-1] + mic_p[y-1] - c

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

Submission status: Accepted

Solution code:
import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range;R=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def NLI(n):return [[int(i) for i in input().split()] for i in range(n)]
def NLI_(n):return [[int(i)-1 for i in input().split()] for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def RLI(n=8,a=1,b=10):return [random.randint(a,b)for i in range(n)]
def RI(a=1,b=10):return random.randint(a,b)
def Rtest(T):
    case,err=0,0
    for i in range(T):
        inp=INP()
        a1,ls=naive(*inp)
        a2=solve(*inp)
        if a1!=a2:
            print((a1,a2),inp)
            err+=1
        case+=1
    print('Tested',case,'case with',err,'errors')
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
    #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in R(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in R(base**n):s=[tb//(base**bt)%base for bt in R(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
#mo=998244353
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**9)
read=sys.stdin.buffer.read;readline=sys.stdin.buffer.readline;input=lambda:sys.stdin.readline().rstrip()


# Verified by 
# https://atcoder.jp/contests/abc014/tasks/abc014_4
# https://atcoder.jp/contests/abc133/tasks/abc133_f

# import SparseTable
# initialize
# Tree() => n; (a,b);*(n-1)
# Tree(n) => (a,b);*(n-1)
# Tree(init=False); Tree.stdin(); => n; (a,b);*(n-1)
# Tree(init=False); Tree.listin(ls,index=0); => (a,b);*(n-1)

import sys,bisect,string,math,time,functools,random,fractions
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
from itertools import permutations,combinations,groupby
rep=range
def Golf():n,*t=map(int,open(0).read().split())
def I():return int(input())
def S_():return input()
def IS():return input().split()
def LS():return [i for i in input().split()]
def MI():return map(int,input().split())
def LI():return [int(i) for i in input().split()]
def LI_():return [int(i)-1 for i in input().split()]
def NI(n):return [int(input()) for i in range(n)]
def NI_(n):return [int(input())-1 for i in range(n)]
def StoLI():return [ord(i)-97 for i in input()]
def ItoS(n):return chr(n+97)
def LtoS(ls):return ''.join([chr(i+97) for i in ls])
def RA():return map(int,open(0).read().split())
def GI(V,E,ls=None,Directed=False,index=1):
    org_inp=[];g=[[] for i in range(V)]
    FromStdin=True if ls==None else False
    for i in range(E):
        if FromStdin:
            inp=LI()
            org_inp.append(inp)
        else:
            inp=ls[i]
        if len(inp)==2:
            a,b=inp;c=1
        else:
            a,b,c=inp
        if index==1:a-=1;b-=1
        aa=(a,c);bb=(b,c);g[a].append(bb)
        if not Directed:g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1):
    #h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0},boundary=1) # sample usage
    mp=[boundary]*(w+2);found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[boundary]+[mp_def[j] for j in s]+[boundary]
    mp+=[boundary]*(w+2)
    return h+2,w+2,mp,found
def TI(n):return GI(n,n-1)
def accum(ls):
    rt=[0]
    for i in ls:rt+=[rt[-1]+i]
    return rt
def bit_combination(n,base=2):
    rt=[]
    for tb in range(base**n):s=[tb//(base**bt)%base for bt in range(n)];rt+=[s]
    return rt
def gcd(x,y):
    if y==0:return x
    if x%y==0:return y
    while x%y!=0:x,y=y,x%y
    return y
def YN(x):print(['NO','YES'][x])
def Yn(x):print(['No','Yes'][x])
def show(*inp,end='\n'):
    if show_flg:print(*inp,end=end)

mo=10**9+7
inf=float('inf')
FourNb=[(-1,0),(1,0),(0,1),(0,-1)];EightNb=[(-1,0),(1,0),(0,1),(0,-1),(1,1),(-1,-1),(1,-1),(-1,1)];compas=dict(zip('WENS',FourNb));cursol=dict(zip('LRUD',FourNb))
l_alp=string.ascii_lowercase
#sys.setrecursionlimit(10**7)
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
input=lambda: sys.stdin.readline().rstrip()


show_flg=False
show_flg=True

ans=0


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

price_min = min(fridges) + min(microwaves)
for _ in range(m):
	x, y, c = list(map(int, input().split()))
	price = fridges[x-1] + microwaves[y-1] - c
	if price < price_min:
		price_min = price
		
print(price_min)

Submission status: Accepted

Solution code:
# B - Nice Shopping
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=[input().split() for l in range(M)]
tot_1=min(a)+min(b)
lis=[]
for i in range(M):
    tot=a[int(x[i][0])-1]+b[int(x[i][1])-1]-int(x[i][2])
    lis.append(tot)
if tot_1<=min(lis):
    print(tot_1)
else:
    print(min(lis))

Submission status: Accepted

Solution code:
# 日立製作所 社会システム事業部 プログラミングコンテスト2020: B – Nice Shopping
A, B, M = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(j) for j in input().split()]
x, y, c = [], [], []
for k in range(M):
    tmp = [int(i) for i in input().split()]
    x.append(tmp[0])
    y.append(tmp[1])
    c.append(tmp[2])

prices = []

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

for k in range(M):
    prices.append(a[x[k] - 1] + b[y[k] - 1] - c[k])

print(min(prices))

Submission status: Accepted

Solution code:
def main():

    # 割引時の最小値と個別購入の最小値を比較する必要あり

    a_num, b_num, m = map(int, input().split())

    a_val = list(map(int, input().split()))
    b_val = list(map(int, input().split()))

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

    set_value = []
    for i in range(len(values)):
        set_value.append(a_val[values[i][0]-1] +
                         b_val[values[i][1]-1] - values[i][2])
    min_set_value = min(set_value)

    solo_value = min(a_val) + min(b_val)

    answer = min_set_value if min_set_value <= solo_value else solo_value

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def inpl(): return list(map(int, input().split()))
A, B, M = inpl()
A = inpl()
B = inpl()
res = min(A) + min(B)
for _ in range(M):
    x, y, c = inpl()
    tmp = A[x-1] + B[y-1] - c
    if tmp < res:
        res = tmp
print(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()))
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())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ret = 2**60
for _ in range(m):
  x,y,c = map(int,input().split())
  x -= 1
  y -= 1
  ret = min(ret, A[x]+B[y]-c)
print( min(ret, min(A)+min(B)) )

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = min(a)+min(b)
for i in range(M):
  x,y,c=xyc[i]
  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()))
ans = min(a) + min(b)

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

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 = 10000000

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

Submission status: Accepted

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

ai = list(map(int, input().split()))
bi = list(map(int, input().split()))
tmp_min = min(ai) + min(bi)

for i in range(m):
    x,y,c = map(int, input().split())
    z = ai[x-1] + bi[y-1] - c
    if z < tmp_min:
        tmp_min =  z

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

mini = float('inf')

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

if min(a) + min(b) < mini:
    print(min(a) + min(b))
else:
    print(mini)

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
AA = map(int,input().split())
AAA = list(AA)
BB = map(int,input().split())
BBB = list(BB)
mini = min(AAA) + min(BBB)
for i in range(M):
  x,y,c = map(int,input().split())
  xxx = AAA[x-1] + BBB[y-1] - c
  if mini >xxx:
    mini = xxx
print(mini)

Submission status: Accepted

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

A, B, M = map(int, input().split())
a = [10 ** 10] + list(map(int, input().split()))
b = [10 ** 10] + list(map(int, input().split()))
ans = 10 ** 10
for _ in range(M):
    x, y, c = map(int, input().split())
    ans = min(ans, a[x] + b[y] - c)
ans = min(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()))

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(res,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()))
z = [list(map(int, input().split())) for i in range(M)]

price = min(a) + min(b)

for e in z:
    tmp_price = a[e[0]-1] + b[e[1]-1] - e[2]

    if(price > tmp_price):
        price = tmp_price

print(price)

Submission status: Accepted

Solution code:
def main():
    a, b, m = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    xyc_lst = [list(map(int, input().split())) for _ in range(m)]

    min_a = min(a_lst)
    min_b = min(b_lst)
    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]

        cost = a_lst[x - 1] + b_lst[y - 1] - c

        if cost < ans:
            ans = cost

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a, b, m = map(int, input().split())
a_price = list(map(int, input().split()))
b_price = list(map(int, input().split()))
coupon = []
for _ in range(m):
    coupon.append(list(map(int, input().split())))

a_copy = a_price.copy()
a_copy.sort()
b_copy = b_price.copy()
b_copy.sort()
a_min = a_copy[0]
b_min = b_copy[0]
min = a_min + b_min
totals = [min]
for list in coupon:
    tmp = a_price[list[0] - 1] + b_price[list[1] - 1] - list[2]
    totals.append(tmp)

totals.sort()
ans = totals[0]

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
#import numpy as np
import math
#sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def solve():
    ans = 0
    flag = True
    for rep in discount:
        pay = a[rep[0]-1] + b[rep[1]-1] - rep[2]
        if flag:
            ans = pay
            flag = False
        else:
            ans = min(ans,pay)
    
    ans = min(ans,min(a)+min(b))
    print(ans)
    return

if __name__=='__main__':
    A,B,M = IL()
    a = list(IL())
    b = list(IL())
    discount = [list(IL()) for _ in range(M)]
    solve()

Submission status: Accepted

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

all_prices = []
for x, y, c in zip(X, Y, C):
        all_prices.append(a[x-1] + b[y-1] - c)

prices = [min(a) + min(b), min(all_prices)]

print(min(prices))

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())
re = list(map(int, input().split()))
de = list(map(int, input().split()))
mre = min(re)
mde = min(re)
list1 = []
for i in range(m):
  x, y, c = map(int, input().split())
  price = re[x-1] + de[y-1] -c
  list1.append(price)
mlist = min(list1)
m1 = mre + mde

if m1 < mlist:
  print(m1)
else:
  print(mlist)

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):
    x0,y0,c0=map(int,input().split())
    x.append(x0)
    y.append(y0)
    c.append(c0)

ans = min(a) + min(b)

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

print(ans)