Problem: Wall Painting

Here stands a wall made of a number of vertical panels. The panels are not painted yet.

You have a number of robots each of which can paint panels in a single color, either red, green, or blue. Each of the robots, when activated, paints panels between a certain position and another certain position in a certain color. The panels painted and the color to paint them are fixed for each of the robots, but which of them are to be activated and the order of their activation can be arbitrarily decided.

You’d like to have the wall painted to have a high aesthetic value. Here, the aesthetic value of the wall is defined simply as the sum of aesthetic values of the panels of the wall, and the aesthetic value of a panel is defined to be:

* 0, if the panel is left unpainted.
* The bonus value specified, if it is painted only in a single color, no matter how many times it is painted.
* The penalty value specified, if it is once painted in a color and then overpainted in one or more different colors.



Input

The input consists of a single test case of the following format.


$n$ $m$ $x$ $y$
$c_1$ $l_1$ $r_1$
.
.
.
$c_m$ $l_m$ $r_m$


Here, $n$ is the number of the panels ($1 \leq n \leq 10^9$) and $m$ is the number of robots ($1 \leq m \leq 2 \times 10^5$). $x$ and $y$ are integers between $1$ and $10^5$, inclusive. $x$ is the bonus value and $-y$ is the penalty value. The panels of the wall are consecutively numbered $1$ through $n$. The $i$-th robot, when activated, paints all the panels of numbers $l_i$ through $r_i$ ($1 \leq l_i \leq r_i \leq n$) in color with color number $c_i$ ($c_i \in \\{1, 2, 3\\}$). Color numbers 1, 2, and 3 correspond to red, green, and blue, respectively.

Output

Output a single integer in a line which is the maximum achievable aesthetic value of the wall.

Sample Input 1


8 5 10 5
1 1 7
3 1 2
1 5 6
3 1 4
3 6 8


Sample Output 1


70


Sample Input 2


26 3 9 7
1 11 13
3 1 11
3 18 26


Sample Output 2


182


Sample Input 3


21 10 10 5
1 10 21
3 4 16
1 1 7
3 11 21
3 1 16
3 3 3
2 1 17
3 5 18
1 7 11
2 3 14


Sample Output 3


210


Sample Input 4


21 15 8 7
2 12 21
2 1 2
3 6 13
2 13 17
1 11 19
3 3 5
1 12 13
3 2 2
1 12 15
1 5 17
1 2 3
1 1 9
1 8 12
3 8 9
3 2 9


Sample Output 4


153






Example

Input

8 5 10 5
1 1 7
3 1 2
1 5 6
3 1 4
3 6 8


Output

70

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



struct Info{

	bool operator<(const struct Info &arg) const{
		if(left != arg.left){

			return left < arg.left;
		}else{

			return right < arg.right;
		}
	};

	ll color,left,right;
};

int N;
ll table[7][2000005];
map<int,int> MAP;
Info info[200005];


void init(int first_N){
	while(N < first_N)N *= 2;
}

void update(int index,int loc,ll value){
	loc += N-1;

	if(table[index][loc] >= value)return;

	table[index][loc] = value;

	if(N == 1)return;

	int parent = (loc-1)/2;

	while(true){
		table[index][parent] = max(table[index][2*parent+1],table[index][2*parent+2]); //親は左右の子の大きい方の値を持つ

		if(parent == 0)break;
		else{
			parent = (parent-1)/2;
		}
	}
}


ll query(int index,int search_left,int search_right,int node_id,int node_left,int node_right){

	if(search_right < node_left || search_left > node_right)return -HUGE_NUM;

	if(search_left <= node_left && search_right >= node_right){
		return table[index][node_id];
	}

	ll left_max = query(index,search_left,search_right,2*node_id+1,node_left,(node_left+node_right)/2);
	ll right_max = query(index,search_left,search_right,2*node_id+2,(node_left+node_right)/2+1,node_right);

	return max(left_max,right_max);
}


int main(){

	int num_panel,num_robot;
	ll X,Y;

	scanf("%d %d %lld %lld",&num_panel,&num_robot,&X,&Y);

	vector<int> V;
	V.push_back(0);

	for(int i = 0; i < num_robot; i++){

		scanf("%lld %lld %lld",&info[i].color,&info[i].left,&info[i].right);
		V.push_back(info[i].left);
		V.push_back(info[i].right);
	}

	sort(V.begin(),V.end());
	V.erase(unique(V.begin(),V.end()),V.end());

	for(int i = 0; i < V.size(); i++){

		MAP[V[i]] = i;
	}

	int first_N = V.size();
	N = 1;
	init(first_N);

	for(int i = 0; i < 7; i++){
		for(int k = 0; k <= 2*N-2; k++){

			table[i][k] = -HUGE_NUM;
		}
	}

	update(0,0,0);

	sort(info,info+num_robot);

	ll ans = 0;
	ll tmp_value;

	for(int i = 0; i < num_robot; i++){

		tmp_value = query(0,0,MAP[info[i].left]-1,0,0,N-1)+(info[i].right-info[i].left+1)*X; //過去最大値+今回の値(重なりなし)

		//重なりあり
		for(int k = 1; k <= 3; k++){
			if(k == info[i].color){

				tmp_value = max(tmp_value,query(k,MAP[info[i].left],MAP[info[i].right]-1,0,0,N-1)+info[i].right*X);

			}else{

				tmp_value = max(tmp_value,query(k+3,MAP[info[i].left],MAP[info[i].right]-1,0,0,N-1)+
						(info[i].left+info[i].right-1)*X+(info[i].left-1)*Y);
			}
		}

		ans = max(ans,tmp_value);

		update(0,MAP[info[i].right],tmp_value); //全体
		update(info[i].color,MAP[info[i].right],tmp_value-info[i].right*X);
		update(info[i].color+3,MAP[info[i].right],tmp_value-info[i].right*(2*X+Y));
	}

	printf("%lld\n",ans);

	return 0;
}