Problem:
Score : 100 points

Problem Statement

Consider a string of length N consisting of 0 and 1.
The score for the string is calculated as follows:

For each i (1 \leq i \leq M), a_i is added to the score if the string contains 1 at least once between the l_i-th and r_i-th characters (inclusive).

Find the maximum possible score of a string.

Constraints

All values in input are integers.

1 \leq N \leq 2 × 10^5

1 \leq M \leq 2 × 10^5

1 \leq l_i \leq r_i \leq N

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
l_1 r_1 a_1
l_2 r_2 a_2
:
l_M r_M a_M

Output

Print the maximum possible score of a string.

Sample Input 1

5 3
1 3 10
2 4 -10
3 5 10

Sample Output 1

20

The score for 10001 is a_1 + a_3 = 10 + 10 = 20.

Sample Input 2

3 4
1 3 100
1 1 -10
2 2 -20
3 3 -30

Sample Output 2

90

The score for 100 is a_1 + a_2 = 100 + (-10) = 90.

Sample Input 3

1 1
1 1 -10

Sample Output 3

0

The score for 0 is 0.

Sample Input 4

1 5
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000

Sample Output 4

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 5

6 8
5 5 3
1 1 10
1 6 -8
3 6 5
3 4 9
5 5 -2
1 3 -6
4 6 -7

Sample Output 5

10

For example, the score for 101000 is a_2 + a_3 + a_4 + a_5 + a_7 = 10 + (-8) + 5 + 9 + (-6) = 10.

Solution in Python (3.4.3) (judge verdict: Time Limit Exceeded)

class RMQ:
    def __init__(self, n):
        k = 1
        while k < n:
            k *= 2
        self.N = k
        self.node = [0] * (2 * self.N)
        self.ma = [0] * (2 * self.N)

    def _update(self, a, b, x, i, l, r):
        if l >= r:
            return
        if a <= l and r <= b:
            self.node[i] += x
            self.ma[i] += x
        elif l < b and a < r:
            mid = (l + r) // 2
            self._update(a, b, x, 2 * i, l, mid)
            self._update(a, b, x, 2 * i + 1, mid, r)
            self.ma[i] = self.node[i] + max(self.ma[2 * i], self.ma[2 * i + 1])

    def _find(self, a, b, i, l, r):
        if r <= a or b <= l:
            return -float("inf")
        if a <= l and r <= b:
            return self.ma[i]
        mid = (l + r) // 2
        return self.node[i] + max(self._find(a, b, 2 * i, l, mid), self._find(a, b, 2 * i + 1, mid, r))

    def find(self, a, b):
        return self._find(a, b, 1, 0, self.N)

    def update(self, a, b, x):
        self._update(a, b, x, 1, 0, self.N)


N, M = map(int, input().split())
rmq = RMQ(N)
a = {}
d = {}
for i in range(M):
    l, r, x = map(int, input().split())
    if l in a:
        a[l].append(x)
    else:
        a[l] = [x]

    if r in d:
        d[r].append((l, x))
    else:
        d[r] = [(l, x)]

ans = 0

for i in range(1, N + 1):
    if i in a:
        for a1 in a[i]:
            rmq.update(0, i, a1)
    tmp = rmq.find(0, i)
    ans = max(ans, tmp)
    rmq.update(i, i + 1, tmp)
    if i in d:
        for fir, sec in d[i]:
            rmq.update(0, fir, -sec)

print(ans)