1 #include<algorithm>
2 #include<iostream>
3 #include<cstring>
4 #include<cstdio>
5 //by NeighThorn
6 #define inf 0x3f3f3f3f
7 using namespace std;
8
9 const int maxn=60000+5,maxm=400000+5;
10
11 int n,m,S,T,cnt,sum,hd[maxn],fl[maxm],to[maxm],nxt[maxm],pos[maxn];
12
13 inline bool bfs(void){
14 memset(pos,-1,sizeof(pos));
15 int head=0,tail=0,q[maxn];
16 q[0]=S,pos[S]=0;
17 while(head<=tail){
18 int top=q[head++];
19 for(int i=hd[top];i!=-1;i=nxt[i])
20 if(pos[to[i]]==-1&&fl[i])
21 pos[to[i]]=pos[top]+1,q[++tail]=to[i];
22 }
23 return pos[T]!=-1;
24 }
25
26 inline int find(int v,int f){
27 if(v==T)
28 return f;
29 int res=0,t;
30 for(int i=hd[v];i!=-1&&f>res;i=nxt[i])
31 if(pos[to[i]]==pos[v]+1&&fl[i])
32 t=find(to[i],min(f-res,fl[i])),fl[i]-=t,fl[i^1]+=t,res+=t;
33 if(!res)
34 pos[v]=-1;
35 return res;
36 }
37
38 inline int dinic(void){
39 int res=0,t;
40 while(bfs())
41 while(t=find(S,inf))
42 res+=t;
43 return res;
44 }
45
46 inline void add(int s,int x,int y){
47 fl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++;
48 fl[cnt]=0;to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++;
49 }
50
51 signed main(void){
52 scanf("%d%d",&n,&m);T=n+m+1;
53 memset(hd,-1,sizeof(hd));S=sum=0;
54 for(int i=1,x;i<=n;i++)
55 scanf("%d",&x),add(x,i,T);
56 for(int i=1,s,x,y;i<=m;i++)
57 scanf("%d%d%d",&x,&y,&s),add(inf,i+n,x),add(inf,i+n,y),add(s,S,i+n),sum+=s;
58 printf("%d\n",sum-dinic());
59 return 0;
60 }//Cap ou pas cap. Cap.