Illumination
Time limit2sMemory limit512 MB
Choose a subset of trees to decorate, maximizing total beauty, so that for each of M given intervals at most one tree inside it is chosen.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree, Greedy, Sorting
- Solved
- No attempts yet
Problem
JOI has N trees on his property. The trees are lined up in a row and numbered 1 through N in order.
This winter, JOI decided to choose some of the trees and decorate them with illuminations. An illumination has a value called beauty. The beauty of decorating tree i with an illumination is A_i.
JOI realized that decorating two trees that are too close together with illuminations can be too dazzling. Specifically, for j = 1, 2, ..., M, it turned out that no more than one of the trees L_j, L_j + 1, ..., R_j may be decorated with an illumination.
Find the maximum total beauty when decorating illuminations under this condition.
Input
The input is given from standard input in the following format.
N M
A_1 A_2 ... A_N
L_1 R_1
L_2 R_2
⋮
L_M R_M
Output
Print the maximum total beauty of the illuminations on one line.
Constraints
- 1 ≦ N ≦ 200000 (= 2×10^5)
- 1 ≦ M ≦ 200000 (= 2×10^5)
- 1 ≦ A_i ≦ 1000000000 (= 10^9) (1 ≦ i ≦ N)
- 1 ≦ L_j ≦ R_j ≦ N (1 ≦ j ≦ M)