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 from collections import defaultdict groups = 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 groups[(A_j, B_j)] += Y_j # Precompute positions for each color (1-based) color_pos = defaultdict(list) for i in range(N): color = C[i] color_pos[color].append(i+1) # 1-based sum_Y = [0]*(N+1) for (a, b), total in groups.items(): if a > N: continue if b not in color_pos: continue pos_list = color_pos[b] # Find first index >= a idx_p = bisect.bisect_left(pos_list, a) for p in pos_list[idx_p:]: K = p - a if K <= N: sum_Y[K] += total max_score = 0 for K in range(N+1): current = X * K + sum_Y[K] if current > max_score: max_score = current print(max_score) if __name__ == "__main__": main()