結果

問題 No.1345 Beautiful BINGO
ユーザー 小野寺健
提出日時 2021-05-27 14:23:36
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,202 bytes
コンパイル時間 89 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2024-11-06 08:53:44
合計ジャッジ時間 6,976 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 23 TLE * 1 -- * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import combinations

N, M = map(int, input().split())

RowSum = []
A = []
for _ in range(N):
    l = list(map(int, input().split()))
    A.append(l)
    RowSum.append(sum(l))
    
RowBit = [] 
for i in range(1 << N):
    cnt = 0
    row = []
    for j in range(N):
        if (i & (1 << j)) != 0:
            row.append(j)
            cnt += RowSum[j]
    RowBit.append((row, cnt))
    
ans = []
for row, cnt in RowBit:
    if len(row) > M:
        continue
    elif len(row) == M:
        ans.append(cnt)
    else:
        for comb in combinations(range(N+2), M-len(row)):
            n = 0
            for c in comb:
                if c == N:
                    for i in range(N):
                        if i not in row and i not in comb:
                            n += A[i][i]
                elif c == N+1:
                    for i in range(N):
                        if i not in row and N-1-i not in comb and not (N in comb and i == N-1-i): 
                            n += A[i][N-1-i]
                else:
                    for i in range(N):
                        if i not in row:
                            n += A[i][c]
            ans.append(cnt+n)
print(min(ans))
0