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

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

ll N, M, Q;
ll maxi;
vector<tuple<ll, ll, ll, ll>> Queries;
vector<ll> A;

void dfs(ll index) {
  if (index == N + 1) {
    ll count = 0;
    iter(i, Queries) {
      if (A[get<1>(i)] - A[get<0>(i)] == get<2>(i))
        count += get<3>(i);
    }
    maxi = max(maxi, count);
    return;
  }
  repi2(i, A[index], M) {
    A[index + 1] = i;
    dfs(index + 1);
  }
}

int main(void) {
  cin >> N >> M >> Q;
  A.resize(N + 2, 1);
  rep(i, Q) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    Queries.emplace_back(a, b, c, d);
  }
  dfs(0);
  cout << maxi << endl;
  return 0;
}


Error: WA

Fixed submission:

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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

ll N, M, Q;
ll maxi;
vector<tuple<ll, ll, ll, ll>> Queries;
vector<ll> A;

void dfs(ll index) {
  if (index == N + 1) {
    // ll count = 0;
    // iter(i, Queries) {
    //     if(A[get<1>(i)] - A[get<0>(i)] == get<2>(i)) count += get<3>(i);
    // }
    // maxi = max(maxi, count);
    return;
  }
  repi2(i, A[index], M) {
    A[index + 1] = i;
    dfs(index + 1);
  }
}

int main(void) {
  cin >> N >> M >> Q;
  A.resize(N + 1, 1);
  rep(i, Q) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    Queries.emplace_back(a, b, c, d);
  }
  dfs(0);
  cout << maxi << endl;
  return 0;
}