Problem:
Score : 1200 points

Problem Statement

Let M be a positive integer.

You are given 2 N integers a_1, a_2, \ldots, a_{2 N}, where 0 \leq a_i < M for each i.

Consider dividing the 2 N integers into N pairs.
Here, each integer must belong to exactly one pair.

We define the ugliness of a pair (x, y) as (x + y) \mod M.
Let Z be the largest ugliness of the N pairs. Find the minimum possible value of Z.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^9

0 \leq a_i < M

Input

Input is given from Standard Input in the following format:

N M
a_1 a_2 \cdots a_{2N}

Output

Print the minimum possible value of Z, where Z is the largest ugliness of the N pairs.

Sample Input 1

3 10
0 2 3 4 5 9

Sample Output 1

5

One solution is to form pairs (0, 5), (2, 3) and (4, 9), with ugliness 5, 5 and 3, respectively.

Sample Input 2

2 10
1 9 1 9

Sample Output 2

0

Pairs (1, 9) and (1, 9) should be formed, with ugliness both 0.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver2 {
 public:
  int n, m;
  vector<map<int, int>> t;
  Solver2(LL n, LL m) : n(n), m(m), t(n){};

  void solve() {
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      t[--a][--b] = 0;
      t[b][a] = 0;
    }
    bool ans = true;
    int ans2 = 0;
    int cnt6 = 0;
    vector<int> v4;
    for (int i = 0; i < n; ++i) {
      if (t[i].size() % 2) ans = false;
      ans2 += max(0, (int)t[i].size() - 2);
      if (t[i].size() >= 6) cnt6++;
      if (t[i].size() == 4) v4.push_back(i);
    }
    if (ans && ans2 >= 4) {
      if (ans2 >= 6 || cnt6) {
        cout << "Yes" << endl;
      } else {
        cout << (check(v4[0], v4[1]) ? "Yes" : "No") << endl;
      }
    } else {
      cout << "No" << endl;
    }
  }
  bool check(int s, int v) {
    int now = s;
    vector<int> cnt(n);
    while (1) {
      cnt[now]++;
      if (cnt[s] == 2) return false;
      if (cnt[v] == 2) return true;
      for (auto e : t[now]) {
        if (e.second == 0) {
          e.second = 1;
          now = e.first;
          break;
        }
      }
    }
  }
};
}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0, m;
  std::cin >> n >> m;

  Problem::Solver2 sol(n, m);
  sol.solve();
  return 0;
}