import heapq n, k = map(int, input().split()) A = list(map(int, input().split())) B = list(map(int, input().split())) P = [list(map(int, input().split())) for _ in range(n)] sum_p_squared = sum(p * p for row in P for p in row) remaining_supply = A.copy() remaining_demand = B.copy() heap = [] for i in range(n): for j in range(n): if remaining_supply[i] > 0 and remaining_demand[j] > 0: initial_m = 1 - 2 * P[i][j] heapq.heappush(heap, (initial_m, i, j)) sum_m = 0 for _ in range(k): while True: if not heap: break # This should not happen as per problem statement m, i, j = heapq.heappop(heap) if remaining_supply[i] > 0 and remaining_demand[j] > 0: break sum_m += m remaining_supply[i] -= 1 remaining_demand[j] -= 1 next_m = m + 2 heapq.heappush(heap, (next_m, i, j)) print(sum_p_squared + sum_m)