1 #include<iostream>
2 #include<string>
3 #include<algorithm>
4 #include<cstdio>
5 #include<cstring>
6 #include<cstdlib>
7 #include<cmath>
8 #include<map>
9 using namespace std;
10
11 typedef long long s64;
12 const int ONE=5000005;
13 const s64 INF=21474836400000;
14
15 int n,x;
16 s64 res;
17 int tou,wei,S,T;
18 int Dep[ONE],q[1000001],E[ONE];
19 int next[ONE],first[ONE],go[ONE],tot;
20 s64 w[ONE];
21 s64 Ans;
22
23 int get()
24 {
25 int res=1,Q=1;char c;
26 while( (c=getchar())<48 || c>57 )
27 if(c==‘-‘)Q=-1;
28 res=c-48;
29 while( (c=getchar())>=48 && c<=57 )
30 res=res*10+c-48;
31 return res*Q;
32 }
33
34 int Add(int u,int v,s64 z)
35 {
36 next[++tot]=first[u]; first[u]=tot; go[tot]=v; w[tot]=z;
37 next[++tot]=first[v]; first[v]=tot; go[tot]=u; w[tot]=0;
38 }
39
40 int Bfs()
41 {
42 memset(Dep,0,sizeof(Dep));
43 tou=0; wei=1;
44 q[1]=S; Dep[S]=1;
45 for(int i=S;i<=T;i++) E[i]=first[i];
46 while(tou<wei)
47 {
48 int u=q[++tou];
49 for(int e=first[u];e;e=next[e])
50 {
51 int v=go[e];
52 if(Dep[v] || !w[e]) continue;
53 Dep[v]=Dep[u]+1;
54 q[++wei]=v;
55 }
56 }
57 return (Dep[T]>0);
58 }
59
60 s64 Dfs(int u,s64 Limit)
61 {
62 if(u==T || !Limit) return Limit;
63 s64 from=0,f;
64 for(int &e=E[u];e;e=next[e])
65 {
66 int v=go[e];
67 if(Dep[v]!=Dep[u]+1 || !w[e]) continue;
68 f=Dfs(v,min(Limit,w[e]));
69 w[e]-=f;
70 w[((e-1)^1)+1]+=f;
71 Limit-=f;
72 from+=f;
73 if(!Limit) break;
74 }
75 return from;
76 }
77
78 int main()
79 {
80 n=get();
81 S=0; T=n+1;
82 for(int i=1;i<=n;i++)
83 {
84 x=get();
85 Add(S,i,x);
86 }
87
88 for(int i=1;i<=n;i++)
89 {
90 res=0;
91 for(int j=1;j<=n;j++)
92 {
93 x=get();
94 res+=x; Ans+=x;
95 Add(i,j,2*x);
96 }
97 Add(i,T,res);
98 }
99
100 while(Bfs()) Ans-=Dfs(S,INF);
101
102 printf("%lld",Ans);
103
104 }