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: Wrong Answer

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

A_list = list(map(int,input().split()))
B_list = list(map(int,input().split()))

x_list = []
y_list = []
c_list = []

#print(A,B,M)
#print(A_list)
#print(B_list)

min = 10000000
for i in range(M):
    x,y,c= list(map(int,input().split()))
    #print(x,y,c)
    try:
        index = x_list.index(x-1)
        if c_list[index] < c:
            c_list[index] = c
    except ValueError:
        x_list.append(x-1)
        y_list.append(y-1)
        c_list.append(c)

#print(x_list)
#print(y_list)
#print(c_list)

if A >= B:
    for j in range(B):
        #print(j)
        try:
            index = x_list.index(j)
            sum = A_list[j] + B_list[j] - c_list[index]
        except ValueError:
            #print(ValueError)
            sum = A_list[j] + B_list[j]
        #print(sum)
        if sum < min:
            min = sum


else:
    for k in range(A):
        #print(k)
        try:
            index = x_list.index(k)
            sum = A_list[k] + B_list[k] - c_list[index]
        except ValueError:
            #print(ValueError)
            sum = A_list[k] + B_list[k]
        #print(sum)
        if sum < min:
            min = sum


    

print(min)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, raw_input().split())
a_s = sorted(enumerate(map(int, raw_input().split())), key=lambda t: t[1])
b_s = sorted(enumerate(map(int, raw_input().split())), key=lambda t: t[1])
w = [[0] * B] * A
max_w = 0
for _ in xrange(M):
    x, y, c = map(int, raw_input().split())
    w[x-1][y-1] = max(w[x-1][y-1], c)
    max_w = max(max_w, c)

cost = 10 ** 30
for i, a in a_s:
    if a + b_s[0][1] - max_w >= cost:
        break
    for j, b in b_s:
        cost = min(cost, a + b - w[i][j])
        if a + b - max_w >= cost:
            break

print cost

Submission status: Wrong Answer

Solution code:
p = []
list1 = input()
a = input()
b = input()
list_1 = list(map(int, list1.split(" ")))
alist = list(map(int, a.split(" ")))
blist = list(map(int, b.split(" ")))
A = list_1[0]
B = list_1[1]
M = list_1[2]

for i in range(M):
  l = input()
  xyc = list(map(int, l.split(" ")))
  x = xyc[0]
  y = xyc[1]
  c = xyc[2]
  for i in range(A):
    for j in range(B):
      if x == i and y == j:
        p.append(alist[i]+blist[j]-c)
for i in range(A):
  for j in range(B):
    p.append(alist[i]+blist[j])
print(min(p))

Submission status: Wrong Answer

Solution code:
A,B,M=map(int,input().split())
A_list=list(map(int, input().split()))
B_list=list(map(int, input().split()))
M_list=list(list(map(int, input().split())) for i in range(M))

dp=[]
for a in range(A):
    dp.append(list([0]*B))
    
res1=A_list[M_list[0][0]-1]+B_list[M_list[0][1]-1]-M_list[0][2]
for m in range(1,M):
    res1=min(res1,A_list[M_list[m][0]-1]+B_list[M_list[m][1]-1]-M_list[m][2])
    dp[M_list[m][0]-1][M_list[m][1]-1]=1
    
res2 = res1
for a in range(A):
    for b in range(B):
        if dp[a][b]==0:
            print(a,b)
            res2 = min(res2,A_list[a]+B_list[b])
print(res2)

Submission status: Wrong Answer

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

p=int()
q=int()
p=a[0]+b[0]
number1=0
number2=0
for i in a:
    number1+=1
    for s in b: 
        number2+=1
        for m in range(M):
            if number1==int(t[m]):
                if number2==int(x[m]):
                    r=int(y[m])
                else:
                    r=0
            else :
                r=0
            q=i+s-r
            if q<p:
                p=q
print(p)

Submission status: Wrong Answer

Solution code:
p = []
list1 = input()
a = input()
b = input()
list_1 = list(map(int, list1.split(" ")))
alist = list(map(int, a.split(" ")))
blist = list(map(int, b.split(" ")))
A = list_1[0]
B = list_1[1]
M = list_1[2]

for i in range(M):
  l = input()
  xyc = list(map(int, l.split(" ")))
  x = xyc[0]
  y = xyc[1]
  c = xyc[2]
  for i in range(A):
    for j in range(B):
      if x == i and y == j:
        p.append(alist[i]*blist[j]-c)
for i in range(A):
  for j in range(B):
    p.append(alist[i]*blist[j])
print(min(p))

Submission status: Wrong Answer

Solution code:
def main():
    a, b, m = map(int, input().split())
    priceOfMicrowaves = list(map(int, input().split()))
    priceOfRefrigerators = list(map(int, input().split()))
    priceTable = [list(map(int,input().split())) for i in range(m)]
    cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
    for p in priceOfMicrowaves:
        if cheepest == 0:
            break
        if p >= cheepest:
            continue
        for q in priceOfRefrigerators:
            if q >= cheepest:
                continue
            if p + q < cheepest:
                cheepest = p + q
    for price in priceTable:
        if cheepest == 0:
            break
        if priceOfMicrowaves[price[0]-1] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
        elif priceOfMicrowaves[price[1]-1] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
        elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
    print(cheepest)
    
main()

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))
mList = [list(map(int, input().split())) for i in range(M)]
Min = 20001

for i in range(A):
    for j in range(B):
        for k in range(M):
           if i+1 == mList[k][0] and j+1 == mList[k][1]:
               cost = aList[i] + bList[j] - mList[k][2]
           else:
               cost = aList[i] + bList[j]
        Min = min(cost, Min)

print(Min)

Submission status: Wrong Answer

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

mini = min(a) + min(b)
credit = []

for i in s:
    credit.append(int(i[-1]))

Credit = max(credit)

a_s,b_s,c_s = s[credit.index(Credit)]

if mini <= a[int(a_s)-1] +b[int(b_s)-1] -int(Credit):
    print(mini)
else:
    print(a[int(a_s)-1] +b[int(b_s)-1] -int(Credit))

Submission status: Wrong Answer

Solution code:
def main():
    a, b, m = map(int, input().split())
    priceOfMicrowaves = list(map(int, input().split()))
    priceOfRefrigerators = list(map(int, input().split()))
    priceTable = [list(map(int,input().split())) for i in range(m)]
    cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
    for p in priceOfMicrowaves:
        if cheepest <= 1:
            break
        if p >= cheepest:
            continue
        for q in priceOfRefrigerators:
            if q >= cheepest:
                continue
            if p + q < cheepest:
                cheepest = p + q
    for price in priceTable:
        if cheepest == 0:
            break
        if priceOfMicrowaves[price[0]-1] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
        elif priceOfMicrowaves[price[1]-1] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
        elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
    print(cheepest)
    

main()

Submission status: Wrong Answer

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

a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.sort()
b.sort()
Min = a[0] + b[0]
x = []
y = []
c = []
for i in range(m):
    X,Y,C = map(int,input().split())
    x.append(X)
    y.append(Y)
    c.append(C)
for i in range(m):
    a_index = x[i]-1
    b_index = y[i]-1
    tmp = a[int(a_index)] + b[int(b_index)] - c[i]
    Min = min(Min,tmp)
print(Min)

Submission status: Wrong Answer

Solution code:
A,B,M=map(int,input().split())
vecA=list(map(int,input().split()))
vecB=list(map(int,input().split()))
matX=[input().split() for k in range(M)]
price = 10**6
for i in range(0,M):
    pricecom = int(vecA[int(matX[i][0]) -1]) + int(vecB[int(matX[i][1]) -1]) - int(matX[i][2])
    if pricecom<= price:
        price=pricecom
    else:
        pass
print(price)

Submission status: Wrong Answer

Solution code:
import sys

lines = sys.stdin.readlines()
num_a ,num_b, num_m = [int(v) for v in lines[0].split()]
# ----

a_list = [int(v) for v in lines[1].split()[:num_a+1]]
b_list = [int(v) for v in lines[2].split()[:num_b+1]]

# print(a_list)
# print(b_list)

result = max(a_list) + max(b_list)
ignores = set()
for c in lines[3:num_m+3]:
    a_i, b_i, discount = map(int, c.split())
    try:
        ignores.add((int(a_i), int(b_i)))
        
        price = a_list[a_i-1] + b_list[b_i-1] - discount
        
        if price < result:
            result = price
    except IndexError:
        print('------')
        print(c)
        pass

# ---
# result = max(a_list) + max(b_list)
for i, a in enumerate(a_list):
    for j, b in enumerate(b_list):
        if (i+1, j+1) not in ignores:
            price = a + b
            result = price if price < result else result
            
    
result
# c_list = {
#     (int()):int(l[4]) for l in lines[2:num_m+3]
# }

# a_list

# print(max(c_list.values()))
# print(a_list)
# print(b_list)
# print(c_list)

# list(itertools.product(a_list, b_list))

# for c in c_list:
    

# for i in []

Submission status: Wrong Answer

Solution code:
def main():
    a, b, m = map(int, input().split())
    priceOfMicrowaves = list(map(int, input().split()))
    priceOfRefrigerators = list(map(int, input().split()))
    priceTable = [list(map(int,input().split())) for i in range(m)]
    cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
    for price in priceTable:
        if priceOfMicrowaves[price[0]-1] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
        elif priceOfMicrowaves[price[1]-1] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
        elif priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
    for p in priceOfMicrowaves:
        if cheepest <= 1:
            break
        if p >= cheepest:
            continue
        for q in priceOfRefrigerators:
            if q >= cheepest:
                continue
            if p + q < cheepest:
                cheepest = p + q
    print(cheepest)
    
main()

Submission status: Wrong Answer

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



a=input().split()
b=input().split()
m=[]
for i in range(M):
    m.append(input().split())

counta=1
countb=1
nummin=int(a[0])+int(b[0])

for i in a:
    i=int(i)
    for j in b:
        j=int(j)
        num=i+j
        for k in m:
            if int(k[0])==counta and int(k[1])==countb:

                num-=int(k[2])

                if num<nummin:
                    nummin=num
                    num=i+j
        if num<nummin:
            nummin=num
        countb+=1
        num=0
    countb=1
    counta+=1
print(nummin)

Submission status: Wrong Answer

Solution code:
import sys

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

prices_A=list(map(int,input().split()))
prices_B=list(map(int,input().split()))

dc = dict()
dc_max = 0
dc_max_A= [0 for i in range(A)]
dc_max_B= [0 for i in range(B)]
y_dc_max_A= [-1 for i in range(A)]
for i in range(M):
    (x,y,c)=map(int,input().split())
    idx="{} {}".format(x,y)
    dc[idx]=c
    if dc_max < c:
        dc_max = c
    if dc_max_A[x-1] < c:
        dc_max_A[x-1] = c
        y_dc_max_A[x-1] = y

#print(prices_A)
#print(prices_B)
#print(dc)

min_p = prices_A[0] + prices_B[0]
for i in range(A):
    if prices_A[i] - dc_max_A[i] < min_p:
        x=i+1       
        j=y_dc_max_A[i]-1
        if j>=0:
            c=0
            idx = "{} {}".format(x,y)
            if idx in dc:
                c=dc[idx]
            p = prices_A[i]+prices_B[j]-c
            if p < min_p:
                min_p=p
print(min_p)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, raw_input().split())
a_s = sorted(enumerate(map(int, raw_input().split())), key=lambda t: t[1])
b_s = sorted(enumerate(map(int, raw_input().split())), key=lambda t: t[1])
w = [[0] * B] * A
max_w = 0
for _ in xrange(M):
    x, y, c = map(int, raw_input().split())
    w[x-1][y-1] = max(w[x-1][y-1], c)
    max_w = max(max_w, c)

cost = 10 ** 20
for i, a in a_s:
    if a + b_s[0][1] - max_w > cost:
        break
    for j, b in b_s:
        cost = min(cost, a + b - w[i][j])
        if a + b - max_w > cost:
            break

print cost

Submission status: Wrong Answer

Solution code:
A,B,M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
acheap = [ a.index(min(a))+1,min(a)]
bcheap = [ b.index(min(b))+1,min(b)]
bigestDis = [0,0,0]
ans1 = 0
ans2 = 0
ans = 0
for _ in range(M):
    x,y,c = map(int, input().split())
    if c>bigestDis[2]:
        bigestDis = [x,y,c]
if acheap[0] == bigestDis[0] and bcheap[0] == bigestDis[1]:
    k = 0
    if acheap[1]-bigestDis[2]<0:
        k = 0
    else:
        k = acheap[1]-bigestDis[2]
    ans1 = k + bcheap[1]
    if bcheap[0] == bigestDis[1]:
        if bcheap[1]-bigestDis[2]<0:
            k = 0
        else:
            k = bcheap[1]-bigestDis[2]
        ans2 = k + acheap[1]
        ans = min(ans1,ans2)
elif acheap[0]==bigestDis[0]:
    k = 0
    if acheap[1]-bigestDis[2]<0:
        k = 0
    else:
        k = acheap[1]-bigestDis[2]
    ans = k + bcheap[1]
elif bcheap[0] == bigestDis[1]:
    if bcheap[1]-bigestDis[2]<0:
        k = 0
    else:
        k = bcheap[1]-bigestDis[2]
    ans = k + acheap[1]
else:
    ans = acheap[1]+bcheap[1]
print(ans)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
a, b = [], []
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x, y, c = [], [], []
for i in range(M):
  tmp = list(map(int, input().split()))
  x.append(tmp[0])
  y.append(tmp[1])
  c.append(tmp[2])

# 割引券なし
min_a, min_b, min_total_wo = 10**5+1, 10**5+1, 0
for i in range(A):
  if min_a > a[i]:
    min_a = a[i]
for i in range(B):
  if min_b > b[i]:
    min_b = b[i]
min_total_wo = min_a + min_b
  
# 割引券あり
min_total_w = 10**5+1
for i in range(M):
  tmp = a[x[i]-1] + b[y[i]-1] - c[i]
  if min_total_w > tmp:
    min_total_w = tmp
if min_total_wo < min_total_w:
  print(min_total_wo)
else:
  print(min_total_w)

Submission status: Wrong Answer

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

    grid = []
    for i in range(M):
        array = list(map(int, input().strip().split()))
        grid.append(array)

    res = 10000 + 10000
    for i in range(A):
        for j in range(B):
            for m in grid:
                total = a[i]+b[j]
                if m[0] == i+1 and m[1] == j+1:
                    total -= m[2]
            
            if total < res:
                res = total

    print(res)
    
resolve()

Submission status: Wrong Answer

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

min_a = min(a)
min_b = min(b)
sa = min_a + min_b
si = 10**5
for i in range(M):
    su = a[m[i][0]-1] + b[m[i][1]-1] - m[i][2]
    if si > su:
        si = su
if sa > si:
    print(si)
else:
    print(sa)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))
mList = [list(map(int, input().split())) for i in range(M)]
Min = 10000
nebiki = 100000

for i in range(A):
    for j in range(B):
        cost = aList[i] + bList[j]
        for k in range(M):
           if i+1 == mList[k][0] and j+1 == mList[k][1]:
               nebiki = cost - mList[k][2]
        Min = min(Min, nebiki, cost)

print(Min)

Submission status: Wrong Answer

Solution code:
def main():
    a, b, m = map(int, input().split())
    priceOfMicrowaves = list(map(int, input().split()))
    priceOfRefrigerators = list(map(int, input().split()))
    priceTable = [list(map(int,input().split())) for i in range(m)]
    cheepest = priceOfMicrowaves[0] + priceOfRefrigerators[0]
    for price in priceTable:
        if cheepest <= 1:
            break
        if priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2] < cheepest:
            cheepest = priceOfMicrowaves[price[0]-1]+priceOfRefrigerators[price[1]-1]-price[2]
    for p in priceOfMicrowaves:
        if cheepest <= 1:
            break
        if p >= cheepest:
            continue
        for q in priceOfRefrigerators:
            if q >= cheepest:
                continue
            if p + q < cheepest:
                cheepest = p + q
    print(cheepest)
    

main()

Submission status: Wrong Answer

Solution code:
A,B,M = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
S = P
T = Q
S.sort()
T.sort()
minimum = (S[0]+T[0])
K = 10**9
for i in range(M):
    x,y,c = map(int,input().split())
    S = P[x-1]+Q[y-1]-c
    if S<=K:
        if S <= 0:
            K = 0
        else:
            K = S
if minimum>K:
    print(K)
else:
    print(minimum)

Submission status: Wrong Answer

Solution code:
A,B,M = [int(x) for x in input().split()]
A_list = [int(x) for x in input().split()]
B_list = [int(y) for y in input().split()]
discount = []
min = 100000
for _ in range(M):
  case = [int(x) for x in input().split()]
  discount.append(case)

min_a = sorted(A_list)[0]
min_b = sorted(B_list)[0]

for i in range(M):
  temp = A_list[discount[i][0]-1] + B_list[discount[i][1]-1] - discount[i][2]
  if temp < min:
    min = temp
    
if min_a + min_b > min:
  print(min)
else:
  print(min_a + min_b)

Submission status: Wrong Answer

Solution code:
import itertools


A, B, M = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = {}
for _ in range(M):
    x, y, z = map(int, input().split())
    key = str(x) + "&" + str(y)
    if key in m.keys():
        if m[key] < z:
            m[key] = z
    else:
        m[key] = z
A_B = list(itertools.product(range(A), range(B)))
A_B_sum = list(map(lambda x: x[0] + x[1], (itertools.product(a, b))))
for i in range(len(A_B)):
    # print(str(A_B[i][0]) + "&" + str(A_B[i][1]))
    if str(A_B[i][0]) + "&" + str(A_B[i][1]) in m.keys():
        print(str(A_B[i][0]) + "&" + str(A_B[i][1]))
        A_B_sum[i] -= m[str(A_B[i][0]) + "&" + str(A_B[i][1])]
print(min(A_B_sum))

Submission status: Wrong Answer

Solution code:
A,B,M = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
S = P
T = Q
S.sort()
T.sort()
minimum = (S[0]+T[0])
K = 10**9
for i in range(M):
    x,y,c = map(int,input().split())
    S = P[x-1]+Q[y-1]-c
    if S<K:
        if S <= 0:
            K = 0
        else:
            K = S
if minimum>K:
    print(K)
else:
    print(minimum)

Submission status: Wrong Answer

Solution code:
a,b,m = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
X = []
Y = []
C = []
total = 2*10**5
for i in range(m):
    x,y,c = map(int,input().split())
    X.append(x)
    Y.append(y)
    C.append(c)
for i in range(m):
    result = int(A[int(X[int(i)])-1])+int(B[int(Y[int(i)])-1])-int(C[int(i)])
    if (total > result):
        total = result
#print(int(min(A))+int(min(B)))
if(int(min(A))+int(min(B))>result):
    print(result)
else:
    print(int(min(A))+int(min(B)))

Submission status: Wrong Answer

Solution code:
import numpy as np

S = list(map(int,input().split()))
A = S[0]
B = S[1]
M = S[2]

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

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

sum = np.inf

if len(a) > len(b):
    length = len(b)
else:
    length = len(a)

for i in range(0, length):
    num = a[i] + b[i]
    if sum > num:
        sum = num

#print(list1)

for i in range(0, M):
    x = list1[i][0]
    y = list1[i][1]
    c = list1[i][2]
    num = a[x-1]+b[y-1]-c
    #print(i)
    if sum > num:
        sum = num

print(sum)

Submission status: Wrong Answer

Solution code:
abm = input('')
a = int(abm.split(' ')[0])
b = int(abm.split(' ')[1])
m = int(abm.split(' ')[2])
clist = []
dlist = []
wdic = {}
pdic = {}
clist = input('').split(' ')
dlist = input('').split(' ')

for i in range(0,a):
  for j in range(0,b):
    key = str(i+1) + "-" + str(j+1)
    val = int(clist[i]) + int(dlist[j])
    pdic[key] = val

for i in range(0,m):
  win = input('').split(' ')
  wc = win[0]
  wd = win[1]
  wkey = wc + "-" + wd
  wp = int(win[2])
  if wkey in wdic.keys():
    if wp > wdic[wkey]:
      wdic[wkey] = wp
  else:
    wdic[wkey] = wp

min_price = -1

for key,price in pdic.items():
  wkey = str(i+1) + "-" + str(j+1)
  w = 0
  if wkey in wdic.keys():
     w = wdic[wkey]
  total = price - w
  if min_price == -1:
    min_price = total
  elif total < min_price:
    min_price = total      

print(min_price)

Submission status: Wrong Answer

Solution code:
import itertools

if __name__ == "__main__":
    A, B, M = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    xyc = []
    for i in range(M):
        x, y, c = map(int, input().split())
        xyc.append([x, y, c])

    p = []
    for a, b in itertools.product(A, B):
        p.append(a + b)
    print(A)
    print(B)
    print(xyc)
    for j in xyc:
        p.append(A[j[0]-1] + B[j[1]-1] - j[2])
    ret = min(p)    
    print(ret)

Submission status: Wrong Answer

Solution code:
# input
a_n, b_n, m = map(int, input().split())
a = input().split()
b = input().split()
x = [input().split() for i in range(m)]

# calc
min_price = 200000
for p in x:
   tmp_price = int(a[int(p[0])-1]) + int(b[int(p[1])-1]) - int(p[2])
   if tmp_price < min_price:
       min_price = tmp_price
print(min_price)

Submission status: Wrong Answer

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

min_price = 10 ** 5
# 割引券を使ったときの最低価格をみる
for ticket in discount:
    tmp = rfg[ticket[0]-1] + mcw[ticket[1]-1] - ticket[2]
    if tmp < min_price:
        min_price = tmp


# 普通に買ったときの最低価格を見る
# min_priceを下回った時点でおわり
rrfg = sorted(rfg)
mmcw = sorted(mcw)

is_true = False
for i in range(A):
    for j in range(i, B):
        tmp = rrfg[i] + mmcw[j]
        if tmp >= min_price:
            break
        else:
            min_price = tmp
            is_true = True
            break

    if is_true:
        break

print(min_price)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, input().split())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))
mList = [list(map(int, input().split())) for i in range(M)]
Min = 1000
nebiki = 1000

for i in range(A):
    for j in range(B):
        cost = aList[i] + bList[j]
        for k in range(M):
           if i == mList[k][0] and j == mList[k][1]:
               nebiki = cost - mList[k][2]
        Min = min(Min, nebiki, cost)

print(Min)

Submission status: Wrong Answer

Solution code:
import numpy as np
A,B,M = map(int,input().rstrip().split())
a = list(map(int,input().rstrip().split()))
b = list(map(int,input().rstrip().split()))

suml = [[0] * len(b)] * len(a)

for i in range(len(a)):
  for j in range(len(b)):
    suml[i][j] = a[i]+b[j]


sumk = [[0] * len(b)] * len(a)

for k in range(M):
  i,j,c = list(map(int,input().rstrip().split()))
  if sumk[i-1][j-1] != 0:
    sumk[i-1][j-1] = max(sumk[i-1][j-1],c)
  else:
    sumk[i-1][j-1] = c

x = np.array(suml)
y = np.array(sumk)

z = x - y

print(np.amin(z))

Submission status: Wrong Answer

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

m = []

for i in range(M):
    x, y, z = map(int, input().split())
    m.append([x, y, z])

m.sort(key = lambda x: x[2], reverse=True)

if a[m[0][0]-1] + b[m[0][1]-1] < m[0][2]:
    print(min(a) + min(b))
    exit()

if a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] > min(a) + min(b):
    print(min(a) + min(b))
    exit()
elif a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] < min(a) + min(b):
    print(a[m[0][0]-1] + b[m[0][1]-1] - m[0][2])
    exit()
elif a[m[0][0]-1] + b[m[0][1]-1] - m[0][2] == min(a) + min(b):
    print(min(a) + min(b))
    exit()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

A, B, M = input().split(" ")
a = np.array(input().split(" "), dtype=np.int64)
b = np.array(input().split(" "), dtype=np.int64)

x_list = []
for i in range(int(M)):
  temp = input().split(" ")
  x_list.append(temp)

x_np = np.array(x_list, dtype=np.int64)

# 候補1はa,bの最小値同士で足したもの
a_min = a.min()
b_min = b.min()
a_min_idx = np.argmin(a)
b_min_idx = np.argmin(b)

cand = x_np[(x_np[:,0]==a_min_idx)&(x_np[:,1]==b_min_idx)]
if cand.shape[0]>0:
  tgt_idx = np.argmax(cand, axis=0)[2]
  cand_payment1 = a_min + b_min - cand[tgt_idx,2]
else:
  cand_payment1 = a_min + b_min
  
# 候補2は割引額が最大
disc_idx = np.argmax(x_np, axis=0)[2]
cand_idx = x_np[disc_idx,:]
cand_payment2 = a[cand_idx[0]-1] + b[cand_idx[1]-1] - cand_idx[2]

if cand_payment1<cand_payment2:
  print(cand_payment1)
else:
  print(cand_payment2)

Submission status: Wrong Answer

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

p=int()
q=int()
p=a[0]+b[0]
for i in a:
    for s in b: 
       #if i==:
        #else :
        q=i+s
        if q<p:
            p=q
print(p)

Submission status: Wrong Answer

Solution code:
# input
a_n, b_n, m = map(int, input().split())
a = input().split()
b = input().split()
x = [input().split() for i in range(m)]

# calc
min_price = 200000
tmp_price = min_price
for p in x:
   tmp_price = int(a[int(p[0])-1]) + int(b[int(p[1])-2]) - int(p[2])
   if tmp_price < min_price:
       min_price = tmp_price
print(min_price)

Submission status: Wrong Answer

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)]
pay = 0
min_pay = 0
for i in range(M):
  pay = a[xyc[i][0]-1] + b[xyc[i][1]-1] - xyc[i][2]
  if i == 0:
    min_pay = pay
  min_pay = min(min_pay,pay)
for j in range(min(len(a),len(b))):
  min_pay = min(min_pay,a[j]+b[j])
print(min_pay)

Submission status: Wrong Answer

Solution code:
A,B,M = map(int,input().split())
P = list(map(int,input().split()))
Q = list(map(int,input().split()))
S = P
T = Q
S.sort()
T.sort()
minimum = (S[0]+T[0])
for i in range(M):
    x,y,c = map(int,input().split())
    S = P[x-1]+Q[y-1]-c
    if S<minimum:
        if S <= 0:
            minimum = 0
        else:
            minimum = S
print(minimum)

Submission status: Wrong Answer

Solution code:
# input
a_n, b_n, m = map(int, input().split())
a = input().split()
b = input().split()
x = [input().split() for i in range(m)]

# calc
min_price = 100000
tmp_price = min_price
for p in x:
   tmp_price = int(a[int(p[0])-1]) + int(b[int(p[1])-2]) - int(p[2])
   if tmp_price < min_price:
       min_price = tmp_price
print(min_price)

Submission status: Wrong Answer

Solution code:
import sys

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

prices_A=list(map(int,input().split()))
prices_B=list(map(int,input().split()))

dc = dict()
dc_max = 0
dc_max_A= [0 for i in range(A)]
dc_max_B= [0 for i in range(B)]
for i in range(M):
    (x,y,c)=map(int,input().split())
    idx="{} {}".format(x,y)
    dc[idx]=c
    if dc_max < c:
        dc_max = c
    if dc_max_A[x-1] < c:
        dc_max_A[x-1] = c
    if dc_max_B[y-1] < c:
        dc_max_B[y-1] = c

#print(prices_A)
#print(prices_B)
#print(dc)

min_p = prices_A[0] + prices_B[0]
for i in range(A):
    if prices_A[i] - dc_max_A[i] < min_p:
        x=i+1       
        for j in range(B):
            if prices_A[i] + prices_B[j] - dc_max_B[j] < min_p:
                y=j+1
                c=0
                idx = "{} {}".format(x,y)
                if idx in dc:
                    c=dc[idx]
                p = prices_A[i]+prices_B[j]-c

                if p < min_p:
                    min_p=p
print(min_p)

Submission status: Wrong Answer

Solution code:
A, B, M = map(int, raw_input().split())
a_s = sorted(enumerate(map(int, raw_input().split())), key=lambda t: t[1])
b_s = sorted(enumerate(map(int, raw_input().split())), key=lambda t: t[1])
w = [[0] * B] * A
max_w = 0
for _ in xrange(M):
    x, y, c = map(int, raw_input().split())
    w[x-1][y-1] = c
    max_w = max(max_w, c)

cost = 10 ** 20
for i, a in a_s:
    if a + b_s[0][1] - max_w > cost:
        break
    for j, b in b_s:
        cost = min(cost, a + b - w[i][j])
        if a + b - max_w > cost:
            break

print cost