結果

問題 No.1345 Beautiful BINGO
コンテスト
ユーザー rlangevin
提出日時 2023-03-03 08:58:54
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 1,035 ms / 2,000 ms
コード長 1,036 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 134 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 81,536 KB
最終ジャッジ日時 2026-06-03 21:35:33
合計ジャッジ時間 18,884 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

def popcount(n):
    cnt = 0
    while n:
        cnt += n & 1
        n >>= 1
    return cnt

def f(i, j):
    return i * N + j

N, M = map(int, input().split())
A = []
for i in range(N):
    A.extend(list(map(int, input().split())))

ans = 10 ** 18
for s in range(1 << (N + 2)):
    c = popcount(s)
    if c > M:
        continue
    seen = [0] * (N * N)
    cnt = 0
    for i in range(N):
        for j in range(N):
            if (s >> i) & 1:
                seen[f(i, j)] = 1
                cnt += A[f(i, j)]
                continue
            if (s >> N) & 1 and i == j:
                seen[f(i, j)] = 1
                cnt += A[f(i, j)]
                continue
            if (s >> (N + 1)) & 1 and i + j == N - 1:
                seen[f(i, j)] = 1
                cnt += A[f(i, j)]
                continue
    L = [0] * N
    for i in range(N):
        for j in range(N):
            if seen[f(i, j)]:
                continue
            L[j] += A[f(i, j)]
    L.sort()
    ans = min(ans, cnt + sum(L[:M-c]))

print(ans)
0