Problem p02695 (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(int cur, const int n, const int m, vector<int> &a,
       const vector<vector<int>> &req) {
  if (cur == n + 1) {
    ll res = 0;
    for (auto v : req) {
      if (a.at(v.at(1)) - a.at(v.at(0)) == v.at(2))
        res += v.at(3);
    }
    return res;
  } else {
    ll res = -1;
    int s = 1;
    if (cur > 1)
      s = a.at(cur - 1);
    for (int i = s; i <= m; ++i) {
      a.at(cur) = i;
      chmax(res, rec(cur + 1, n, m, a, req));
    }
    return res;
  }
}

void solve() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<vector<int>> req(q, vector<int>(4));
  for (int i = 0; i < q; ++i)
    cin >> req.at(i).at(0) >> req.at(i).at(1) >> req.at(i).at(2) >>
        req.at(i).at(3);
  vector<int> a(n + 1);
  cout << rec(1, n, m, a, req) << '\\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 100) >= this->size() (which is 4)


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() {
  int n, m, q;
  cin >> n >> m >> q;
  vector<vector<int>> req(q, vector<int>(4));
  for (int i = 0; i < q; ++i)
    cin >> req.at(i).at(0) >> req.at(i).at(1) >> req.at(i).at(2) >>
        req.at(i).at(3);

  UnionFind uf(n + 1);
  vector<int> a(n + 1, 1);
  sort(req.begin(), req.end(), [](auto a, auto b) {
    if (a.at(3) > b.at(3)) {
      return true;
    } else {
      return false;
    }
  });
  ll res = 0;
  for (auto v : req) {
    int dif = a.at(v.at(1)) - a.at(v.at(0));
    if (uf.same(v.at(0), v.at(1))) {
      if (dif == v.at(2))
        res += v.at(3);
    } else {
    }
    if (dif > v.at(2))
      continue;
    dif = v.at(2) - dif;
    bool ok = true;
    for (int i = 1; i < n + 1; ++i) {
      if (uf.same(i, v.at(1))) {
        if (a.at(v.at(i)) + dif > m) {
          ok = false;
          break;
        }
      }
    }
    if (ok) {
      uf.unite(v.at(0), v.at(1));
      res += v.at(3);
      for (int i = 1; i < n + 1; ++i) {
        if (uf.same(i, v.at(1)))
          a.at(i) += dif;
      }
    }
  }
  cout << res << '\\n';
}

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

  solve();

  return 0;
}