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

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define DUMP(i, v)                                                             \\
  for (ll i = 0; i < v.size(); i++)                                            \\
  cout << v[i] << " "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct Edge {
  ll from, to, cost;
};
struct node {
  ll cost, to;
  bool friend operator>(node a, node b) { return a.cost > b.cost; }
};

ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

ll gcd(ll a, ll b) {
  ll r = a % b;
  if (r == 0)
    return b;
  else
    return gcd(b, a % b);
}

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}
ll N, M, Q;
llvec a, b, c, d;
/*
struct cond{
  ll a, b, c, d;
};
*/

ll ans = 0;
void dfs(string s) {
  if (s.size() == N) {
    ll tmp = 0;
    rep(i, Q) {
      if ((ll)(s[b[i]] - s[a[i]]) == c[i])
        tmp += d[i];
    }
    ans = max(tmp, ans);
    return;
  } else {
    for (ll i = (ll)(s[s.size() - 1] - '0'); i < M; i++) {
      dfs(s + to_string(i));
    }
  }
  return;
}

/**************************************
** A main function starts from here  **
***************************************/
int main() {
  cin >> N >> M >> Q;
  a = llvec(Q);
  b = llvec(Q);
  c = llvec(Q);
  d = llvec(Q);
  rep(i, Q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  dfs("0");
  cout << ans;
  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define DUMP(i, v)                                                             \\
  for (ll i = 0; i < v.size(); i++)                                            \\
  cout << v[i] << " "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct Edge {
  ll from, to, cost;
};
struct node {
  ll cost, to;
  bool friend operator>(node a, node b) { return a.cost > b.cost; }
};

ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

ll gcd(ll a, ll b) {
  ll r = a % b;
  if (r == 0)
    return b;
  else
    return gcd(b, a % b);
}

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}
ll N, M, Q;
llvec a, b, c, d;
/*
struct cond{
  ll a, b, c, d;
};
*/

ll ans;
void dfs(string s) {
  if (s.size() == N) {
    ll tmp = 0;
    rep(i, Q) {
      if ((ll)(s[b[i]] - s[a[i]]) == c[i])
        tmp += d[i];
    }
    ans = max(tmp, ans);
    return;
  } else {
    for (ll i = (ll)(s[s.size() - 1] - '0'); i < 10; i++) {
      dfs(s + to_string(i));
    }
  }
  return;
}

/**************************************
** A main function starts from here  **
***************************************/
int main() {
  cin >> N >> M >> Q;
  a = llvec(N);
  b = llvec(N);
  c = llvec(N);
  d = llvec(N);
  rep(i, Q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  dfs("0");
  cout << ans << endl;
  // cout << cnt;
  return 0;
}