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

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

min_val = min(a) + min(b)


for x,y,c in XYC:
    min_val = min(min_val,
                  a[x-1]+b[y-1]-c)
print(min_val)

Submission status: Accepted

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

ans = min(a) + min(b)

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

mmA = min(A)
mmB = min(B)

ans = mmA+mmB

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

    mm = A[x]+B[y]-c
    ans = min(ans,mm)

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

Submission status: Accepted

Solution code:
a,b,m=map(int,input().split())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
d=min(s)+min(t)
for i in range(m):
    p,q,r=map(int,input().split())
    d=min(d,s[p-1]+t[q-1]-r)
print(d)

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:
aa, bb, m = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

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

ans = 10 ** 19
for i in range(m):
    x, y, c = info[i]
    x -= 1
    y -= 1
    ans = min(ans, a[x] + b[y] - c)
a_min = min(a)
b_min = min(b)
ans = min(ans, a_min + b_min)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

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

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

a,b,m = l_in()
A = l_in()
B = l_in()

res = min(A)+min(B)

for _ in range(m):
    x,y,c = l_in()
    res = min(res, A[x-1]+B[y-1]-c)
print(res)

Submission status: Accepted

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

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

ans = min(As) + min(Bs)
for a, b, c in Ms:
    p = As[a-1] + Bs[b-1] - c
    ans = ans if ans < p else p
print(ans)

Submission status: Accepted

Solution code:
import sys
a, b, m = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
b_ls = [int(i) for i in sys.stdin.readline().split()]
best = min(a_ls) + min(b_ls)
for i in range(m):
    x, y, c = [int(i) for i in sys.stdin.readline().split()]
    x -= 1
    y -= 1
    best = min(best, a_ls[x] + b_ls[y] - c)
print(best)

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

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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

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 m in range(M):
    x, y, c = map(int, input().split())
    tmp = As[x - 1] + Bs[y - 1] - c
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
A, B, M = map(int, raw_input().split())
a = map(int, raw_input().split())
b = map(int, raw_input().split())
x = [0]*M
y = [0]*M
c = [0]*M
for i in range(M):
  x[i], y[i], c[i] = map(int, raw_input().split())
  x[i] -= 1
  y[i] -= 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(A, B, XYC):
    ans = min(A) + min(B)
    for x, y, c in XYC:
        t = A[x-1] + B[y-1] - c
        ans = min(t, ans)
    return ans


def main():
    A, B, M = read_int_n()
    A = read_int_n()
    B = read_int_n()
    XYC = [read_int_n() for _ in range(M)]
    print(slv(A, B,XYC))


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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

a,b,m = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = min(A); b=min(B)
mans = a+b
for _ in range(m):
  x,y,c = map(int,input().split())
  x-=1;y-=1
  tmp = A[x] + B[y] - c
  mans = min(mans,tmp)
print(mans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input(): return sys.stdin.readline().rstrip()

def main():
    A,B,M=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    ans=INF
    for i 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()
    ans=min(ans,a[0]+b[0])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
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 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():
    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 _ in range(M):
        x, y, c = map(int, input().split())
        ans = min(ans, A[x-1] + B[y-1] - c)
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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


main()

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:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**5)

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 _ in range(M)]

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

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


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

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

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

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:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__


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

print(min(cand))

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(map(int,input().split()))
b = list(map(int,input().split()))
ans = 10**10
for i in range(m):
    x,y,c = 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())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
ans=min(aa)+min(bb)
for i in range(m):
    x,y,z=map(int,input().split())
    ans=min(ans,max(0,aa[x-1]+bb[y-1]-z))
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()))
W=[tuple(map(int,input().split())) for i in range(M)]

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

print(ans)

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()]
L = [[int(l) for l in input().split()] for _ in range(M)]

ans = 10**15
for l in L:
    ans = min(ans, A[l[0]-1]+B[l[1]-1]-l[2])
ans = min(ans, min(A)+min(B))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

mi = min(a) + min(b)

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

print(mi)

Submission status: Accepted

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

a,b,m = map(int,readline().split())
lst1 = list(map(int,readline().split()))
lst2 = list(map(int,readline().split()))
lst3 = []
for i in range(m):
    x,y,c = map(int,readline().split())
    lst3.append(lst1[x-1]+lst2[y-1]-c)

amin = min(lst1)
bmin = min(lst2)

abmin = min(lst3)

print(min(amin+bmin,abmin))

Submission status: Accepted

Solution code:
def main():
    _, _, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    m = 10**9
    for _ in range(M):
        x, y, c = list(map(int, input().split()))
        m = min(A[x - 1] + B[y - 1] - c, m)
    m = min(m, min(A) + min(B))
    return m

print(main())

Submission status: Accepted

Solution code:
def main():
    A,B,M=map(int, input().split())
    AS=list(map(int, input().split()))
    BS=list(map(int, input().split()))
    XYC=[tuple(map(int, input().split()))for _ in range(M)]
    ans=min(AS)+min(BS)
    for x,y,c in XYC:
        ans=min(ans, AS[x-1]+BS[y-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()))

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

print(ans)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7


A, B, M = MI()
A = LMI()
B = LMI()

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

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:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


NA, NB, M = list(map(int, sys.stdin.buffer.readline().split()))
A = list(map(int, sys.stdin.buffer.readline().split()))
B = list(map(int, sys.stdin.buffer.readline().split()))
XYC = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(M)]

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

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

Submission status: Accepted

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

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

res = min(aa) + min(bb)

for i in range(M):
    x, y, c = map(int, readline().split())
    x -= 1
    y -= 1
    res = min(res, aa[x] + bb[y] - c)

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 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:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    A,B,m = LI()
    a = LI()
    b = LI()
    ans = min(a)+min(b)
    g = LIR(m)
    for i,j,c in g:
        i -= 1
        j -= 1
        k = a[i]+b[j]-c
        if k < ans:
            ans = k
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    A,B,M=MI()
    A=LI()
    B=LI()
    ans = min(A)+min(B)
    for i in range(M):
        a,b,c=MI()
        ans = min(ans, A[a-1]+B[b-1]-c)
    print(ans)
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()
xyc = [inpl() for _ in range(M)]

tmp = min(A) + min(B)

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

print(tmp)

Submission status: Accepted

Solution code:
q, p, 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(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: Runtime Error

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])]+b[int(x[i][1])]-int(x[i][2])
    lis.append(tot)
if tot_1<=min(lis):
    print(tot_1)
else:
    print(min(lis))

Submission status: Runtime Error

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

Submission status: Runtime Error

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_list[list[0]-1] + b_list[list[1]-1] - list[2]
  totals.append(tmp)

totals.sort()
ans = totals[0]

print(ans)

Submission status: Runtime Error

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()
b_copy = b_price.copy()
a_min = a_copy.sort()[0]
b_min = b_copy.sort()[0]
min = a_min + b_min
totals = [min]
for list in coupon:
  tmp = a_list[list[0]-1] + b_list[list[1]-1] - list[2]
  totals.append(tmp)

totals.sort()
ans = totals[0]

print(ans)

Submission status: Runtime Error

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_min = a_price.sort()[0]
b_min = b_price.sort()[0]
min = a_min + b_min
totals = [min]
for list in coupon:
  tmp = a_list[list[0]-1] + b_list[list[1]-1] - list[2]
  totals.append(tmp)

totals.sort()
ans = totals[0]

print(ans)

Submission status: Runtime Error

Solution code:
A,B,M=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
D=[]
for i in range(M):
    D.append(list(map(int,input().split())))
    
S=min(a)+min(b)
P=[]
for i in range(M):
    P.append(a[D[i][0]-1]+b[D[i][0]-1]-D[i][2])
P.append(S)
print(min(P))

Submission status: Runtime Error

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

table=[[a[i]+b[j] for i in range(A)]for j in range(B)]
for i in range(M):
    minn=min(minn,table[List[i][0]-1][List[i][1]-1]-List[i][2])

print(minn)

Submission status: Runtime Error

Solution code:
a,b,m = list(map(int,input().split()))
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))
cost = [min(a_li) + min(b_li)]

for i in range(m):
  x,y,c = list(map(int,input().split()))
  price = a_li[x-1] + a_li[y-1] - c
  cost.append(price)
print(min(cost))

Submission status: Runtime Error

Solution code:
a,b,m = list(map(int,input().split()))
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))
cost = [min(a_li) + min(b_li)]

for i in range(m):
  x,y,c = map(int,input().split())
  price = a_li[x-1] + a_li[y-1] - c
  cost.append(price)
print(min(cost))

Submission status: Runtime Error

Solution code:
a,b,m = map(int,input().split())
a_li = list(map(int,input().split()))
b_li = list(map(int,input().split()))
cost = [min(a_li) + min(b_li)]

for i in range(m):
  x,y,c = map(int,input().split())
  price = a_li[x-1] + a_li[y-1] - c
  cost.append(price)
print(min(cost))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a, b, m = map(int, input().split())
pa = [10 * 10] + list(map(int, input().split()))
pb = [10 * 10] + list(map(int, input().split()))
res = min(pa) + min(pb)
for i in range(m):
  x, y, c = map(int, input().split())
  res = min(res, pa[x] + pa[y] - c)
print(res)

Submission status: Runtime Error

Solution code:
import sys

A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = []
for e in sys.stdin:
  c.append(list(map(int, e.split())))

ans = min(a) + min(b)

for i in range(len(c)):
  temp_val = a[c[i][0]] + b[c[i][1]] - c[i][2]
  if temp_val < ans:
    ans = temp_val

print(ans)

Submission status: Runtime Error

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(a[x-1]+b[y-1]-c, ans)

prins(ans)

Submission status: Runtime Error

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

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

mini = min(a_s) + min(b_s)

for am, bm, sale in m_s:
    mini = min(mini, a_s[am]+b_s[bm]-sale)

print(mini)

Submission status: Runtime Error

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

A, B, M = map(int, input().split())
List_A = list(map(int, input().split()))
List_B = list(map(int, input().split()))
List_discount = [list(map(int, input().split())) for i in range(M)]

# print(List_discount)
ans = 2 * 10**5
for d in List_discount:
    # print(d)
    p = List_A[d[0]-1] + List_B[d[0]-1] - d[2]
    ans = min(ans, p)
no_discount = min(List_A) + min(List_B)
ans = min(ans, no_discount)
print(ans)

Submission status: Runtime Error

Solution code:
A, B, M = map(int, input().split())
F = list(map(int, input().split()))
M = list(map(int, input().split()))
C = []

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

minF = min(F)
minM = min(M)
ans = minF + minM

for x, y, z in C:
    tmp = F[x] + F[y] - z
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Runtime Error

Solution code:
import sys
a,b,m = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = sys.maxsize
summoney = 0
for i in range(m):
  x,y,c = map(int,input().split())
  summoney = a[x-1] + b[y-1] - c
  ans = min(ans,summoney)
for i in range(a):
  for j in range(b):
    summoney = a[i] + a[j]
    ans = min(ans,summoney)
print(ans)

Submission status: Runtime Error

Solution code:
a, b, m = map(int, input().split())
reizo = list(map(int, input().split()))
renji = list(map(int, input().split()))
p = [list(map(int,input().split())) for i in range(m)]
ans = []
for i in range(m):
  cost = reizo[p[i][0]] + renji[p[i][1]] - p[i][2]
  ans.append(cost)
ans.append(min(reizo) + min(renji))
print(min(ans))