import bisect 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 # Preprocess positions for each color (1-based) pos = [[] for _ in range(6)] # colors 1-5 for i in range(N): c = C[i] pos[c].append(i + 1) # positions are 1-based ans = [0] * (N + 1) # ans[k] for 0 <= k <= N 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 # Get positions for B_j lst = pos[B_j] # Find the first index where position >= A_j idx_p = bisect.bisect_left(lst, A_j) # Iterate from idx_p to end of lst, but ensure p <= N for p in lst[idx_p:]: if p > N: break k = p - A_j if k <= N: ans[k] += Y_j max_score = 0 for k in range(N + 1): current = X * k + ans[k] if current > max_score: max_score = current print(max_score) if __name__ == "__main__": main()