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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using lint = long long; //
template <typename T> T sq(T x) { return x * x; }
vector<int> buf;
int max_ans = 0;
void dfs(int i, const int size, int range_start, const int range_end,
         vector<int> A, int a[50][4], int Q, int max_limt) {
  if (i == size) {
    for (int i = 0; i < size; ++i) {
      A.at(i) = buf[i];
    }
    int ans = 0;
    rep(j, Q) {
      int ai = a[j][0];
      int bi = a[j][1];
      int ci = a[j][2];
      // cout << "aaa" << A.at(ai-1) << A.at(bi-1) << ci <<endl;
      if (A.at(bi - 1) - A.at(ai - 1) == ci) { // a.at(j).at(2)){
        ans += a[j][3];
        // cout << ans << endl;
      }
    }
    if (ans > max_ans) {
      max_ans = ans;
      if (max_ans == max_limt)
        return;
    }
  } else {
    for (int j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      range_start = j;
      dfs(i + 1, size, range_start, range_end, A, a, Q, max_limt);
    }
  }
}
signed main() {
  int N, M, Q;
  cin >> N >> M >> Q;

  int a[50][4];
  rep(i, Q) {
    cin >> a[i][0];
    cin >> a[i][1];
    cin >> a[i][2];
    cin >> a[i][3];
  }
  // cout << a[0][0] << endl;
  vector<int> A(N);
  int size = N;
  int range_start = 1;
  int range_end = M;
  buf.resize(size);
  int max_limt = 0;
  rep(i, Q) { max_limt += a[i][3]; }
  dfs(0, size, range_start, range_end, A, a, Q, max_limt);
  cout << max_ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
using namespace std;
using lint = long long; //
template <typename T> T sq(T x) { return x * x; }
vector<int> buf;
int max_ans = 0;
void dfs(int i, const int size, const int range_start, const int range_end,
         vector<int> A, int a[50][4], int Q, int max_limt) {
  if (i == size) {
    for (int i = 0; i < size; ++i) {
      A.at(i) = buf[i];
    }
    int ans = 0;
    sort(all(A));

    // cout << A.size() << endl;
    rep(j, Q) {
      int ai = a[j][0];
      int bi = a[j][1];
      int ci = a[j][2];
      // cout << "aaa" << A.at(ai-1) << A.at(bi-1) << ci <<endl;
      if (A.at(bi - 1) - A.at(ai - 1) == ci) { // a.at(j).at(2)){
        ans += a[j][3];
        // cout << ans << endl;
      }
    }
    if (ans > max_ans) {
      max_ans = ans;
      if (max_ans == max_limt)
        return;
    }
  } else {
    for (int j = range_start; j <= range_end; ++j) {
      buf[i] = j;
      dfs(i + 1, size, range_start, range_end, A, a, Q, max_limt);
    }
  }
}
signed main() {
  int N, M, Q;
  cin >> N >> M >> Q;

  int a[50][4];
  rep(i, Q) {
    cin >> a[i][0];
    cin >> a[i][1];
    cin >> a[i][2];
    cin >> a[i][3];
  }
  // cout << a[0][0] << endl;
  vector<int> A(N);
  int size = N;
  int range_start = 1;
  int range_end = M;
  buf.resize(size);
  int max_limt = 0;
  rep(i, Q) { max_limt += a[i][3]; }
  dfs(0, size, range_start, range_end, A, a, Q, max_limt);
  cout << max_ans << endl;

  return 0;
}