终于搞明白了……
x到y有边意味着选x必须选y,所以才会有闭合子图中一个点的后继一定也在这个闭合子图中。
接下来按照s连正权,负权连t就ok了
type node=record
go,next,c:longint;
end;
var i,n,m,s,t,max,tmp,ans,tot,x,y,z:longint;
h,first,q,cur:array[..] of longint;
e:array[..] of node;
function min(x,y:longint):longint;
begin
if x<y then exit(x) else exit(y);
end;
procedure insert(x,y,z:longint);
begin
inc(tot);
e[tot].go:=y;
e[tot].c:=z;
e[tot].next:=first[x];
first[x]:=tot;
inc(tot);
e[tot].go:=x;
e[tot].c:=;
e[tot].next:=first[y];
first[y]:=tot;
end;
procedure init;
begin
readln(n,m);
s:=;t:=m+n+;tot:=;
for i:= to n do
begin
read(x);
insert(s,i,x);
end;
max:=;
for i:= to m do
begin
readln(x,y,z);inc(max,z);
insert(n+i,t,z);
insert(x,n+i,maxlongint>>);
insert(y,n+i,maxlongint>>);
end;
end;
function bfs:boolean;
var i,x,y,head,tail:longint;
begin
fillchar(h,sizeof(h),);
head:=;tail:=;q[]:=s;h[s]:=;
while head<tail do
begin
inc(head);
x:=q[head];
i:=first[x];
while i<> do
begin
y:=e[i].go;
if (h[y]=) and (e[i].c<>) then
begin
h[y]:=h[x]+;
inc(tail);
q[tail]:=y;
end;
i:=e[i].next;
end;
end;
exit(h[t]<>);
end;
function dfs(x,f:longint):longint;
var i,tmp,used:longint;
begin
if (x=t) or (f=) then exit(f);
tmp:=;used:=;
i:=cur[x];
while i<> do
begin
y:=e[i].go;
if (e[i].c<>) and (h[y]=h[x]+) then
begin
tmp:=dfs(y,min(e[i].c,f-used));
dec(e[i].c,tmp);
inc(e[i xor ].c,tmp);
if e[i].c<> then cur[x]:=i;
inc(used,tmp);
if used=f then exit(f);
end;
i:=e[i].next;
end;
if used= then h[x]:=-;
exit(used);
end;
procedure dinic;
begin
ans:=;
while bfs do
begin
for i:= to t do cur[i]:=first[i];
inc(ans,dfs(,maxlongint>>));
end;
end;
procedure main;
begin
dinic;
writeln(max-ans);
end;
begin
init;
main;
end.