Keep it energized

Buy energy packs at level shops so the stored energy covers each level cost in order for the least total cash.

Medium7Dynamic programmingSegment treePrefix sumBinary searchNo attempts yetTime limit3sMemory limit256 MB

Problem

A console maker is building Adventures of Captain Mikado, the game that ships with its new machine. The game has NN levels numbered 11 through NN, and level ii costs exactly EiE_i units of energy to clear. That is, the player's energy when level ii starts must be at least EiE_i, and clearing the level drops the energy by exactly EiE_i. Winning means clearing every level in increasing order, from level 11 to level NN, without ever going back to a level that is already cleared.

The player starts with energy 00. Energy comes from energy packs sold by the MM shops scattered across the levels. Each shop sells one kind of energy pack, with a strength SS and a cost CC. The player can buy an energy pack only from a shop on the level he is currently on, and only before starting that level. Buying a pack of strength SS sets the energy to SS at once, whatever it was just before.

Find the smallest amount of in-game cash that clears all NN levels.

Input

The first line contains the number of levels NN and the number of shops MM (1N,M1051 \le N, M \le 10^5).

The second line contains NN integers E1,E2,,ENE_1, E_2, \dots, E_N, where EiE_i is the energy needed to clear level ii (1Ei1041 \le E_i \le 10^4).

Each of the next MM lines describes one shop with the integers LL, SS and CC. LL is the level the shop is on, and SS and CC are the strength and the cost of the energy pack it sells (1LN1 \le L \le N, 1S1091 \le S \le 10^9, 1C1041 \le C \le 10^4).

Output

Print on one line the minimum amount of in-game cash needed to clear all NN levels. If clearing every level is impossible, print -1.