1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<iostream>
#include<algorithm>
#include<vector>

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)

using namespace std;

const int MAX_N = 100, MAX_M = 200;
const int MAX_P = 1000000000, MAX_Q = 32000, MAX_R = 32000;

short T[1+MAX_N][1+MAX_Q];
int R[1+MAX_N][1+MAX_R];

int P[1+MAX_N];
int W[1+MAX_M], V[1+MAX_M];
short A[1+MAX_M], B[1+MAX_M];

int T1[1+MAX_Q];

int nTC, n, m;

vector<pair<short,int>> q1, q2;

vector<short> E[1+MAX_N], F[1+MAX_N];

int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> nTC;
	REP(iTC, nTC) {
		//cerr << "test iTC="<<iTC<<endl;
		cin >> n >> m;
		FOR(i, 1, n) cin >> P[i];
		FOR(i, 1, n) { E[i].clear(); F[i].clear(); }
		REP(i, m) cin >> A[i] >> B[i] >> W[i];
		REP(i, m) {
			V[i] = P[B[i]] / W[i];
			E[A[i]].push_back(i);
			F[B[i]].push_back(i);
		}
		FOR(i, 1, n) {			
			sort(E[i].begin(), E[i].end(), [](int i, int j) { 
				return W[i] < W[j]; 
			});
			sort(F[i].begin(), F[i].end(), [](int i, int j) { 
				return W[i] < W[j]; 
			});
		}
		FOR(i, 1, n) FOR(j, 1, MAX_Q) T[i][j] = 0;
		
		T[1][1] = 1;
		q1.clear(); q2.clear();
		q1.push_back(make_pair(1, 1));
		
		{
			auto& q = q1;
			auto& q0 = q2;
		
			while(q.size()) {
				//cerr << "forward pass..., q.size()=" << q.size() <<endl;
				q0.clear();
				int qs = q.size();
				REP(iq, qs) {
					short ix = q[iq].first;
					int s = q[iq].second;
					//cerr << "T: ix=" << ix << " s=" << s << endl;
					int max_w = MAX_Q/s;
					auto es = E[ix].size();
					REP(j, es) {
						short i = E[ix][j];
						short b = B[i];
						int w = W[i];
						//cerr << "i=" << i << " b="<<b << " w=" << w << " s*w=" << s*w << endl;
						if (w > max_w) break;
						if (s <= V[i] && !T[b][s*w]) {
							T[b][s*w] = 1;
							q0.push_back(make_pair(b, s*w));
						}
					}
				}
				swap(q, q0);
			}
		}
		q1.clear(); q2.clear();
		FOR(i, 1, n) FOR(j, 1, MAX_R) R[i][j] = 0;
		R[n][1] = P[n];
		q1.push_back(make_pair(n, 1));				
		{			
			auto& q = q1;
			auto& q0 = q2;
			
			while(q.size()) {
				//cerr << "reverse pass..., q.size()=" << q.size() <<endl;
				q0.clear();
				int qs = q.size();
				REP(iq, qs) {
					short ix = q[iq].first;
					int s = q[iq].second;
					int x = R[ix][s];
					//cerr << "R: ix=" << ix << " s=" << s << " x=" << x << endl;
					int max_w = MAX_R/s;
					auto es = F[ix].size();
					REP(j, es) {
						short i = F[ix][j];
						short b = A[i];
						int w = W[i];
						int new_x = min(P[b], x / w);
						//cerr<<"i="<<i<<" b="<<b<<" w="<<w<<" s*w="<<s*w<<" new_x="<< new_x << endl;
						if (w > max_w) break;
						if (new_x > R[b][s*w]) {
							R[b][s*w] = new_x;
							q0.push_back(make_pair(b, s*w));
						}
					}
				}
				swap(q, q0);
				//break;
			}
		}
		//cerr << "matching pass..." <<endl;
		int res = -1;
		FOR(i, 1, n) {
			int tix = 0;
			FOR(j, 1, MAX_Q) {
				if (T[i][j]) tix = j;
				T1[j] = tix;
			}
			FOR(j, 1, MAX_R) {
				if (R[i][j]) {
					res = max(res, j*T1[min(MAX_Q, R[i][j])]);
				}
			}
		}
		REP(k, m) {
			int i=A[k], b=B[k], w=W[k];
			int tix = 0;
			FOR(j, 1, MAX_Q) {
				if (T[i][j]) tix = j;
				T1[j] = tix;
			}
			FOR(j, 1, MAX_R) {
				if (R[b][j]) {
					res = max(res, j*w*T1[min(MAX_Q, R[b][j] / w)]);
				}
			}
		}
		//cerr << "output result..." <<endl;
		
		cout << ((res>0) ? res : -1) << endl;
			
	}

	return 0;
}