Problem p02869 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <random>
#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  if (2 * k + 2 * n - 1 > k + 2 * n - 1 + (n + 1) / 2) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> a(n), b(n), c(n);
  REP(i, n) {
    a[i] = k + i;
    b[i] = k + n + i;
    c[i] = k + n + n + i;
  }
  int s = (n - 1) / 2;
  REP(i, s) { swap(a[i], a[2 * s - i]); }
  if (n % 2 == 0)
    abort();
  vector<int> pos(n);
  REP(i, n) pos[i] = i;
  sort(ALL(pos),
       [&](int i, int j) { return a[i] + b[n - 1 - i] < a[j] + b[n - 1 - j]; });
  REP(i, n) {
    assert(a[pos[i]] + b[n - 1 - pos[i]] <= c[i]);
    cout << a[pos[i]] << " " << b[n - 1 - pos[i]] << " " << c[i] << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"
#include <random>
#define REP(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREP(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = m; i < ll(n); ++i)
#define RFOR(i, m, n) for (ll i = ll(n) - 1; i >= ll(m); --i)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

constexpr int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;
  if (2 * k + 2 * n - 1 > k + 2 * n - 1 + (n + 1) / 2) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> a(n), b(n), c(n);
  REP(i, n) {
    a[i] = k + i;
    b[i] = k + n + i;
    c[i] = k + n + n + i;
  }
  if (n & 1) {
    int s = (n / 2 + 1) / 2, t = n / 4;
    REP(i, s) { swap(a[i], a[2 * s - 1 - i]); }
    REP(i, t) { swap(a[2 * s + i], a[2 * s + 2 * t - i]); }

  } else {
    int s = ((n - 1) / 2 + 1) / 2, t = (n - 1) / 4;
    REP(i, s) { swap(a[i], a[2 * s - 1 - i]); }
    REP(i, t) { swap(a[2 * s + i], a[2 * s + 2 * t - i]); }
  }
  vector<int> pos(n);
  REP(i, n) pos[i] = i;
  sort(ALL(pos),
       [&](int i, int j) { return a[i] + b[n - 1 - i] < a[j] + b[n - 1 - j]; });
  REP(i, n) {
    assert(a[pos[i]] + b[n - 1 - pos[i]] <= c[i]);
    cout << a[pos[i]] << " " << b[n - 1 - pos[i]] << " " << c[i] << endl;
  }
}