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

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\\n"

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M, Q;
  cin >> N >> M >> Q;
  vll a(Q), b(Q), c(Q), d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
    a.at(i)--;
    b.at(i)--;
  }

  vll num(N, 0);

  auto calc = [&] {
    ll sum = 0;
    for (int i = 0; i < Q; i++) {
      if (num.at(b.at(i)) - num.at(a.at(i)) == c.at(i))
        sum += d.at(i);
    }
    return sum;
  };
  function<ll(ll, ll)> dfs = [&](ll depth, ll x) {
    num.at(depth - 1) = x;
    if (depth == N)
      return calc();

    ll res = 0;
    for (int i = x; i <= M; i++) {
      chmax(res, dfs(depth + 1, i));
    }
    return res;
  };

  cout << dfs(1, 1) << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\\n"

struct nummm {
  ll a, b, c, d;
  nummm() {}
  nummm(ll p, ll q, ll r, ll s) {
    a = p;
    b = q;
    c = r;
    d = s;
  }
};
bool comp(nummm p, nummm q) {
  if (p.a != q.a)
    return p.a < p.a;
  else if (p.b != q.b)
    return p.b < q.b;
  else if (p.c != q.c)
    return p.c < q.c;
  else
    return p.d > q.d;
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, M, Q;
  cin >> N >> M >> Q;
  vll a(Q), b(Q), c(Q), d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
    a.at(i)--;
    b.at(i)--;
  }

  vll num(N, 0);
  ll ans = 0;
  for (int i = 0; i < (1 << N - 1); i++) {
    ll sum = 0;
    a.at(0) = 1;
    bool flag = true;
    for (int j = 1; j <= N - 1; j++) {
      if ((1 << j) & i) {
        a.at(j) = a.at(j - 1) + 1;
        if (a.at(j) > M)
          flag = false;
      } else
        a.at(j) = a.at(j - 1);
    }
    if (!flag)
      continue;
    for (int j = 0; j < Q; j++) {
      if (num.at(b.at(j)) - num.at(a.at(j)) == c.at(j))
        sum += d.at(j);
    }
    chmax(ans, sum);
  }
  cout << ans << endl;

  /*
  for(int i = 0; i < N; i++){
    ll before;
    if(i == 0) before = 0;
    else before = num.at(i-1);

    for(int j = before; j <= M; j++){
      for(int k = 0; k < ; k++){

      }
    }
  } */
}