Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string_view>
#include <vector>
using namespace std;

/*----------macros----------*/

// output================
#define endl '\n'
#define fcout(a) cout << fixed << setprecision((int)(a))

// typedef===============
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

// const number==========
constexpr ll INF9 = (ll)1e9 + 1;
constexpr ll INF18 = (ll)1e18 + 1;
constexpr ll MOD = 1000000007;
constexpr ll MOD2 = 998244353;
constexpr ld PI = 3.141592653589793L;

// member================
#define pb push_back
#define eb emplace_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second

// others================
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; (i) < (n); ++(i))
#define reps(i, n) for (int i = 1; (i) <= (n); ++(i))
#define rrep(i, n) for (int i = n - 1; (i) >= 0; --(i))
#define repbit(bit, n) for (int bit = 0; (bit) < (1 << (n)); ++(bit))
#define ifbit(bit, i) if ((bit) & (1 << (i)))
#define CtoI(c) int((c)) - int('0')
#define ItoC(c) char((c) + '0')

// functions=============
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

// vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vector<int> dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };

/*--------additional--------*/

int n, m, q;
vector<int> a(100), b(100), c(100), d(100);
ll ans = 0;

void dfs(vector<int> _v, int _cnt, int _init) {
  if (_cnt == n) {
    int res = 0;
    rep(i, q) {
      if (_v[b[i]] - _v[a[i]] == c[i])
        res += d[i];
    }
    chmax(ans, res);
  } else {
    for (int i = _init; i <= m; ++i) {
      vector<int> _nv = _v;
      _nv.pb(i);
      dfs(_nv, ++_cnt, i);
    }
  }
}

/*----------main------------*/

int main() {
  /*----------fastio----------*/
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  std::ios::sync_with_stdio(false);
  /*----------solve-----------*/

  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    --a[i], --b[i];
  }

  vector<int> v;

  dfs(v, 0, 1);

  cout << ans << endl;

  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")

#define _GLIBCXX_DEBUG
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string_view>
#include <vector>
using namespace std;

/*----------macros----------*/

// output================
#define endl '\n'
#define fcout(a) cout << fixed << setprecision((int)(a))

// typedef===============
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;

// const number==========
constexpr ll INF9 = (ll)1e9 + 1;
constexpr ll INF18 = (ll)1e18 + 1;
constexpr ll MOD = 1000000007;
constexpr ll MOD2 = 998244353;
constexpr ld PI = 3.141592653589793L;

// member================
#define pb push_back
#define eb emplace_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second

// others================
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; (i) < (n); ++(i))
#define reps(i, n) for (int i = 1; (i) <= (n); ++(i))
#define rrep(i, n) for (int i = n - 1; (i) >= 0; --(i))
#define repbit(bit, n) for (int bit = 0; (bit) < (1 << (n)); ++(bit))
#define ifbit(bit, i) if ((bit) & (1 << (i)))
#define CtoI(c) int((c)) - int('0')
#define ItoC(c) char((c) + '0')

// functions=============
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

// vector<int> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vector<int> dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };

/*--------additional--------*/

int n, m, q;
vector<int> a(100), b(100), c(100), d(100);
ll ans = 0;

void dfs(vector<int> _v, int _cnt, int _init) {
  if (_cnt == n) {
    int res = 0;
    rep(i, q) {
      if (_v[b[i]] - _v[a[i]] == c[i])
        res += d[i];
    }
    chmax(ans, res);
  } else {
    for (int i = _init; i <= m; ++i) {
      vector<int> _nv = _v;
      _nv.pb(i);
      dfs(_nv, _cnt + 1, i);
    }
  }
}

/*----------main------------*/

int main() {
  /*----------fastio----------*/
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  std::ios::sync_with_stdio(false);
  /*----------solve-----------*/

  cin >> n >> m >> q;
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    --a[i], --b[i];
  }

  vector<int> v;

  dfs(v, 0, 1);

  cout << ans << endl;

  return 0;
}