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

#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, s, n) for (i64 i = (s), i##_limit = (n); i < i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define repc(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define var(type, ...)                                                         \\
  type __VA_ARGS__;                                                            \\
  read(__VA_ARGS__);
#ifndef DBG
#define dump(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> makeV(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeV(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(makeV<T>(initValue, args...))>(
      sz, makeV<T>(initValue, args...));
}

template <class T> inline istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &e : vec)
    is >> e;
  return is;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &os, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    os << e << delim[&e == &vec.back()];
  return os;
}

template <class Container> struct Rev {
  Container &x_;
  inline Rev(Container &x) : x_(x) {}
  inline auto begin() { return rbegin(x_); }
  inline auto end() { return rend(x_); }
};
// }}} End Header

int N, M, Q;
vector<int> a, b, c, d;
vector<int> xs;

int dfs(int idx) {
  if (idx > N) {
    int sum = 0;
    rep(i, 0, Q) {
      if (xs[b[i]] - xs[a[i]] == c[i])
        sum += d[i];
    }
    return sum;
  }

  int ret = 0;
  repc(v, xs[idx - 1], M) {
    xs[idx] = v;
    chmax(ret, dfs(idx + 1));
  }

  return ret;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.setf(ios_base::fixed);
  cout.precision(12);

  read(N, M, Q);
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  rep(i, 0, Q) { read(a[i], b[i], c[i], d[i]); }

  xs.resize(N + 1);
  xs[0] = 1;

  print(dfs(1));

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define rep(i, s, n) for (i64 i = (s), i##_limit = (n); i < i##_limit; ++i)
#define repr(i, s, t) for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#define repc(i, s, t) for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define var(type, ...)                                                         \\
  type __VA_ARGS__;                                                            \\
  read(__VA_ARGS__);
#ifndef DBG
#define dump(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  return b > a && (a = b, true);
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  return b < a && (a = b, true);
}
constexpr int INF = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T> inline vector<T> makeV(const T &initValue, size_t sz) {
  return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto makeV(const T &initValue, size_t sz, Args... args) {
  return vector<decltype(makeV<T>(initValue, args...))>(
      sz, makeV<T>(initValue, args...));
}

template <class T> inline istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &e : vec)
    is >> e;
  return is;
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail &...tail) {
  cin >> head;
  read(tail...);
}

inline void print() { cout << "\\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail &&...tail) {
  cout << head;
  if (sizeof...(tail))
    cout << ' ';
  print(forward<Tail>(tail)...);
}

template <class T>
inline ostream &operator<<(ostream &os, const vector<T> &vec) {
  static constexpr const char *delim[] = {" ", ""};
  for (const auto &e : vec)
    os << e << delim[&e == &vec.back()];
  return os;
}

template <class Container> struct Rev {
  Container &x_;
  inline Rev(Container &x) : x_(x) {}
  inline auto begin() { return rbegin(x_); }
  inline auto end() { return rend(x_); }
};
// }}} End Header

int N, M, Q;
vector<tuple<int, int, int, int>> X;

map<vector<int>, int> memo[15];
int dfs(int qi, vector<int> A) {
  if (qi >= Q) {
    return 0;
  }

  if (memo[qi].count(A))
    return memo[qi][A];

  int ret = dfs(qi + 1, A);
  int a, b, c, d;
  tie(a, b, c, d) = X[qi];
  if (A[a] == -1 && A[b] == -1) {
    A[a] = *max_element(A.begin(), A.begin() + a);
    A[b] = A[a] + c;
    chmax(ret, dfs(qi + 1, A) + d);
  } else if (A[a] == -1) {
    const int lb = *max_element(A.begin(), A.begin() + a);
    if (A[b] - c >= lb) {
      A[a] = A[b] - c;
      chmax(ret, dfs(qi + 1, A) + d);
    }
  } else if (A[b] == -1) {
    const int ub = *min_element(A.begin() + b, A.end());
    if (A[a] + c <= ub || ub == -1) {
      A[b] = A[a] + c;
      chmax(ret, dfs(qi + 1, A) + d);
    }
  } else if (A[b] - A[a] == c) {
    chmax(ret, dfs(qi + 1, A) + d);
  }

  return memo[qi][A] = ret;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.setf(ios_base::fixed);
  cout.precision(12);

  read(N, M, Q);
  X.resize(Q);
  rep(i, 0, Q) {
    var(int, a, b, c, d);
    --a, --b;
    X[i] = {a, b, c, d};
  }

  sort(all(X));

  vector<int> A(N, -1);
  A[0] = 1;
  int ans = dfs(0, A);

  print(ans);

  return 0;
}