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

#include <bits/stdc++.h>

using namespace std;

namespace variables {
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
struct triple {
  ll first, second, third;
};
} // namespace variables

using namespace variables;
void testt();
namespace andu2006 {
/// debugging
void debug(set<ll> _var, string _name = "") {
  cerr << "Debug #set " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END] : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(set<ll> _var, string _name = "") {
  cerr << "Debug #set " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END]\\n\\n";
}
void debug(map<ll, ll> _var, string _name = "") {
  cerr << "Debug #map (size = " << _var.size() << ")\\n";
  for (auto it : _var)
    cerr << _name << "[" << it.first << "] = " << it.second << '\\n';
}
void light_debug(map<ll, ll> _var, string _name = "") {
  cerr << "Debug #map \\n";
  for (auto it : _var)
    cerr << _name << "[" << it.first << "] = " << it.second << '\\n';
}
void debug(deque<ll> _var, string _name = "") {
  cerr << "Debug #deque " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END] : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(deque<ll> _var, string _name = "") {
  cerr << "Debug #deque " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END]\\n\\n";
}
void debug(vector<ll> _var, string _name = "") {
  cerr << "Debug #vector " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END] : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(vector<ll> _var, string _name = "") {
  cerr << "Debug #vector " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END]\\n\\n";
}
void debug(string _var, string _name = "") {
  cerr << "Debug #string " << _name << " = ";
  cerr << '"' << _var << '"';
  cerr << " : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(string _var, string _name = "") {
  cerr << "Debug #string : " << _name << " = " << '"' << _var << '"' << "\\n\\n";
}
void debug(ll _var, string _name = "") {
  cerr << "Debug #int64 : " << _name << " = " << _var << "\\n\\n";
}
void debug(ull _var, string _name = "") {
  cerr << "Debug #uint64 : " << _name << " = " << _var << "\\n\\n";
}
void debug(uint _var, string _name = "") {
  cerr << "Debug #uint32 : " << _name << " = " << _var << "\\n\\n";
}
void debug(int _var, string _name = "") {
  cerr << "Debug #int32 : " << _name << " = " << _var << "\\n\\n";
}
void debug(char _var, string _name = "") {
  cerr << "Debug #char : " << _name << " = '" << _var << "'\\n\\n";
}
void debug(bool _var, string _name = "") {
  cerr << "Debug #Boolean : " << _name << " = " << (_var ? "1 True" : "0 False")
       << "\\n\\n";
}
/// misc
string emptyString;
vector<ll> emptyVector;
stack<ll> emptyStack;
queue<ll> emptyQueue;
void multipleTests() {
  ll __test_count;
  cin >> __test_count;
  while (__test_count--)
    testt();
}
void done() { exit(0); }
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void rseed() { srand(time(NULL)); }
void mreset(void *_pointer, uint _size) { memset(_pointer, 0, _size); }
/// compare functions
template <char compArg> bool compare(ll a, ll b) {
  if (compArg == '<')
    return a < b;
  else if (compArg == '>')
    return a > b;
  return false;
}
template <char compArg1, char compArg2>
bool compair(pll a, pll b) { /// HAHA pun
  return compare<compArg1>(a.first, b.first) ||
         (a.first == b.first && compare<compArg2>(a.second, b.second));
}
template <char compArg1, char compArg2> struct objcompair {
  bool operator()(pll a, pll b) {
    return compare<compArg1>(a.first, b.first) ||
           (a.first == b.first && compare<compArg2>(a.second, b.second));
  }
};
/// binary functions
ll trailingZeroes(ll x) {
  if (x == 0)
    return 0;
  return (x & 1 ? 0 : 1 + trailingZeroes(x / 2));
}
ll trailingOnes(ll x) {
  if (x == 0)
    return 0;
  return (x & 1 ? 1 + trailingZeroes(x / 2) : 0);
}
ll trailingPowerOfTwo(ll x) { return 1ll << trailingZeroes(x); }
ll popCount(ll x) {
  if (x == 0)
    return x;
  return 1 + popCount(x & -x);
}
ll log2(ll x) {
  if (x == 0)
    return -1;
  return 1 + log2(x / 2);
}
ll getBit(ll x, ll pos) { return (x >> pos) & 1; }
ll cutLastOne(ll x) { return x & -x; }
/// modular functions; "mod" MUST BE PRIME
ll binPow(ll _num, ll _exponent, ll _mod) { /// a^b
  if (_exponent == 0)
    return 1;
  ll _half = binPow(_num, _exponent / 2, _mod);

  return _half * _half % _mod * (_exponent & 1 ? _num : 1) % _mod;
}
ll modularInverse(ll _x, ll _mod) { /// (a / b ) % mod
  return binPow(_x, _mod - 2, _mod);
}
ll comb(ll _n, ll _k, ll _mod) { /// binom(a, b)
  ll _ans = 1;

  for (ll i = _n - _k + 1; i <= _n; i++)
    _ans = _ans * i % _mod;

  for (ll i = 2; i <= _k; i++)
    _ans = _ans * modularInverse(i, _mod) % _mod;
  return _ans;
}
/// math
const ld PI = acos(-1);
const ld rad = 180 / PI;
ld degsin(ll __alpha) { return sin(__alpha / rad); }
ld degcos(ll __alpha) { return cos(__alpha / rad); }
ld degtan(ll __alpha) { return tan(__alpha / rad); }
ld degasin(ll _sine) { return asin(_sine) * rad; }
ld degacos(ll _cosine) { return acos(_cosine) * rad; }
ld degatan(ll _tangent) { return atan(_tangent) * rad; }
ll gcd(ll _num1, ll _num2) {
  if (_num2 == 0)
    return _num1;
  return gcd(_num2, _num1 % _num2);
}
ll lcm(ll _num1, ll _num2) { return _num1 * _num2 / gcd(_num1, _num2); }
ll divisors(ll _num) { /// number of divisors

  if (_num == 0)
    return 0;

  ll _cnt = 1, _exp;

  while (_num % 2 == 0) {
    _cnt++;
    _num /= 2;
  }
  for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
    _exp = 0;
    while (_num % _divisor == 0) {
      _exp++;
      _num /= _divisor;
    }
    _cnt *= (_exp + 1);
  }
  if (_num > 1)
    _cnt *= 2;
  return _cnt;
}
ll eulerTotient(ll _num) { /// computes the euler totient function
  if (_num == 0)
    return 0;

  ll _cnt = 1, _divisor = 2;

  while (_num % _divisor == 0) {
    _cnt *= _divisor;
    _num /= _divisor;
  }
  _cnt /= _divisor;
  _cnt *= (_divisor - 1);
  for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
    if (_num % _divisor == 0) {
      while (_num % _divisor == 0) {
        _cnt *= _divisor;
        _num /= _divisor;
      }
      _cnt /= _divisor;
      _cnt *= (_divisor - 1);
    }
  }
  if (_num > 1)
    _cnt *= _num - 1;
  return _cnt;
}
void sieve(bool *_startpos, bool *_endpos) {
  /// erathostenes' sieve; 0 means the number is prime; 1 means the number is
  /// composite
  ll _maxnum = _endpos - _startpos;
  for (ll i = 4; i < _maxnum; i += 2)
    *(_startpos + i) = 1;
  for (ll i = 3; i * i < _maxnum; i += 2)
    if (*(_startpos + i) == 0)
      for (ll j = i * i; j < _maxnum; j += i)
        *(_startpos + j) = 1;
}
} // namespace andu2006
/// - end of namespaces and start of program

#define MOD 998244353
#define NMAX 100
struct str {
  ll a, b, c, d;
} v[NMAX];
ll sol[NMAX], ans = 0, n, m, q;
void testt() {}
ll sum() {
  ll s = 0;
  for (ll i = 0; i < q; i++) {
    if (sol[v[i].b - 1] - sol[v[i].a - 1] == v[i].c)
      s += v[i].d;
  }
  return s;
}
void bkt(ll k) {
  if (k == n) {
    ans = max(ans, sum());
  } else {
    for (ll i = (k ? sol[k - 1] : 1); i <= m; i++) {
      sol[k] = i;
      bkt(k + 1);
    }
  }
}
int main() {
  andu2006::fastIO();
  cin >> n >> m >> q;
  for (ll i = 0; i < q; i++)
    cin >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
  bkt(0);
  cout << ans;
  andu2006::done();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

namespace variables {
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
struct triple {
  ll first, second, third;
};
} // namespace variables

using namespace variables;
void testt();
namespace andu2006 {
/// debugging
void debug(set<ll> _var, string _name = "") {
  cerr << "Debug #set " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END] : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(set<ll> _var, string _name = "") {
  cerr << "Debug #set " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END]\\n\\n";
}
void debug(map<ll, ll> _var, string _name = "") {
  cerr << "Debug #map (size = " << _var.size() << ")\\n";
  for (auto it : _var)
    cerr << _name << "[" << it.first << "] = " << it.second << '\\n';
}
void light_debug(map<ll, ll> _var, string _name = "") {
  cerr << "Debug #map \\n";
  for (auto it : _var)
    cerr << _name << "[" << it.first << "] = " << it.second << '\\n';
}
void debug(deque<ll> _var, string _name = "") {
  cerr << "Debug #deque " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END] : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(deque<ll> _var, string _name = "") {
  cerr << "Debug #deque " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END]\\n\\n";
}
void debug(vector<ll> _var, string _name = "") {
  cerr << "Debug #vector " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END] : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(vector<ll> _var, string _name = "") {
  cerr << "Debug #vector " << _name << " = [";
  for (auto it : _var)
    cerr << it << ",";
  cerr << "END]\\n\\n";
}
void debug(string _var, string _name = "") {
  cerr << "Debug #string " << _name << " = ";
  cerr << '"' << _var << '"';
  cerr << " : "
       << "size = " << _var.size() << "\\n\\n";
}
void light_debug(string _var, string _name = "") {
  cerr << "Debug #string : " << _name << " = " << '"' << _var << '"' << "\\n\\n";
}
void debug(ll _var, string _name = "") {
  cerr << "Debug #int64 : " << _name << " = " << _var << "\\n\\n";
}
void debug(ull _var, string _name = "") {
  cerr << "Debug #uint64 : " << _name << " = " << _var << "\\n\\n";
}
void debug(uint _var, string _name = "") {
  cerr << "Debug #uint32 : " << _name << " = " << _var << "\\n\\n";
}
void debug(int _var, string _name = "") {
  cerr << "Debug #int32 : " << _name << " = " << _var << "\\n\\n";
}
void debug(char _var, string _name = "") {
  cerr << "Debug #char : " << _name << " = '" << _var << "'\\n\\n";
}
void debug(bool _var, string _name = "") {
  cerr << "Debug #Boolean : " << _name << " = " << (_var ? "1 True" : "0 False")
       << "\\n\\n";
}
/// misc
string emptyString;
vector<ll> emptyVector;
stack<ll> emptyStack;
queue<ll> emptyQueue;
void multipleTests() {
  ll __test_count;
  cin >> __test_count;
  while (__test_count--)
    testt();
}
void done() { exit(0); }
void fastIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
}
void rseed() { srand(time(NULL)); }
void mreset(void *_pointer, uint _size) { memset(_pointer, 0, _size); }
/// compare functions
template <char compArg> bool compare(ll a, ll b) {
  if (compArg == '<')
    return a < b;
  else if (compArg == '>')
    return a > b;
  return false;
}
template <char compArg1, char compArg2>
bool compair(pll a, pll b) { /// HAHA pun
  return compare<compArg1>(a.first, b.first) ||
         (a.first == b.first && compare<compArg2>(a.second, b.second));
}
template <char compArg1, char compArg2> struct objcompair {
  bool operator()(pll a, pll b) {
    return compare<compArg1>(a.first, b.first) ||
           (a.first == b.first && compare<compArg2>(a.second, b.second));
  }
};
/// binary functions
ll trailingZeroes(ll x) {
  if (x == 0)
    return 0;
  return (x & 1 ? 0 : 1 + trailingZeroes(x / 2));
}
ll trailingOnes(ll x) {
  if (x == 0)
    return 0;
  return (x & 1 ? 1 + trailingZeroes(x / 2) : 0);
}
ll trailingPowerOfTwo(ll x) { return 1ll << trailingZeroes(x); }
ll popCount(ll x) {
  if (x == 0)
    return x;
  return 1 + popCount(x & -x);
}
ll log2(ll x) {
  if (x == 0)
    return -1;
  return 1 + log2(x / 2);
}
ll getBit(ll x, ll pos) { return (x >> pos) & 1; }
ll cutLastOne(ll x) { return x & -x; }
/// modular functions; "mod" MUST BE PRIME
ll binPow(ll _num, ll _exponent, ll _mod) { /// a^b
  if (_exponent == 0)
    return 1;
  ll _half = binPow(_num, _exponent / 2, _mod);

  return _half * _half % _mod * (_exponent & 1 ? _num : 1) % _mod;
}
ll modularInverse(ll _x, ll _mod) { /// (a / b ) % mod
  return binPow(_x, _mod - 2, _mod);
}
ll comb(ll _n, ll _k, ll _mod) { /// binom(a, b)
  ll _ans = 1;

  for (ll i = _n - _k + 1; i <= _n; i++)
    _ans = _ans * i % _mod;

  for (ll i = 2; i <= _k; i++)
    _ans = _ans * modularInverse(i, _mod) % _mod;
  return _ans;
}
/// math
const ld PI = acos(-1);
const ld rad = 180 / PI;
ld degsin(ll __alpha) { return sin(__alpha / rad); }
ld degcos(ll __alpha) { return cos(__alpha / rad); }
ld degtan(ll __alpha) { return tan(__alpha / rad); }
ld degasin(ll _sine) { return asin(_sine) * rad; }
ld degacos(ll _cosine) { return acos(_cosine) * rad; }
ld degatan(ll _tangent) { return atan(_tangent) * rad; }
ll gcd(ll _num1, ll _num2) {
  if (_num2 == 0)
    return _num1;
  return gcd(_num2, _num1 % _num2);
}
ll lcm(ll _num1, ll _num2) { return _num1 * _num2 / gcd(_num1, _num2); }
ll divisors(ll _num) { /// number of divisors

  if (_num == 0)
    return 0;

  ll _cnt = 1, _exp;

  while (_num % 2 == 0) {
    _cnt++;
    _num /= 2;
  }
  for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
    _exp = 0;
    while (_num % _divisor == 0) {
      _exp++;
      _num /= _divisor;
    }
    _cnt *= (_exp + 1);
  }
  if (_num > 1)
    _cnt *= 2;
  return _cnt;
}
ll eulerTotient(ll _num) { /// computes the euler totient function
  if (_num == 0)
    return 0;

  ll _cnt = 1, _divisor = 2;

  while (_num % _divisor == 0) {
    _cnt *= _divisor;
    _num /= _divisor;
  }
  _cnt /= _divisor;
  _cnt *= (_divisor - 1);
  for (ll _divisor = 3; _divisor * _divisor <= _num; _divisor += 2) {
    if (_num % _divisor == 0) {
      while (_num % _divisor == 0) {
        _cnt *= _divisor;
        _num /= _divisor;
      }
      _cnt /= _divisor;
      _cnt *= (_divisor - 1);
    }
  }
  if (_num > 1)
    _cnt *= _num - 1;
  return _cnt;
}
void sieve(bool *_startpos, bool *_endpos) {
  /// erathostenes' sieve; 0 means the number is prime; 1 means the number is
  /// composite
  ll _maxnum = _endpos - _startpos;
  for (ll i = 4; i < _maxnum; i += 2)
    *(_startpos + i) = 1;
  for (ll i = 3; i * i < _maxnum; i += 2)
    if (*(_startpos + i) == 0)
      for (ll j = i * i; j < _maxnum; j += i)
        *(_startpos + j) = 1;
}
} // namespace andu2006
/// - end of namespaces and start of program

#define MOD 998244353
#define NMAX 11
struct str {
  ll a, b, c, d;
} v[NMAX];
ll sol[NMAX], ans = 0, n, m, q;
void testt() {}
ll sum() {
  ll s = 0;
  for (ll i = 0; i < q; i++) {
    if (sol[v[i].b - 1] - sol[v[i].a - 1] == v[i].c)
      s += v[i].d;
  }
  return s;
}
void bkt(ll k) {
  if (k == n) {
    ans = max(ans, sum());
  } else {
    for (ll i = (k ? sol[k - 1] : 1); i <= m; i++) {
      sol[k] = i;
      bkt(k + 1);
    }
  }
}
int main() {
  andu2006::fastIO();
  cin >> n >> m >> q;
  for (ll i = 0; i < q; i++)
    cin >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
  bkt(0);
  cout << ans;
  andu2006::done();
}