Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#pragma region RegionDefs
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, l, r) for (int i = (l), i##_len = (r); i < i##_len; ++i)
#define all(x) begin(x), end(x)
using namespace std;
const int INF = 1e9;
template <class T = int> using V = vector<T>;
template <class T = int> using PQ = priority_queue<T>;
template <class T = int> using PQG = priority_queue<T, V<T>, greater<T>>;
typedef long long ll;
const ll MOD = 1000000007LL;
void in() {}
template <class Head, class... Tail> void in(Head &&head, Tail &&...tail) {
  cin >> head;
  in(move(tail)...);
}
#define IN(...)                                                                \
  ll __VA_ARGS__;                                                              \
  in(__VA_ARGS__)
// 時間が厳しいとき使う
#define INI(...)                                                               \
  int __VA_ARGS__;                                                             \
  in(__VA_ARGS__)
#define INS(T, ...)                                                            \
  T __VA_ARGS__;                                                               \
  in(__VA_ARGS__)
#define VIN(T, v, n)                                                           \
  V<T> v(n);                                                                   \
  for (auto &_elem : v)                                                        \
  cin >> _elem
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define y0 y__0
#define y1 y__1
#define j0 j__0
#define j1 j__1
#define YN(e) cout << ((e) ? "YES" : "NO") << endl
#define yn(e) cout << ((e) ? "Yes" : "No") << endl
#pragma endregion RegionDefs

template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
                                      bool (*callback)(const vector<T> &),
                                      int depth, vector<int> &used,
                                      vector<T> &temp) {
  if (depth == size)
    return callback(temp);
  for (int i = depth == 0 ? 0 : used[depth - 1]; i < seed.size(); ++i) {
    temp[depth] = seed[i];
    used[depth] = i;
    if (combination_with_replacement_dfs(seed, size, callback, depth + 1, used,
                                         temp))
      return true;
  }
  return false;
}

template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
                                      bool (*callback)(const vector<T> &)) {
  vector<int> used(size);
  vector<T> temp(size);
  combination_with_replacement_dfs(seed, size, callback, 0, used, temp);
}

vector<tuple<ll, ll, ll, ll>> cond;
ll mx = 0;

// callbackがtrueを返すと、その時点で探索を終了する。
bool test_callback(const vector<ll> &comb) {
  ll pts = 0;
  for (auto &&t : cond) {
    if (comb[get<1>(t)] - comb[get<0>(t)] == get<2>(t))
      pts += get<3>(t);
  }
  chmax(mx, pts);
  return false;
}

void solve() {
  IN(n, m, q);
  vector<ll> v(m);
  rep(i, m) v[i] = i + 1;
  rep(i, q) {
    IN(a, b, c, d);
    cond.emplace_back(a - 1, b - 1, c, d);
  }
  combination_with_replacement_dfs(v, n, test_callback);
  cout << mx << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(numeric_limits<double>::max_digits10);

  solve();

  return 0;
}

Fixed submission:
#pragma region RegionDefs
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, l, r) for (int i = (l), i##_len = (r); i < i##_len; ++i)
#define all(x) begin(x), end(x)
using namespace std;
const int INF = 1e9;
template <class T = int> using V = vector<T>;
template <class T = int> using PQ = priority_queue<T>;
template <class T = int> using PQG = priority_queue<T, V<T>, greater<T>>;
typedef long long ll;
const ll MOD = 1000000007LL;
void in() {}
template <class Head, class... Tail> void in(Head &&head, Tail &&...tail) {
  cin >> head;
  in(move(tail)...);
}
#define IN(...)                                                                \
  ll __VA_ARGS__;                                                              \
  in(__VA_ARGS__)
// 時間が厳しいとき使う
#define INI(...)                                                               \
  int __VA_ARGS__;                                                             \
  in(__VA_ARGS__)
#define INS(T, ...)                                                            \
  T __VA_ARGS__;                                                               \
  in(__VA_ARGS__)
#define VIN(T, v, n)                                                           \
  V<T> v(n);                                                                   \
  for (auto &_elem : v)                                                        \
  cin >> _elem
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define y0 y__0
#define y1 y__1
#define j0 j__0
#define j1 j__1
#define YN(e) cout << ((e) ? "YES" : "NO") << endl
#define yn(e) cout << ((e) ? "Yes" : "No") << endl
#pragma endregion RegionDefs

template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
                                      bool (*callback)(const vector<T> &),
                                      int depth, vector<int> &used,
                                      vector<T> &temp) {
  if (depth == size)
    return callback(temp);
  for (int i = depth == 0 ? 0 : used[depth - 1]; i < seed.size(); ++i) {
    temp[depth] = seed[i];
    used[depth] = i;
    if (combination_with_replacement_dfs(seed, size, callback, depth + 1, used,
                                         temp))
      return true;
  }
  return false;
}

template <class T>
bool combination_with_replacement_dfs(const vector<T> &seed, int size,
                                      bool (*callback)(const vector<T> &)) {
  vector<int> used(size);
  vector<T> temp(size);
  return combination_with_replacement_dfs(seed, size, callback, 0, used, temp);
}

vector<tuple<ll, ll, ll, ll>> cond;
ll mx = 0;

// callbackがtrueを返すと、その時点で探索を終了する。
bool test_callback(const vector<ll> &comb) {
  ll pts = 0;
  for (auto &&t : cond) {
    if (comb[get<1>(t)] - comb[get<0>(t)] == get<2>(t))
      pts += get<3>(t);
  }
  chmax(mx, pts);
  return false;
}

void solve() {
  IN(n, m, q);
  vector<ll> v(m);
  rep(i, m) v[i] = i + 1;
  rep(i, q) {
    IN(a, b, c, d);
    cond.emplace_back(a - 1, b - 1, c, d);
  }
  combination_with_replacement_dfs(v, n, test_callback);
  cout << mx << endl;
}

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(numeric_limits<double>::max_digits10);

  solve();

  return 0;
}