#include using namespace std; #include using namespace atcoder; using ll = long long; using P = pair; #define fix(x) fixed << setprecision(x) #define asc(x) x, vector, greater #define rep(i, n) for(ll i = 0; i < n; i++) #define all(x) (x).begin(),(x).end() templatebool chmin(T&a, const T&b){if(a>b){a=b;return 1;}return 0;} templatebool chmax(T&a, const T&b){if(a> n >> m >> x; vector> c(5,vector(n,0)), b(5,vector(n,0)); rep(i,n){ int a; cin >> a; c[a-1][n-i-1] = 1; } rep(i,m){ int p,q,r; cin >> p >> q >> r; p--; q--; b[q][p] += r; } vector> d(5); rep(i,5){ d[i] = convolution_ll(c[i],b[i]); } ll ans = n*x; rep(i,n){ ll upd = x*(n-i-1); rep(j,5) upd += d[j][i]; chmax(ans, upd); } cout << ans << '\n'; return 0; }