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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.util.Map.Entry;



import java.io.*;

public class Main{
	static int mod;
	static long[] sta;
	static long[] oa;
	static long[] lazy;

	static void preprocess(long[] dp) {
		oa = dp;
		int ht = getlog(oa.length);
		int sl = (int) Math.pow(2, ht + 1) - 1;
		sta = new long[sl];
		lazy = new long[sl];
		build(0, 0, oa.length - 1);
	}

	static void build(int si, int ss, int se) {
		if (ss == se) {
			sta[si] = oa[ss];
			return;
		}
		int mid = (ss + se) / 2;
		build(2 * si + 1, ss, mid);
		build(2 * si + 2, mid + 1, se);
		sta[si] = operation(sta[2 * si + 1], sta[2 * si + 2]);
	}

	static void update(int idx, int val, int si, int ss, int se) {
		if (ss == idx && se == idx) {
			sta[si] = val;
			oa[idx] = val;
			return;
		}
		if (ss > se) {
			return;
		}
		int mid = (ss + se) / 2;
		if (idx <= mid) {
			update(idx, val, 2 * si + 1, ss, mid);
		} else {
			update(idx, val, 2 * si + 2, mid + 1, se);
		}
		sta[si] = operation(sta[2 * si + 1], sta[2 * si + 2]);

	}

	static long query(int qs, int qe, int si, int ss, int se) {
		handlingpedig(si, ss, se);
		if (ss >= qs && se <= qe) {
			return sta[si];
		} else if (se < qs || ss > qe) {
			return identity();
		} else {
			int mid = (ss + se) / 2;
			long lc = query(qs, qe, 2 * si + 1, ss, mid);
			long rc = query(qs, qe, 2 * si + 2, mid + 1, se);
			return operation(lc, rc);
		}
	}

	static void Update(int qs, int qe, long max, int si, int ss, int se) {
		handlingpedig(si, ss, se);

		if (ss >= qs && se <= qe) {
			lazy[si] = max;
			handlingpedig(si, ss, se);
			return;
		} else if (se < qs || ss > qe) {
			return;
		} else {
			int mid = (ss + se) / 2;
			Update(qs, qe, max, 2 * si + 1, ss, mid);
			Update(qs, qe, max, 2 * si + 2, mid + 1, se);
			sta[si] = operation(sta[2 * si + 1], sta[2 * si + 2]);
		}
	}

	static void handlingpedig(int si, int ss, int se) {
		sta[si] += lazy[si];
		if (ss != se) {
			lazy[2 * si + 1] += lazy[si];
			lazy[2 * si + 2] += lazy[si];
		}

		lazy[si] = 0;
	}

	static int getlog(int n) {
		int log = 0;
		while ((1 << log) < n) {
			log++;
		}
		return log;
	}

	static int identity() {
		// return 0;// sum
		// return Integer.MAX_VALUE;//min
		return Integer.MIN_VALUE;// max
	}

	static long operation(long ls, long rs) {
		// return ls + rs;
		return Math.max(ls, rs);// max
		// return Math.min(ls, rs);
	}

	public static class pair {
		int sc;
		int l;
		int r;

		pair(int l, int r, int sc) {
			this.l = l;
			this.r = r;
			this.sc = sc;
		}
	}

	public static void main(String[] args) {
		Scanner scn = new Scanner(System.in);
		int n = scn.nextInt();
		int m = scn.nextInt();
		pair p[] = new pair[m];
		ArrayList<Integer> g[] = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			g[i] = new ArrayList<>();
		}
		for (int i = 0; i < m; i++) {
			int l = scn.nextInt() - 1;
			int r = scn.nextInt() - 1;
			int sc = scn.nextInt();
			p[i] = new pair(l, r, sc);
			g[r].add(i);
		}
		long dp[] = new long[n];
		preprocess(dp);
		long max = 0;
		for (int i = 0; i < n; i++) {
			max = query(0, (i > 0) ? (i - 1) : 0, 0, 0, dp.length - 1);
			Update(i, i, max, 0, 0,n-1);
			for (int j = 0; j < g[i].size(); j++) {
				Update(p[g[i].get(j)].l, p[g[i].get(j)].r, p[g[i].get(j)].sc, 0, 0, dp.length - 1);
			}
		}
		
		
		System.out.println(query(0, n-1, 0, 0, n-1));

	}

}