Problem p03113 (C++)

Buggy submission (status: Runtime Error):
// #define NDEBUG
#include <cstddef>
#include <cstdint>
#include <vector>

using i8 = ::std::int_least8_t;
using i32 = ::std::int_least32_t;
using i64 = ::std::int_least64_t;
using u8 = ::std::uint_least8_t;
using u32 = ::std::uint_least32_t;
using u64 = ::std::uint_least64_t;
using isize = ::std::ptrdiff_t;
using usize = ::std::size_t;

template <class T = usize> class rep {
  const T f, l;

public:
  class itr {
    friend rep;
    T i;
    constexpr itr(T x) noexcept : i(x) {}

  public:
    void operator++() noexcept { ++i; }
    constexpr T operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  constexpr rep(const T first, const T last) noexcept : f(first), l(last) {}
  constexpr itr begin() const noexcept { return itr(f); }
  constexpr itr end() const noexcept { return itr(l); }
};
template <class T = usize> class revrep {
  const T f, l;

public:
  class itr {
    friend revrep;
    T i;
    constexpr itr(T x) noexcept : i(x) {}

  public:
    void operator++() noexcept { --i; }
    constexpr T operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  constexpr revrep(const T first, const T last) noexcept : f(first), l(last) {}
  constexpr itr begin() const noexcept { return itr(l - 1); }
  constexpr itr end() const noexcept { return itr(f - 1); }
};
template <class T> bool amax(T &a, const T &b) {
  return a <= b ? (a = b, 1) : 0;
}
template <class T> bool amin(T &a, const T &b) {
  return b <= a ? (a = b, 1) : 0;
}
template <class T> bool asmax(T &a, const T &b) {
  return b <= a ? 0 : (a = b, 1);
}
template <class T> bool asmin(T &a, const T &b) {
  return a <= b ? 0 : (a = b, 1);
}
template <class T> using vec_alias = ::std::vector<T>;
template <class T> auto md_vec(usize n, const T &value) {
  return ::std::vector<T>(n, value);
}
template <class... Args> auto md_vec(usize n, Args... args) {
  return ::std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> const T &as_const(const T &v) noexcept { return v; }
template <class T> constexpr T difference(const T &a, const T &b) {
  return a <= b ? b - a : a - b;
}

#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <utility>

int main() {
  usize n, k;
  ::std::cin >> n >> k;
  ::std::vector<usize> a(n), rem(n);
  for (auto &e : a) {
    ::std::cin >> e;
  }

  for (const auto i : rep<>(0, n)) {
    if (a[i] < k + 1) {
      rem[i] = k + 1 - a[i];
    }
  }

  if ((k + 1) / 2 < *::std::max_element(rem.cbegin(), rem.cend())) {
    ::std::cout << "-1" << ::std::endl;
    return 0;
  }
  if (k < ::std::accumulate(rem.cbegin(), rem.cend(), static_cast<usize>(0))) {
    ::std::cout << "-1" << ::std::endl;
    return 0;
  }

  ::std::vector<::std::pair<usize, usize>> rr(n);
  for (const auto i : rep<>(0, n)) {
    rr[i] = {rem[i], i};
  }
  ::std::sort(rr.rbegin(), rr.rend());

  ::std::vector<usize> rs;
  for (const auto i : rep<>(0, n)) {
    for (const auto j : rep<>(0, rr[i].first)) {
      rs.emplace_back(rr[i].second);
    }
  }

  while (rs.size() < k) {
    rs.emplace_back(rr.back().second);
  }
  const usize os = k % 2 ? k : k - 1;

  ::std::vector<usize> joints(k);

  for (const auto i : rep<>(0, k)) {
    if (i * 2 < k) {
      joints[i * 2] = rs[i];
    } else {
      joints[i * 2 - os] = rs[i];
    }
  }
  joints.push_back(n);

  ::std::vector<usize> ans;

  for (const auto v : rep<>(0, n)) {
    if (v != joints.front()) {
      ans.emplace_back(v);
    }
  }
  for (const auto i : rep<>(0, k)) {
    ans.emplace_back(joints[i]);
    for (const auto v : rep<>(0, n)) {
      if (v != joints[i] && v != joints[i + 1]) {
        ans.emplace_back(v);
      }
    }
  }

  const usize q = ans.size();

  assert(q == (n - 1) * k + n);

  ::std::cout << q << ::std::endl;

  for (const auto i : rep<>(0, q)) {
    ::std::cout << ans[i] + 1 << " \n"[i + 1 == q];
  }

  return 0;
}


Fixed submission:
// #define NDEBUG
#include <cstddef>
#include <cstdint>
#include <vector>

using i8 = ::std::int_least8_t;
using i32 = ::std::int_least32_t;
using i64 = ::std::int_least64_t;
using u8 = ::std::uint_least8_t;
using u32 = ::std::uint_least32_t;
using u64 = ::std::uint_least64_t;
using isize = ::std::ptrdiff_t;
using usize = ::std::size_t;

template <class T = usize> class rep {
  const T f, l;

public:
  class itr {
    friend rep;
    T i;
    constexpr itr(T x) noexcept : i(x) {}

  public:
    void operator++() noexcept { ++i; }
    constexpr T operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  constexpr rep(const T first, const T last) noexcept : f(first), l(last) {}
  constexpr itr begin() const noexcept { return itr(f); }
  constexpr itr end() const noexcept { return itr(l); }
};
template <class T = usize> class revrep {
  const T f, l;

public:
  class itr {
    friend revrep;
    T i;
    constexpr itr(T x) noexcept : i(x) {}

  public:
    void operator++() noexcept { --i; }
    constexpr T operator*() const noexcept { return i; }
    constexpr bool operator!=(const itr x) const noexcept { return i != x.i; }
  };
  constexpr revrep(const T first, const T last) noexcept : f(first), l(last) {}
  constexpr itr begin() const noexcept { return itr(l - 1); }
  constexpr itr end() const noexcept { return itr(f - 1); }
};
template <class T> bool amax(T &a, const T &b) {
  return a <= b ? (a = b, 1) : 0;
}
template <class T> bool amin(T &a, const T &b) {
  return b <= a ? (a = b, 1) : 0;
}
template <class T> bool asmax(T &a, const T &b) {
  return b <= a ? 0 : (a = b, 1);
}
template <class T> bool asmin(T &a, const T &b) {
  return a <= b ? 0 : (a = b, 1);
}
template <class T> using vec_alias = ::std::vector<T>;
template <class T> auto md_vec(usize n, const T &value) {
  return ::std::vector<T>(n, value);
}
template <class... Args> auto md_vec(usize n, Args... args) {
  return ::std::vector<decltype(md_vec(args...))>(n, md_vec(args...));
}
template <class T> const T &as_const(const T &v) noexcept { return v; }
template <class T> constexpr T difference(const T &a, const T &b) {
  return a <= b ? b - a : a - b;
}

#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <utility>

int main() {
  usize n, k;
  ::std::cin >> n >> k;
  ::std::vector<usize> a(n), rem(n);
  for (auto &e : a) {
    ::std::cin >> e;
  }

  for (const auto i : rep<>(0, n)) {
    if (a[i] < k + 1) {
      rem[i] = k + 1 - a[i];
    }
  }

  if ((k + 1) / 2 < *::std::max_element(rem.cbegin(), rem.cend())) {
    ::std::cout << "-1" << ::std::endl;
    return 0;
  }
  if (k < ::std::accumulate(rem.cbegin(), rem.cend(), static_cast<usize>(0))) {
    ::std::cout << "-1" << ::std::endl;
    return 0;
  }

  ::std::vector<::std::pair<usize, usize>> rr(n);
  for (const auto i : rep<>(0, n)) {
    rr[i] = {rem[i], i};
  }
  ::std::sort(rr.rbegin(), rr.rend());

  ::std::vector<usize> rs;
  for (const auto i : rep<>(0, n)) {
    for (const auto j : rep<>(0, rr[i].first)) {
      rs.emplace_back(rr[i].second);
    }
  }

  while (rs.size() < k) {
    rs.emplace_back(rr.back().second);
  }
  const usize os = k % 2 ? k : k - 1;

  ::std::vector<usize> joints(k);

  for (const auto i : rep<>(0, k)) {
    if (i * 2 < k) {
      joints[i * 2] = rs[i];
    } else {
      joints[i * 2 - os] = rs[i];
    }
  }
  joints.push_back(n);

  ::std::vector<usize> ans;

  for (const auto v : rep<>(0, n)) {
    if (v != joints.front()) {
      ans.emplace_back(v);
    }
  }
  for (const auto i : rep<>(0, k)) {
    ans.emplace_back(joints[i]);
    for (const auto v : rep<>(0, n)) {
      if (v != joints[i] && v != joints[i + 1]) {
        ans.emplace_back(v);
      }
    }
  }

  const usize q = ans.size();

  // assert(q == (n - 1) * k + n);

  ::std::cout << q << ::std::endl;

  for (const auto i : rep<>(0, q)) {
    ::std::cout << ans[i] + 1 << " \n"[i + 1 == q];
  }

  return 0;
}