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

#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  int n, m, q;
  cin >> n >> m >> q;
  vi a(q), b(q), c(q), d(q);
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  vi A(n, 1);
  A[0] = 1;
  ll ans = 0;

  for (A[1] = A[0]; A[1] <= m; A[1]++) {
    for (A[2] = A[1]; A[2] <= m; A[2]++) {
      for (A[3] = A[2]; A[3] <= m; A[3]++) {
        for (A[4] = A[3]; A[4] <= m; A[4]++) {
          for (A[5] = A[4]; A[5] <= m; A[5]++) {
            for (A[6] = A[5]; A[6] <= m; A[6]++) {
              for (A[7] = A[6]; A[7] <= m; A[7]++) {
                for (A[8] = A[7]; A[8] <= m; A[8]++) {
                  for (A[9] = A[8]; A[9] <= m; A[9]++) {
                    ll tot = 0;
                    rep(j, q) {
                      if (A[b[j]] - A[a[j]] == c[j])
                        tot += d[j];
                    }
                    // cout << "loop i: " << i << endl;
                    // for(auto k : A) cout << k << " ";
                    // cout << endl;
                    ans = max(ans, tot);
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

void Main() {
  int n, m, q;
  cin >> n >> m >> q;
  vi a(q), b(q), c(q), d(q);
  rep(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    a[i]--;
    b[i]--;
  }

  int nn = 1;
  rep(i, n - 1) nn *= m;
  // nn = m^(n-1)
  vi A(n);
  A[0] = 0;

  ll ans = 0;
  rep(i, nn) {
    int temp = i;
    ll tot = 0;
    int j = 1;
    bool flag = false;
    while (j < n) {
      // m 進法, n-1 桁
      A[j] = temp % m;
      if (A[j] < A[j - 1]) {
        flag = true;
        break;
      }
      temp = (temp - A[j]) / m;
      j++;
    }
    if (flag)
      continue;
    rep(j, q) {
      if (A[b[j]] - A[a[j]] == c[j])
        tot += d[j];
    }
    // cout << "loop i: " << i << endl;
    // for(auto k : A) cout << k << " ";
    // cout << endl;
    ans = max(ans, tot);
  }
  cout << ans << endl;
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}