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

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge {
  int to, cost;
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  hei(int) a(q), b(q), c(q), d(q);
  int i;
  rep(i, 0, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  int ans = 0;
  int s[10];
  m++;
  rep(s[0], 1, m) {
    rep(s[1], s[0], m) {
      rep(s[2], s[1], m) {
        rep(s[3], s[2], m) {
          rep(s[4], s[3], m) {
            rep(s[5], s[4], m) {
              rep(s[6], s[5], m) {
                rep(s[7], s[6], m) {
                  rep(s[8], s[7], m) {
                    rep(s[9], s[8], m) {
                      int z = 0;
                      int sum = 0;
                      rep(z, 0, q) {
                        if (s[b[z] - 1] - s[a[z] - 1] == c[z])
                          sum += d[z];
                      }
                      ans = max(sum, ans);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge {
  int to, cost;
};

int main() {
  int n, m, q;
  cin >> n >> m >> q;
  hei(int) a(q), b(q), c(q), d(q);
  rep(i, 0, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  int ans = 0;
  stack<string> st;
  rep(i, 1, m + 1) st.push(to_string(i));
  while (!st.empty()) {
    string now = st.top();
    st.pop();
    if (now.size() == n) {
      int sum = 0;
      rep(i, 0, q) {
        if (now[b[i] - 1] - now[a[i] - 1] == c[i])
          sum += d[i];
      }
      ans = max(ans, sum);
    } else {
      for (int i = now[now.size() - 1] - '0'; i <= m; i++) {
        string next = now + to_string(i);
        st.push(next);
      }
    }
  }
  cout << ans << endl;
  return 0;
}