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

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)

const int MOD = 1e9 + 7;

int mod_pow(int a, int b, int M = MOD) {
  int res = 1;

  while (b) {
    if (b & 1)
      res = (res * a) % M;
    a = (a * a) % M;
    b >>= 1;
  }

  return res;
}

const int N = 1200;
int a[N], b[N], c[N], d[N];
int n, m, q;
int res = 0;

vector<int> seq;

int go() {
  int vv = 0;

  f(i, q) {
    if (seq[b[i] - 1] - seq[a[i] - 1] == c[i])
      vv += d[i];
  }

  return vv;
}

void gen(int num, int last) {
  if (num == 0) {
    res = max(res, go());
    return;
  }

  for (int i = last; i <= m; i++) {
    seq.pb(i);
    gen(num - 1, i);
    seq.pop_back();
  }
}

void solve() {
  cin >> n >> m >> q;

  f(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];

  gen(n, 1);

  cout << res;
}

signed main() {
  fast;

  int t = 1;

  //    cin >> t;

  while (t--)

    solve();
}

Error: WA

Fixed submission:

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int getRand(int l, int r) {
  uniform_int_distribution<int> uid(l, r);
  return uid(rng);
}

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define precise(x) fixed << setprecision(x)

const int MOD = 1e9 + 7;

int mod_pow(int a, int b, int M = MOD) {
  int res = 1;

  while (b) {
    if (b & 1)
      res = (res * a) % M;
    a = (a * a) % M;
    b >>= 1;
  }

  return res;
}

const int N = 12;
int a[N], b[N], c[N], d[N];
int n, m, q;
int res = 0;

vector<int> seq;

int go() {
  int vv = 0;

  f(i, q) {
    if (seq[b[i] - 1] - seq[a[i] - 1] == c[i])
      vv += d[i];
  }

  return vv;
}

void gen(int num, int last) {
  if (num == 0) {
    res = max(res, go());
    return;
  }

  for (int i = last; i <= m; i++) {
    seq.pb(i);
    gen(num - 1, i);
    seq.pop_back();
  }
}

void solve() {
  cin >> n >> m >> q;

  f(i, q) cin >> a[i] >> b[i] >> c[i] >> d[i];

  gen(n, 1);

  cout << res;
}

signed main() {
  fast;

  int t = 1;

  //    cin >> t;

  while (t--)

    solve();
}