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
// {{{ file: hea.cpp | time: 16:28 16.03.2025
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

#define each(...)    for (auto& __VA_ARGS__)
#define rep(i, b, e) for (int i = (b); i <= (e); i++)
#define rev(i, b, e) for (int i = (e); i >= (b); i--)
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define tC template<class
#define $(x) #x<<'='<<(x)<<' '

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vb = vector<bool>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vpi = vector<pii>;
using vpl = vector<pll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
tC T, class C=greater<T>> using min_priority_queue = priority_queue<T,vector<T>,C>;
tC T> int sz(const T& a) { return (int)a.size(); }
tC T> bool amin(T& a, T b) { return b < a ? a = b, 1 : 0; }
tC T> bool amax(T& a, T b) { return b > a ? a = b, 1 : 0; }
const int oo = 1e9+1;
const ll OO = ll(1e18)+1;

auto now() { return chrono::high_resolution_clock::now().time_since_epoch().count(); }
mt19937 rnd(4488);
tC T> T rand(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); }

struct Debug {
#ifdef SPONGE
  tC T>Debug operator<<(const T& x) { cerr<<"\033[1;33m"<<x<<"\033[0m"; return *this; }
#else
  tC T>Debug operator<<(const T&) { return *this; }
#endif
} dbg;

namespace { void solve(int); }
// }}}

int main()
{
  //freopen("input.txt","r",stdin);
  //freopen("output.txt","w",stdout);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed,ios::floatfield);
  cerr.precision(15);
  cerr.setf(ios::fixed,ios::floatfield);
  //rnd.seed(now());
  int t=1;
  cin>>t;
  rep(i,1,t){
    //cerr<<"Case #"<<i<<": ";
    solve(i);
  }
}

namespace {

const int N=105;
const int M=31630;

int n,m,p[N];
vpi in[N],out[N];

bool dp_pref[N][M+1];
int dp_suff[N][M+1];
queue<pii> kolej;
priority_queue<pair<int,pii>> maks_kolej;

void solve(int testcase)
{
  cin>>n>>m;

  rep(i,1,n) cin>>p[i];
  rep(i,1,m){
    int a,b,wsp;
    cin>>a>>b>>wsp;
    in[b].pb(mp(a,wsp));
    out[a].pb(mp(b,wsp));
  }

  kolej.push(mp(1,1));
  dp_pref[1][1]=true;
  while(sz(kolej)){
    auto [a,ilo]=kolej.front();
    kolej.pop();
    each([b,wsp]:out[a]){
      if(ll(ilo)*wsp<=min(M,p[b]) && !dp_pref[b][ilo*wsp]){
        dp_pref[b][ilo*wsp]=true;
        kolej.push(mp(b,ilo*wsp));
      }
    }
  }

  maks_kolej.push(mp(p[n],mp(n,1)));
  dp_suff[n][1]=p[n];
  while(sz(maks_kolej)){
    auto [wartosc,para]=maks_kolej.top();
    auto [b,ilo]=para;
    maks_kolej.pop();
    
    if(dp_suff[b][ilo]==wartosc){
      each([a,wsp]:in[b]){
        int nowa_wartosc=min(wartosc/wsp,p[a]);
        if(ll(ilo)*wsp<=M && dp_suff[a][ilo*wsp] < nowa_wartosc){
          dp_suff[a][ilo*wsp]=nowa_wartosc;
          maks_kolej.push(mp(nowa_wartosc,mp(a,ilo*wsp)));
        }
      }
    }
  }

  ll wynik=-1;
  if(n==1) wynik=1;

  rep(a,1,n) each([b,wsp]:out[a]){
    vpi tmp,vek;
    rep(ilo,1,M) if(dp_suff[b][ilo]) tmp.pb(mp(dp_suff[b][ilo],ilo));
    sort(all(tmp));
    each([wart,ilo]:tmp){
      while(sz(vek)){
        auto [wart_last,ilo_last]=vek.back();
        if(ilo_last<=ilo) vek.pop_back(); else break;
      }
      vek.pb(mp(wart,ilo));
    }

    rep(ilo,1,M) if(dp_pref[a][ilo] && ll(wsp)*ilo <= p[b]){
      auto it=lower_bound(all(vek),mp(wsp*ilo,0));
      if(it!=vek.end()){
        amax(wynik,ll(wsp) * ilo * it->y);
      }
    }
  }

  cout<<wynik<<endl;

  rep(i,1,n) in[i].clear();
  rep(i,1,n) out[i].clear();
  rep(i,1,n) rep(j,1,M) dp_pref[i][j]=false;
  rep(i,1,n) rep(j,1,M) dp_suff[i][j]=0;
}

}