Fashionista

Interview

Time limit1sMemory limit128 MB

Summary
For each day pick any clothing whose temperature range covers that day's high, maximizing the sum of absolute flashiness differences between consecutive days.
Level

Medium5 of 10

Topics
Dynamic programming, Array, Implementation, Math
Solved
No attempts yet

Problem

Sang-geun is planning what to wear on each of the next DD days (day 11 through day DD). Because clothing style is closely tied to the day's high temperature, he plans based on the weather forecast. The high temperature on day ii is TiT_i.

Sang-geun owns NN pieces of clothing, numbered from 11 to NN. Clothing jj (1≤j≤N1 \le j \le N) can only be worn on a day whose high temperature is between AjA_j and BjB_j inclusive, and its flashiness is CjC_j.

He may wear the same clothing on several days, and some clothing may never be worn.

Wearing similar clothing on consecutive days is unappealing, so he wants to maximize the total difference in flashiness between the clothing worn on adjacent days. That is, if he wears clothing xix_i on day ii, he wants to maximize ∣Cx1−Cx2∣+∣Cx2−Cx3∣+⋯+∣CxD−1−CxD∣|C_{x_1} - C_{x_2}| + |C_{x_2} - C_{x_3}| + \cdots + |C_{x_{D-1}} - C_{x_D}|.

Write a program that computes the maximum value of this sum.

Input

The first line contains DD and NN. (2≤D,N≤2002 \le D, N \le 200)

Each of the next DD lines contains the high temperature of one day; the ii-th line contains TiT_i. (0≤Ti≤600 \le T_i \le 60)

Each of the following NN lines describes one piece of clothing with AjA_j, BjB_j, CjC_j. (0≤Aj≤Bj≤600 \le A_j \le B_j \le 60, 0≤Cj≤1000 \le C_j \le 100)

On every day there is at least one piece of clothing that can be worn.

Output

Print the maximum total difference in flashiness on a single line.

Hint

In the first example, wearing clothing 44 on day 11, clothing 22 on day 22, and clothing 33 on day 33 gives ∣40−90∣+∣90−60∣=80|40 - 90| + |90 - 60| = 80, which is the maximum.

Examples3

  1. Example 1

    Input
    3 4
    31
    27
    35
    20 25 30
    23 29 90
    21 35 60
    28 33 40
    
    Expected output
    80
    
  2. Example 2

    Input
    5 2
    26
    28
    32
    29
    34
    30 35 0
    25 30 100
    
    Expected output
    300
    
  3. Example 3

    Input
    2 2
    0
    0
    0 0 5
    0 0 10
    
    Expected output
    5