Problem p02669 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

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

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

ll rec(ll n, ll a, ll b, ll c, ll d, map<ll, ll> &mp) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (mp.count(n))
    return mp[n];

  ll res = LINF;
  if (n < LINF / d)
    chmin(res, n * d);
  chmin(res, a + n % 2 * d + rec(n / 2, a, b, c, d, mp));
  if (n != 2)
    chmin(res, a + (2 - n % 2) * d + rec(n / 2 + 1, a, b, c, d, mp));
  chmin(res, b + n % 3 * d + rec(n / 3, a, b, c, d, mp));
  chmin(res, b + (3 - n % 3) * d + rec(n / 3 + 1, a, b, c, d, mp));
  chmin(res, c + n % 5 * d + rec(n / 5, a, b, c, d, mp));
  chmin(res, c + (5 - n % 5) * d + rec(n / 5 + 1, a, b, c, d, mp));

  return mp[n] = res;
}

void solve() {
  int t;
  cin >> t;
  for (int i = 0; i < t; ++i) {
    ll n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    map<ll, ll> mp;
    cout << rec(n, a, b, c, d, mp) << '\\n';
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 10) >= this->size() (which is 5)


Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

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

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

class UnionFind {
private:
  std::vector<int> par;

public:
  UnionFind(int n) {
    par.resize(n);
    for (int i = 0; i < n; i++)
      par.at(i) = i;
  }
  void unite(int x, int y) {
    int xPar = find(x);
    int yPar = find(y);
    if (xPar == yPar)
      return;

    par.at(xPar) = yPar;
  }
  int find(int x) {
    if (par.at(x) == x)
      return x;
    else
      return par.at(x) = find(par.at(x));
  }
  bool same(int x, int y) { return find(x) == find(y); }
};

void solve() {
  ll n;
  cin >> n;
  vector<int> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p.at(i);
    if (p.at(i) > 0)
      --p.at(i);
  }

  UnionFind uf(n);
  for (int i = 0; i < n; ++i) {
    if (p.at(i) != -1)
      uf.unite(i, p.at(i));
  }
  vector<pair<int, bool>> cnt(n, {0, false});
  for (int i = 0; i < n; ++i) {
    ++cnt.at(uf.find(i)).first;
    if (p.at(i) == -1)
      cnt.at(uf.find(i)).second = true;
  }
  vector<pair<int, bool>> groups;
  for (auto p : cnt) {
    if (p.first > 0)
      groups.push_back(p);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}