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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
#define _USE_MATH_DEFINES

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// DON'T USE THESE MACROS DURING ICPC PRACTICE
#define For(i,n) for (int i=0; i<n; i++)
#define FOR(i,a,b) for (int i=a; i<=b; i++)
#define Down(i,n) for (int i=n-1; i>=0; i--)
#define DOWN(i,a,b) for (int i=b; i>=a; i--)

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
typedef complex<ld> pt;
typedef vector<pt> pol;
typedef vector<int> vi;

const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll MOD = 1e9+7;
const ld EPS = 1e-10;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

struct Point {
	int x,o,a;
	bool operator < (const Point& p) const {
		if (x!=p.x) return x<p.x;
		else return o<p.o;
	}
};

const int N = 1<<18;
ll t[2*N], z[2*N];
ll dp[N];

void push(int i) {
	if(z[i]) {
		z[2*i] += z[i];
		z[2*i+1] += z[i];
		t[2*i] += z[i];
		t[2*i+1] += z[i];
		z[i] = 0;
	}
}

void update(int l, int r, ll v, int i=1, int s=0, int e=N-1) {
	if(r<s||e<l) return;
	if(l<=s&&e<=r) {
		t[i] += v;
		z[i] += v;
		return;
	}
	push(i);
	int m = (s+e)/2;
	update(l,r,v,2*i,s,m);
	update(l,r,v,2*i+1,m+1,e);
	t[i] = max(t[2*i],t[2*i+1]);
}

ll query(int l, int r, int i=1, int s=0, int e=N-1) {
	if(r<s||e<l) return -INFLL;
	if(l<=s&&e<=r) {
		return t[i];
	}
	push(i);
	int m = (s+e)/2;
	return max(query(l,r,2*i,s,m),query(l,r,2*i+1,m+1,e));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cout << fixed << setprecision(10);

	int n, m;
	cin >> n >> m;
	vector<Point> p;
	for(int i=0;i<m;i++) {
		int l,r,a;
		cin >> l >> r >> a;
		p.push_back({l,-2,a});
		p.push_back({r,l,a});
	}
	for (int i=1;i<N;i++) {
		p.push_back({i,-1});
	}
	sort(p.begin(),p.end());

	for(const Point& it:p) {
		if(it.o==-2) {
			update(0, it.x-1, it.a);
		} else if(it.o==-1) {
			dp[it.x] = query(0, it.x-1);
			update(it.x, it.x, dp[it.x]);
		} else {
			update(0, it.o-1, -it.a);
		}
	}

	ll ans = 0;
	for(int i=0;i<N;i++) {
		ans = max(ans, dp[i]);
	}
	cout << ans << nl;

	return 0;
}