結果

問題 No.1345 Beautiful BINGO
ユーザー 👑 rin204rin204
提出日時 2022-04-16 17:28:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,118 ms / 2,000 ms
コード長 2,336 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 87,092 KB
実行使用メモリ 80,012 KB
最終ジャッジ日時 2023-08-26 19:43:30
合計ジャッジ時間 19,927 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,328 KB
testcase_01 AC 70 ms
71,336 KB
testcase_02 AC 69 ms
71,196 KB
testcase_03 AC 69 ms
71,380 KB
testcase_04 AC 69 ms
71,324 KB
testcase_05 AC 85 ms
75,688 KB
testcase_06 AC 72 ms
71,276 KB
testcase_07 AC 121 ms
77,408 KB
testcase_08 AC 69 ms
71,084 KB
testcase_09 AC 68 ms
71,216 KB
testcase_10 AC 71 ms
70,924 KB
testcase_11 AC 73 ms
71,096 KB
testcase_12 AC 71 ms
71,468 KB
testcase_13 AC 122 ms
77,268 KB
testcase_14 AC 69 ms
71,200 KB
testcase_15 AC 70 ms
71,324 KB
testcase_16 AC 72 ms
71,152 KB
testcase_17 AC 71 ms
71,084 KB
testcase_18 AC 71 ms
71,396 KB
testcase_19 AC 71 ms
71,248 KB
testcase_20 AC 69 ms
71,192 KB
testcase_21 AC 69 ms
71,132 KB
testcase_22 AC 81 ms
75,972 KB
testcase_23 AC 83 ms
75,876 KB
testcase_24 AC 125 ms
77,724 KB
testcase_25 AC 183 ms
79,020 KB
testcase_26 AC 294 ms
78,056 KB
testcase_27 AC 1,113 ms
78,956 KB
testcase_28 AC 132 ms
77,480 KB
testcase_29 AC 293 ms
78,056 KB
testcase_30 AC 85 ms
76,152 KB
testcase_31 AC 146 ms
78,392 KB
testcase_32 AC 158 ms
78,444 KB
testcase_33 AC 1,116 ms
78,396 KB
testcase_34 AC 85 ms
76,408 KB
testcase_35 AC 103 ms
76,940 KB
testcase_36 AC 443 ms
78,380 KB
testcase_37 AC 81 ms
76,188 KB
testcase_38 AC 339 ms
78,832 KB
testcase_39 AC 1,103 ms
78,720 KB
testcase_40 AC 444 ms
78,100 KB
testcase_41 AC 340 ms
79,004 KB
testcase_42 AC 71 ms
71,216 KB
testcase_43 AC 70 ms
71,396 KB
testcase_44 AC 72 ms
71,288 KB
testcase_45 AC 73 ms
71,196 KB
testcase_46 AC 71 ms
71,096 KB
testcase_47 AC 70 ms
71,488 KB
testcase_48 AC 70 ms
71,212 KB
testcase_49 AC 72 ms
71,280 KB
testcase_50 AC 71 ms
71,328 KB
testcase_51 AC 69 ms
71,144 KB
testcase_52 AC 95 ms
77,152 KB
testcase_53 AC 1,066 ms
78,964 KB
testcase_54 AC 292 ms
78,860 KB
testcase_55 AC 445 ms
79,464 KB
testcase_56 AC 97 ms
76,992 KB
testcase_57 AC 1,093 ms
80,012 KB
testcase_58 AC 1,115 ms
78,964 KB
testcase_59 AC 1,118 ms
78,992 KB
testcase_60 AC 603 ms
78,724 KB
testcase_61 AC 618 ms
78,632 KB
testcase_62 AC 87 ms
76,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def popcount(x):
    x = x - ((x >> 1) & 0x55555555)
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
    x = (x + (x >> 4)) & 0x0f0f0f0f
    x += x >> 8
    x += x >> 16
    return x & 0x0000003f

n, m = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(n)]

ans = 1 << 30
for bit in range(1 << n):
    pc = popcount(bit)
    if pc > m or pc + n < m:
        continue
    tmp = 0
    lst = [0] * n
    for i in range(n):
        if bit >> i & 1:
            for j in range(n):
                tmp += A[i][j]
        else:
            for j in range(n):
                lst[j] += A[i][j]
    lst.sort()
    tmp += sum(lst[:m - pc])
    ans = min(ans, tmp)
    
x = 0
for i in range(n):
    x += A[i][i]
    
for bit in range(1 << n):
    pc = popcount(bit) + 1
    if pc > m or pc + n < m:
        continue
    tmp = x
    lst = [0] * n
    for i in range(n):
        if bit >> i & 1:
            for j in range(n):
                if i != j:
                    tmp += A[i][j]
        else:
            for j in range(n):
                if i != j:
                    lst[j] += A[i][j]
    lst.sort()
    tmp += sum(lst[:m - pc])
    ans = min(ans, tmp)
    
    
x = 0
for i in range(n):
    x += A[i][n - 1 - i]
    
for bit in range(1 << n):
    pc = popcount(bit) + 1
    if pc > m or pc + n < m:
        continue
    tmp = x
    lst = [0] * n
    for i in range(n):
        if bit >> i & 1:
            for j in range(n):
                if i != n - 1 - j:
                    tmp += A[i][j]
        else:
            for j in range(n):
                if i != n - 1 - j:
                    lst[j] += A[i][j]
    lst.sort()
    tmp += sum(lst[:m - pc])
    ans = min(ans, tmp)


x = 0
for i in range(n):
    x += A[i][i]
    if i != n - 1 - i:
        x += A[i][n - 1 - i]
    
for bit in range(1 << n):
    pc = popcount(bit) + 2
    if pc > m or pc + n < m:
        continue
    tmp = x
    lst = [0] * n
    for i in range(n):
        if bit >> i & 1:
            for j in range(n):
                if i != n - 1 - j and i != j:
                    tmp += A[i][j]
        else:
            for j in range(n):
                if i != n - 1 - j and i != j:
                    lst[j] += A[i][j]
    lst.sort()
    tmp += sum(lst[:m - pc])
    ans = min(ans, tmp)

print(ans)
            
    
    
0