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 <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
#include <vector>


using usize = std::size_t;
using i64 = std::int_fast64_t;

constexpr i64 Minf = std::numeric_limits<i64>::min() / 5;

struct segtree {
  std::vector<i64> c;

  segtree() = default;
  segtree(usize n) : c(n * 2, Minf) {}

  void set(usize i, i64 v) {
    i += c.size() / 2;
    c[i] = std::max(c[i], v);
    while (i != 1) {
      i /= 2;
      c[i] = std::max(c[i * 2], c[i * 2 + 1]);
    }
  }

  i64 get(usize l, usize r) const {
    i64 ret = Minf;
    l += c.size() / 2;
    r += c.size() / 2;
    while (l < r) {
      if (l % 2 == 1) {
        ret = std::max(ret, c[l]);
        l += 1;
      }
      l /= 2;
      if (r % 2 == 1) {
        r -= 1;
        ret = std::max(ret, c[r]);
      }
      r /= 2;
    }
    return ret;
  }

  void dump() const {
    for (usize i = 0; i < c.size() / 2; ++i) {
      std::cout << c[i + c.size() / 2] << ' ';
    }
    std::cout << std::endl;
  }
};

int main() {
  usize n, m;
  std::cin >> n >> m;
  i64 x, y;
  std::cin >> x >> y;

  struct inter_t {
    usize c;
    usize r;
  };
  std::vector<std::vector<inter_t>> is;
  std::vector<i64> zip;

  struct interval_t {
    usize c;
    i64 l, r;
  };
  std::vector<interval_t> intervals(m);
  for (auto& e : intervals) {
    std::cin >> e.c >> e.l >> e.r;
    --e.c;
    --e.l;
    zip.push_back(e.l);
    zip.push_back(e.r);
  }
  std::sort(zip.begin(), zip.end());
  zip.erase(std::unique(zip.begin(), zip.end()), zip.end());
  n = zip.size();
  const auto rzip = [&](const i64 x) -> usize {
    return std::lower_bound(zip.cbegin(), zip.cend(), x) - zip.cbegin();
  };
  is.resize(n);
  for (const auto& e : intervals) {
    is[rzip(e.l)].push_back({ e.c, rzip(e.r) });
  }

  std::array<segtree, 3> dp, dps;
  i64 curmax = Minf;
  for (usize i = 0; i < 3; ++i) {
    dp[i] = segtree(n);
    dps[i] = segtree(n);
    dp[i].set(0, -(x + x + y) * zip[0]);
    dps[i].set(0, -x * zip[0]);
  }
  for (usize i = 0; i < n; ++i) {
    for (usize d = 0; d < 3; ++d) {
      curmax = std::max(curmax, dps[d].get(i, i + 1) + x * zip[i]);
    }
    for (const auto& e : is[i]) {
      {
        const i64 t = curmax + x * (zip[e.r] - zip[i]);
        dp[e.c].set(e.r, t - (x + x + y) * zip[e.r]);
        dps[e.c].set(e.r, t - x * zip[e.r]);
      }
      for (usize d = 0; d < 3; ++d) {
        if (e.c == d) {
          const i64 t =
            dps[d].get(i, e.r + 1) + x * zip[i] + x * (zip[e.r] - zip[i]);
          dp[e.c].set(e.r, t - (x + x + y) * zip[e.r]);
          dps[e.c].set(e.r, t - x * zip[e.r]);
        }
        else {
          const i64 t = dp[d].get(i, e.r + 1) + (x + x + y) * zip[i] +
            x * (zip[e.r] - zip[i]);
          dp[e.c].set(e.r, t - (x + x + y) * zip[e.r]);
          dps[e.c].set(e.r, t - x * zip[e.r]);
        }
      }
    }
  }
  std::cout << curmax << std::endl;
  return 0;
}