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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var avl=new Avl2(true);
		avl.Ud(0,s[0]+1,0);
		var h=new int[s[1]][];
		var g=new int[s[1]][];
		for(int i = 0;i<s[1];i++) {h[i]=g[i]=sc.Ia3(i);}
		Array.Sort(h,(u,v)=>u[1]-v[1]);
		Array.Sort(g,(u,v)=>u[2]-v[2]);
		for(int i = 1,j=0,k=0;i<=s[0];i++) {
			while(j<s[1]&&h[j][1]==i){
				avl.Ud(0,h[j][1]-1,h[j][3]);
				j++;
			}
			avl.Ud(i,i,avl.Get(0,i-1).Item2);
			while(k<s[1]&&g[k][2]==i){
				avl.Ud(0,g[k][1]-1,-g[k][3]);
				k++;
			}
		}
		Console.WriteLine("{0}",avl.Get(0,s[0]).Item2);
	}
}
public class Avl2{
	public class Nd{
		public int h=1,c=1;
		public int nl,nr,dnl;
		public long d,dd=0;
		public Nd l,r;
		public bool lf;
		public Nd(int nl,int nr,long d,Nd du){this.nl=dnl=nl;this.nr=nr;this.d=d;l=du;r=du;lf=true;}
		public Nd(int nl,int nr,Nd l,Nd r){
			this.nl=nl;this.nr=nr;this.l=l;this.r=r;
			compare(this);
			h=Max(l.h,r.h)+1;
			lf=false;
		}
	}
	public Nd root,du;
	public int cnt=0;
	private static Action<Nd> compare;
	private static Func<long,long,bool> compare2;
	public Avl2(bool bo){
		if(bo){compare=CoMax;compare2=CoMax2;}
		else{compare=CoMin;compare2=CoMin2;}
		du=new Nd(int.MinValue,int.MaxValue,bo?int.MinValue:int.MaxValue,du);
		du.h=0;du.c=0;
		root=du;
	}
	private void CoMax(Nd nd){
		if(nd.l.d+nd.l.dd>nd.r.d+nd.r.dd){
			nd.d=nd.l.d+nd.l.dd;
			nd.dnl=nd.l.dnl;
		}
		else{
			nd.d=nd.r.d+nd.r.dd;
			nd.dnl=nd.r.dnl;
		}
	}
	private void CoMin(Nd nd){
		if(nd.l.d+nd.l.dd<nd.r.d+nd.r.dd){
			nd.d=nd.l.d+nd.l.dd;
			nd.dnl=nd.l.dnl;
		}
		else{
			nd.d=nd.r.d+nd.r.dd;
			nd.dnl=nd.r.dnl;
		}
	}
	private bool CoMax2(long a,long b){return a>=b;}
	private bool CoMin2(long a,long b){return a<=b;}
	public void Ud(int a,int b,long d){
		if(a>b){a^=b;b^=a;a^=b;}
		if(root!=du){
			root=Fu(root,a,b,d);
			if(Abs(root.l.h-root.r.h)>1){root=Rotate(root);}
		}
		else{root=new Nd(a,b,d,du);}
	}
	private Nd Fu(Nd t,int nl,int nr,long d){
		Fdd(t);
		if(!t.lf){
			if(t.nl==nl&&t.nr==nr){
				t.d+=d;
				t.l.dd+=d;
				t.r.dd+=d;
				return t;
			}
			if(t.l.nr>=nl){t.l=Fu(t.l,nl,Min(t.l.nr,nr),d);}
			if(t.l.nr<nr){t.r=Fu(t.r,Max(t.l.nr+1,nl),nr,d);}
			if(Abs(t.l.l.h-t.l.r.h)>1){t.l=Rotate(t.l);}
			if(Abs(t.r.l.h-t.r.r.h)>1){t.r=Rotate(t.r);}
			compare(t);
			t.nl=t.l.nl;
			t.nr=t.r.nr;
			t.h=Max(t.l.h,t.r.h)+1;
		}
		else{
			if(t.nr<nl){return new Nd(t.nl,nr,t,new Nd(nl,nr,d,du));}
			else if(t.nl>nr){return new Nd(nl,t.nr,new Nd(nl,nr,d,du),t);}
			else if(t.nl<nl){
				if(t.nr<nr){
					Nd e=new Nd(t.nl,nr,t,new Nd(nl,nr,new Nd(nl,t.nr,t.d+d,du),new Nd(t.nr+1,nr,d,du)));
					t.nr=nl-1;
					return e;
				}
				else if(t.nr>nr){
					Nd e=new Nd(t.nl,t.nr,t,new Nd(nl,t.nr,new Nd(nl,nr,t.d+d,du),new Nd(nr+1,t.nr,t.d,du)));
					t.nr=nl-1;
					return e;
				}
				else{
					t.nr=nl-1;
					return new Nd(t.nl,nr,t,new Nd(nl,nr,t.d+d,du));
				}
			}
			else if(t.nl>nl){
				if(t.nr<nr){
					Nd e=new Nd(nl,nr,new Nd(nl,t.nl-1,d,du),new Nd(t.nl,nr,t,new Nd(t.nr+1,nr,d,du)));
					t.d+=d;
					return e;
				}
				else if(t.nr>nr){
					Nd e=new Nd(nl,t.nr,new Nd(nl,t.nl-1,d,du),new Nd(t.nl,t.nr,new Nd(t.nl,nr,t.d+d,du),t));
					t.nl=nr+1;
					return e;
				}
				else{
					t.d+=d;
					return new Nd(nl,nr,new Nd(nl,t.nl-1,d,du),t);
				}
			}
			else{
				if(t.nr<nr){
					t.d+=d;
					return new Nd(nl,nr,t,new Nd(t.nr+1,nr,d,du));
				}
				else if(t.nr>nr){
					t.nl=nr+1;
					return new Nd(nl,t.nr,new Nd(nl,nr,t.d+d,du),t);
				}
				else{
					t.d+=d;
					return t;
				}
			}
		}
		return t;
	}
	public Tuple<int,long> Get(int a,int b){return Fdg(root,a,b);}
	private Tuple<int,long> Fdg(Nd t,int nl,int nr){
		Fdd(t);
		if(t.lf){return Tuple.Create(t.dnl,t.d);}
		if(nl<=t.nl&&nr>=t.nr){return Tuple.Create(t.dnl,t.d);}
		var d=Tuple.Create(du.dnl,du.d);
		if(t.l.nr>=nl){d=Fdg(t.l,nl,nr);}
		if(t.r.nl<=nr){
			var e=Fdg(t.r,nl,nr);
			d=compare2(d.Item2,e.Item2)?d:e;
		}
		return d;
	}
	private void Fdd(Nd t){
		if(t.dd!=0){
			t.d+=t.dd;
			t.l.dd+=t.dd;
			t.r.dd+=t.dd;
			t.dd=0;
		}
	}
	private Nd Rotate(Nd t){
		Nd nd=du;
		Fdd(t);
		if(t.l.h>t.r.h){
			Fdd(t.l);
			if(t.l.l.h>t.l.r.h){
				nd=t.l;t.l=t.l.r;nd.r=t;
				Ft(t);
			}
			else{
				Fdd(t.l.r);
				nd=t.l.r;
				t.l.r=nd.l;nd.l=t.l;
				Ft(t.l);
				t.l=nd.r;nd.r=t;
				Ft(t);
			}
		}
		else{
			Fdd(t.r);
			if(t.r.l.h>t.r.r.h){
				Fdd(t.r.l);
				nd=t.r.l;
				t.r.l=nd.r;nd.r=t.r;
				Ft(t.r);
				t.r=nd.l;nd.l=t;
				Ft(t);
			}
			else{
				nd=t.r;t.r=t.r.l;nd.l=t;
				Ft(t);
			}
		}
		Ft(nd);
		return nd;
	}
	public Tuple<int,long> Gl(int a,int b,long d){
		Fdd(root);
		return compare2(root.d,d)?Fgl(root,a,b,d):Tuple.Create(du.nr,-1L);
	}
	private Tuple<int,long> Fgl(Nd t,int a,int b,long d){
		if(t.lf){return Tuple.Create(t.nl,t.d);}
		Fdd(t.l);
		if(a<=t.l.nr&&t.l.nl<=b&&compare2(t.l.d,d)){
			var z=Fgl(t.l,a,b,d);
			if(z.Item1!=du.nr){return z;}
		}
		Fdd(t.r);
		if(a<=t.r.nr&&t.r.nl<=b&&compare2(t.r.d,d)){return Fgl(t.r,a,b,d);}
		return Tuple.Create(du.nr,-1L);
	}
	public Tuple<int,long> Gr(int a,int b,long d){
		Fdd(root);
		return compare2(root.d,d)?Fgr(root,a,b,d):Tuple.Create(du.nl,-1L);
	}
	private Tuple<int,long> Fgr(Nd t,int a,int b,long d){
		if(t.lf){return Tuple.Create(t.nr,t.d);}
		Fdd(t.r);
		if(a<=t.r.nr&&t.r.nl<=b&&compare2(t.r.d,d)){
			var z=Fgr(t.r,a,b,d);
			if(z.Item1!=du.nl){return z;}
		}
		Fdd(t.l);
		if(a<=t.l.nr&&t.l.nl<=b&&compare2(t.l.d,d)){return Fgr(t.l,a,b,d);}
		return Tuple.Create(du.nl,-1L);
	}
	private void Ft(Nd t){
		if(!t.lf){
			t.h=Max(t.l.h,t.r.h)+1;
			t.c=t.l.c+t.r.c;
			compare(t);
			t.nl=t.l.nl;
			t.nr=t.r.nr;
		}
	}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),int.Parse);}
	public int[] Ia3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),long.Parse);}
	public long[] La3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(int a){return Array.ConvertAll((a.ToString()+" "+Console.ReadLine()).Split(),double.Parse);}
	public double[] Da3(bool a,int b,bool c,int d){return Array.ConvertAll(((a?b.ToString()+" ":"")+Console.ReadLine()+(c?" "+d.ToString():"")).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}