Problem p02695 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define DEB2(X) cout << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define DEB2(X)                                                                \\
  {}
#define END                                                                    \\
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
// typedef std::pair<int,int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define NP(X, Y) next_permutation(X, Y)
#define setdouble(X, Y) cout << fixed << setprecision(X) << Y
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N, M, Q;
int A[51], B[51], C[51], D[51];
int E[51];
int ans = 0;

void dfs(vector<int> v) {
  int res = 0;
  if (v.size() == N) {
    REP(i, Q) {
      if (v[B[i]] - v[A[i]] == C[i]) {
        res += D[i];
      }
    }
    ans = max(ans, res);
    return;
  }
  for (int i = v.back(); i <= M; i++) {
    vector<int> c = v;
    c.push_back(i);
    dfs(c);
  }
}

signed main() {
  cin >> N >> M >> Q;
  REP(i, Q) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--, B[i]--;
  }
  for (int i = 1; i <= M; i++) {
    dfs({i});
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define DEB2(X) cout << X << " ";
#define END cout << endl;
#else
#define DEB(X)                                                                 \\
  {}
#define DEB2(X)                                                                \\
  {}
#define END                                                                    \\
  {}
#endif
typedef long long ll;
#define int ll
#define uset unordered_set
#define umap unordered_map
// typedef std::pair<int,int> P;
struct edge {
  int to, cost;
};
const int INF = 100000000000000000;
const int INF2 = 9223372036854775807;
const int MOD = 1000000007;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define min(X, Y) (((int)(X) < (int)(Y)) ? (X) : (Y))
#define max(X, Y) (((int)(X) > (int)(Y)) ? (X) : (Y))
#define NP(X, Y) next_permutation(X, Y)
#define setdouble(X, Y) cout << fixed << setprecision(X) << Y
int ceil2(int a, int b) {
  if (a % b == 0) {
    return a / b;
  } else {
    return a / b + 1;
  }
}
int pow2(int a, int b) {
  int r = 1;
  for (int i = 1; i <= b; i++) {
    r *= a;
  }
  return r;
}
int Log2(int a) {
  int t = 0;
  while (1) {
    if (a == 0 || a == 1) {
      break;
    }
    a /= 2;
    t++;
  }
  return t;
}

int N, M, Q;
int A[51], B[51], C[51], D[51];
int E[11];

int dfs(int s) {
  int res = 0;
  if (s == N) {
    REP(i, Q) {
      if (E[B[i]] - E[A[i]] == C[i]) {
        res += D[i];
      }
    }
    return res;
  }
  for (int i = E[s]; i <= M; i++) {
    E[s + 1] = i;
    res = max(res, dfs(s + 1));
  }
  return res;
}

signed main() {
  cin >> N >> M >> Q;
  REP(i, Q) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    A[i]--, B[i]--;
  }
  int ans = 0;
  for (int i = 1; i <= M; i++) {
    E[0] = i;
    ans = max(ans, dfs(0));
  }
  cout << ans << endl;
  return 0;
}