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
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i=(a); i<(b); i++)
#define FORD(i, a,  b) for (auto i=(a); i>(b); i--)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define ft first
#define sd second
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif

template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = std::min(a, (T1)b);	}
template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = std::max(a, (T1)b);	}

const int maxN = 101, maxSq = 31'777;

int n, sq, res, maxQuot[maxSq][maxN], T[maxN];
std::vector <std::pair <int, int> > G[maxN];
std::vector <int> possPref[maxN], One[maxN], enO[maxN];
std::bitset <maxSq> possPrefBitset[maxN];

std::priority_queue <std::pair <int, int> > H;
std::queue <int> Q;

void clean()
{
	FOR(i, 1, n+1)
	{
		G[i].clear();
		One[i].clear();
		enO[i].clear();
		possPref[i].clear();
		possPrefBitset[i].reset();
		FOR(d, 1, sq+1)
			maxQuot[d][i] = 0;
	}
}

void read()
{
	int m;
	scanf ("%d%d", &n, &m);
	FOR(i, 1, n+1)
		scanf ("%d", T+i);
	while (m--)
	{
		int a, b, w;
		scanf ("%d%d%d", &a, &b, &w);
		if (w == 1)
			One[a].push_back(b), enO[b].push_back(a);
		else
			G[a].push_back({ b, w });
	}
	for (sq = 1; sq * sq <= T[n]; ++sq)
		;
}

void cntPrefs()
{
	possPrefBitset[1].set(1);
	FOR(d, 1, sq+1)
	{
		FOR(i, 1, n+1)
			if (possPrefBitset[i].test(d))
				Q.push(i);
		for (; !Q.empty(); Q.pop())
		{
			int i = Q.front();
			for (int j : One[i])
				if (T[j] >= d and not possPrefBitset[j].test(d))
					possPrefBitset[j].set(d), Q.push(j);
		}	
			
		FOR(i, 1, n+1)
			if (possPrefBitset[i].test(d))
				for (const auto& [j, h] : G[i])
					if (1ll * d * h <= std::min(T[j], sq))
						possPrefBitset[j].set(d * h);
	}
}

int getLower(int v, int d)
{
	return *std::prev(std::upper_bound(ALL(possPref[v]), d));
}

void transPossPref()
{
	FOR(i, 1, n+1)
	{
		possPref[i].push_back(0);
		FOR(d, 1, sq+1)
			if (possPrefBitset[i].test(d))
				possPref[i].push_back(d);
	}
}

void dijkstra(int W[maxN])
{
	auto relax = [&](int v, int d) {
		remin(d, T[v]);
		if (W[v] < d)
			H.push({ W[v] = d, v });
	};

	FOR(i, 1, n+1)
		H.push({ W[i], i });
	while (not H.empty())
	{
		auto [d, v] = H.top();
		H.pop();
		if (d == W[v])
			for (int u : enO[v])
				relax(u, d);
	}
}

void cntSuf()
{
	res = possPrefBitset[n].test(1);
	maxQuot[1][n] = T[n];
	
	FOR(d, 1, sq+1)
	{
		dijkstra(maxQuot[d]);

		FOR(i, 1, n+1) for (const auto& [j, h] : G[i])
		{
			int prefBound = maxQuot[d][j] / h;
			int pref = getLower(i, prefBound);
			remax(res, 1ll * pref * h * d);

			if (1ll * h * d <= 1ll * sq)
				remax(maxQuot[h*d][i], std::min(T[i], prefBound));
		}
	}
}

void solve()
{
	read();
	cntPrefs();
	transPossPref();
	cntSuf();

	if (res == 0)
		res--;
	printf("%d\n", res);
	clean();
}

int main()
{
	int tc = 1;
	scanf ("%d", &tc);	

	FOR(cid, 1, tc+1)
		solve();
	return 0;
}