#include<cstdio>
#include<algorithm>
#define MN 40001
#define int long long
using namespace std;
int read_p,read_ca,read_f;
inline int read(){
read_p=0;read_ca=getchar();read_f=1;
while(read_ca<‘0‘||read_ca>‘9‘) read_f=read_ca==‘-‘?-1:read_f,read_ca=getchar();
while(read_ca>=‘0‘&&read_ca<=‘9‘) read_p=read_p*10+read_ca-48,read_ca=getchar();
return read_p*read_f;
}
const int INF=1e18;
struct na{int y,z,ne;}b[MN*100];
int n,m,a[1001],num=1,no=0,S,T,mmh=0,g[MN],c[MN],d[MN],l[MN];
inline void in(int x,int y,int z){b[++num].y=y;b[num].z=z;b[num].ne=l[x];l[x]=num;}
inline void add(int x,int y,int z,int Z=0){in(x,y,z);in(y,x,Z);}
inline int min(int a,int b){return a<b?a:b;}
int sap(int x,int f){
if (x==T) return f;
int h=0,q;
for (int i=d[x];i;i=b[i].ne)
if (b[i].z&&g[b[i].y]+1==g[x]){
q=sap(b[i].y,min(b[i].z,f-h));
h+=q;b[i].z-=q;b[i^1].z+=q;
if (h==f||g[S]==no) return h;
}
if (!(--c[g[x]])) g[S]=no;d[x]=l[x];c[++g[x]]++;
return h;
}
signed main(){
register int i,j;
n=read();S=n+1;T=no=n+2;
for (i=1;i<=n;i++) add(i,T,read());
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) m=read(),mmh+=m,a[j]+=m,add(i,j,m,m);
for (i=1;i<=n;i++) add(S,i,a[i]);
for (;g[S]<no;mmh-=sap(i,INF));
printf("%lld\n",mmh);
}