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())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cheapest = min(a) + min(b)
for i in range(M):
    l,m,n = map(int, input().split())
    x = a[l-1] + b[m-1]- n
    if x < cheapest:
        cheapest = x
print(cheapest)

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

ans = min(ans, a[0] + b[0])

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

A, B, m = map(int, input().split())
a = [int(i) for i in sys.stdin.readline().rstrip().split()]
b = [int(i) for i in sys.stdin.readline().rstrip().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 tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    a,b,m = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    t = min(A) + min(B)
    ans = 10**6
    for i in range(m):
        x,y,c = map(int, readline().split())
        tmp = A[x-1] + B[y-1] - c
        ans =min(ans, tmp)
    
    print(min(ans,t))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a, b, m = map(int, input().split())
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
waribiki = []
for i in range(m):
    waribiki.append(list(map(int, input().split())))

no_waribiki = min(a_lst) + min(b_lst)

yes_waribiki = 100000000
for i in range(m):
    yes_waribiki = min(yes_waribiki, a_lst[waribiki[i][0]-1] + b_lst[waribiki[i][1]-1] - waribiki[i][2])

print(min(no_waribiki, yes_waribiki))

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

minpay = min(a_l) + min(b_l)
for i in range(m):
    c_l = list(map(int, input().split()))
    if a_l[c_l[0] - 1] + b_l[c_l[1] - 1] - c_l[2] <= minpay:
        minpay = a_l[c_l[0] - 1] + b_l[c_l[1] - 1] - c_l[2]

print(minpay)

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

Submission status: Accepted

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

cost_a = list(map(int, input().split()))
cost_b = list(map(int, input().split()))

ans = min(cost_a) + min(cost_b) 
for i in range(m):
  x, y, c = map(int, input().split())
  cost = cost_a[x - 1] + cost_b[y - 1] - c
  ans = min(ans, cost)

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

Submission status: Accepted

Solution code:
A,B,M = map(int,input().split())
AA = list(map(int,input().split()))
BB = list(map(int,input().split()))
C = [list(map(int,input().split())) for _ in range(M)]

ans = min(AA)+min(BB)
for x,y,c in C:
  x -= 1
  y -= 1
  ans = min(AA[x]+BB[y]-c,ans)
print(ans)

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

ans = min(a_l) + min(b_l)
pairs = []
for a_i, b_i, c in coupons:
    pairs.append(a_l[a_i-1]+b_l[b_i-1]-c)

print(min(ans, min(pairs)))

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()]
ans = 10**19
for i 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:
# print('input >>')
A, B, M = map(int,(input().split()))
al = list(map(int,(input().split())))
bl = list(map(int,(input().split())))
xyms = []
for _ in range(M):
    xyms.append(list(map(int,(input().split()))))


ans = min(al) + min(bl)
for xym in xyms:
    x = xym[0]
    y = xym[1]
    m = xym[2]
    ans = min(al[x-1] + bl[y-1] - m, ans)

# print('-----output-----')
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
#sys.setrecursionlimit(150000)


def solve(A: int, B: int, M: int, a: "List[int]", b: "List[int]", x: "List[int]", y: "List[int]", c: "List[int]"):
    m1=min(a)+min(b)
    for i in range(M):
        m1=min(m1,a[x[i]-1]+b[y[i]-1]-c[i])
        
    print(m1)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
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())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

m1 = 10000000000
m2 = 10000000000
for i in al: m1 = min(m1, i)
for i in bl: m2 = min(m2, i)

ans = m1 + m2
#ans = 100000
#print(ans)
for i in range(m):
    x, y, z = map(int, input().split())
    t = al[x-1]+bl[y-1]-z
    ans = min(ans, t)

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


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

   cost = a[x-1] + b[y-1] - c
   ans = min(ans, cost)

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()))
ans = min(As) + min(Bs)

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

A, B, M = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
minimum = np.min(a) + np.min(b)
for i in range(M):
    x,y,c = map(int, input().split())
    price = a[x-1] + b[y-1] - c
    if price < minimum:
        minimum = price

print(minimum)

Submission status: Accepted

Solution code:
#coding:utf-8

import queue



def main():
    a, b, m = map(int, input().split())
    ref = list(map(int, input().split()))
    ren = list(map(int, input().split()))
    minv = min(ref) + min(ren)
    
    for _ in range(m):
        x, y, c = map(int, input().split())
        minv = min(ref[x - 1] + ren[y - 1] - c, minv)
        
    print(minv)
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = min(price_A) + min(price_B)

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

Submission status: Accepted

Solution code:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    AN, BN, M = iim()
    A = list(iim())
    B = list(iim())

    ans = min(A) + min(B)

    for i in range(M):
        ai, bi, ci = iim()

        xi = A[ai-1] + B[bi-1] -ci
        ans = min(ans, xi)
    print(ans)

if __name__ == "__main__":
    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()))
ans=10**7
for i in range(m):
    x,y,c=map(int,input().split())
    price=a[x-1]+b[y-1]-c
    if price<ans:
        ans=price
a.sort()
b.sort()
p=a[0]+b[0]
if p<ans:
    ans=p
print(ans)

Submission status: Accepted

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

ans = 10**6
for i in range(m):
    p = x[i]-1
    q = y[i]-1
    ans = min(ans, A[p]+B[q]-c[i])

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

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

print(mini)

Submission status: Accepted

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

#############
# Libraries #
#############

import math
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#############
# Main Code #
#############

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

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

m = []

for _ in range(M):
  m.append(list(map(int,input().split())))

  
mini = min(a) + min(b)

for q in m:
  if a[q[0]-1] + b[q[1]-1] - q[2] < mini:
    mini = a[q[0]-1] + b[q[1]-1] - q[2]
    
print(mini)

Submission status: Accepted

Solution code:
A, B, M = [int(_) for _ in input().split()]
aA = [int(_) for _ in input().split()]
aB = [int(_) for _ in input().split()]
XYC = [[int(_) for _ in input().split()] for _ in range(M)]
ans = min(aA) + min(aB)
for x, y, c in XYC:
    ans = min(ans, aA[x - 1] + aB[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()]
x = [0 for i in range(M)]
y = [0 for i in range(M)]
c = [0 for i in range(M)]

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

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

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()))
l=[list(map(int,input().split())) for i in range(m)]
ans=[]
f=0
ma=min(la)
mb=min(lb)
for i in range(m):
  ans.append(la[l[i][0]-1]+lb[l[i][1]-1]-l[i][2])
  if la[l[i][0]-1]==ma and lb[l[i][1]-1]==mb:
    f=1
if f==0:
  ans.append(ma+mb)
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()))

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

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

print(ans)

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):
    i, j, c = map(int, input().split())
    ans = min(ans, a[i-1]+b[j-1]-c)
  
  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()))
L = [list(map(int,input().split())) for _ in range(M)]

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


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

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)

a.sort()
b.sort()
ans=min(ans,a[0]+b[0])

print(ans)

Submission status: Accepted

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

base = min(a) + min(b)
answer = base

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	A,B,M = inputMap()
	a = inputList()
	b = inputList()

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

	print(ans)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# special thanks :
#  https://nagiss.hateblo.jp/entry/2019/07/01/185421
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)

class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

if __name__ == "__main__":
	main()

Submission status: Accepted

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

cnt=10**7
for x,y,c in XYC:
    cnt=min(cnt,A[x-1]+B[y-1]-c)
print(min(cnt,min(A)+min(B)))

Submission status: Accepted

Solution code:
import sys

import numpy as np

readline = sys.stdin.buffer.readline

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

a = np.array(list(map(int, readline().split())))
b = np.array(list(map(int, readline().split())))

XYC = [list(map(int, readline().split())) for _ in range(M)]

m = np.min(a) + np.min(b)

for x, y, c in XYC:
    x -= 1
    y -= 1
    s_c = a[x] + b[y] - c
    if s_c < m:
        m = s_c

print(m)

Submission status: Accepted

Solution code:
[A,B,M] = list(map(int,input().split()))
AL = list(map(int,input().split()))
BL = list(map(int,input().split()))
XYC = []
for i in range(M):
    XYC.append(list(map(int,input().split())))

out = []
for i in range(0,M):
    X = XYC[i][0]
    Y = XYC[i][1]
    C = XYC[i][2]
    out.append(AL[X-1]+BL[Y-1]-C)

print(min(min(out),min(AL)+min(BL)))

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

print(min(ans, a[0] + b[0]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
a, b, m = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

m_list = []

for i in range(m):
    m_list.append(list(map(int, input().split())))


a_min = 1000000000
for item in a_list:
    if item < a_min:
        a_min = item

b_min = 1000000000
for item in b_list:
    if item < b_min:
        b_min = item

val_min = a_min + b_min

for item in m_list:
    tmp = a_list[item[0]-1] + b_list[item[1]-1] - item[2]
    if tmp < val_min:
        val_min = tmp

print(val_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()))
x = []
y = []
c = []
for i in range(m):
    val1,val2,val3 = map(int,input().split())
    x.append(val1)
    y.append(val2)
    c.append(val3)

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

for i in range(m):
    ans.append(a[x[i]-1] + b[y[i]-1] - c[i])

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()))
w=10**7
for i in range(m):
    x,y,c=map(int,input().split())
    w=min(A[x-1]+B[y-1]-c,w)
A=sorted(A)
B=sorted(B)
for i in range(min(a,b)):
    w=min(w,A[i]+B[i])
print(w)

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

print(min(ans_1, ans_2))

Submission status: Accepted

Solution code:
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

na,nb,m = inm()
a = inl()
b = inl()
mn = min(a)+min(b)
for i in range(m):
    x,y,c = inm()
    v = a[x-1]+b[y-1]-c
    if v<mn:
        mn = v
print(mn)

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

Submission status: Accepted

Solution code:
import sys
a, b, m = [int(x) for x in input().split()]
a_s = [int(x) for x in input().split()]
b_s = [int(x) for x in input().split()]
discounts = [[int(x) for x in input().split()] for i in range(m)]
m = min(a_s) + min(b_s)
for (x, y, c) in discounts:
    v = a_s[x - 1] + b_s[y - 1] - c
    m = min(m, v)
print(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()))
xyc = [list(map(int, input().split())) for _ in range(m)]

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

mn = min(A) + min(B)

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

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

Submission status: Accepted

Solution code:
a, b, m = map(int, input().split())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))
minimum = min(aList) + min(bList)

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

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 i in range(M):
    p,q,r = map(int, input().split())
    x.append(p)
    y.append(q)
    c.append(r)

ans = min(a) + min(b)

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

Submission status: Accepted

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


def solve(inp):
    (A, B, M) = map(int, inp.readline().strip().split(' '))
    A = list(map(int, inp.readline().strip().split(' ')))
    B = list(map(int, inp.readline().strip().split(' ')))
    XYC = [map(int, inp.readline().strip().split(' ')) for i in range(M)]

    r = min(A) + min(B)
    for x, y, c in XYC:
        r = min(r, A[x - 1] + B[y - 1] - c)

    return str(r)


def main():
    import sys
    result = solve(sys.stdin)
    if result is not None and result != '':
        print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    a_n, b_n, m = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 10**8
    for _ in range(m):
        x, y, c = map(int, input().split())
        x -= 1
        y -= 1
        buf = a[x] + b[y] - c
        if buf < ans:
            ans = buf
    a.sort()
    b.sort()
    if a[0] + b[0] < ans:
        ans = a[0] + b[0]
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
a,b,m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = min(a)+min(b)
for i in range(m):
    x,y,c = list(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 = map(int, input().split())		# 5 7 2

A =        list( map(int, input().split()) )	# 1 2 3 4 5 ...
B =        list( map(int, input().split()) )	# 1 2 3 4 5 ...

ans=min(A) + min(B)

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

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()]
xyc = [[int(i) for i in input().split()] for j in range(M)]

a_min = min(a)
b_min = min(b)

ans = a_min+b_min

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())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
l=[]

for i in range(m):
  q=list(map(int,input().split()))
  l.append(al[q[0]-1]+bl[q[1]-1]-q[2])
l.append(min(al)+min(bl))
print(min(l))

Submission status: Accepted

Solution code:
#n = int(input())
#a,b = map(int,input().split())
#a_L = list(map(int,input().split()))

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