1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<queue>
5 using namespace std;
6
7 const int INF=0x7f7f7f7f;
8 const int T=1004;
9 const int N=1005;
10 const int M=50005;
11
12 struct Edge
13 {
14 int from,to,v,c,next;//from点到to点 v代表容量 c代表花费 next下一条边
15 }E[M];
16 int node=1;
17 int head[N],from[N],dis[N],vis[N];
18
19 int n,m,ans;
20
21 void ins(int from,int to,int v,int c)
22 {
23 node++;
24 E[node]=(Edge){from,to,v,c,head[from]};
25 head[from]=node;
26 }
27
28 void insert(int from,int to,int v,int c)
29 {
30 ins(from,to,v,c);ins(to,from,0,-c);
31 }
32
33 bool spfa()
34 {
35 queue<int> Q;
36 memset(dis,0x7f,sizeof(dis));
37 Q.push(0);dis[0]=0;vis[0]=1;
38 while(!Q.empty())
39 {
40 int q=Q.front();Q.pop();
41 for(int i=head[q];i;i=E[i].next)
42 if(E[i].v>0&&dis[q]+E[i].c<dis[E[i].to])
43 {
44 dis[E[i].to]=dis[q]+E[i].c;
45 from[E[i].to]=i;
46 if(!vis[E[i].to])
47 {
48 Q.push(E[i].to);
49 vis[E[i].to]=1;
50 }
51 }
52 vis[q]=0;
53 }
54 return dis[T]!=INF;
55 }
56
57 void mcf()
58 {
59 int x=INF;
60 for(int i=from[T];i;i=from[E[i].from])
61 x=min(E[i].v,x);
62 for(int i=from[T];i;i=from[E[i].from])
63 {
64 ans+=x*E[i].c;
65 E[i].v-=x;E[i^1].v+=x;
66 }
67 }
68
69 int main()
70 {
71 scanf("%d %d",&n,&m);
72 int l=0,r;
73 for(int i=1;i<=n;i++)
74 {
75 scanf("%d",&r);
76 int x=r-l;
77 if(x>0) insert(0,i,x,0);
78 else insert(i,T,-x,0);
79 insert(i+1,i,INF,0);
80 l=r;
81 }
82 insert(n+1,T,l,0);
83 for(int i=1;i<=m;i++)
84 {
85 int s,t,c;
86 scanf("%d %d %d",&s,&t,&c);
87 insert(s,t+1,INF,c);
88 }
89 while(spfa()) mcf();
90 printf("%d\n",ans);
91 return 0;
92 }