Save Jewels

All submissions for this problem are available.
### Read problems statements in [Hindi](http://www.codechef.com/download/translated/COOK110/hindi/SAVJEW.pdf), [Mandarin Chinese](http://www.codechef.com/download/translated/COOK110/mandarin/SAVJEW.pdf), [Russian](http://www.codechef.com/download/translated/COOK110/russian/SAVJEW.pdf), [Vietnamese](http://www.codechef.com/download/translated/COOK110/vietnamese/SAVJEW.pdf), and [Bengali](http://www.codechef.com/download/translated/COOK110/bengali/SAVJEW.pdf) as well. It is wellknown that if you become rich, you will have a lot of trouble. Coach Khaled is one of the richest men in the world. He has $N$ jewels (numbered $1$ through $N$); for each valid $i$, the $i$th jewel has value $P_i$. The values of all jewels are pairwise distinct. Unfortunately for Khaled, one of the best thieves in the world, Ghoda, is trying to steal the jewels. Khaled knows that Ghoda wants to try to steal jewels during $M$ days (numbered $1$ through $M$). For each valid $i$, on the $i$th day, consider all jewels with numbers between $L_i$ and $R_i$ (inclusive) which Ghoda has not stolen yet. If there is at least one such jewel, Ghoda decides to steal one of these jewels: the most valuable one. Khaled can only choose one day and prevent Ghoda from stealing any jewels on that day; Ghoda can steal jewels on subsequent days in the same way. Khaled wants to maximise the sum of values of the jewels Ghoda has not stolen during these $M$ days. Can you help him? ### Input  The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.  The first line of each test case contains two spaceseparated integers $N$ and $M$.  The second line contains $N$ spaceseparated integers $P_1, P_2, \ldots, P_N$.  $M$ lines follow. For each $i$ ($1 \le i \le M$), the $i$th of these lines contains two spaceseparated integers $L_i$ and $R_i$. ### Output For each test case, print a single line containing one integer — the maximum possible sum of values of Khaled's remaining jewels. ### Constraints  $1 \le T \le 100$  $1 \le M \le N \le 10^5$  $1 \le P_i \le 10^9$ for each valid $i$  $P_1, P_2, \ldots, P_N$ are pairwise distinct  $1 \le L_i, R_i \le N$ for each valid $i$  the sum of $N$ over all test cases does not exceed $2 \cdot 10^5$ ### Example Input ``` 1 4 4 1 2 4 3 1 3 3 4 1 2 2 3 ``` ### Example Output ``` 4 ``` ### Explanation **Example case 1:** Khaled can prevent Ghoda from stealing on day $2$. Then, Ghoda steals the $3$rd jewel on the $1$st day and the $2$nd jewel on the $3$rd day; he cannot steal anything on the $4$th day. The remaining jewels are the $1$st and $4$th jewel, with values $1$ and $3$ respectively.Author:  i_love_islam 
Editorial  https://discuss.codechef.com/problems/SAVJEW 
Tags  cook110, i_love_islam, medium, observations, queue, segmenttree, taran_1407 
Date Added:  17092019 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, SQL, kotlin, PERL6, TEXT, SCM chicken, PYP3, CLOJ, R, COB, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 