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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

long long n, m, q;
vector<long long> a(55, 0);
vector<long long> b(55, 0);
vector<long long> c(55, 0);
vector<long long> d(55, 0);

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
    a.at(i)--;
    b.at(i)--;
  }

  long long mans = 0;
  for (int e = 1; e <= m; ++e) {
    for (int f = e; f <= m; ++f) {
      for (int g = f; g <= m; ++g) {
        for (int h = g; h <= m; ++h) {
          for (int i = h; i <= m; ++i) {
            for (int j = i; j <= m; ++j) {
              for (int k = j; k <= m; ++k) {
                for (int l = k; l <= m; ++l) {
                  for (int o = l; o <= m; ++o) {
                    for (int p = o; p <= m; ++p) {
                      int A[10];
                      A[0] = e;
                      A[1] = f;
                      A[2] = g;
                      A[3] = h;
                      A[4] = i;
                      A[5] = j;
                      A[6] = k;
                      A[7] = l;
                      A[8] = o;
                      A[9] = p;
                      long long ans = 0;

                      for (int r = 0; r < q; ++r) {
                        if (A[b.at(r)] - A[a.at(r)] == c.at(r))
                          ans += d.at(r);
                      }
                      chmax(mans, ans);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  cout << mans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

long long n, m, q;
vector<long long> a(55, 0);
vector<long long> b(55, 0);
vector<long long> c(55, 0);
vector<long long> d(55, 0);

int main() {
  cin >> n >> m >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a.at(i) >> b.at(i) >> c.at(i) >> d.at(i);
    a.at(i)--;
    b.at(i)--;
  }

  long long mans = 0;
  for (int e = 1; e <= m; ++e) {
    for (int f = e; f <= m; ++f) {
      for (int g = f; g <= m; ++g) {
        for (int h = g; h <= m; ++h) {
          for (int i = h; i <= m; ++i) {
            for (int j = i; j <= m; ++j) {
              for (int k = j; k <= m; ++k) {
                for (int l = k; l <= m; ++l) {
                  for (int o = l; o <= m; ++l) {
                    for (int p = o; p <= m; ++p) {
                      vector<int> A(10, 0);
                      A.at(0) = e;
                      A.at(1) = f;
                      A.at(2) = g;
                      A.at(3) = h;
                      A.at(4) = i;
                      A.at(5) = j;
                      A.at(6) = k;
                      A.at(7) = l;
                      A.at(8) = o;
                      A.at(9) = p;
                      long long ans = 0;
                      for (int r = 0; r < q; ++r) {
                        if (A.at(b.at(r)) - A.at(a.at(r)) == c.at(r))
                          ans += d.at(r);
                      }
                      chmax(mans, ans);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  cout << mans << endl;
}