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

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF (1 << 30 - 1)
#define LLINF (1LL << 60)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

int a[50];
int b[50];
int c[50];
int d[50];

void dfs(vector<int> &x) {
  dump(x);
  if (x.size() == N) {
    dump(x);
    int cnt = 0;
    REP(i, Q) {
      if (x[b[i]] - x[a[i]] == c[i]) {
        cnt += d[i];
      }
    }
    ans = max(cnt, int(ans));
  } else {
    for (int i = x.back(); i <= M; i++) {
      x.push_back(i);
      dfs(x);
    }
  }
  x.pop_back();
}

signed main() {
  cin >> N >> M >> Q;
  REP(i, Q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    b[i]--, a[i]--;
  }
  vi x;
  x.push_back(1);
  dfs(x);
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl "\\n"
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF (1 << 30 - 1)
#define LLINF (1LL << 60)
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define UNIQUE(v)                                                              \\
  v.erase(unique(v.begin(), v.end()), v.end()) // sortしてからつかうこと
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}

int a[50];
int b[50];
int c[50];
int d[50];

void dfs(vector<int> &x) {
  if (x.size() == N) {
    dump(x);
    int cnt = 0;
    REP(i, Q) {
      if (x[b[i]] - x[a[i]] == c[i]) {
        cnt += d[i];
      }
    }
    ans = max(cnt, int(ans));
  } else {
    for (int i = x.back(); i <= M; i++) {
      x.push_back(i);
      dfs(x);
      x.pop_back();
    }
  }
  if (x.size() != 0)
    x.pop_back();
}

signed main() {
  cin >> N >> M >> Q;
  vi p(N, 0);
  REP(i, Q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    b[i]--, a[i]--;
  }
  dump(p);
  vi x;
  x.push_back(1);
  dfs(x);
  cout << ans << endl;
}