1 #include<cstdio>
2 #include<cstdlib>
3 #include<cmath>
4 #include<cstring>
5 #include<algorithm>
6 #include<iostream>
7 #include<vector>
8 #include<map>
9 #include<set>
10 #include<queue>
11 #include<string>
12 #define inf 100000000000000ll
13 #define maxn 100000+5
14 #define maxm 1100000+5
15 #define eps 1e-10
16 #define ll long long
17 #define pa pair<int,int>
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
23 #define mod 1000000007
24 using namespace std;
25 inline ll read()
26 {
27 ll x=0,f=1;char ch=getchar();
28 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
29 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}
30 return x*f;
31 }
32 int n,m,s,t,tot=1,head[maxn],cur[maxn],h[maxn];
33 queue<int>q;
34 ll maxflow,sum,a[maxn],b[maxn];
35 struct edge{int go,next;ll v;}e[2*maxm];
36 void add(int x,int y,ll v)
37 {
38 e[++tot]=(edge){y,head[x],v};head[x]=tot;
39 e[++tot]=(edge){x,head[y],0};head[y]=tot;
40 }
41 bool bfs()
42 {
43 for(int i=s;i<=t;i++)h[i]=-1;
44 q.push(s);h[s]=0;
45 while(!q.empty())
46 {
47 int x=q.front();q.pop();
48 for(int i=head[x];i;i=e[i].next)
49 if(e[i].v&&h[e[i].go]==-1)
50 {
51 h[e[i].go]=h[x]+1;q.push(e[i].go);
52 }
53 }
54 return h[t]!=-1;
55 }
56 ll dfs(int x,ll f)
57 {
58 if(x==t) return f;
59 ll tmp,used=0;
60 for(int i=cur[x];i;i=e[i].next)
61 if(e[i].v&&h[e[i].go]==h[x]+1)
62 {
63 tmp=dfs(e[i].go,min(e[i].v,f-used));
64 e[i].v-=tmp;if(e[i].v)cur[x]=i;
65 e[i^1].v+=tmp;used+=tmp;
66 if(used==f)return f;
67 }
68 if(!used) h[x]=-1;
69 return used;
70 }
71 void dinic()
72 {
73 maxflow=0;
74 while(bfs())
75 {
76 for (int i=s;i<=t;i++)cur[i]=head[i];maxflow+=dfs(s,inf);
77 }
78 }
79 int main()
80 {
81 freopen("input.txt","r",stdin);
82 freopen("output.txt","w",stdout);
83 n=read();s=0;t=n+1;
84 for1(i,n)a[i]=read();
85 for1(i,n)for1(j,n)
86 {
87 ll x=read();sum+=x;b[i]+=x;
88 if(i==j)continue;
89 add(i,j,(ll)2*x);
90 }
91 for1(i,n)add(s,i,a[i]),add(i,t,b[i]);
92 dinic();
93 cout<<sum-maxflow<<endl;
94 return 0;
95 }