Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well.

It is well-known 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 space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $P_{1}, P_{2}, \ldots, P_{N}$.
$M$ lines follow. For each $i$ ($1 ≤ i ≤ M$), the $i$-th of these lines contains two space-separated 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 ≤ T ≤ 100$
$1 ≤ M ≤ N ≤ 10^{5}$
$1 ≤ P_{i} ≤ 10^{9}$ for each valid $i$
$P_{1}, P_{2}, \ldots, P_{N}$ are pairwise distinct
$1 ≤ L_{i}, R_{i} ≤ N$ for each valid $i$
the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$

----- Sample Input 1 ------ 
1

4 4

1 2 4 3

1 3

3 4

1 2

2 3
----- Sample Output 1 ------ 
4
----- explanation 1 ------ 
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.

Difficulty: VERY_HARD
Tags: Data structures

Range queries

Segment trees revisited
Skill types: Data structures

Range queries

Starter code:

Solutions:
from collections import defaultdict

class SegmentTree:

	def __init__(self, values):
		self.data = [0 for _ in values] + values
		self.n = len(values)
		for idx in reversed(range(1, self.n)):
			self.data[idx] = max(self.data[2 * idx], self.data[2 * idx + 1])

	def update(self, idx, value):
		idx += self.n
		self.data[idx] = value
		while idx > 1:
			idx //= 2
			self.data[idx] = max(self.data[2 * idx], self.data[2 * idx + 1])

	def maximum(self, left, right):
		left += self.n
		right += self.n
		maximum = self.data[left]
		while left < right:
			if left % 2:
				maximum = max(maximum, self.data[left])
				left += 1
			if right % 2:
				right -= 1
				maximum = max(maximum, self.data[right])
			left //= 2
			right //= 2
		return maximum
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split(' '))
	val_jewels = 0
	val_stolen = 0
	p = []
	idx = defaultdict(int)
	cnt = 0
	for x in input().split():
		z = int(x)
		val_jewels += z
		p.append(z)
		idx[z] = cnt
		cnt += 1
	st = SegmentTree(p)
	attempted_once = set()
	for o1 in range(m):
		(s, e) = map(int, input().split(' '))
		s = s - 1
		loop = True
		while loop:
			most_precious = st.maximum(s, e)
			if most_precious in attempted_once:
				val_stolen += most_precious
				attempted_once.remove(most_precious)
				st.update(idx[most_precious], 0)
			elif most_precious > 0:
				loop = False
				attempted_once.add(most_precious)
			else:
				loop = False
	if attempted_once:
		val_stolen += sum(attempted_once) - max(attempted_once)
	remaining = val_jewels - val_stolen
	print(remaining)