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
//Sylwia Sapkowska 
#include <bits/stdc++.h> 
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;

void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << "'" << x << "'";}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define int long long
typedef pair<int, int> T;
typedef array<int, 3> F;
const int oo = 1e18, oo2 = 1e9;
const int S = 31624;

void solve(){
    int n, m; cin >> n >> m;
    vector<int>p(n);
    for (auto &x: p) cin >> x;
    vector<vector<T>>g(n);
    vector<F>edges;
    vector one(n, vector<int>(n, -oo));
    for (int i = 0; i<n; i++) one[i][i] = p[i];
    for (int i = 1; i<=m; i++){
        int a, b, w; cin >> a >> b >> w;
        --a;--b;
        g[a].emplace_back(b, w);
        if (w == 1) one[a][b] = min(p[a], p[b]);
        if (w > 1) edges.push_back(F{w, a, b});
    }
    for (int k = 0; k < n; ++k) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                one[i][j] = max(one[i][j], min(one[i][k], one[k][j]));
            }
        }
    }
    // sort(edges.begin(), edges.end()); //niepotrzebne chyba
    //dp[v][wart] = czy moge dojsc z 1 do v konczac z tą wartoscia i nie przemeczajac sie
    vector dp(S, vector<bool>(n));
    dp[1][0] = 1;
    auto propagate = [&](int val){
        for (int i = 0; i < n; i++){
            if (!dp[val][i]) continue;
            for (int j = 0; j<n; j++){
                if (one[i][j] >= val){
                    dp[val][j] = 1;
                }
            }
        }
    };
    propagate(1);
    for (int val = 2; val < S; val++){
        for (auto &[w, a, b]: edges){
            if (p[b] >= val && val % w == 0){
                if (dp[val/w][a]){
                    dp[val][b] = 1;
                }
            }
        }
        propagate(val);
    }
    vector dp2(S, vector<int>(n, -1));
    //dp2[val][i] = max wartosc z jaka moge przyjsc do danego wierzcholka v, zeby wciaz dalo sie dotrzec do n-1
    auto propagate2 = [&](int val){
        for (int i = 0; i < n; i++){
            for (int j = 0; j<n; j++){
                dp2[val][i] = max(dp2[val][i], min(one[i][j], dp2[val][j])); 
            }
        }
    };
    dp2[1][n-1] = p[n-1];
    for (int val = 1; val < S; val++){
        propagate2(val);
        for (auto [w, a, b]: edges){
            if (val * w >= S) continue;
            dp2[val * w][a] = max(dp2[val * w][a], min(dp2[val][b]/w, p[a]));
        }
    }
    int ans = -1;
    for (int val = S-1; val >= 1; val--){
        if (dp[val][n-1]){
            ans = val;
            break;
        }
    }
    vector<T>ord;
    for (auto &[w, a, b]: edges){
        ord.clear();
        for (int reszta = 1; reszta < S; reszta++){
            ord.emplace_back(dp2[reszta][b], reszta);
        }
        sort(ord.begin(), ord.end());
        set<int>secik;
        int ptr = (int)ord.size();
        for (int val = S-1; val * w >= S; val--){
            if (!dp[val][a]) continue;
            //0-(val)->a-(w)->b->(reszta)->n-1 
            if (p[b] < w * val) continue;
            int x = p[n-1]/(val*w);
            //chce dodac wszystkie >= val*w
            while (ptr-1 >= 0 && ord[ptr-1].first >= val * w) {
                ptr--;
                secik.insert(ord[ptr].second);
            }
            //na prefiksie chce znalezc max reszte ze wartosc >= val*w
            // for (int reszta = 1; reszta <= x; reszta++){
            //     if (dp2[reszta][b] >= val*w) {
            //         ans = max(ans, val * w * reszta);
            //     }
            // }
            auto it = secik.upper_bound(x);
            if (it != secik.begin()){
                it--;
                ans = max(ans, val * w * (*it));
            }
        }
    }
    cout << ans << "\n";
    // exit(0);
}
 
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--) solve();

    return 0;
}