import bisect from collections import defaultdict def main(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 M = int(data[idx]) idx += 1 X = int(data[idx]) idx += 1 C = list(map(int, data[idx:idx+N])) idx += N color_pos = defaultdict(list) for i in range(N): color_pos[C[i]].append(i + 1) # Positions are 1-based sum_ya_c = defaultdict(lambda: defaultdict(int)) for _ in range(M): A_j = int(data[idx]) idx += 1 B_j = int(data[idx]) idx += 1 Y_j = int(data[idx]) idx += 1 sum_ya_c[A_j][B_j] += Y_j contrib = [0] * (N + 1) # contrib[K] for K from 0 to N for a in sum_ya_c: for c in sum_ya_c[a]: if c not in color_pos: continue pos_list = color_pos[c] # Find the first position >= a idx_pos = bisect.bisect_left(pos_list, a) for pos in pos_list[idx_pos:]: if pos > N: break # Just in case, though pos is <=N as per input K = pos - a if K <= N: contrib[K] += sum_ya_c[a][c] max_total = 0 for K in range(0, N + 1): current_total = X * K + contrib[K] if current_total > max_total: max_total = current_total print(max_total) if __name__ == "__main__": main()