1 #include<cstdio>
2
3 #include<cstdlib>
4
5 #include<cmath>
6
7 #include<cstring>
8
9 #include<algorithm>
10
11 #include<iostream>
12
13 #include<vector>
14
15 #include<map>
16
17 #include<set>
18
19 #include<queue>
20
21 #include<string>
22
23 #define inf 1000000000
24
25 #define maxn 200000+5
26
27 #define maxm 2000000+5
28
29 #define eps 1e-10
30
31 #define ll long long
32
33 #define pa pair<int,int>
34
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42
43 #define for4(i,x) for(int i=head[x],y;i;i=e[i].next)
44
45 #define for5(n,m) for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)
46
47 #define mod 1000000007
48
49 using namespace std;
50
51 inline int read()
52
53 {
54
55 int x=0,f=1;char ch=getchar();
56
57 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
58
59 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
60
61 return x*f;
62
63 }
64 int n,m,k,cnt[maxn],now[maxn],cntcnt,mincost,maxflow,tot=1,s,t,a[1000],b[1000][1000],sum,c[maxn],head[maxn],d[maxn],from[2*maxm];
65
66 bool v[maxn];
67
68 queue<int>q;
69
70 struct edge{int from,go,next,v,c;}e[2*maxm];
71
72 void add(int x,int y,int v,int c)
73
74 {
75
76 e[++tot]=(edge){x,y,head[x],v,c};head[x]=tot;
77
78 e[++tot]=(edge){y,x,head[y],0,-c};head[y]=tot;
79
80 }
81
82 bool spfa()
83
84 {
85
86 for (int i=0;i<=cntcnt;i++){v[i]=0;d[i]=inf;}
87
88 q.push(s);d[s]=0;v[s]=1;
89
90 while(!q.empty())
91
92 {
93
94 int x=q.front();q.pop();v[x]=0;
95
96 for (int i=head[x],y;i;i=e[i].next)
97
98 if(e[i].v&&d[x]+e[i].c<d[y=e[i].go])
99
100 {
101
102 d[y]=d[x]+e[i].c;from[y]=i;
103
104 if(!v[y]){v[y]=1;q.push(y);}
105
106 }
107
108 }
109
110 return d[t]!=inf;
111
112 }
113
114 void update()
115
116 {
117
118 for(int i=from[t];i!=s;i=from[e[i].from])
119 {
120 e[i].v--;e[i^1].v++;
121 }
122 mincost+=d[t];sum--;
123
124 }
125
126 int main()
127
128 {
129
130 freopen("input.txt","r",stdin);
131
132 freopen("output.txt","w",stdout);
133
134 n=read();m=read();
135 for1(i,n)a[i]=read(),sum+=a[i];
136 for1(i,n)for1(j,m)b[i][j]=read();
137 s=0;t=n+1;cntcnt=n+m+1;
138 for1(i,n)add(s,i,a[i],0);
139 for1(i,m)add(n+1+i,t,1,0),cnt[i]=1,now[i]=tot;
140 for1(i,n)for1(j,m)add(i,n+1+j,1,b[i][j]);
141 while(sum)
142 {
143 if(spfa())update();
144 for1(i,m)
145 if(!e[now[i]^1].v)
146 {
147 add(++cntcnt,t,1,0),now[i]=tot,cnt[i]++;
148 for1(j,n)add(j,cntcnt,1,cnt[i]*b[j][i]);
149 break;
150 }
151 }
152 cout<<mincost<<endl;
153
154 return 0;
155
156 }