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 9.2.1) (judge verdict: Wrong Answer)

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

const int MAX_FEUILLE = (1 << 19);

struct Evenement{
    int temps, autre, type, nombre;

    bool operator<(Evenement autre) {
        return temps < autre.temps;
    }
};

struct Noeud{
    int debut, fin, maxInter, aAdd;
    int milieu() {return (fin-debut+1)/2;}
};

Noeud arbre[MAX_FEUILLE*2];

int nbFeuille;

void propage(int sommet) {
    arbre[sommet].maxInter += arbre[sommet].aAdd;
    if(sommet < nbFeuille) {
        arbre[sommet*2].aAdd += arbre[sommet].aAdd;
        arbre[sommet*2+1].aAdd += arbre[sommet].aAdd;
    }
    arbre[sommet].aAdd = 0;
}

void construireInter(int id) {
    if(id < nbFeuille) {
        arbre[id*2].debut = arbre[id].debut;
        arbre[id*2].fin = arbre[id].milieu() + arbre[id].debut - 1;
        arbre[id*2+1].debut = arbre[id*2].fin+1;
        arbre[id*2+1].fin = arbre[id].fin;
        construireInter(id*2);
        construireInter(id*2+1);
    }
}

bool estInclu(int d1, int f1, int d2, int f2) {
    return !(d2 > f1 || d1 > f2);
}

int reponseReq = -1e16;

void descendre(int id, int gauche, int droite, int nombre) {
    if(gauche > droite)
        return;
    if(gauche <= arbre[id].debut && droite >= arbre[id].fin) {
        arbre[id].aAdd += nombre;
        propage(id);

        reponseReq = max(reponseReq, arbre[id].maxInter);
    }
    else {
        propage(id);
        propage(id*2);
        propage(id*2+1);

        for(int i = 0; i < 2; i++) {
            if(estInclu(arbre[id*2+i].debut, arbre[id*2+i].fin, gauche, droite)) {
                descendre(id*2+i, gauche, droite, nombre);
            }
        }

        arbre[id].maxInter = max(arbre[id*2].maxInter, arbre[id*2+1].maxInter);
    }
}

int32_t main() {

    int taille, nbInter;
    cin >> taille >> nbInter;

    nbFeuille = (1 << ((int)ceil(log2(taille))));

    arbre[1].debut = 0;
    arbre[1].fin = nbFeuille-1;

    construireInter(1);

    vector<Evenement> event;

    int dp[taille] = {0};

    for(int i = 0; i < nbInter; i++) {
        int a,b,c;
        cin >> a >> b >> c;
        a--;

        if(a == 0) {
            dp[0] += c;
        }

        event.push_back({a, b, 1, c});
        event.push_back({b, a, 0, c});
    }

    sort(event.begin(), event.end());

    int idEvent = 0;

    while(idEvent < event.size() && event[idEvent].temps == 0) idEvent++;


    descendre(1, 0, 0, dp[0]);

    for(int bit = 1; bit < taille; bit++) {
        while(idEvent < nbInter*2 && event[idEvent].temps == bit) {
            if(event[idEvent].type == 1) {
                descendre(1, 0, bit-1, event[idEvent].nombre);
            }
            else {
                descendre(1, 0, event[idEvent].autre-1, -event[idEvent].nombre);
            }
            idEvent++;
        }
        reponseReq = -1e16;
        descendre(1, 0, bit-1, 0);
        dp[bit] = reponseReq;
        descendre(1, bit, bit, dp[bit]);

    }

    int total = 0;

    for(int i = 0; i < taille; i++) total = max(total, dp[i]);

    cout << total;

    return 0;
}