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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, m, q;
  cin >> n >> m >> q;
  ll ans = 0;
  vi a(q);
  vi b(q);
  vi c(q);
  vi d(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vi x(10, 1);
  for (ll i = 1; i <= m; i++) {
    x[0] = i;
    for (ll j = i; j <= m; j++) {
      x[1] = j;
      for (ll k = j; k <= m; k++) {
        x[2] = k;
        for (ll l = k; l <= m; l++) {
          x[3] = l;
          for (ll o = l; o <= m; o++) {
            x[4] = o;
            for (ll p = o; p <= m; p++) {
              x[5] = p;
              for (ll r = p; r <= m; r++) {
                x[6] = r;
                for (ll s = r; s <= m; s++) {
                  x[7] = s;
                  for (ll t = s; t <= m; t++) {
                    x[8] = t;
                    for (ll u = t; u <= m; u++) {
                      x[9] = u;
                      ll total = 0;
                      rep(v, q) {
                        if (x[b[v] - 1] - x[a[v] - 1] == c[v]) {
                          total += d[v];
                        }
                      }
                      ans = max(ans, total);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  print(ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, m, q;
  cin >> n >> m >> q;
  ll ans = 0;
  vi a(q);
  vi b(q);
  vi c(q);
  vi d(q);
  rep(i, q) { cin >> a[i] >> b[i] >> c[i] >> d[i]; }
  vi x(10, 1);
  for (ll i = 1; i <= m; i++) {
    x[0] = i;
    for (ll j = i; j <= m; j++) {
      x[1] = j;
      for (ll k = j; k <= m; k++) {
        x[2] = k;
        for (ll l = k; l <= m; k++) {
          x[3] = l;
          for (ll o = l; o <= m; l++) {
            x[4] = o;
            for (ll p = o; p <= m; p++) {
              x[5] = p;
              for (ll r = p; r <= m; r++) {
                x[6] = r;
                for (ll s = r; s <= m; s++) {
                  x[7] = s;
                  for (ll t = s; t <= m; t++) {
                    x[8] = t;
                    for (ll u = t; u <= m; u++) {
                      x[9] = u;
                      ll total = 0;
                      rep(v, q) {
                        if (x[b[v]] - x[a[v]] == x[c[v]]) {
                          total += d[v];
                        }
                      }
                      ans = max(ans, total);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  print(ans);
}