1 #include<cstdio>
2 #include<cstring>
3 #include<iostream>
4 #define maxn 1000000
5 using namespace std;
6
7 struct edge{
8 int v,from;
9 }e1[maxn],e2[maxn];
10
11 int first1[maxn],first2[maxn],tot1,tot2;
12
13 int minn(int a,int b){
14 return a<b?a:b;
15 }
16
17 int minnn(int a,int b,int c){
18 return minn(a,minn(b,c));
19 }
20
21 int mx(int a,int b){
22 return a>b?a:b;
23 }
24
25 int maxxx(int a,int b,int c){
26 return mx(a,mx(b,c));
27 }
28
29 void insert(int u,int v){
30 tot1++;
31 e1[tot1].from = first1[u];
32 e1[tot1].v = v;
33 first1[u] = tot1;
34
35 tot2++;
36 e2[tot2].from = first2[v];
37 e2[tot2].v = u;
38 first2[v] = tot2;
39 }
40
41 int a,b,c,prize[maxn],minpri[maxn],maxpri[maxn];
42 int dis[maxn],que[maxn],head,tail,lag[maxn],n,m;
43 bool book[maxn];
44
45 void STEP1(){
46 for(int i = 1;i <= n;i++){
47 dis[i] = 999999999;
48 minpri[i] = 999999999;
49 }
50 memset(book,false,sizeof(book));
51 tail = 0,head = 0;
52
53 que[head++] = 1;
54 book[1] = true;
55 dis[1] = 0;
56
57 while(tail < head){
58 int p = que[tail];
59
60 for(int i = first1[p];i;i = e1[i].from){
61 minpri[e1[i].v] = minnn(prize[e1[i].v],minpri[e1[i].v],minpri[p]);
62 if(dis[e1[i].v] > dis[p]){
63
64 dis[e1[i].v] = dis[p];
65 if(!book[e1[i].v]){
66 que[head++] = e1[i].v;
67 book[e1[i].v] = true;
68 }
69 }
70 }
71
72 book[p] = 0;
73 tail++;
74 }
75
76 for(int i = 1;i <= n;i++){
77 if(!dis[i]) lag[i]++;
78 }
79
80 for(int i = 1;i <= n;i++) dis[i] = 999999999;
81 tail = 0,head = 0;
82
83 que[head++] = n;
84 book[n] = true;
85 dis[n] = 0;
86
87 while(tail < head){
88 int p = que[tail];
89
90 for(int i = first2[p];i;i = e2[i].from){
91 maxpri[e2[i].v] = maxxx(maxpri[e2[i].v],maxpri[p],prize[e2[i].v]);
92 if(dis[e2[i].v] > dis[p]){
93
94 dis[e2[i].v] = dis[p];
95 if(!book[e2[i].v]){
96 que[head++] = e2[i].v;
97 book[e2[i].v] = true;
98 }
99 }
100 }
101
102 book[p] = 0;
103 tail++;
104 }
105
106 for(int i = 1;i <= n;i++){
107 if(!dis[i]) lag[i]++;
108 }
109 }
110
111 int main(){
112 scanf("%d%d",&n,&m);
113
114 for(int i = 1;i <= n;i++)
115 scanf("%d",&prize[i]);
116
117 for(int i = 0;i < m;i++){
118 scanf("%d%d%d",&a,&b,&c);
119 insert(a,b);
120 if(c%2 == 0) insert(b,a);
121 }
122
123 STEP1();
124
125 int maxx = 0;
126
127 for(int i = 1;i <= n;i++){
128 if(lag[i] == 2){
129 maxx = mx(maxx,maxpri[i]-minpri[i]);
130 }
131 }
132
133 // for(int i = 1;i <= n;i++){
134 // printf("%d/%d ",maxpri[i],minpri[i]);
135 // }
136
137 // cout << endl;
138
139 printf("%d",maxx);
140
141 return 0;
142 }