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: Runtime Error)

#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
using namespace std;

#define int long long


const int MAX_N = 130000;
const int INF = 1LL<<30;

typedef int VAL;     // モノイドの型
typedef int DELAY;   // 作用 (遅延評価) の型

const VAL UNITY_VAL = -1e18;    // モノイドの単位元
const DELAY UNITY_DELAY = 0;  // 作用の単位元

static int SIZE_SEG;
struct SegTree {
  VAL val[4*MAX_N];
  DELAY delay[4*MAX_N];

  SegTree(int n = MAX_N, VAL v = UNITY_VAL) {
    init(n, v);
  }

  void init(int n = MAX_N, VAL v = UNITY_VAL) {
    SIZE_SEG = 1;
    while (SIZE_SEG < n) SIZE_SEG *= 2;
    for (int i = 0; i < 2*SIZE_SEG-1; ++i) val[i] = v, delay[i] = UNITY_DELAY;
  }


  ///////////////////////////////
  // モノイド演算の定義、作用の定義: ここだけ書き換えれば OK
  ///////////////////////////////

  // モノイド演算
  inline VAL merge(VAL a, VAL b) {

    // Starry Sky Tree の場合: min
    VAL res = max(a, b);
    return res;
  }

  // 作用元での演算
  inline void add(DELAY &d, DELAY x) {

    // Starry Sky Tree の場合: +
    d += x;
  }

  // モノイドへの作用
  inline void reflect(VAL &a, DELAY d) {

    // Starry Sky Tree の場合: 加算
    a += d;
  }


  ///////////////////////////////
  // ここから下は書き換え不要！！！
  ///////////////////////////////

  inline void redelay(int k, int l, int r) {
    reflect(val[k], delay[k]);
    if (k < SIZE_SEG-1) {
      add(delay[k*2+1], delay[k]);
      add(delay[k*2+2], delay[k]);
    }
    delay[k] = UNITY_DELAY;
  }

  inline void renode(int k, int l, int r) {
    val[k] = merge(val[k*2+1], val[k*2+2]);
  }

  inline void set(int a, int b, DELAY x, int k = 0, int l = 0, int r = SIZE_SEG) {
    if (a <= l && r <= b) {                 // included
      add(delay[k], x);
      redelay(k, l, r);
    }
    else if (a < r && l < b) {              // intersected
      redelay(k, l, r);
      set(a, b, x, k*2+1, l, (l+r)/2);
      set(a, b, x, k*2+2, (l+r)/2, r);
      renode(k, l, r);
    }
    else {                                  // no-intersected
      redelay(k, l, r);
    }
  }

  inline VAL get(int a, int b, int k = 0, int l = 0, int r = SIZE_SEG) {
    redelay(k, l, r);
    if (a <= l && r <= b) {                 // included
      return val[k];
    }
    else if (a < r && l < b) {              // intersected
      VAL t1 = get(a, b, k*2+1, l, (l+r)/2);
      VAL t2 = get(a, b, k*2+2, (l+r)/2, r);
      renode(k, l, r);

      return merge(t1, t2);
    }
    else {                                  // no-intersected
      return UNITY_VAL;
    }
  }

  /*
  inline VAL operator [] (int i) {return get(i, i+1);}

  void print(long long r = SIZE_SEG) {
    long long t = 2;
    for (int i = 0; i < r*2-1; ++i) {
      cout << make_pair(val[i], delay[i]) << ",";
      if (i == t-2) {cout << endl; t *= 2;}
    }
    for (int i = 0; i < r; ++i) {
      cout << get(i, i+1) << ", ";
    }
    cout << endl << endl;
  }
  */
};



signed main() {
  int n, q;
  cin >> n >> q;
  typedef pair<int,int> P;
  typedef pair<P,int> PP;
  std::vector<PP> v(q);
  for(int i=0;i<q;i++){
    int l,r,a;cin>>l>>r>>a;
    v[i]=PP(P(r,l),a);
  }
  sort(v.begin(),v.end());
  SegTree seg(n+5, 0);
  for (int i = 0; i < q; ++i) {
    int left=v[i].first.second,right=v[i].first.first,val=v[i].second;

    int res = seg.get(1, right+1);
    int now=seg.get(right,right+1);
    int change=res-now;
    seg.set(right, right+1, change);
    seg.set(left,right+1,val);
  }
  cout<<seg.get(1,n+3)<<endl;
}