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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vll = vector<ll>;
using vvll = vector<vll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vb = vector<bool>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// pow(llpow,modpow)
template <class T> ll llpow(ll x, T n) {
  ll ans = 1;
  if (x == 0)
    ans = 0;
  while (n) {
    if (n & 1)
      ans *= x;
    x *= x;
    n >>= 1;
  }
  return ans;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
// 最大公約数
template <class T> inline T gcd(T x, T y) {
  if (y == 0)
    return x;
  else {
    return gcd(y, x % y);
  }
}
// 最小公倍数
template <class T> inline T lcm(T x, T y) { return x / gcd(x, y) * y; }
// 逆元
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)
// 試験導入
#define irep(i, end_i, begin_i)                                                \\
  for (ll i = (ll)begin_i - 1; i >= (ll)end_i; i--)

long long INF = 1LL << 60;
int main() {
  ll n, m;
  cin >> n >> m;
  ll q;
  cin >> q;
  vi a(q), b(q), c(q), d(q);
  rep(i, 0, q) cin >> a[i] >> b[i] >> c[i] >> d[i], a[i]--, b[i]--;
  vi res(n + m - 1, 1);
  rep(i, 0, m - 1) res[i] = 0;
  ll ans = 0;
  do {
    vi A(n, 1);
    int cnt = 0;
    int Ai = 0;
    rep(i, 0, n + m - 1) {
      if (!res[i])
        cnt++;
      else
        A[Ai++] += cnt;
    }
    ll tmp = 0;
    rep(i, 0, q) {
      if (A[b[i]] - A[a[i]] == c[i])
        tmp += d[i];
    }
    chmax(ans, tmp);
  } while (next_permutation(res.begin(), res.end()));
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using vll = vector<ll>;
using vvll = vector<vll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vb = vector<bool>;
using pii = pair<int, int>;
using vpii = vector<pair<int, int>>;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// pow(llpow,modpow)
template <class T> ll llpow(ll x, T n) {
  ll ans = 1;
  if (x == 0)
    ans = 0;
  while (n) {
    if (n & 1)
      ans *= x;
    x *= x;
    n >>= 1;
  }
  return ans;
}
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
// 最大公約数
template <class T> inline T gcd(T x, T y) {
  if (y == 0)
    return x;
  else {
    return gcd(y, x % y);
  }
}
// 最小公倍数
template <class T> inline T lcm(T x, T y) { return x / gcd(x, y) * y; }
// 逆元
long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

#define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++)
// 試験導入
#define irep(i, end_i, begin_i)                                                \\
  for (ll i = (ll)begin_i - 1; i >= (ll)end_i; i--)

long long INF = 1LL << 60;
int main() {
  ll n, m;
  cin >> n >> m;
  ll q;
  cin >> q;
  vi a(q), b(q), c(q), d(q);
  rep(i, 0, n) cin >> a[i] >> b[i] >> c[i] >> d[i], a[i]--, b[i]--;
  vi res(n + m - 1, 0);
  rep(i, 0, m - 1) res[i] = 1;
  ll ans = 0;
  do {
    vi A(n, 1);
    int cnt = 0;
    int Ai = 0;
    rep(i, 0, n + m - 1) {
      if (res[i])
        cnt++;
      else
        A[Ai++] += cnt;
    }
    ll tmp = 0;
    rep(i, 0, q) {
      if (b[i] - a[i] == c[i])
        tmp += d[i];
    }
    chmax(ans, tmp);
  } while (next_permutation(res.begin(), res.end()));
  cout << ans << endl;
  return 0;
}