Under construction

Still being built. Things may change

Aarelia Mountains

Time limit2sMemory limit256 MB

Summary
Given n region heights and m Earthquake spells with lengths and costs, find the cheapest way to make the heights non-decreasing, or print -1 if it is impossible.
Level

Hard8 of 10

Topics
Graph, Shortest path, Prefix sum
Solved
No attempts yet

Problem

You are a powerful wizard travelling across the Aarelia kingdom. Your path lies across a land of steep hills and mountains. The mountain area is a row of nn regions, and region ii has elevation hih_i.

You have no climbing equipment (and, frankly, no climbing skills), and your Flight spells are used up. You can cross the area only when the elevations form a non-decreasing sequence, that is, hi≤hi+1h_i \leq h_{i+1} for each ii from 1 to (n−1)(n-1). You travel from right to left, so high falls are no threat.

The only spell that can help is the Earthquake spell. Each Earthquake spell affects several adjacent regions and changes their elevation. You know mm varieties of the spell. Variety ii has a range lil_i (the number of adjacent regions it affects) and an energy cost cic_i, and it either raises or lowers all affected regions by 1. A variety cannot both raise and lower. It can be applied to any range of length lil_i that lies completely within the area, and you may cast it an unlimited number of times, paying the energy cost after each cast. Some regions may end up with negative elevation.

Determine whether you can alter the landscape so that you can travel safely, and if so, find the lowest total energy you must spend on casting spells.

Input

The first line contains two integers nn and mm (1≤n,m≤2001 \leq n, m \leq 200).

The second line contains nn integers hih_i (0≤hi≤1060 \leq h_i \leq 10^6), the initial elevations of the regions.

The next mm lines describe the varieties of the Earthquake spell. Line ii has the form t_i l_i c_i, where t_i is + if the spell raises elevations by 1, or - if it lowers them by 1. Here 1≤li≤n1 \leq l_i \leq n and 1≤ci≤1061 \leq c_i \leq 10^6.

Output

If travel is possible, print the lowest total energy that must be spent on casting spells. Otherwise, print -1.

Examples2

  1. Example 1

    Input
    3 2
    3 2 1
    + 1 1
    - 1 1
    
    Expected output
    2
    
  2. Example 2

    Input
    3 1
    3 2 1
    + 2 1
    
    Expected output
    -1