Problem:
Iron Bars

A boy PCK had N straight iron bars, which were serially indexed. Unfortunately, the first M bars (0 ≤ M ≤ N) among them were bent during transportation. They all suffered a perpendicular bend at one point.

He is planning to make a cube using a set of bars selected using the following rules: X bars from bent ones, Y bars from straight ones, where 2X + Y = 12. Any two bars can be jointed only at the apexes of the cube. He wants to count how many types of rectangular parallelepipeds (hereafter RP) he can make using these bars.

Make a program to count out types (different shapes) of RPs that PCK can make using the following information: the number of bars and length of each one, position of the bend, and the number of bars to be used to construct an RP. Note that any two RPs similar in shape are considered identical: namely if the length of three defining sides of two RPs coincide if arranged in increasing/decreasing order (e.g., three sides of RP i and j are A_i, B_i, C_i, and A_j, B_j and C_j in increasing order, then the relations A_i = A_j, B_i = B_j, and C_i = C_j hold. Note also that the bars are sufficiently thin to allow you to consider them as idealized lines.

Input

The input is given in the following format.

N M X Y
a_1
a_2
:
a_N
b_1
b_2
:
b_M

The first line provides the total number of iron bars and bent bars, and those straight and bent bars used to construct an RP: N (6 ≤ N ≤ 6000), M (0 ≤ M ≤ N), X (0 ≤ X ≤ 6), and Y (0 ≤ Y ≤ 12). The following relations always hold for them: 2X+Y=12, X+Y ≤ N, X ≤ M. Each of the subsequent N lines provides the length of the i-th bar a_i (1 ≤ a_i ≤ 6000) in integers. Furthermore, each of the subsequent M lines provides the location at which the i-th bent bar suffered a perpendicular bend b_i (1 ≤ b_i ≤ 3000) in centimeters from one end of the bar (note: 1 ≤ a_i-b_i ≤ 3000).

Output

Output the number of constructible rectangular parallelepipeds.

Sample Input 1

18 8 3 6
4
3
3
3
3
2
2
2
1
1
1
1
1
2
2
3
3
3
1
1
1
1
1
1
1
1

Sample Output 1

3

Solution in C++14 (judge verdict: Wrong Answer)

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

using LL = long long;

int N, M, X, Y;
vector<int> A, B;
vector<vector<int>> E(6001, vector<int>(6001));
vector<int> D(6001);

LL combination(auto n, auto r) {
  if(n < r) return 0;
  LL res = 1;
  for(auto i = 0; i < r; ++i) res *= n - i;
  for(auto i = 0; i < r; ++i) res /= i + 1;
  return res;
}

LL straight() {
  auto four = 0, eight = 0, twelve = 0;
  for(auto i: D) {
    if(12 <= i) ++twelve;
    if( 8 <= i) ++eight;
    if( 4 <= i) ++four;
  }
  return combination(four, 3) + combination(eight, 1) * combination(four - 1, 1) + combination(twelve, 1);
}

vector<vector<int>> split(auto x) {
  vector<vector<int>> res;
  vector<int> v(3);
  function<void(int, int)> dfs = [&](auto x, auto d) {
    if(d == 3) {
      if(x == 0) res.emplace_back(v);
      return;
    }
    for(auto i = 0; i <= min(4, x); ++i) {
      v[d] = i;
      dfs(x - i, d + 1);
    }
  };
  dfs(x, 0);
  return res;
}

LL cube(auto x) {
  if(E[x][x] < X) return 0;
  if(2 * X + D[x] < 12) return 0;
  return 1;
}

LL cuboidA(auto x, auto y) {
  if(E[x][x] + E[x][y] < X) return 0;
  for(auto i = 0; i <= X; ++i) {
    auto j = X - i;
    if(E[x][x] < i) continue;
    if(E[x][y] < j) continue;
    if(8 <= 2 * i + j + D[x] && 4 <= j + D[y]) return 1;
  }
  return 0;
}

LL cuboidB(auto x, auto y) {
  if(E[x][y] + E[y][y] < X) return 0;
  for(auto i = 0; i <= X; ++i) {
    auto j = X - i;
    if(E[x][y] < i) continue;
    if(E[y][y] < j) continue;
    if(4 <= i + D[x] && 8 <= i + 2 * j + D[y]) return 1;
  }
  return 0;
}

LL cuboidC(auto x, auto y, auto z) {
  if(E[x][y] + E[x][z] + E[y][z] < X) return 0;
  for(auto v: split(X)) {
    if(E[x][y] < v[0]) continue;
    if(E[x][z] < v[1]) continue;
    if(E[y][z] < v[2]) continue;
    auto a = v[0] + v[1];
    auto b = v[0] + v[2];
    auto c = v[1] + v[2];
    if(4 < a || 4 < b || 4 < c) continue;
    if(4 <= a + D[x] && 4 <= b + D[y] && 4 <= c + D[z]) return 1;
  }
  return 0;
}

LL solve() {
  auto res = 0;
  set<vector<int>> used;
  for(auto i = 0; i < M; ++i) for(auto j = 1; j <= 6000; ++j) {
    vector<int> L = {A[i], B[i], j};
    sort(begin(L), end(L));

    if(used.count(L)) continue;
    used.emplace(L);

    if(L[0] == L[2])      res += cube(L[0]);
    else if(L[0] == L[1]) res += cuboidA(L[0], L[2]);
    else if(L[1] == L[2]) res += cuboidB(L[0], L[1]);
    else                  res += cuboidC(L[0], L[1], L[2]);
  }
  return res;
}

int main() {
  cin >> N >> M >> X >> Y;
  A.resize(N);
  for(auto& i: A) cin >> i;
  B.resize(M);
  for(auto& i: B) cin >> i;

  for(auto i = 0; i < N; ++i) {
    if(i < M) {
      A[i] -= B[i];
      if(A[i] > B[i]) swap(A[i], B[i]);
      ++E[A[i]][B[i]];
    } else {
      ++D[A[i]];
    }
  }

  if(X) cout << solve() << endl;
  else  cout << straight() << endl;
}