#ARC137E. [ARC137E] Bakery

[ARC137E] Bakery

Score : 800800 points

Problem Statement

Snuke runs a bakery. He is planning for the next NN days. Let us call these days Day 1,2,,N1,2,\cdots,N.

At the moment, the bakery hires nobody. There are MM bakers that Snuke can hire, numbered 11 to MM.

CiC_i yen must be paid to hire Baker ii. If Baker ii is hired, they will work on Day Li,Li+1,,RiL_i, L_i+1, \cdots, R_i, baking one loaf of bread each day.

For each jj (1jN1 \leq j \leq N), there is a limit AjA_j to the number of loaves of bread that can be sold on Day jj. If xjx_j loaves of bread are baked on Day jj, min(xj,Aj)\min(x_j, A_j) loaves will be sold on that day.

Each loaf of bread sold will yield a profit of DD yen.

Snuke wants to choose the set of bakers to hire to maximize the final profit: ((The total number of loaves sold)×D() \times D - (The total cost of hiring bakers)). Find the maximum possible value of this.

Constraints

  • 1N20001 \leq N \leq 2000
  • 1M20001 \leq M \leq 2000
  • 1D1091 \leq D \leq 10^9
  • 1AjM1 \leq A_j \leq M
  • 1LiRiN1 \leq L_i \leq R_i \leq N
  • 1Ci1091 \leq C_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN MM DD

A1A_1 A2A_2 \cdots ANA_N

L1L_1 R1R_1 C1C_1

L2L_2 R2R_2 C2C_2

\vdots

LML_M RMR_M CMC_M

Output

Print the answer.

7 4 3
1 1 1 1 1 1 1
1 2 3
2 4 5
4 6 3
6 7 1
11

Snuke should hire Baker 1,3,41, 3, 4. In this case, the total cost to hire bakers is 77, and the number of loaves baked on Day 1,2,,N1, 2, \cdots, N will be 1,1,0,1,1,2,11,1,0,1,1,2,1, respectively. A total of 66 loaves will be sold, for a final profit of 6×37=116 \times 3-7=11.

3 1 5
1 1 1
2 2 10
0

It is optimal to hire no baker.

10 10 42
6 5 1 5 2 4 2 7 10 9
3 4 4
3 7 136
9 9 14
2 7 152
3 3 33
2 4 100
3 3 38
1 10 28
3 5 66
8 8 15
543