Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
  int a = 1, po = x;
  while (p) {
    if (p & 1)
      a = 1ll * a * po % M;
    po = 1ll * po * po % M;
    p >>= 1;
  }
  return a;
}
int cnt = 0;
int ans = 0;
vector<int> A[10];

void count(int N, int M, int Q, vector<int> a, vector<int> b, vector<int> c,
           vector<int> d, int x) {
  if (x == N) {
    cnt = 0;
    rep(i, Q) {
      if (A[x].at(b.at(i) - 1) - A[x].at(a.at(i) - 1) == c.at(i)) {
        cnt += d.at(i);
      }
    }
    A[x] = {};
    ans = max(ans, cnt);
  } else if (x == 0) {
    for (int j = 1; j <= M; j++) {
      A[1] = {j};
      count(N, M, Q, a, b, c, d, 1);
    }
  } else {
    for (int j = A[x].at(x - 1); j <= M; j++) {
      A[x + 1] = A[x];
      A[x + 1].push_back(j);
      count(N, M, Q, a, b, c, d, x + 1);
    }
  }
}

// Start from Here
int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a, b, c, d;
  rep(i, Q) {
    int x, y, z, w;
    cin >> x >> y >> z >> w;
    a.push_back(x);
    b.push_back(y);
    c.push_back(z);
    d.push_back(w);
  }
  count(N, M, Q, a, b, c, d, 0);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
  int a = 1, po = x;
  while (p) {
    if (p & 1)
      a = 1ll * a * po % M;
    po = 1ll * po * po % M;
    p >>= 1;
  }
  return a;
}
int cnt = 0;
int ans = 0;
vector<int> A[11];

void count(int N, int M, int Q, vector<int> a, vector<int> b, vector<int> c,
           vector<int> d, int x) {
  if (x == N) {
    cnt = 0;
    rep(i, Q) {
      if (A[x].at(b.at(i) - 1) - A[x].at(a.at(i) - 1) == c.at(i)) {
        cnt += d.at(i);
      }
    }
    A[x] = {};
    ans = max(ans, cnt);
  } else if (x == 0) {
    for (int j = 1; j <= M; j++) {
      A[1] = {j};
      count(N, M, Q, a, b, c, d, 1);
    }
  } else {
    for (int j = A[x].at(x - 1); j <= M; j++) {
      A[x + 1] = A[x];
      A[x + 1].push_back(j);
      count(N, M, Q, a, b, c, d, x + 1);
    }
  }
}

// Start from Here
int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> a, b, c, d;
  rep(i, Q) {
    int x, y, z, w;
    cin >> x >> y >> z >> w;
    a.push_back(x);
    b.push_back(y);
    c.push_back(z);
    d.push_back(w);
  }
  count(N, M, Q, a, b, c, d, 0);
  cout << ans << endl;
}