Problem p02695 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define fs first
#define sc second
#define _GLIBCXX_DEBUG

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }

template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int ans = 0;
int n, m, q;
void dfs(int i, int j, vector<int> A, vector<tuple<int, int, int, int>> &data) {
  if (i == n) {
    print(A);
    int asw = 0;
    for (auto [a, b, c, d] : data) {
      if (A[b - 1] - A[a - 1] == c) {
        asw += d;
      }
    }
    ans = max(ans, asw);
    return;
  }
  for (int k = j; k <= m; k++) {
    A[i] = k;
    dfs(i + 1, k, A, data);
  }
}

int main(void) {
  cin >> n >> m >> q;
  vector<tuple<int, int, int, int>> data;
  Rep(q) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    data.push_back(make_tuple(a, b, c, d));
  }
  dfs(0, 1, vector<int>(n, 0), data);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define fs first
#define sc second
#define _GLIBCXX_DEBUG

template <class S> S sum(vector<S> &a) { return accumulate(all(a), S()); }
template <class S> S max(vector<S> &a) { return *max_element(all(a)); }
template <class S> S min(vector<S> &a) { return *min_element(all(a)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }

template <class S> void print(vector<S> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    cerr << v[i] << ' ';
  }
  cerr << endl;
}
template <class S> void print(vector<vector<S>> &v) {
  for (ll i = 0; i < (ll)v.size(); i++) {
    for (ll j = 0; j < (ll)v[i].size(); j++) {
      cerr << v[i][j] << ' ';
    }
    cerr << endl;
  }
}

int ans = 0;
int n, m, q;
void dfs(int i, int j, vector<int> A, vector<tuple<int, int, int, int>> &data) {
  if (i == n) {
    // print(A);
    int asw = 0;
    for (auto [a, b, c, d] : data) {
      if (A[b - 1] - A[a - 1] == c) {
        asw += d;
      }
    }
    ans = max(ans, asw);
    return;
  }
  for (int k = j; k <= m; k++) {
    A[i] = k;
    dfs(i + 1, k, A, data);
  }
}

int main(void) {
  cin >> n >> m >> q;
  vector<tuple<int, int, int, int>> data;
  Rep(q) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    data.push_back(make_tuple(a, b, c, d));
  }
  dfs(0, 1, vector<int>(n, 0), data);
  cout << ans << endl;
}