結果

問題 No.1324 Approximate the Matrix
ユーザー gew1fw
提出日時 2025-06-12 20:34:27
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 913 bytes
コンパイル時間 315 ms
コンパイル使用メモリ 82,132 KB
実行使用メモリ 82,756 KB
最終ジャッジ日時 2025-06-12 20:35:15
合計ジャッジ時間 5,154 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

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