using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); // Test(); } static void Test() { var r = new Random(); var count = 0; while (true) { var n = 16; var m = 16; var x = r.Next(100); var a = new int[n]; for (var i = 0; i < n; ++i) a[i] = 1; var map = new int[m][]; for (var i = 0; i < m; ++i) map[i] = new int[] { i + 1, 1, 100 }; // 愚直解 var s1 = All(n, m, x, a, map); // 作成解 var s2 = Slimes(n, m, x, a, map); if (s1 != s2) { WriteLine(n + " " + m + " " + x); WriteLine(string.Join(" ", a)); WriteLine(string.Join("\n", map.Select(m => string.Join(" ", m)))); WriteLine(s1); WriteLine(s2); return; } ++count; if (count % 1000 == 0) WriteLine(count); } } static int All(int n, int m, int x, int[] a, int[][] map) { var ans = 0; for (var i = 0; i < n; ++i) { var sub = x * i; for (var j = 0; j < map.Length; ++j) { var pos = map[j][0] + i - 1; if (pos < n && a[pos] == map[j][1]) sub += map[j][2]; } ans = Math.Max(ans, sub); } return ans; } static void Solve() { var c = NList; var (n, m, x) = (c[0], c[1], c[2]); var a = NList; var map = NArr(m); WriteLine(Slimes(n, m, x, a, map)); } static long Slimes(int n, int m, int x, int[] a, int[][] map) { var colors = new long[5][]; for (var i = 0; i < colors.Length; ++i) colors[i] = new long[n]; for (var i = 0; i < n; ++i) ++colors[a[i] - 1][i]; var points = new long[5][]; for (var i = 0; i < points.Length; ++i) points[i] = new long[n]; for (var i = 0; i < map.Length; ++i) points[map[i][1] - 1][map[i][0] - 1] += map[i][2]; for (var i = 0; i < points.Length; ++i) Array.Reverse(points[i]); var cmb = new ConvolutionCalculator(998_244_353); var combo = new int[5][]; for (var i = 0; i < combo.Length; ++i) combo[i] = cmb.Calc(colors[i], points[i]); var ans = 0; for (var j = n - 1; j < combo[0].Length; ++j) { var sum = (j - n + 1) * x; for (var i = 0; i < combo.Length; ++i) sum += combo[i][j]; ans = Math.Max(ans, sum); } return ans; } // 畳み込みを mod で計算する class ConvolutionCalculator { int mod; public ConvolutionCalculator(int mod) { this.mod = mod; } public int[] Calc(long[] a, long[] b) { var _a = new int[a.Length]; var _b = new int[b.Length]; for (var i = 0; i < a.Length; ++i) _a[i] = (int)(a[i] % mod); for (var i = 0; i < b.Length; ++i) _b[i] = (int)(b[i] % mod); return Calc(_a, _b); } public int[] Calc(int[] a, int[] b) { var n = a.Length; var m = b.Length; if (n == 0 || m == 0) { return new int[0]; } if (Math.Min(n, m) <= 100) { return ShortComvolution(a, b); } var z = 1 << CeilPow(n + m - 1); var za = new int[z]; var zb = new int[z]; Array.Copy(a, za, a.Length); Array.Copy(b, zb, b.Length); InitButterfly(); Butterfly(za); Butterfly(zb); for (var i = 0; i < z; ++i) { za[i] = (int)((long)za[i] * zb[i] % mod); } ButterflyInv(za); var iz = PowMod(z, mod - 2, mod); var ans = new int[n + m - 1]; for (var i = 0; i < ans.Length; ++i) { ans[i] = (int)((long)za[i] * iz % mod); } return ans; } int[] ShortComvolution(int[] a, int[] b) { var ans = new int[a.Length + b.Length - 1]; for (var i = 0; i < a.Length; ++i) { for (var j = 0; j < b.Length; ++j) { ans[i + j] = (int)((ans[i + j] + (long)a[i] * b[j] % mod) % mod); } } return ans; } int[] sume = new int[30]; int[] sumie = new int[30]; void InitButterfly() { var g = PrimitiveRoot(mod); var es = new int[30]; var ies = new int[30]; var cnt2 = BSF(mod - 1); var e = PowMod(g, mod - 1 >> cnt2, mod); var ie = PowMod(e, mod - 2, mod); for (var i = cnt2; i >= 2; --i) { es[i - 2] = e; ies[i - 2] = ie; e = (int)((long)e * e % mod); ie = (int)((long)ie * ie % mod); } var now = 1L; var inow = 1L; for (var i = 0; i < cnt2 - 2; ++i) { sume[i] = (int)(es[i] * now % mod); now = now * ies[i] % mod; sumie[i] = (int)(ies[i] * inow % mod); inow = inow * es[i] % mod; } } void Butterfly(int[] a) { var h = CeilPow(a.Length); for (var ph = 1; ph <= h; ++ph) { var w = 1 << (ph - 1); var p = 1 << (h - ph); var now = 1L; for (var s = 0; s < w; ++s) { var offset = s << (h - ph + 1); for (var i = 0; i < p; ++i) { var l = a[i + offset]; var r = (int)(a[i + offset + p] * now % mod); a[i + offset] = (l + r) % mod; a[i + offset + p] = (int)((long)mod + l - r) % mod; } now = now * sume[BSF(~(uint)s)] % mod; } } } void ButterflyInv(int[] a) { var h = CeilPow(a.Length); for (var ph = h; ph >= 1; --ph) { var w = 1 << (ph - 1); var p = 1 << (h - ph); var inow = 1L; for (var s = 0; s < w; ++s) { var offset = s << (h - ph + 1); for (var i = 0; i < p; ++i) { var l = a[i + offset]; var r = a[i + offset + p]; a[i + offset] = (l + r) % mod; a[i + offset + p] = (int)(((long)mod + l - r) * inow % mod); } inow = inow * sumie[BSF(~(uint)s)] % mod; } } } static int PowMod(int n, int p, int mod) { long _n = n % mod; var _p = p; var result = 1L; if ((_p & 1) == 1) { result *= _n; } while (_p > 0) { _n = _n * _n % mod; _p >>= 1; if ((_p & 1) == 1) { result = (result * _n) % mod; } } return (int)result; } static int PrimitiveRoot(int m) { if (m == 2) return 1; if (m == 167772161) return 3; if (m == 469762049) return 3; if (m == 754974721) return 11; if (m == 998244353) return 3; if (m == 1000000007) return 5; var divs = new int[20]; divs[0] = 2; var cnt = 1; var x = (m - 1) / 2; while (x % 2 == 0) x /= 2; for (var i = 3; (long)i * i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) x /= i; } } if (x > 1) divs[cnt++] = x; for (var g = 2;; ++g) { var ok = true; for (var i = 0; i < cnt; ++i) { if (PowMod(g, (m - 1) / divs[i], m) == 1) { ok = false; break; } } if (ok) return g; } } static int CeilPow(int n) { var x = 0; while ((1U << x) < (uint)n) x++; return x; } static int BSF(int n) { if (n == 0) return 0; var count = 0; while ((n & 1) == 0) { ++count; n >>= 1; } return count; } static int BSF(uint n) { if (n == 0) return 0; var count = 0; while ((n & 1) == 0) { ++count; n >>= 1; } return count; } public long[] ConvolutionLL(long[] a, long[] b) { if (a.Length == 0 && b.Length == 0) return new long[0]; const ulong MOD1 = 754_974_721; const ulong MOD2 = 167_772_161; const ulong MOD3 = 469_762_049; const ulong M2M3 = 78_812_994_116_517_889; const ulong M1M3 = 354_658_471_880_163_329; const ulong M1M2 = 126_663_740_442_542_081; const ulong M1M2M3 = 560_135_205_046_714_369; const ulong i1 = 190_329_765; const ulong i2 = 58_587_104; const ulong i3 = 187_290_749; ulong[] offset = new ulong[] { 0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3 }; var c1 = new ConvolutionCalculator((int)MOD1).Calc(a, b); var c2 = new ConvolutionCalculator((int)MOD2).Calc(a, b); var c3 = new ConvolutionCalculator((int)MOD3).Calc(a, b); var ans = new long[a.Length + b.Length - 1]; for (var i = 0; i < ans.Length; ++i) { ulong x = 0; x += (ulong)c1[i] * i1 % MOD1 * M2M3; x += (ulong)c2[i] * i2 % MOD2 * M1M3; x += (ulong)c3[i] * i3 % MOD3 * M1M2; var diff = c1[i] - SafeMod((long)x, (long)MOD1); if (diff < 0) diff += (long)MOD1; x -= offset[diff % 5]; ans[i] = (long)x; } return ans; } static long SafeMod(long a, long b) { a %= b; if (a < 0) a += b; return a; } } }