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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 5;

#define st first
#define nd second

typedef pair<int,int> pun;
typedef long long ll;

vector<int> p;
vector<vector<pair<int,int>>> G;
vector<vector<pair<int,int>>> T;

vector<set<int>> from_one;

int get_node(pair<int, int> e) {
	return e.st;
}
int get_weight(pair<int, int> e) {
	return e.nd;
}

set<pair<int,int>> to_explore;

void explore(int v, int mult, int bound) {
		for (auto e : G[v]) {
			int node = get_node(e);
			int w = get_weight(e);
			int limit = p[node];
			ll P = mult * (ll)w;
			if (P <= limit && P <= bound && (from_one[node].find(P) == from_one[node].end())) {
				from_one[node].insert(P);
				to_explore.insert({node, P});
			}
		}
}

void explore_init(int bound) {
	to_explore.insert({1, 1});
	while (!to_explore.empty()) {
		auto iter = to_explore.begin();
		int node = iter->first;
		int mult = iter->second;
		to_explore.erase(iter);
		explore(node, mult, bound);
	}
}



vector<map<int,int>> from_end; // You can get from x to end <max_mult, limit>

set<pair<int,pair<int,int>>> bfs_queue; // <limit, <mult, node_id>>

map<pair<int,int>,bool> vis;

void explore_back(int v, int mult, int bound) {
//	cerr << "v=" << v << " mult=" << mult << " limit=" << from_end[v][mult] << "\n";
//	assert(!vis[make_pair(v, mult)]);
//	vis[make_pair(v, mult)] = true;
	// TODO: remove assert
//	assert(from_end[v].find(mult) != from_end[v].end());
	int limit = from_end[v][mult];
	for (auto e : T[v]) {
		int node = get_node(e);
		int w = get_weight(e);
		int edge_limit = p[node];
		ll new_mult = mult * (ll)w;
		ll new_limit = min(edge_limit, (int)(floor(limit / (double)w)));
		if (new_limit <= 0) continue;
		if (new_mult > bound) continue;
		auto iter = from_end[node].find(new_mult);
		if (iter == from_end[node].end() || iter->second < new_limit) {
			bfs_queue.erase({from_end[node][new_mult], {new_mult, node}});
			from_end[node][new_mult] = new_limit;
			bfs_queue.insert({new_limit, {new_mult, node}});
		}
	}
}

void explore_back_init(int n, int bound) {
	from_end[n][1] = p[n];
	bfs_queue.insert({p[n], {1, n}});
	while(!bfs_queue.empty()) {
		auto iter = bfs_queue.rbegin();
		int node = iter->second.second;
		int mult = iter->second.first;
		bfs_queue.erase(*iter);
		explore_back(node, mult, bound); 
	}
}


void clear() {
	G.clear();
	T.clear();
	p.clear();
	from_one.clear();
	from_end.clear();
	bfs_queue.clear();
	vis.clear();
}



void test() {
	clear();
	int n, m;
	cin >> n >> m;
	p.resize(n+1);
	G.resize(n+1);
	T.resize(n+1);
	for (int i = 1; i <= n; i ++) cin >> p[i];
	for (int i = 0; i < m; i ++) {
		int a, b, w;
		cin >> a >> b >> w;
		G[a].push_back({b, w});
		T[b].push_back({a, w});
	}
/*	for (int i = 1; i <= n; i ++ ){
		cerr << ss[i] << " ";
	}
	cerr << "\n";

	for (int i = 1; i <= n; i ++ ){
		cerr << post[i] << " ";
	}
	cerr << "\n";
*/


	from_one.resize(n+1);
	from_end.resize(n+1);
	int bound = sqrt(p[n]) + 1;
	from_one[1].insert(1);
	explore_init(bound);
	explore_back_init(n, bound);

	ll score = -1;

	if (n == 1) score = 1;

	for (int a = 1; a <= n; a ++) {
//		cerr << "a = " << a << "\n";
		for (auto x : from_end[a]) {
			int mult = x.st;
			int maxi = x.nd;
//			cerr << "mult=" << mult << " maxi=" << maxi << "\n";
			for (auto e : T[a]) {
				int node = get_node(e);
				int w = get_weight(e);
				int limit = p[node];
				ll new_maxi = min(limit, (int)(floor(maxi / (double)w)));
				ll new_mult = mult * w;
//				cerr << "new_mult=" << new_mult << " new_maxi=" << new_maxi << "\n";
				if (from_one[node].empty()) continue;
				auto iter = from_one[node].upper_bound(new_maxi);
				if (iter == from_one[node].begin()) continue;
				iter --;
//				cerr << "found path:: " << *iter << "\n"; 
				score = max(score, (*iter) * new_mult);
			}
		}
	}
	cout << score << "\n";	
//	cerr << explore_back_cnt << "\n";
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	for(int i =1; i <= t; i++) test();

}