Problem p03421 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "debug_deque.hpp"
#include "rprint2.hpp"
#define vector DebugDeque
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(prints)
FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde);
#endif
template <class S, class T> istream &operator>>(istream &in, pair<S, T> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
template <char d = ' ', class T>
ostream &operator<<(ostream &out, vector<T> &v) {
  for (auto &e : v) {
    out << e << d;
  }
  return out;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, a, b;
  cin >> n >> a >> b;
  vector<int> v(n);
  iota(v.begin(), v.end(), 1);
  reverse(v.begin(), v.end());
  for (int i = 0; i < n; i += a) {
    reverse(v.begin() + i, v.begin() + min(n, i + a));
  }
  int bb = (n + a - 1) / a;
  if (bb > b) {
    cout << -1 << '\n';
    return 0;
  } else if (bb < b) {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (cnt + (n - i) == b) {
        int idx = n / a * a;
        int wid = min(a, n - idx);
        for (int j = 0; j < i % a; j++) {
          v[idx + j] += wid - (i % a);
        }
        for (int j = 0; j < wid - i % a; j++) {
          v[idx + i % a + j] -= i % a;
        }
        sort(v.begin() + i, v.end());
        reverse(v.begin() + i, v.end());
        break;
      }
      cnt += i % a == 0;
    }
  }
  prints(v);
  for (int i = 0; i < 2; i++) {
    vector<int> dp(n + 1, M);
    for (auto &e : v) {
      *lower_bound(dp.begin(), dp.end(), e) = e;
    }
    int len = lower_bound(dp.begin(), dp.end(), M) - dp.begin();
    if ((!i && len != a) || (i && len != b)) {
      cout << -1 << '\n';
      return 0;
    }
    reverse(v.begin(), v.end());
  }
  cout << v << '\n';
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "debug_deque.hpp"
#include "rprint2.hpp"
#define vector DebugDeque
#else
#define FUNC(name)                                                             \
  template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(prints)
FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde);
#endif
template <class S, class T> istream &operator>>(istream &in, pair<S, T> &p) {
  in >> p.first >> p.second;
  return in;
}
template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (auto &e : v) {
    in >> e;
  }
  return in;
}
template <char d = ' ', class T>
ostream &operator<<(ostream &out, vector<T> &v) {
  for (auto &e : v) {
    out << e << d;
  }
  return out;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, a, b;
  cin >> n >> a >> b;
  vector<int> v(n);
  iota(v.begin(), v.end(), 1);
  reverse(v.begin(), v.end());
  for (int i = 0; i < n; i += a) {
    reverse(v.begin() + i, v.begin() + min(n, i + a));
  }
  int bb = (n + a - 1) / a;
  if (bb > b) {
    cout << -1 << '\n';
    return 0;
  } else if (bb < b) {
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      if (cnt + (n - i) == b) {
        int idx = i / a * a;
        int wid = min(a, n - idx);
        for (int j = 0; j < i % a; j++) {
          v[idx + j] += wid - (i % a);
        }
        for (int j = 0; j < wid - i % a; j++) {
          v[idx + i % a + j] -= i % a;
        }
        sort(v.begin() + i, v.end());
        reverse(v.begin() + i, v.end());
        break;
      }
      cnt += i % a == 0;
    }
  }
  prints(v);
  for (int i = 0; i < 2; i++) {
    vector<int> dp(n + 1, M);
    for (auto &e : v) {
      *lower_bound(dp.begin(), dp.end(), e) = e;
    }
    int len = lower_bound(dp.begin(), dp.end(), M) - dp.begin();
    if ((!i && len != a) || (i && len != b)) {
      cout << -1 << '\n';
      return 0;
    }
    reverse(v.begin(), v.end());
  }
  cout << v << '\n';
}