prithivMLmods/Coder-Stat huggingface dataset:
Problem p03091 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
vector<int> G[100006];
int life[100006];

void dfs(int now, int par, int home) {
  int e;
  rep(i, G[now].size()) {
    int f = G[now][i];
    if (f == par)
      continue;
    if (life[f] == 0)
      continue;
    e = f;
  }
  //	cout << now << ' ' << e << endl;
  life[now]--;
  if (e == home)
    return;
  dfs(e, now, home);
  return;
}

signed main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int flg1 = 0, maxo = 0;
  rep(i, n) {
    if (G[i].size() & 1)
      flg1++;
    maxo = max(maxo, (int)G[i].size());
  }
  if (flg1 || maxo < 3) {
    cout << "No" << endl;
    return 0;
  }
  if (maxo >= 6) {
    cout << "Yes" << endl;
    return 0;
  }
  int cnt4 = 0;
  rep(i, n) if (G[i].size() == 4) cnt4++;
  if (cnt4 == 1) {
    cout << "No" << endl;
    return 0;
  }
  if (cnt4 == 3) {
    cout << "Yes" << endl;
    return 0;
  }

  rep(i, n) life[i] = G[i].size() / 2;

  int v1 = -1, v2;
  rep(i, n) {
    if (G[i].size() == 4) {
      if (v1 == -1)
        v1 = i;
      else
        v2 = i;
    }
  }
  dfs(v1, -1, v1);
  life[v2]++;
  dfs(v2, -1, v2);
  life[v2]--;
  rep(i, n) {
    if (life[i]) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
vector<int> G[100006];
int life[100006];

void dfs(int now, int par, int home) {
  int e;
  rep(i, G[now].size()) {
    int f = G[now][i];
    if (f == par)
      continue;
    if (life[f] == 0)
      continue;
    e = f;
    break;
  }
  //	cout << now << ' ' << e << endl;
  life[now]--;
  if (e == home)
    return;
  dfs(e, now, home);
  return;
}

signed main() {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int flg1 = 0, maxo = 0;
  rep(i, n) {
    if (G[i].size() & 1)
      flg1++;
    maxo = max(maxo, (int)G[i].size());
  }
  if (flg1 || maxo < 3) {
    cout << "No" << endl;
    return 0;
  }
  if (maxo >= 6) {
    cout << "Yes" << endl;
    return 0;
  }
  int cnt4 = 0;
  rep(i, n) if (G[i].size() == 4) cnt4++;
  if (cnt4 == 1) {
    cout << "No" << endl;
    return 0;
  }
  if (cnt4 == 3) {
    cout << "Yes" << endl;
    return 0;
  }

  rep(i, n) life[i] = G[i].size() / 2;

  int v1 = -1, v2;
  rep(i, n) {
    if (G[i].size() == 4) {
      if (v1 == -1)
        v1 = i;
      else
        v2 = i;
    }
  }
  dfs(v1, -1, v1);
  life[v2]++;
  dfs(v2, -1, v2);
  life[v2]--;
  rep(i, n) {
    if (life[i]) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
}
Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<vector<int>> adj;
vector<bool> passed;

int dfs(int n, int to, int p = -1) {
  passed[n] = true;
  ll result = 0;
  for (auto x : adj[n]) {
    if (x == p)
      continue;
    if (x == to)
      result++;
    else
      result += dfs(x, to, n);
  }
  return result;
}

int main() {
  int N, M;
  cin >> N >> M;
  adj.resize(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  bool ok = true, to6 = false;
  vector<int> to4;
  rep(i, N) {
    if (adj[i].size() % 2)
      ok = false;
    if (adj[i].size() == 4)
      to4.push_back(i);
    if (adj[i].size() >= 6)
      to6 = true;
  }

  string result = "No";
  if (ok) {
    if (to6 || to4.size() >= 3)
      result = "Yes";
    else if (to4.size() == 2) {
      passed.resize(N, false);
      if (dfs(to4[0], to4[1]) == 2)
        result = "Yes";
    }
  }
  cout << result << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (int i = (int)from; i < (int)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<vector<int>> adj;
vector<bool> passed;

int dfs(int n, int to, int p = -1) {
  passed[n] = true;
  ll result = 0;
  for (auto x : adj[n]) {
    if (x == p || passed[x])
      continue;
    if (x == to)
      result++;
    else
      result += dfs(x, to, n);
  }
  return result;
}

int main() {
  int N, M;
  cin >> N >> M;
  adj.resize(N);
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  bool ok = true, to6 = false;
  vector<int> to4;
  rep(i, N) {
    if (adj[i].size() % 2)
      ok = false;
    if (adj[i].size() == 4)
      to4.push_back(i);
    if (adj[i].size() >= 6)
      to6 = true;
  }

  string result = "No";
  if (ok) {
    if (to6 || to4.size() >= 3)
      result = "Yes";
    else if (to4.size() == 2) {
      passed.resize(N, false);
      if (dfs(to4[0], to4[1]) == 2)
        result = "Yes";
    }
  }
  cout << result << endl;
  return 0;
}
Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  int odd = 0;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  int mx = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    mx = max(mx, (int)g[i].size());
    if (g[i].size() == 4) {
      cnt++;
    }
    if (g[i].size() % 2 == 1) {
      puts("No");
      return 0;
    }
  }
  if (mx >= 6 || cnt >= 3) {
    puts("Yes");
    return 0;
  }
  if (mx == 2 || cnt < 2) {
    puts("No");
    return 0;
  }
  int x = -1, y;
  for (int i = 0; i < n; i++) {
    if (g[i].size() == 4) {
      y = i;
      if (x == -1) {
        swap(x, y);
      }
    }
  }
  int ok = 4;
  function<void(int, int)> dfs = [&](int v, int p) {
    for (int to : g[v]) {
      if (to == p)
        continue;
      if (to == y) {
        ok--;
        break;
      }
      dfs(to, v);
    }
  };
  dfs(x, -1);
  if (ok) {
    puts("Yes");
  } else {
    puts("No");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> g(n);
  int odd = 0;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].emplace_back(b);
    g[b].emplace_back(a);
  }
  int mx = 0, cnt = 0;
  for (int i = 0; i < n; i++) {
    mx = max(mx, (int)g[i].size());
    if (g[i].size() == 4) {
      cnt++;
    }
    if (g[i].size() % 2 == 1) {
      puts("No");
      return 0;
    }
  }
  if (mx >= 6 || cnt >= 3) {
    puts("Yes");
    return 0;
  }
  if (mx == 2 || cnt < 2) {
    puts("No");
    return 0;
  }
  int x = -1, y;
  for (int i = 0; i < n; i++) {
    if (g[i].size() == 4) {
      y = i;
      if (x == -1) {
        swap(x, y);
      }
    }
  }
  int ok = 4;
  function<void(int, int)> dfs = [&](int v, int p) {
    for (int to : g[v]) {
      if (to == p)
        continue;
      if (to == y) {
        ok--;
        break;
      }
      if (to == x) {
        break;
      }
      dfs(to, v);
    }
  };
  dfs(x, -1);
  if (ok) {
    puts("Yes");
  } else {
    puts("No");
  }
  return 0;
}
Problem p03091 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

const int N = 100005;

int n, m, deg[N], head[N], tot;
struct edge {
  int to, nxt;
} e[N << 1];

void link(int x, int y) {
  e[++tot] = {y, head[x]}, head[x] = tot;
  e[++tot] = {x, head[y]}, head[y] = tot;
}
int dfs(int x, int start, int target, int f = 0) {
  if (x == target)
    return 1;
  int ret = 0;
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != start && e[i].to != f)
      ret += dfs(e[i].to, start, target);
  return ret;
}
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m;
  for (int i = 1, x, y; i <= m; ++i)
    std::cin >> x >> y, ++deg[x], ++deg[y], link(x, y);
  // partition Euler circuit into three circuits
  for (int i = 1; i <= n; ++i)
    if (deg[i] & 1) // no Euler circuit
      return std::cout << "No" << '\n', 0;
  for (int i = 1; i <= n; ++i)
    if (deg[i] >= 6) { // AAA => A + A + A
      return std::cout << "Yes" << '\n', 0;
    }
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    cnt += deg[i] == 4;
  if (cnt >= 3) {
    // ABCABC, ABCACB, ABCBAC => AB + BC + CA
    // ABCBCA => A + ABC + BC
    // ABCCAB => AB + ABC + C
    // ABCCBA => A + ABCB + C
    return std::cout << "Yes" << '\n', 0;
  } else if (cnt <= 1) {
    // AA : no solution
    return std::cout << "No" << '\n', 0;
  }
  // ABBA => A + AB + B
  // ABAB : no solution
  int L = 0, R = 0;
  for (int i = 1; i <= n; ++i)
    if (deg[i] == 4)
      (L ? R : L) = i;
  std::cout << (dfs(L, L, R) == 2 ? "Yes\n" : "No\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

const int N = 100005;

int n, m, deg[N], head[N], tot;
struct edge {
  int to, nxt;
} e[N << 1];

void link(int x, int y) {
  e[++tot] = {y, head[x]}, head[x] = tot;
  e[++tot] = {x, head[y]}, head[y] = tot;
}
int dfs(int x, int start, int target, int f = 0) {
  if (x == target)
    return 1;
  int ret = 0;
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != start && e[i].to != f)
      ret += dfs(e[i].to, start, target, x);
  return ret;
}
int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m;
  for (int i = 1, x, y; i <= m; ++i)
    std::cin >> x >> y, ++deg[x], ++deg[y], link(x, y);
  // partition Euler circuit into three circuits
  for (int i = 1; i <= n; ++i)
    if (deg[i] & 1) // no Euler circuit
      return std::cout << "No" << '\n', 0;
  for (int i = 1; i <= n; ++i)
    if (deg[i] >= 6) { // AAA => A + A + A
      return std::cout << "Yes" << '\n', 0;
    }
  int cnt = 0;
  for (int i = 1; i <= n; ++i)
    cnt += deg[i] == 4;
  if (cnt >= 3) {
    // ABCABC, ABCACB, ABCBAC => AB + BC + CA
    // ABCBCA => A + ABC + BC
    // ABCCAB => AB + ABC + C
    // ABCCBA => A + ABCB + C
    return std::cout << "Yes" << '\n', 0;
  } else if (cnt <= 1) {
    // AA : no solution
    return std::cout << "No" << '\n', 0;
  }
  // ABBA => A + AB + B
  // ABAB : no solution
  int L = 0, R = 0;
  for (int i = 1; i <= n; ++i)
    if (deg[i] == 4)
      (L ? R : L) = i;
  std::cout << (dfs(L, L, R) == 2 ? "Yes\n" : "No\n");
  return 0;
}
Problem p03092 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

ll dp[1005][1005];
// 数字iまで見たときに、iを絶対位置j番目に置くような場合の数

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);

  ll n, a, b;
  cin >> n >> a >> b;

  ll p[n];
  map<ll, int> pos;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    pos[p[i]] = i;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j < pos[i])
        dp[i][j] += b;
      if (j > pos[i])
        dp[i][j] += a;
      if (j)
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
    }
  }

  cout << dp[n][n - 1] << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
const ll MOD = 1000000007LL;

ll dp[5005][5005];
// 数字iまで見たときに、iを絶対位置j番目に置くような場合の数

int main() {
  cin.sync_with_stdio(false);
  cin.tie(0);

  ll n, a, b;
  cin >> n >> a >> b;

  ll p[n];
  map<ll, int> pos;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    pos[p[i]] = i;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      dp[i][j] = dp[i - 1][j];
      if (j < pos[i])
        dp[i][j] += b;
      if (j > pos[i])
        dp[i][j] += a;
      if (j)
        dp[i][j] = min(dp[i][j], dp[i][j - 1]);
    }
  }

  cout << dp[n][n - 1] << "\n";
  return 0;
}
Problem p03092 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9, MOD = 1e9 + 7;

ll n, d[5000][5000], A, B;

int main() {
  cin >> n >> A >> B;

  for (ll i = 1; i <= n; i++) {
    ll a;

    scanf("%lld", &a);

    for (ll j = 0; j <= n; j++) {
      if (j < a)
        d[i][j] = d[i - 1][j] + A;
      else if (j == a)
        d[i][j] = d[i - 1][j];
      else
        d[i][j] = d[i - 1][j] + B;
      if (j)
        d[i][j] = min(d[i][j], d[i][j - 1]);
    }
  }

  cout << d[n][n];
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <vector>

using namespace std;

typedef long long ll;

const ll INF = 1e9, MOD = 1e9 + 7;

ll n, d[5001][5001], A, B;

int main() {
  cin >> n >> A >> B;

  for (ll i = 1; i <= n; i++) {
    ll a;

    scanf("%lld", &a);

    for (ll j = 0; j <= n; j++) {
      if (j < a)
        d[i][j] = d[i - 1][j] + A;
      else if (j == a)
        d[i][j] = d[i - 1][j];
      else
        d[i][j] = d[i - 1][j] + B;
      if (j)
        d[i][j] = min(d[i][j], d[i][j - 1]);
    }
  }

  cout << d[n][n];
}
Problem p03092 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e3 + 10;

int n, A, B, p[N];
ll dp[N];

int main() {
  scanf("%d %d %d", &n, &A, &B);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
  }
  p[++n] = n;
  for (int i = 1; i <= n; i++) {
    dp[i] = (ll)1e18;
    ll tmp = 0;
    for (int j = i - 1; j >= 0; j--) {
      if (p[j] < p[i]) {
        dp[i] = min(dp[i], dp[j] + tmp);
        tmp += B;
      } else {
        tmp += A;
      }
    }
  }
  printf("%lld\n", dp[n]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 5e3 + 10;

int n, A, B, p[N];
ll dp[N];

int main() {
  scanf("%d %d %d", &n, &A, &B);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p[i]);
  }
  p[++n] = n;
  for (int i = 1; i <= n; i++) {
    dp[i] = (ll)1e18;
    ll tmp = 0;
    for (int j = i - 1; j >= 0; j--) {
      if (p[j] < p[i]) {
        dp[i] = min(dp[i], dp[j] + tmp);
        tmp += B;
      } else {
        tmp += A;
      }
    }
  }
  printf("%lld\n", dp[n]);
  return 0;
}
Problem p03092 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#define MN 5010

typedef long long ll;

ll Min[MN], lazy[MN];

void add(int x, ll v) {
  lazy[x] += v;
  Min[x] += v;
}
void pushdown(int x) {
  if (lazy[x]) {
    add(x << 1, lazy[x]);
    add(x << 1 | 1, lazy[x]);
    lazy[x] = 0;
  }
}

void modify(int x, int l, int r, int L, int R, ll v, int t) {
  if (L > R)
    return;
  if (l == L && r == R) {
    if (t == 0)
      add(x, v);
    else
      Min[x] = v;
    return;
  }
  pushdown(x);
  int mid = l + r >> 1;
  if (R <= mid)
    modify(x << 1, l, mid, L, R, v, t);
  else if (L > mid)
    modify(x << 1 | 1, mid + 1, r, L, R, v, t);
  else
    modify(x << 1, l, mid, L, mid, v, t),
        modify(x << 1 | 1, mid + 1, r, mid + 1, R, v, t);
  Min[x] = std::min(Min[x << 1], Min[x << 1 | 1]);
}

ll query(int x, int l, int r, int L, int R) {
  if (l == L && r == R)
    return Min[x];
  pushdown(x);
  int mid = l + r >> 1;
  if (R <= mid)
    return query(x << 1, l, mid, L, R);
  else if (L > mid)
    return query(x << 1 | 1, mid + 1, r, L, R);
  else
    return std::min(query(x << 1, l, mid, L, mid),
                    query(x << 1 | 1, mid + 1, r, mid + 1, R));
}

int main() {
  int n, a, b;
  scanf("%d%d%d", &n, &a, &b);
  modify(1, 0, n, 0, n, 1e18, 0);
  modify(1, 0, n, 0, 0, 0, 1);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    modify(1, 0, n, x, x, query(1, 0, n, 0, x), 1);
    modify(1, 0, n, 0, x - 1, a, 0);
    modify(1, 0, n, x + 1, n, b, 0);
  }
  printf("%lld\n", query(1, 0, n, 0, n));
}

Fixed submission:
#include <cstdio>
#include <iostream>
#define MN 20010

typedef long long ll;

ll Min[MN], lazy[MN];

void add(int x, ll v) {
  lazy[x] += v;
  Min[x] += v;
}
void pushdown(int x) {
  if (lazy[x]) {
    add(x << 1, lazy[x]);
    add(x << 1 | 1, lazy[x]);
    lazy[x] = 0;
  }
}

void modify(int x, int l, int r, int L, int R, ll v, int t) {
  if (L > R)
    return;
  if (l == L && r == R) {
    if (t == 0)
      add(x, v);
    else
      Min[x] = v;
    return;
  }
  pushdown(x);
  int mid = l + r >> 1;
  if (R <= mid)
    modify(x << 1, l, mid, L, R, v, t);
  else if (L > mid)
    modify(x << 1 | 1, mid + 1, r, L, R, v, t);
  else
    modify(x << 1, l, mid, L, mid, v, t),
        modify(x << 1 | 1, mid + 1, r, mid + 1, R, v, t);
  Min[x] = std::min(Min[x << 1], Min[x << 1 | 1]);
}

ll query(int x, int l, int r, int L, int R) {
  if (l == L && r == R)
    return Min[x];
  pushdown(x);
  int mid = l + r >> 1;
  if (R <= mid)
    return query(x << 1, l, mid, L, R);
  else if (L > mid)
    return query(x << 1 | 1, mid + 1, r, L, R);
  else
    return std::min(query(x << 1, l, mid, L, mid),
                    query(x << 1 | 1, mid + 1, r, mid + 1, R));
}

int main() {
  int n, a, b;
  scanf("%d%d%d", &n, &a, &b);
  modify(1, 0, n, 0, n, 1e18, 0);
  modify(1, 0, n, 0, 0, 0, 1);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    modify(1, 0, n, x, x, query(1, 0, n, 0, x), 1);
    modify(1, 0, n, 0, x - 1, a, 0);
    modify(1, 0, n, x + 1, n, b, 0);
  }
  printf("%lld\n", query(1, 0, n, 0, n));
}
Problem p03092 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll A, B;
int n;
const int N = 2010;
int a[N], pos[N];
ll f[N][N];

int main() {
  scanf("%d%lld%lld", &n, &A, &B);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    pos[a[i]] = i;
  }
  for (int i = 0; i <= n; i++)
    f[0][i] = B * i;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      f[i][j] = f[i - 1][j];
      if (j)
        f[i][j] = min(f[i][j], f[i][j - 1] + (pos[j] > i ? B : A));
      if (j && pos[j] == i)
        f[i][j] = min(f[i][j], f[i - 1][j - 1]);
      //			cout<<i<<" "<<j<<" :: "<<f[i][j]<<endl;
    }
  }
  cout << f[n][n] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll A, B;
int n;
const int N = 5010;
int a[N], pos[N];
ll f[N][N];

int main() {
  scanf("%d%lld%lld", &n, &A, &B);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    pos[a[i]] = i;
  }
  for (int i = 0; i <= n; i++)
    f[0][i] = B * i;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      f[i][j] = f[i - 1][j];
      if (j)
        f[i][j] = min(f[i][j], f[i][j - 1] + (pos[j] > i ? B : A));
      if (j && pos[j] == i)
        f[i][j] = min(f[i][j], f[i - 1][j - 1]);
      //			cout<<i<<" "<<j<<" :: "<<f[i][j]<<endl;
    }
  }
  cout << f[n][n] << endl;
}
Problem p03092 (Python)

Buggy submission (status: Time Limit Exceeded):
from bisect import bisect

n, a, b = map(int, input().split())
ppp = map(int, input().split())
qqq = [0] * n
for i, p in enumerate(ppp, start=1):
    qqq[p - 1] = i

dp = [(0, 0)]
for i in qqq:
    s = bisect(dp, (i,))
    ndp = [(j, cost + b) for j, cost in dp[:s]]
    stay_cost = dp[s - 1][1]
    ndp.append((i, stay_cost))
    ndp.extend((j, cost + a) for j, cost in dp[s:] if stay_cost > cost + a)
    dp = ndp
print(dp[-1][1])


Fixed submission:
from bisect import bisect

n, a, b = map(int, input().split())
ppp = map(int, input().split())
qqq = [0] * n
for i, p in enumerate(ppp, start=1):
    qqq[p - 1] = i

dp = [(0, 0)]
for i in qqq:
    s = bisect(dp, (i,))
    ndp = [(j, cost + b) for j, cost in dp[:s]]
    stay_cost = dp[s - 1][1]
    ndp.append((i, stay_cost))
    remain = iter(dp[s:])
    for j, cost in remain:
        if stay_cost > cost + a:
            ndp.append((j, cost + a))
            break
    ndp.extend((j, cost + a) for j, cost in remain)
    dp = ndp
print(dp[-1][1])
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9')
    sum = sum * 10 + c - 48, c = getchar();
  return sum;
}

const int maxn = 200005;

int n, a[maxn], p;

bool check(int t) {
  for (int i = 2 * t + 1; i <= (n << 1); ++i)
    if (a[i] + a[((n + t) << 1) - i + i] < p)
      return 0;
  return 1;
}

int main() {
  n = gi();
  p = gi();
  for (int i = 1; i <= (n << 1); ++i)
    a[i] = gi();
  sort(a + 1, a + n + n + 1);

  int l = 0, r = n, mid;
  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }

  int ans = 0;
  for (int i = 1; i <= (l << 1); ++i)
    ans = max(ans, a[i] + a[(l << 1) - i + 1]);
  for (int i = (l << 1) + 1; i <= (n << 1); ++i)
    ans = max(ans, a[i] + a[((n + l) << 1) - i + 1] - p);
  printf("%d\n", ans);

  return 0;
}


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

inline int gi() {
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  int sum = 0;
  while ('0' <= c && c <= '9')
    sum = sum * 10 + c - 48, c = getchar();
  return sum;
}

const int maxn = 200005;

int n, a[maxn], p;

bool check(int t) {
  for (int i = 2 * t + 1; i <= (n << 1); ++i)
    if (a[i] + a[((n + t) << 1) - i + 1] < p)
      return 0;
  return 1;
}

int main() {
  n = gi();
  p = gi();
  for (int i = 1; i <= (n << 1); ++i)
    a[i] = gi();
  sort(a + 1, a + n + n + 1);

  int l = 0, r = n, mid;
  while (l < r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }

  int ans = 0;
  for (int i = 1; i <= (l << 1); ++i)
    ans = max(ans, a[i] + a[(l << 1) - i + 1]);
  for (int i = (l << 1) + 1; i <= (n << 1); ++i)
    ans = max(ans, a[i] + a[((n + l) << 1) - i + 1] - p);
  printf("%d\n", ans);

  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define N 100005

int n, m;
int a[N << 1];

int main() {
  scanf("%d %d", &n, &m);
  int nn = n << 1;
  for (int i = 1; i <= nn; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + nn + 1);
  int st = 0, en = n + 1;
  while (en - st > 1) {
    int mid = en + st >> 1;
    int len = mid * 2;
    int pos = nn - len;
    bool flag = 0;
    for (int i = 1; i <= pos && !flag; i++) {
      if (a[i] + a[pos + 1 - i] >= m)
        flag = 1;
    }
    for (int i = pos + 1; i <= nn && !flag; i++) {
      if (a[i] + a[nn + pos + 1 - i] < m)
        flag = 1;
    }
    if (flag)
      en = mid;
    else
      st = mid;
  }
  int len = st * 2;
  int mx = 0, pos = nn - len;
  for (int i = 1; i <= pos; i++) {
    mx = max(mx, a[i] + a[pos + 1 - i]);
    if (a[i] + a[pos + 1 - i] >= m)
      assert(0), assert(st == 0 && a[nn] + a[nn - 1] >= m);
    // assert(a[i]+a[pos+1-i]<m);
  }
  for (int i = pos + 1; i <= nn; i++) {
    mx = max(mx, a[i] + a[nn + pos + 1 - i] - m);
    assert(a[i] + a[nn + pos + 1 - i] >= m);
  }
  printf("%d\n", mx);
  return 0;
}

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

#define N 100005

int n, m;
int a[N << 1];

int main() {
  scanf("%d %d", &n, &m);
  int nn = n << 1;
  for (int i = 1; i <= nn; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + nn + 1);
  int st = 0, en = n + 1;
  while (en - st > 1) {
    int mid = en + st >> 1;
    int len = mid * 2;
    int pos = nn - len;
    bool flag = 0;
    // for (int i = 1; i <= pos && !flag; i ++) {
    //     if (a[i] + a[pos+1-i] >= m) flag = 1;
    // }
    for (int i = pos + 1; i <= nn && !flag; i++) {
      if (a[i] + a[nn + pos + 1 - i] < m)
        flag = 1;
    }
    if (flag)
      en = mid;
    else
      st = mid;
  }
  int len = st * 2;
  int mx = 0, pos = nn - len;
  for (int i = 1; i <= pos; i++) {
    mx = max(mx, a[i] + a[pos + 1 - i]);
    if (a[i] + a[pos + 1 - i] >= m)
      assert(0), assert(st == 0 && a[nn] + a[nn - 1] >= m);
    // assert(a[i]+a[pos+1-i]<m);
  }
  for (int i = pos + 1; i <= nn; i++) {
    mx = max(mx, a[i] + a[nn + pos + 1 - i] - m);
    assert(a[i] + a[nn + pos + 1 - i] >= m);
  }
  printf("%d\n", mx);
  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m, a[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + n * 2 + 1);
  int l = 0, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    bool flag = 1;
    for (int i = 1; i <= n - mid; ++i)
      if (a[mid * 2 + i] + a[2 * n - i + 1] < m)
        flag = 0;
    if (!flag)
      l = mid + 1;
    else
      r = mid;
  }
  int ans = 0;

  for (int i = 1; i <= l; ++i)
    ans = max(ans, (a[i] + a[l * 2 - i + 1]) % m);
  for (int i = 1; i <= n - l; ++i)
    ans = max(ans, (a[l * 2 + i] + a[2 * n - i + 1]) % m);
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int n, m, a[N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + n * 2 + 1);
  int l = 0, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    bool flag = 1;
    for (int i = 1; i <= n - mid; ++i)
      if (a[mid * 2 + i] + a[2 * n - i + 1] < m)
        flag = 0;
    if (!flag)
      l = mid + 1;
    else
      r = mid;
  }
  int ans = 0;

  for (int i = 1; i <= l; ++i)
    ans = max(ans, (a[i] + a[l * 2 - i + 1]) % m);
  for (int i = 1; i <= n - l; ++i)
    ans = max(ans, (a[l * 2 + i] + a[2 * n - i + 1]) % m);
  printf("%d", ans);
  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
int n, m, a[100100], Ans, ans;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 2 * n + 1);
  int L = 0, R = n, ans;
  while (L <= R) {
    int mid = (L + R) / 2;
    bool bo = true;
    for (int i = mid + 1; i <= n; i++)
      if (a[mid + i] + a[2 * n - i + mid + 1] < m) {
        bo = false;
        break;
      }
    if (bo)
      ans = mid, R = mid - 1;
    else
      L = mid + 1;
  }
  for (int i = 1; i <= ans; i++)
    Ans = max(Ans, a[i] + a[ans * 2 - i + 1]);
  for (int i = ans + 1; i <= n; i++)
    Ans = max(Ans, a[ans + i] + a[2 * n - i + ans + 1] - m);
  printf("%d\n", Ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
int n, m, a[200100], Ans, ans;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 2 * n + 1);
  int L = 0, R = n, ans;
  while (L <= R) {
    int mid = (L + R) / 2;
    bool bo = true;
    for (int i = mid + 1; i <= n; i++)
      if (a[mid + i] + a[2 * n - i + mid + 1] < m) {
        bo = false;
        break;
      }
    if (bo)
      ans = mid, R = mid - 1;
    else
      L = mid + 1;
  }
  for (int i = 1; i <= ans; i++)
    Ans = max(Ans, a[i] + a[ans * 2 - i + 1]);
  for (int i = ans + 1; i <= n; i++)
    Ans = max(Ans, a[ans + i] + a[2 * n - i + ans + 1] - m);
  printf("%d\n", Ans);
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int n, m, a[120000], ans;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * 2; ++i)
    scanf("%d", a + i);
  sort(a + 1, a + n * 2 + 1);
  int l = 0, r = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    bool fl = 1;
    int tmp = 0;
    for (int i = 1; i <= mid; ++i) {
      fl &= a[i] + a[mid * 2 - i + 1] < m;
      tmp = max(tmp, a[i] + a[mid * 2 - i + 1]);
    }
    if (!fl) {
      r = mid - 1;
      continue;
    }
    for (int i = 1; i <= n - mid; ++i) {
      fl &= a[mid * 2 + i] + a[n * 2 - i + 1] >= m;
      tmp = max(tmp, a[mid * 2 + i] + a[n * 2 - i + 1] - m);
    }
    if (!fl) {
      l = mid + 1;
      continue;
    }
    ans = tmp;
    r = mid - 1;
  }
  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n, m, a[300000], ans;

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n * 2; ++i)
    scanf("%d", a + i);
  sort(a + 1, a + n * 2 + 1);
  int l = 0, r = n;
  while (l <= r) {
    int mid = (l + r) >> 1;
    bool fl = 1;
    int tmp = 0;
    for (int i = 1; i <= mid; ++i) {
      fl &= a[i] + a[mid * 2 - i + 1] < m;
      tmp = max(tmp, a[i] + a[mid * 2 - i + 1]);
    }
    if (!fl) {
      r = mid - 1;
      continue;
    }
    for (int i = 1; i <= n - mid; ++i) {
      fl &= a[mid * 2 + i] + a[n * 2 - i + 1] >= m;
      tmp = max(tmp, a[mid * 2 + i] + a[n * 2 - i + 1] - m);
    }
    if (!fl) {
      l = mid + 1;
      continue;
    }
    ans = tmp;
    r = mid - 1;
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ci const int &
using namespace std;
int n, m, a[100010], l, r, mid, ans;
bool Check(int x) {
  if (!(x & 1))
    --x;
  for (int i = x, j = (n << 1); i < j; ++i, --j)
    if (a[i] + a[j] < m)
      return 0;
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (n << 1); ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + (n << 1) + 1);
  l = 1, r = (n << 1) + 1;
  while (l < r)
    mid = l + r >> 1, Check(mid) ? r = mid : l = mid + 1;
  ans = 0;
  for (int i = 1, j = l - 1; i < j; ++i, --j)
    ans = max(ans, a[i] + a[j]);
  for (int i = l, j = (n << 1); i < j; ++i, --j)
    ans = max(ans, a[i] + a[j] - m);
  printf("%d", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ci const int &
using namespace std;
int n, m, a[200010], l, r, mid, ans;
bool Check(int x) {
  if (!(x & 1))
    --x;
  for (int i = x, j = (n << 1); i < j; ++i, --j)
    if (a[i] + a[j] < m)
      return 0;
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (n << 1); ++i)
    scanf("%d", &a[i]);
  sort(a + 1, a + (n << 1) + 1);
  l = 1, r = (n << 1) + 1;
  while (l < r)
    mid = l + r >> 1, Check(mid) ? r = mid : l = mid + 1;
  ans = 0;
  for (int i = 1, j = l - 1; i < j; ++i, --j)
    ans = max(ans, a[i] + a[j]);
  for (int i = l, j = (n << 1); i < j; ++i, --j)
    ans = max(ans, a[i] + a[j] - m);
  printf("%d", ans);
  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define MAXN 100005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> P;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq;
int n, m, a[MAXN];
bool check(int x) {
  int sum = 2 * n + 2 * x + 1;
  for (int i = 2 * x + 1; i <= sum - i; i++)
    if (a[i] + a[sum - i] < m)
      return false;
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 2 * n + 1);
  int l = -1, r = n;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  int ans = 0;
  int sum1 = 2 * r + 1, sum2 = 2 * n + 2 * r + 1;
  for (int i = 1; i <= sum1 - i; i++)
    ans = max(ans, (a[i] + a[sum1 - i]) % m);
  for (int i = 2 * r + 1; i <= sum2 - i; i++)
    ans = max(ans, (a[i] + a[sum2 - i]) % m);
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define MAXN 200005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> P;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef __gnu_pbds::priority_queue<int, greater<int>, pairing_heap_tag> pq;
int n, m, a[MAXN];
bool check(int x) {
  int sum = 2 * n + 2 * x + 1;
  for (int i = 2 * x + 1; i <= sum - i; i++)
    if (a[i] + a[sum - i] < m)
      return false;
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 2 * n + 1);
  int l = -1, r = n;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  int ans = 0;
  int sum1 = 2 * r + 1, sum2 = 2 * n + 2 * r + 1;
  for (int i = 1; i <= sum1 - i; i++)
    ans = max(ans, (a[i] + a[sum1 - i]) % m);
  for (int i = 2 * r + 1; i <= sum2 - i; i++)
    ans = max(ans, (a[i] + a[sum2 - i]) % m);
  printf("%d\n", ans);
  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>

using namespace std;

int a[100000];

int main() {
  int n, m, l, r, mid, ans = 0, i, j;

  scanf("%d %d", &n, &m);

  for (i = 0; i < n * 2; i++)
    scanf("%d", &a[i]);

  sort(a, a + n * 2);

  l = -1, r = n, mid = (l + r) / 2;

  while (r - l > 1) {
    for (i = mid * 2, j = n * 2 - 1; i < j; i++, j--) {
      if (a[i] + a[j] < m)
        break;
    }

    if (i < j) {
      l = mid;
      mid = (l + r) / 2;
    } else {
      r = mid;
      mid = (l + r) / 2;
    }
  }

  for (i = 0, j = r * 2 - 1; i < j; i++, j--)
    ans = max(ans, a[i] + a[j]);
  for (i = r * 2, j = n * 2 - 1; i < j; i++, j--)
    ans = max(ans, a[i] + a[j] - m);

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

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

int a[200000];

int main() {
  int n, m, l, r, mid, ans = 0, i, j;

  scanf("%d %d", &n, &m);

  for (i = 0; i < n * 2; i++)
    scanf("%d", &a[i]);

  sort(a, a + n * 2);

  l = -1, r = n, mid = (l + r) / 2;

  while (r - l > 1) {
    for (i = mid * 2, j = n * 2 - 1; i < j; i++, j--) {
      if (a[i] + a[j] < m)
        break;
    }

    if (i < j) {
      l = mid;
      mid = (l + r) / 2;
    } else {
      r = mid;
      mid = (l + r) / 2;
    }
  }

  for (i = 0, j = r * 2 - 1; i < j; i++, j--)
    ans = max(ans, a[i] + a[j]);
  for (i = r * 2, j = n * 2 - 1; i < j; i++, j--)
    ans = max(ans, a[i] + a[j] - m);

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

  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a)-1; i >= (b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MOD = 1000000007;

int A[111111];

int main() {
  // ios_base::sync_with_stdio(0);

  int N, M;
  scanf("%d%d", &N, &M);
  N *= 2;
  REP(i, N) { scanf("%d", &A[i]); }
  sort(A, A + N);
  int S = 0;
  FORD(i, N, 0) {
    int K = lower_bound(A, A + N, M - A[i]) - A;
    S = max(S, K - (N - 1 - i));
  }
  S += S % 2;

  int Z = 0;
  REP(i, S) Z = max(Z, A[i] + A[S - 1 - i]);
  FOR(i, S, N) Z = max(Z, A[i] + A[N - 1 - (i - S)] - M);
  printf("%d\n", Z);
}


Fixed submission:
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a)-1; i >= (b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MOD = 1000000007;

int A[211111];

int main() {
  // ios_base::sync_with_stdio(0);

  int N, M;
  scanf("%d%d", &N, &M);
  N *= 2;
  REP(i, N) { scanf("%d", &A[i]); }
  sort(A, A + N);
  int S = 0;
  FORD(i, N, 0) {
    int K = lower_bound(A, A + N, M - A[i]) - A;
    S = max(S, K - (N - 1 - i));
  }
  S += S % 2;

  int Z = 0;
  REP(i, S) Z = max(Z, A[i] + A[S - 1 - i]);
  FOR(i, S, N) Z = max(Z, A[i] + A[N - 1 - (i - S)] - M);
  printf("%d\n", Z);
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200005;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
int read() {
  int f = 1, g = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    g = g * 10 + ch - '0';
  return f * g;
}
int n, m, a[N];
int check(int mid) {
  int t = 0;
  for (int i = 1; i <= mid; i++)
    t = max(t, a[i] + a[mid * 2 - i + 1]);
  for (int i = 1; i + mid <= n; i++)
    if (a[i + mid * 2] + a[n * 2 - i + 1] < m)
      return -1;
    else
      t = max(t, a[i] + a[mid * 2 - i + 1] - m);
  return t;
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n * 2; i++)
    a[i] = read();
  sort(a + 1, a + n * 2 + 1);
  int l = 0, r = n, mid;
  while (l != r) {
    mid = (l + r) >> 1;
    if (check(mid) >= 0)
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", check(l));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200005;
const ll inf = 0x3f3f3f3f3f3f3f3fll;
int read() {
  int f = 1, g = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    g = g * 10 + ch - '0';
  return f * g;
}
int n, m, a[N];
int check(int mid) {
  int t = 0;
  for (int i = 1; i <= mid; i++)
    t = max(t, a[i] + a[mid * 2 - i + 1]);
  for (int i = 1; i + mid <= n; i++)
    if (a[i + mid * 2] + a[n * 2 - i + 1] < m)
      return -1;
    else
      t = max(t, a[i + mid * 2] + a[n * 2 - i + 1] - m);
  return t;
}
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n * 2; i++)
    a[i] = read();
  sort(a + 1, a + n * 2 + 1);
  int l = 0, r = n, mid;
  while (l != r) {
    mid = (l + r) >> 1;
    if (check(mid) >= 0)
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", check(l));
  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

vector<int> a;
int n, M;
int Q(int k) {
  int f = -1;
  for (int i = 0; i < k; i++) {
    f = max(f, a[i] + a[2 * k - 1 - i]);
  }
  for (int i = 0; i < (n - k); i++) {
    int r = a[2 * k + i] + a[2 * n - 1 - i];
    if (r < M)
      return -1;
    f = max(f, r - M);
  }
  assert(f < M);
  return f;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> M;
  a.resize(2 * n);
  for (int i = 0; i < 2 * n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  int s = -1; // not
  int e = n;  // fine
  while (s + 1 < e) {
    int m = (s + e) / 2;
    if (Q(m) == -1) {
      s = m;
    } else {
      e = m;
    }
  }
  s++;
  e = n;
  int ans = M;
  ans = min(ans, Q(e));
  ans = min(ans, Q(s));
  while (s + 3 < e) {
    int m1 = (s + s + e) / 3;
    int m2 = (s + e + e) / 3;
    if (Q(m1) <= Q(m2)) {
      e = m2;
    } else {
      s = m1;
    }
  }
  for (int m = s; m <= e; m++) {
    ans = min(Q(m), ans);
  }
  cout << ans << endl;
}


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

using ll = long long;

vector<int> a;
int n, M;
int Q(int k) {
  int f = -1;
  for (int i = 0; i < k; i++) {
    f = max(f, (a[i] + a[2 * k - 1 - i]) % M);
  }
  for (int i = 0; i < (n - k); i++) {
    int r = a[2 * k + i] + a[2 * n - 1 - i];
    if (r < M)
      return -1;
    f = max(f, r - M);
  }
  assert(f < M);
  return f;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> M;
  a.resize(2 * n);
  for (int i = 0; i < 2 * n; i++)
    cin >> a[i];
  sort(a.begin(), a.end());
  int s = -1; // not
  int e = n;  // fine
  while (s + 1 < e) {
    int m = (s + e) / 2;
    if (Q(m) == -1) {
      s = m;
    } else {
      e = m;
    }
  }
  s++;
  e = n;
  int ans = M;
  ans = min(ans, Q(e));
  ans = min(ans, Q(s));
  while (s + 3 < e) {
    int m1 = (s + s + e) / 3;
    int m2 = (s + e + e) / 3;
    if (Q(m1) <= Q(m2)) {
      e = m2;
    } else {
      s = m1;
    }
  }
  for (int m = s; m <= e; m++) {
    ans = min(Q(m), ans);
  }
  cout << ans << endl;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for ((i) = 1; (i) <= (n); (i)++)
using namespace std;
int n, m, i, j, a[100005], l, r, mid, ans, res;
bool check(int x) {
  int i, j;
  for (i = 2 * x + 1, j = n * 2; i <= j; i++, j--) {
    if (a[i] + a[j] < m)
      return 0;
  }
  return 1;
}
int main() {
  cin >> n >> m;
  rep(i, n + n) cin >> a[i];
  sort(a + 1, a + n + n + 1);
  l = -1;
  r = n;
  ans = n + 1;
  while (l < r) {
    mid = (l + r + 1) / 2;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else {
      l = mid;
    }
  }
  for (i = 1, j = ans * 2; i <= j; i++, j--) {
    res = max(res, a[i] + a[j]);
  }
  for (i = ans * 2 + 1, j = n * 2; i <= j; i++, j--) {
    res = max(res, a[i] + a[j] - m);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for ((i) = 1; (i) <= (n); (i)++)
using namespace std;
int n, m, i, j, a[200005], l, r, mid, ans, res;
bool check(int x) {
  int i, j;
  for (i = 2 * x + 1, j = n * 2; i <= j; i++, j--) {
    if (a[i] + a[j] < m)
      return 0;
  }
  return 1;
}
int main() {
  cin >> n >> m;
  rep(i, n + n) cin >> a[i];
  sort(a + 1, a + n + n + 1);
  l = -1;
  r = n;
  ans = n + 1;
  while (l < r) {
    mid = (l + r + 1) / 2;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else {
      l = mid;
    }
  }
  for (i = 1, j = ans * 2; i <= j; i++, j--) {
    res = max(res, a[i] + a[j]);
  }
  for (i = ans * 2 + 1, j = n * 2; i <= j; i++, j--) {
    res = max(res, a[i] + a[j] - m);
  }
  cout << res << endl;
  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
// E.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, ai[MAX_N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; i++)
    scanf("%d", &ai[i]);
  sort(ai + 1, ai + 1 + 2 * n);
  int l = 0, r = n, res;
  while (l <= r) {
    int mid = (l + r) >> 1;
    bool valid = true;
    for (int i = 2 * mid + 1; i <= (n << 1); i++)
      if (ai[i] + ai[2 * n + 2 * mid + 1 - i] < m) {
        valid = false;
        break;
      }
    if (valid)
      r = mid - 1, res = mid;
    else
      l = mid + 1;
  }
  int pos = res << 1;
  int ans = 0;
  for (int i = 1; i <= pos; i++)
    ans = max(ans, ai[i] + ai[pos - i + 1]);
  for (int i = pos + 1; i <= (n << 1); i++)
    ans = max(ans, (ai[i] + ai[(n << 1) + pos - i + 1]) % m);
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
// E.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 200;

int n, m, ai[MAX_N];

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= 2 * n; i++)
    scanf("%d", &ai[i]);
  sort(ai + 1, ai + 1 + 2 * n);
  int l = 0, r = n, res;
  while (l <= r) {
    int mid = (l + r) >> 1;
    bool valid = true;
    for (int i = 2 * mid + 1; i <= (n << 1); i++)
      if (ai[i] + ai[2 * n + 2 * mid + 1 - i] < m) {
        valid = false;
        break;
      }
    if (valid)
      r = mid - 1, res = mid;
    else
      l = mid + 1;
  }
  int pos = res << 1;
  int ans = 0;
  for (int i = 1; i <= pos; i++)
    ans = max(ans, ai[i] + ai[pos - i + 1]);
  for (int i = pos + 1; i <= (n << 1); i++)
    ans = max(ans, (ai[i] + ai[(n << 1) + pos - i + 1]) % m);
  printf("%lld\n", ans);
  return 0;
}
Problem p03093 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11;
int n, mod;
int a[N];
bool check(int pos) {
  int l = pos + 1, r = 2 * n;
  while (l <= r) {
    if (a[l] + a[r] < mod)
      return 0;
    l++;
    r--;
  }
  return 1;
}
int main() {
  cin >> n >> mod;
  for (int i = 1; i <= n + n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a + 1, a + 1 + n + n);
  int l = 0, r = n, ans = n;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid << 1))
      r = mid - 1, ans = min(ans, mid);
    else
      l = mid + 1;
  }
  l = 1, r = 2 * ans;
  int Z = 0;
  while (l <= r) {
    Z = max(Z, a[l] + a[r]);
    l++;
    r--;
  }
  l = 2 * ans + 1;
  r = 2 * n;
  while (l <= r) {
    Z = max(Z, a[l] + a[r] - mod);
    l++;
    r--;
  }
  // printf("ans=%d Z=%d\n", ans * 2, Z);
  cout << Z << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 11;
int n, mod;
int a[N];
bool check(int pos) {
  int l = pos + 1, r = 2 * n;
  while (l <= r) {
    if (a[l] + a[r] < mod)
      return 0;
    l++;
    r--;
  }
  return 1;
}
int main() {
  cin >> n >> mod;
  for (int i = 1; i <= n + n; i++) {
    scanf("%d", &a[i]);
  }
  sort(a + 1, a + 1 + n + n);
  int l = 0, r = n, ans = n;
  while (l <= r) {
    int mid = l + r >> 1;
    if (check(mid << 1))
      r = mid - 1, ans = min(ans, mid);
    else
      l = mid + 1;
  }
  l = 1, r = 2 * ans;
  int Z = 0;
  while (l <= r) {
    Z = max(Z, a[l] + a[r]);
    l++;
    r--;
  }
  l = 2 * ans + 1;
  r = 2 * n;
  while (l <= r) {
    Z = max(Z, a[l] + a[r] - mod);
    l++;
    r--;
  }
  // printf("ans=%d Z=%d\n", ans * 2, Z);
  cout << Z << endl;
  return 0;
}