forked from abeaumont/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd.py
More file actions
executable file
·29 lines (29 loc) · 760 Bytes
/
d.py
File metadata and controls
executable file
·29 lines (29 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env pypy3
# https://abc099.contest.atcoder.jp/tasks/abc099_d
n, k = map(int, input().split())
c, d = [0] * n, [0] * k
for i in range(k): d[i] = [int(x) for x in input().split()]
for i in range(n): c[i] = [int(x) - 1 for x in input().split()]
z = {}
def f(k, m):
if (k, m) in z: return z[(k, m)]
s = 0
for i in range(n):
for j in range(n):
if (i + j) % 3 == m:
s += d[c[i][j]][k]
z[(k, m)] = s
return s
def g(i, s):
if i == 3: return 0
m = None
for l in range(k):
if s[l]: continue
s[l] = True
r = g(i + 1, s)
s[l] = False
if r is None: continue
r += f(l, i)
if m is None or r < m: m = r
return m
print(g(0, [False] * k))