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) color_positions = {i: [] for i in range(1, 6)} for pos in range(1, N+1): color = C[pos-1] color_positions[color].append(pos) sum_Y = [0] * (N + 1) # sum_Y[K] for K from 0 to 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 positions = color_positions.get(B_j, []) if not positions: continue # Find the first position >= A_j using bisect idx_pos = bisect.bisect_left(positions, A_j) for pos in positions[idx_pos:]: K = pos - A_j if K <= N: # Ensure K is within valid range sum_Y[K] += Y_j max_score = 0 for K in range(N + 1): current = sum_Y[K] + K * X if current > max_score: max_score = current print(max_score) if __name__ == "__main__": main()