1 #include <bits/stdc++.h>
2 using namespace std;
3 struct edge
4 {
5 int u, v, w, c, nxt;
6 }e[80005];
7 int n, fst[1005], t[45][105], etot = 1, level[1005];
8 int q[1005], belong[1005], fin[105], pth[1005], use;
9 bool inq[1005];
10
11 void addedge(int u, int v, int w, int c)
12 {
13 e[++etot] = (edge){u, v, w, c, fst[u]}, fst[u] = etot;
14 e[++etot] = (edge){v, u, 0, -c, fst[v]}, fst[v] = etot;
15 }
16
17 bool SPFA()
18 {
19 int front = 0, back, u;
20 memset(level, 63, sizeof(level));
21 level[n + 1] = 0;
22 q[back = 1] = n + 1, inq[n + 1] = true;
23 while(front != back)
24 {
25 u = q[(++front % 1005)];
26 front %= 1005, inq[u] = false;
27 for(int i = fst[u]; i; i = e[i].nxt)
28 if(e[i].w && level[e[i].v] > level[u] + e[i].c)
29 {
30 level[e[i].v] = level[u] + e[i].c;
31 pth[e[i].v] = i;
32 if(!inq[e[i].v])
33 {
34 q[(++back % 1005)] = e[i].v;
35 back %= 1005, inq[e[i].v] = true;
36 }
37 }
38 }
39 return level[n + 2] < 1000000000;
40 }
41
42 int Edmond_Karp()
43 {
44 for(int i = pth[n + 2]; i; i = pth[e[i].u])
45 {
46 --e[i].w, ++e[i ^ 1].w;
47 if(e[i].u == n + 1) use = e[i].v;
48 }
49 return level[n + 2];
50 }
51
52 int main()
53 {
54 int m, tmp, ans = 0, ptot;
55 scanf("%d%d", &n, &m);
56 for(int i = 1; i <= n; ++i)
57 {
58 scanf("%d", &tmp);
59 addedge(i, n + 2, tmp, 0);
60 }
61 for(int i = 1; i <= n; ++i)
62 for(int j = 1; j <= m; ++j)
63 scanf("%d", &t[i][j]);
64 ptot = n + 2;
65 for(int i = 1; i <= m; ++i)
66 {
67 belong[++ptot] = i, ++fin[i];
68 for(int j = 1; j <= n; ++j)
69 addedge(ptot, j, 1, t[j][i]);
70 addedge(n + 1, ptot, 1, 0);
71 }
72 while(SPFA())
73 {
74 ans += Edmond_Karp();
75 tmp = belong[++ptot] = belong[use], ++fin[tmp];
76 for(int i = 1; i <= n; ++i)
77 addedge(ptot, i, 1, t[i][tmp] * fin[tmp]);
78 addedge(n + 1, ptot, 1, 0);
79 }
80 printf("%d\n", ans);
81 return 0;
82 }