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

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef int32_t i32;
typedef int64_t i64;

typedef struct add_max_node {
  i64 add;
  i64 max;
} add_max_node;

typedef struct RMQandRAQ {
  add_max_node *a;
  i32 bit;
  i32 size;
} RMQandRAQ;

RMQandRAQ* new_RMQandRAQ (const i32 n) {
  i32 k = 0;
  while ((1 << k) < n) ++k;
  RMQandRAQ *s = (RMQandRAQ *) calloc (1, sizeof (RMQandRAQ));
  s->a = (add_max_node *) calloc (2 << k, sizeof (add_max_node));
  s->bit = k;
  s->size = 1 << k;
  return s;
}

static inline i64 max (i64 a, i64 b) {
  return a > b ? a : b;
}

static inline i64 eval (RMQandRAQ *s, i32 k) {
  return s->a[k].add + s->a[k].max;
}

void propagate (RMQandRAQ *s, i32 x) {
  x += s->size;
  for (i32 bit = s->bit; bit > 0; bit--) {
    i32 k = x >> bit;
    s->a[2 * k].add += s->a[k].add;
    s->a[2 * k + 1].add += s->a[k].add;
    s->a[k].add = 0;
    s->a[k].max = max (eval (s, 2 * k), eval (s, 2 * k + 1));
  }
}

void save (RMQandRAQ *s, i32 x) {
  for (i32 k = (x + s->size) >> 1; k >= 1; k >>= 1) {
    s->a[k].max = max (eval (s, 2 * k), eval (s, 2 * k + 1));
  }
}

void add (RMQandRAQ *s, i32 l, i32 r, i64 v) {
  for (i32 x = l + s->size, y = r + s->size; x < y; x >>= 1, y >>= 1) {
    if (x & 1) s->a[x++].add += v;
    if (y & 1) s->a[--y].add += v;
  }
  save (s, l);
  save (s, r - 1);
}

i64 find (RMQandRAQ *s, i32 l, i32 r) {
  propagate (s, l);
  propagate (s, r - 1);
  i64 ans = 0;
  for (l += s->size, r += s->size; l < r; l >>= 1, r >>= 1) {
    if (l & 1) ans = max (ans, eval (s, l++));
    if (r & 1) ans = max (ans, eval (s, --r));
  }
  return ans;
}

typedef struct node {
  i32 l, v, next;
} node;

void run (void) {
  i32 n, m;
  scanf ("%" SCNi32 "%" SCNi32, &n, &m);
  node *a = (node *) calloc (m + 1, sizeof (node));
  i32 *start = (i32 *) calloc (n + 1, sizeof (i32));
  for (i32 i = 1; i <= m; ++i) {
    i32 l, r, v;
    scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32, &l, &r, &v);
    a[i] = (node) {l, v, start[r]};
    start[r] = i;
  }
  RMQandRAQ *s = new_RMQandRAQ (n + 1);
  for (i32 i = 1; i <= n; ++i) {
    i64 v = find (s, 0, i);
    add (s, i, i + 1, v);
    for (i32 p = start[i]; p; p = a[p].next) {
      add (s, a[p].l, i + 1, a[p].v);
    }
  }
  printf ("%" PRIi64 "\n", find (s, 0, n + 1));
}

int main (void) {
  run();
  return 0;
}