Problem p03421 (C++)

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

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *min_element(all(a))
#define MAX(a) *max_element(all(a))
#define SUM(a) accumulate(all(a), 0LL)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (int(i) = (n)-1; (i) >= (m); i--)
#define debug(x) cerr << #x << " = " << x << endl
// #define int long long

typedef long long ll;
// ll const MOD = 998244353;
ll const MOD = 1e9 + 7;
int const inf = 1e9;
ll const INF = 1e18;

inline void ios_() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
template <typename T> int size(const T &a) { return (int)a.size(); }
template <typename T> T Div(T a, T b) { return (a + b - 1) / b; }
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int n, a, b;
  cin >> n >> a >> b;

  if (a + b - 1 > n) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> inc, dec;
  REP(i, a) { inc.push_back(n - i); }
  for (int i = n - a; i > 0; i--) {
    dec.push_back(i);
  }

  vector<int> ans;

  REP(i, a) {
    ans.push_back(inc.back());
    inc.pop_back();

    vector<int> v;
    REP(i, b - 1) {
      if (dec.empty())
        continue;
      v.push_back(dec.back());
      dec.pop_back();
    }
    RREP(i, v.size()) { ans.push_back(v[i]); }
  }

  if (dec.empty()) {
    for (auto i : ans) {
      cout << i << " ";
    }
    cout << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}

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

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *min_element(all(a))
#define MAX(a) *max_element(all(a))
#define SUM(a) accumulate(all(a), 0LL)
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define RREP(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define FORR(i, m, n) for (int(i) = (n)-1; (i) >= (m); i--)
#define debug(x) cerr << #x << " = " << x << endl
// #define int long long

typedef long long ll;
// ll const MOD = 998244353;
ll const MOD = 1e9 + 7;
int const inf = 1e9;
ll const INF = 1e18;

inline void ios_() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}
template <typename T> int size(const T &a) { return (int)a.size(); }
template <typename T> T Div(T a, T b) { return (a + b - 1) / b; }
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  int n, a, b;
  cin >> n >> a >> b;

  if (a + b - 1 > n) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> inc, dec;
  REP(i, a) { inc.push_back(n - i); }
  for (int i = n - a; i > 0; i--) {
    dec.push_back(i);
  }

  vector<int> ans;

  REP(i, a) {
    ans.push_back(inc.back());
    inc.pop_back();

    vector<int> v;
    REP(i, b - 1) {
      if (dec.empty())
        break;
      v.push_back(dec.back());
      dec.pop_back();
    }
    RREP(i, v.size()) { ans.push_back(v[i]); }
  }

  if (dec.empty()) {
    for (auto i : ans) {
      cout << i << " ";
    }
    cout << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}