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

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

// GNU C Compiler
// template<class T> inline string toString(T x) { ostringstream sout; sout <<
// x; return sout.str(); }

// type
using ll = int_fast64_t;
using ull = uint_fast64_t;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
// using pair<T1, T2> = p12;

// container
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define ALL(v) v.begin(), v.end()

// repetition
#define REP(n) for (int i = 0; i < (n); i++)
#define REPj(n) for (int j = 0; j < (n); j++)
#define REPinit(i, a, n) for (int i = (a); i < (n); i++)
#define REPinitj(j, a, n) for (int j = (a); j < (n); j++)

// algorithm
// #define SORT(c) sort((c).begin(),(c).end())

// constant

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// Optimizer
//  #pragma GCC optimize("Ofast")
//  #pragma GCC
//  target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*
    add utility Algorithm
*/

int abcd[50][4];
int N, M, Q;
ll ans = 0;

void dfs(vector<int> v, int cnt, int las) {
  if (cnt == N) {
    ll res = 0;
    REP(Q) {
      if (v[abcd[i][1]] - v[abcd[i][0]] == abcd[i][2])
        res += abcd[i][3];
    }
    ans = max(ans, res);
  } else {
    REPinit(i, las, M + 1) {
      vector<int> new_v = v;
      new_v.push_back(i);
      dfs(new_v, cnt + 1, i);
    }
  }
}

int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> N >> M >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> abcd[i][0] >> abcd[i][1] >> abcd[i][2] >> abcd[i][3];
    abcd[i][0]--;
    abcd[i][1]--;
  }

  vector<int> v;
  dfs(v, 0, 1);
  cout << ans << endl;

  return 0;
}


Error: WA

Fixed submission:

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

// GNU C Compiler
// template<class T> inline string toString(T x) { ostringstream sout; sout <<
// x; return sout.str(); }

// type
using ll = int_fast64_t;
using ull = uint_fast64_t;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
// using pair<T1, T2> = p12;

// container
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define ALL(v) v.begin(), v.end()

// repetition
#define REP(n) for (int i = 0; i < (n); i++)
#define REPj(n) for (int j = 0; j < (n); j++)
#define REPinit(i, a, n) for (int i = (a); i < (n); i++)
#define REPinitj(j, a, n) for (int j = (a); j < (n); j++)

// algorithm
// #define SORT(c) sort((c).begin(),(c).end())

// constant

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// Optimizer
//  #pragma GCC optimize("Ofast")
//  #pragma GCC
//  target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

/*
    add utility Algorithm
*/

int abcd[50][4];
int N, M, Q;
ll ans = 0;

void dfs(vector<int> v, int cnt, int las) {
  if (cnt == N) {
    ll res = 0;
    REP(Q) {
      if (v[abcd[1][i]] - v[abcd[0][i]] == abcd[2][i])
        res += abcd[3][i];
    }
    ans = max(ans, res);
  } else {
    REPinit(i, las, M + 1) {
      vector<int> new_v = v;
      new_v.push_back(i);
      dfs(new_v, cnt + 1, i);
    }
  }
}

int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> N >> M >> Q;

  for (int i = 0; i < Q; i++) {
    cin >> abcd[i][0] >> abcd[i][1] >> abcd[i][2] >> abcd[i][3];
  }

  vector<int> v;
  dfs(v, 0, 1);
  cout << ans << endl;

  return 0;
}