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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\\n'
#define pl cout << endl;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long INFll = 1ll * INF * INF;
const long double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define debug(x) cout << x << endl;
#define sz(a) int((a).size())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  // unordered_map<long long, int, custom_hash>
  // gp_hash_table<long long, int, custom_hash>
};
long long genrand(long long L, long long R) {
  return uniform_int_distribution<long long>(L, R)(rng);
}
int mul(int a, int b, int mod = MOD) { return int(a * 1ll * b % mod); }
int norm(int a, int mod = MOD) {
  while (a >= mod)
    a -= mod;
  while (a < 0)
    a += mod;
  return a;
}
int powmod(int x, int y, int mod = MOD) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = mul(res, x, mod);
    x = mul(x, x, mod);
    y = y >> 1;
  }
  return res;
}
int inv(int a, int mod = MOD) { return powmod(a, mod - 2); }

int n, m, q, ans = 0;
vector<int> v;
int a[50], b[50], c[50], d[50];
void solve(int idx) {
  if (idx == n) {
    int res = 0;
    for (int i = 0; i < q; i++) {
      if (v[b[i]] - v[a[i]] == c[i]) {
        res += d[i];
      }
    }
    ans = max(ans, res);
    return;
  }
  int st = 1;
  if (idx)
    st = v[idx - 1];
  for (int i = st; i <= m; i++) {
    v.pb(i);
    solve(idx + 1);
    v.pop_back();
  }
}

int main() {

  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", a + i, b + i, c + i, d + i);
    a[i]--;
    b[i]--;
  }
  solve(0);
  printf("%d", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\\n'
#define pl cout << endl;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
const long long INFll = 1ll * INF * INF;
const long double PI =
    3.141592653589793238462643383279502884197169399375105820974944;
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) ((a) * (b)) / gcd((a), (b))
#define debug(x) cout << x << endl;
#define sz(a) int((a).size())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
  // unordered_map<long long, int, custom_hash>
  // gp_hash_table<long long, int, custom_hash>
};
long long genrand(long long L, long long R) {
  return uniform_int_distribution<long long>(L, R)(rng);
}
int mul(int a, int b, int mod = MOD) { return int(a * 1ll * b % mod); }
int norm(int a, int mod = MOD) {
  while (a >= mod)
    a -= mod;
  while (a < 0)
    a += mod;
  return a;
}
int powmod(int x, int y, int mod = MOD) {
  int res = 1;
  while (y > 0) {
    if (y & 1)
      res = mul(res, x, mod);
    x = mul(x, x, mod);
    y = y >> 1;
  }
  return res;
}
int inv(int a, int mod = MOD) { return powmod(a, mod - 2); }

int n, m, q, ans = 0;
vector<int> v;
int a[10], b[10], c[10], d[10];
void solve(int idx) {
  if (idx == n) {
    int res = 0;
    for (int i = 0; i < q; i++) {
      if (v[b[i]] - v[a[i]] == c[i]) {
        res += d[i];
      }
    }
    ans = max(ans, res);
    return;
  }
  int st = 1;
  if (idx)
    st = v[idx - 1];
  for (int i = st; i <= m; i++) {
    v.pb(i);
    solve(idx + 1);
    v.pop_back();
  }
}

int main() {

  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", a + i, b + i, c + i, d + i);
    a[i]--;
    b[i]--;
  }
  solve(0);
  printf("%d", ans);
  return 0;
}