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
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define piii pair<pair<int,int>, int>
#define st first.first
#define nd first.second
#define rd second
#define For(i, l, r) for (int i = l; i <= r; i++)
#define Forcin(l, r, a)        \
  for (int i = l; i <= r; i++) \
    cin >> a[i];
#define Ford(i, l, r) for (int i = l; i >= r; i--)
#define ben(v) v.begin(), v.end()
#define LOCAL 0
#define LOCAL2 0
using namespace std;
 
const int M = 205, inf=1e9;
int n, m, p[M], t;
unordered_set<signed> ress[M];
vector <pii> g[M];

void solve(){
    cin>>n>>m;
    For(i, 1, n){
        cin>>p[i];
        g[i].clear();
        ress[i].clear();
    }
    For(i, 1, n-1)
        p[i]=min(p[i], p[n]);
    set <piii> e;
    For(i, 1, m){
        int a, b, w;
        cin>>a>>b>>w;
        e.insert({{a, b}, w});
    }
    for(auto el:e){
        g[el.st].push_back({el.nd, el.rd});
    }
    m=e.size();
    queue<pii> q;
    q.push({1, 1});
    while(!q.empty()){
        auto [v, x]=q.front();
        q.pop();
        if (x > p[v] || ress[v].count(x))
            continue;
        ress[v].insert(x);
        for(auto [u, w]:g[v]){
            if (x*w<=p[u])
                q.push({u, x*w});
        }
    }
    signed maxi=-1;
    for(auto el:ress[n])
        maxi=max(el, maxi);
    cout << maxi<<'\n';
}
 
signed main()
{
    cin.tie(0)->sync_with_stdio();
    if (LOCAL)
        freopen("a.txt", "r", stdin);
    if (LOCAL2)
        freopen("local_out.txt", "w", stdout);
    int t;
    cin>>t;
    For(_, 1, t)
        solve();
}