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: for each color c, store the positions in a sorted list color_positions = {c: [] for c in range(1,6)} for i in range(N): c = C[i] color_positions[c].append(i+1) # since positions are 1-based # Precompute sum_ac: sum_ac[a][c] is sum of Y_j for all j where A_j = a, B_j = c sum_ac = [[0]*6 for _ in range(N+2)] # a can be up 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 sum_ac[A_j][B_j] += Y_j additional_points = [0]*(N+2) # k can be up to N for c in range(1,6): positions = color_positions[c] len_pos = len(positions) for a in range(1, N+1): if sum_ac[a][c] == 0: continue # Find the first s in positions where s >= a idx_pos = bisect.bisect_left(positions, a) for s in positions[idx_pos:]: k = s - a if k <= N: additional_points[k] += sum_ac[a][c] max_total = 0 for k in range(0, N+1): total = k * X + additional_points[k] if total > max_total: max_total = total print(max_total) if __name__ == '__main__': main()