1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5 #include <cmath>
6 #define inf 0x7fffffff
7 #define hh 1e9
8 #define M 60100
9 using namespace std;
10 struct data
11 {
12 int v,next,to;
13 }e[M*6];
14 int head[M],deep[M],cur[M];
15 int n,m,cnt,sum,S,T;
16 inline int read()
17 {
18 int f=1,ans=0;
19 char c;
20 while (!isdigit(c=getchar())) if (c==‘-‘) f=-1;
21 ans=c-‘0‘;
22 while (isdigit(c=getchar())) ans=ans*10+c-‘0‘;
23 return ans*f;
24 }
25 inline void Add(int x,int y,int z)
26 {
27 e[cnt].next=head[x]; e[cnt].to=y; e[cnt].v=z; head[x]=cnt++;
28 e[cnt].next=head[y]; e[cnt].to=x; e[cnt].v=0; head[y]=cnt++;
29 }
30 bool Bfs()
31 {
32 memset(deep,-1,sizeof(deep));
33 deep[0]=0;
34 //for (int i=0;i<=T;i++) cur[i]=head[i];
35 queue<int> q;
36 q.push(0);
37 while (!q.empty())
38 {
39 int now=q.front();q.pop();
40 for (int i=head[now];~i;i=e[i].next)
41 if (deep[e[i].to]==-1 && e[i].v)
42 deep[e[i].to]=deep[now]+1,q.push(e[i].to);
43 }
44 return deep[T]!=-1;
45 }
46 int Dfs(int now,int flow)
47 {
48 if (now==T) return flow;
49 int used=0,f;
50 for (int i=cur[now];~i;i=e[i].next)
51 {
52 //cur[now]=i;
53 if (deep[e[i].to]==deep[now]+1)
54 {
55 f=Dfs(e[i].to,min(flow-used,e[i].v));
56 e[i].v-=f;
57 e[i^1].v+=f;
58 if (e[i].v) cur[now]=i;
59 used+=f;
60 //flow-=f;
61 if (used==flow) return flow;
62 }
63 }
64 if (!used) deep[now]=-1;
65 return used;
66 }
67 int Dinic()
68 {
69 int ans=0;
70 while (Bfs())
71 {
72 for (int i=S;i<=T;i++) cur[i]=head[i];
73 ans+=Dfs(S,inf);
74 }
75 return ans;
76 }
77 int x,y,z;
78 int main()
79 {
80 //memset(head,-1,sizeof(head));
81 n=read(); m=read();
82 S=0; T=n+m+1;
83 memset(head,-1,sizeof(head));
84 for (int i=1;i<=n;i++)
85 {
86 x=read();
87 Add(m+i,m+n+1,x);
88 }
89 for (int i=1;i<=m;i++)
90 {
91 //int x,y,z;
92 x=read(); y=read(); z=read();
93 sum+=z;
94 Add(i,m+x,inf); Add(i,m+y,inf);
95 Add(0,i,z);
96 }
97 printf("%d\n",sum-Dinic());
98 return 0;
99 }