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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;

namespace std {
template <class T> string to_string(T s);
template <class S, class T> string to_string(pair<S, T> p);
string to_string(string s) { return s; }
string to_string(const char s[]) { return to_string(string(s)); }

template <class T> string to_string(T v) {
  if (v.empty())
    return "{}";
  string ret = "{";
  for (auto x : v)
    ret += to_string(x) + ",";
  ret.back() = '}';
  return ret;
}

template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + ":" + to_string(p.second) + "}";
}
} // namespace std
void debug() { std::cerr << std::endl; }

template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  std::cerr << std::to_string(head) << " ";
  debug(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  std::string separator = " ";
  template <class T> inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus)
      x = -x;
  }
  inline void read(std::string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\\n' && c != EOF);
  }
  template <class T> inline void read(std::vector<T> &v) {
    for (auto &x : v)
      read(x);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &...tail) {
    read(head);
    read(tail...);
  }
  template <class T> inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0)
      putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(std::string x) {
    for (char c : x)
      putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i)
      putchar_unlocked(s[i]);
  }
  template <class T> inline void write(std::vector<T> v) {
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  template <class Head, class... Tail>
  inline void writeln(Head head, Tail... tail) {
    write(head, tail...);
    write("\\n");
  }
  void set_separator(std::string s) { separator = s; }
} io;

using namespace std;

int main() {
  int N, M, Q;
  io.read(N, M, Q);
  vector<int> a(Q), b(Q), c(Q), d(Q);
  REP(i, Q) io.read(a[i], b[i], c[i], d[i]);
  vector<int> A(N + 1);
  int ans = 0;
  A[0] = 1;
  function<void(int)> dfs = [&](int i) {
    if (i == N + 1) {
      int ret = 0;
      REP(k, Q) {
        if (A[b[k]] - A[a[k]] == c[k])
          ret += d[k];
      }
      debug(A);
      ans = max(ans, ret);
    } else {
      for (int j = A[i - 1]; j <= M; ++j) {
        A[i] = j;
        dfs(i + 1);
      }
    }
    return;
  };
  dfs(1);
  io.writeln(ans);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(i, N) for (int i = 0; i < (int)N; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define ALL(x) (x).begin(), (x).end()
#define INF (1 << 30)
#define LLINF (1LL << 62)
#define DEBUG(...) debug(__LINE__, ":" __VA_ARGS__)

constexpr int MOD = 1000000007;
using ll = long long;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;

namespace std {
template <class T> string to_string(T s);
template <class S, class T> string to_string(pair<S, T> p);
string to_string(string s) { return s; }
string to_string(const char s[]) { return to_string(string(s)); }

template <class T> string to_string(T v) {
  if (v.empty())
    return "{}";
  string ret = "{";
  for (auto x : v)
    ret += to_string(x) + ",";
  ret.back() = '}';
  return ret;
}

template <class S, class T> string to_string(pair<S, T> p) {
  return "{" + to_string(p.first) + ":" + to_string(p.second) + "}";
}
} // namespace std
void debug() { std::cerr << std::endl; }

template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  std::cerr << std::to_string(head) << " ";
  debug(tail...);
}

struct IO {
#ifdef _WIN32
  inline char getchar_unlocked() { return getchar(); }
  inline void putchar_unlocked(char c) { putchar(c); }
#endif
  std::string separator = " ";
  template <class T> inline void read(T &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c != '-' && (c < '0' || '9' < c));
    bool minus = 0;
    if (c == '-') {
      minus = 1;
      c = getchar_unlocked();
    }
    x = 0;
    while ('0' <= c && c <= '9') {
      x *= 10;
      x += c - '0';
      c = getchar_unlocked();
    }
    if (minus)
      x = -x;
  }
  inline void read(std::string &x) {
    char c;
    do {
      c = getchar_unlocked();
    } while (c == ' ' || c == '\\n');
    x.clear();
    do {
      x += c;
      c = getchar_unlocked();
    } while (c != ' ' && c != '\\n' && c != EOF);
  }
  template <class T> inline void read(std::vector<T> &v) {
    for (auto &x : v)
      read(x);
  }
  template <class Head, class... Tail>
  inline void read(Head &head, Tail &...tail) {
    read(head);
    read(tail...);
  }
  template <class T> inline void write(T x) {
    char buf[32];
    int p = 0;
    if (x < 0) {
      x = -x;
      putchar_unlocked('-');
    }
    if (x == 0)
      putchar_unlocked('0');
    while (x > 0) {
      buf[p++] = (x % 10) + '0';
      x /= 10;
    }
    while (p) {
      putchar_unlocked(buf[--p]);
    }
  }
  inline void write(std::string x) {
    for (char c : x)
      putchar_unlocked(c);
  }
  inline void write(const char s[]) {
    for (int i = 0; s[i] != 0; ++i)
      putchar_unlocked(s[i]);
  }
  template <class T> inline void write(std::vector<T> v) {
    for (auto itr = v.begin(); itr + 1 != v.end(); ++itr) {
      write(*itr);
      write(separator);
    }
    write(v.back());
  }
  template <class Head, class... Tail>
  inline void write(Head head, Tail... tail) {
    write(head);
    write(separator);
    write(tail...);
  }
  template <class Head, class... Tail>
  inline void writeln(Head head, Tail... tail) {
    write(head, tail...);
    write("\\n");
  }
  void set_separator(std::string s) { separator = s; }
} io;

using namespace std;

int main() {
  int N, M, Q;
  io.read(N, M, Q);
  vector<int> a(Q), b(Q), c(Q), d(Q);
  REP(i, Q) io.read(a[i], b[i], c[i], d[i]);
  vector<int> A(N + 1);
  int ans = 0;
  function<void(int)> dfs = [&](int i) {
    if (i == N + 1) {
      int ret = 0;
      REP(k, Q) {
        if (A[b[k]] - A[a[k]] == c[k])
          ret += d[k];
      }
      ans = max(ans, ret);
    } else {
      for (int j = A[i - 1]; j <= M; ++j) {
        A[i + 1] = j;
        dfs(i + 1);
      }
    }
    return;
  };
  dfs(1);
  io.writeln(ans);
  return 0;
}