#include #include using namespace std; using namespace atcoder; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; long long x; cin >> x; vector c(n); for(int i = 0; i < n; i++){ cin >> c[i]; c[i]--; } vector a(m), b(m); vector y(m); for(int i = 0; i < m; i++){ cin >> a[i] >> b[i] >> y[i]; a[i]--, b[i]--; } vector res(n + 1); for(long long i = 0; i <= n; i++){ res[i] = x * i; } vector> q(5, vector(n)); for(int i = 0; i < m; i++){ q[b[i]][a[i]] += y[i]; } for(int i = 0; i < 5; i++){ vector p(n); for(int j = 0; j < n; j++){ if(c[j] == i){ p[j] += 1; } } reverse(q[i].begin(), q[i].end()); vector r = convolution_ll(p, q[i]); for(int j = n - 1; j < 2 * n - 1; j++){ res[j - n + 1] += r[j]; } } long long ans = *max_element(res.begin(), res.end()); cout << ans << endl; }