Problem p02695 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const long long INF = INT_MAX / 4;
const long long MOD = 1'000'000'007;
const double EPS = 1e-14;
const bool DEBUG = false;

const string YES = "YES";
const string NO = "NO";
const string Yes = "Yes";
const string No = "No";

template <class T> void debug(T head) {
  if (DEBUG) {
    cout << head << endl;
  }
}

template <class Head, class... Body> void debug(Head head, Body... body) {
  if (DEBUG) {
    cout << head << " ";
    debug(body...);
  }
}

/////
class q_set {
public:
  long a, b, c, d;
};

int N, M, Q;
vector<q_set> q;
long max_ans = 0;

long calc(vector<int> a) {
  long ans = 0;
  for (int i = 0; i < Q; ++i) {
    if (a[q[i].b - 1] - a[q[i].a - 1] == q[i].c) {
      ans += q[i].d;
    }
  }
  // debug(a[0], a[1], a[2], ans);
  if (max_ans < ans) {
    max_ans = ans;
  }
}

void dfs(int d, vector<int> a) {
  if (d == N) {
    if (a.size() == N) {
      calc(a);
    }
  } else if (d == 0) {
    for (int i = 1; i <= M; ++i) {
      a.push_back(i);
      dfs(d + 1, a);
      a.pop_back();
    }
  } else {
    for (int i = a[a.size() - 1]; i <= M; ++i) {
      a.push_back(i);
      dfs(d + 1, a);
      a.pop_back();
    }
  }
}

void answer() {
  int a, b, c, d;
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; ++i) {
    cin >> a >> b >> c >> d;
    q.push_back({a, b, c, d});
  }

  vector<int> aa;
  dfs(0, aa);

  cout << max_ans << endl;

  return;
}

/////

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.precision(16);

  answer();

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const long long INF = INT_MAX / 4;
const long long MOD = 1'000'000'007;
const double EPS = 1e-14;
const bool DEBUG = false;

const string YES = "YES";
const string NO = "NO";
const string Yes = "Yes";
const string No = "No";

template <class T> void debug(T head) {
  if (DEBUG) {
    cout << head << endl;
  }
}

template <class Head, class... Body> void debug(Head head, Body... body) {
  if (DEBUG) {
    cout << head << " ";
    debug(body...);
  }
}

/////
class q_set {
public:
  long a, b, c, d;
};

int N, M, Q;
vector<q_set> q;
long max_ans = 0;

void calc(vector<int> a) {
  long ans = 0;
  for (int i = 0; i < Q; ++i) {
    if (a[q[i].b - 1] - a[q[i].a - 1] == q[i].c) {
      ans += q[i].d;
    }
  }
  // debug(a[0], a[1], a[2], ans);
  if (max_ans < ans) {
    max_ans = ans;
  }
}

void dfs(int d, vector<int> a) {
  if (d == N) {
    if (a.size() == N) {
      calc(a);
    }
  } else if (d == 0) {
    for (int i = 1; i <= M; ++i) {
      a.push_back(i);
      dfs(d + 1, a);
      a.pop_back();
    }
  } else {
    for (int i = a[a.size() - 1]; i <= M; ++i) {
      a.push_back(i);
      dfs(d + 1, a);
      a.pop_back();
    }
  }
}

void answer() {
  int a, b, c, d;
  cin >> N >> M >> Q;
  for (int i = 0; i < Q; ++i) {
    cin >> a >> b >> c >> d;
    q.push_back({a, b, c, d});
  }

  vector<int> aa;
  dfs(0, aa);

  cout << max_ans << endl;

  return;
}

/////

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.precision(16);

  answer();

  return 0;
}