結果
問題 | No.1345 Beautiful BINGO |
ユーザー |
|
提出日時 | 2021-01-16 13:42:06 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 619 ms / 2,000 ms |
コード長 | 1,502 bytes |
コンパイル時間 | 149 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 83,024 KB |
最終ジャッジ日時 | 2024-11-28 11:51:13 |
合計ジャッジ時間 | 14,441 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 61 |
ソースコード
N,M = map(int,input().split()) A = [list(map(int,input().split())) for i in range(N)] S = [sum(A[j][i] for j in range(N)) for i in range(N)] tmp_A = [[A[i][j] for j in range(N)] for i in range(N)] tmp_S = [S[i] for i in range(N)] def dfs(i,cost,cnt): res = 10**18 if i==N+2: column = [tmp_S[j] for j in range(N)] column.sort() cost += sum(column[:cnt]) return cost elif i<N: tmp1 = dfs(i+1,cost,cnt) for j in range(N): tmp_A[i][j] = 0 cost += A[i][j] tmp_S[j] -= A[i][j] tmp2 = dfs(i+1,cost,cnt-1) for j in range(N): tmp_A[i][j] = A[i][j] tmp_S[j] += A[i][j] return min(res,tmp1,tmp2) elif i==N: tmp1 = dfs(i+1,cost,cnt) memo = [tmp_A[j][j] for j in range(N)] for j in range(N): tmp_A[j][j] = 0 cost += memo[j] tmp_S[j] -= memo[j] tmp2 = dfs(i+1,cost,cnt-1) for j in range(N): tmp_A[j][j] = memo[j] tmp_S[j] += memo[j] return min(res,tmp1,tmp2) else: tmp1 = dfs(i+1,cost,cnt) memo = [tmp_A[N-1-j][j] for j in range(N)] for j in range(N): tmp_A[N-1-j][j] = 0 cost += memo[j] tmp_S[j] -= memo[j] tmp2 = dfs(i+1,cost,cnt-1) for j in range(N): tmp_A[N-1-j][j] = memo[j] tmp_S[j] += memo[j] return min(res,tmp1,tmp2) print(dfs(0,0,M))