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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
#ifdef DEBUG
auto operator<<(auto&o,auto x)->decltype(x.end(),o);
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

template<typename T>
void self_max(T& x, T y) {
	x = max(x, y);
}

/*
 * Opis: konstruktor O(n), get\_compressed O(n \log n).
 *   \texttt{group[v]} to numer silnie spójnej wierzchołka $v$,
 *   \texttt{order} to toposort, w którym krawędzie idą w lewo (z lewej są liście),
 *   \texttt{get\_compressed()} zwraca graf silnie spójnych,
 *   \texttt{get\_compressed(false)} nie usuwa multikrawędzi.
 */
struct SCC {
	int n;
	vector<vector<int>> &graph;
	int group_cnt = 0;
	vector<int> group;
	vector<vector<int>> rev_graph;
	vector<int> order;
	void order_dfs(int v) {
		group[v] = 1;
		for(int u : rev_graph[v])
			if(group[u] == 0)
				order_dfs(u);
		order.emplace_back(v);
	}
	void group_dfs(int v, int color) {
		group[v] = color;
		for(int u : graph[v])
			if(group[u] == -1)
				group_dfs(u, color);
	}
	SCC(vector<vector<int>> &_graph) : graph(_graph) {
		n = ssize(graph);
		rev_graph.resize(n);
		REP(v, n)
			for(int u : graph[v])
				rev_graph[u].emplace_back(v);
		group.resize(n);
		REP(v, n)
			if(group[v] == 0)
				order_dfs(v);
		reverse(order.begin(), order.end());
		debug(order);
		group.assign(n, -1);
		for(int v : order)
			if(group[v] == -1)
				group_dfs(v, group_cnt++);
	}
	vector<vector<int>> get_compressed(bool delete_same = true) {
		vector<vector<int>> ans(group_cnt);
		REP(v, n)
			for(int u : graph[v])
				if(group[v] != group[u])
					ans[group[v]].emplace_back(group[u]);
		if(not delete_same)
			return ans;
		REP(v, group_cnt) {
			sort(ans[v].begin(), ans[v].end());
			ans[v].erase(unique(ans[v].begin(), ans[v].end()), ans[v].end());
		}
		return ans;
	}
};

constexpr size_t factor = 1ll << 30;

struct pair_hash {
    inline std::size_t operator()(const std::pair<int,int> & v) const {
        return v.first * factor + v.second;
    }
};

using P = pair<int, int>;
unordered_map<P, int, pair_hash> cache;

const int S = 200;
vector<vector<int>> vertices_faster(S);
vector<vector<pair<int, int>>> edges_faster(S);
vector<vector<int>> values_faster(S, vector (S, -1));

template<typename T>
void my_clear(T& s) {
	const int n = ssize(s);
	REP(i, n)
		s.erase(s.begin());
}

void solve() {
	my_clear(cache);

	int n, m;
	cin >> n >> m;
	debug(n, m);
	vector<int> p(n);
	REP(i, n)
		cin >> p[i];
	debug(p);
	vector<vector<pair<int, int>>> graph(n);
	REP(i, m) {
		int a, b, w;
		cin >> a >> b >> w;
		--a, --b;
		if (a == b and w == 1)
			continue;
		graph[b].emplace_back(a, w);
	}
	REP(i, n) {
		sort(graph[i].begin(), graph[i].end(), [](P a, P b) {
				return a.second < b.second;
				});
	}
	debug(graph);

	int next_free = 0;

	const auto calc = [&](auto&& self, int v, int bound) -> int {
		if (bound == 0)
			return 0;
		{
			const P hash = {v, bound};
			auto it = cache.find(hash);
			if (it != cache.end())
				return it->second;
		}

		const int my_level = next_free;
		++next_free;

		auto& vertices = vertices_faster[my_level];
		auto& edges = edges_faster[my_level];
		auto& values = values_faster[my_level];

		vertices.clear();
		edges.clear();

		const auto dfs = [&](auto&& dfs_self, int x) -> void {
			int ret = x == 0;
			vertices.emplace_back(x);
			values[x] = 0;
			for (auto [a, w] : graph[x]) {
				if (w > 1 or bound > p[a]) {
					if (w * min(p[a], bound / w) > ret) {
						ret = max(ret, w * self(self, a, min(p[a], bound / w)));
					}
				}
				else {
					auto it = cache.find(P{a, bound});
					if (it == cache.end()) {
						edges.emplace_back(x, a);
						if (values[a] == -1) {
							dfs_self(dfs_self, a);
						}
						ret = max(ret, values[a]);
					}
					else {
						ret = max(ret, it->second);
					}
				}
			}
			values[x] = ret;
		};
		dfs(dfs, v);

		const int s = ssize(vertices);

		sort(vertices.begin(), vertices.end());
		auto get_id = [&](int x) -> int {
			return int(lower_bound(vertices.begin(), vertices.end(), x) - vertices.begin());
		};

		vector<vector<int>> mini_graph(s);
		for (auto [a, b] : edges)
			mini_graph[get_id(a)].emplace_back(get_id(b));

		SCC scc(mini_graph);

		const int r = scc.group_cnt;
		const auto dag = scc.get_compressed(false);
		vector<int> best_in_group(r), visited(r);
		REP(i, s) {
			self_max(best_in_group[scc.group[i]], values[vertices[i]]);
		}
		auto mini_dfs = [&](auto&& mini_dfs_self, int x) -> void {
			visited[x] = true;
			for (auto h : dag[x]) {
				if (not visited[h])
					mini_dfs_self(mini_dfs_self, h);
				self_max(best_in_group[x], best_in_group[h]);
			}
		};
		REP(i, r)
			if (not visited[i])
				mini_dfs(mini_dfs, i);
		REP(i, s) {
			const auto hash = P{vertices[i], bound};
			const auto value = best_in_group[scc.group[i]];
			cache.emplace(hash, value);
		}

		const int ret = best_in_group[scc.group[get_id(v)]];

		--next_free;
		for (auto x : vertices)
			values[x] = -1;

		return ret;
	};

	int answer = calc(calc, n - 1, p.back());
	if (answer == 0)
		answer = -1;
	cout << answer << '\n';
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	const int limit_size = 2e7;
	cache.reserve(limit_size);

	REP(i, S) {
		vertices_faster[i].reserve(S);
		edges_faster[i].reserve(S);
	}

	int t;
	cin >> t;
	REP(tt, t) {
		debug(tt);
		solve();
#ifdef TESTS
		if (tt and tt % 1000 == 0)
			cerr << tt << endl;
#endif
	}
}