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

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

typedef struct {
  int ai = 0, b = 0, c = 0, d = 0;
} ten;

int main(void) {
  int n = 0, m = 0, q = 0, s = 0, max = 0;
  cin >> n >> m >> q;
  int a[10];
  vector<ten> t(q);
  a[0] = 1;

  for (int i = 0; i < q; i++) {
    cin >> t[i].ai >> t[i].b >> t[i].c >> t[i].d;
  }

  for (int i = 1; i <= m; i++) {
    a[1] = i;
    for (int j = i; j <= m; j++) {
      a[2] = j;
      for (int k = j; k <= m; k++) {
        a[3] = k;
        for (int l = k; l <= m; l++) {
          a[4] = l;
          for (int o = l; o <= m; o++) {
            a[5] = o;
            for (int p = o; p <= m; p++) {
              a[6] = p;
              for (int r = p; r <= m; r++) {
                a[7] = r;
                for (int u = r; u <= m; u++) {
                  a[8] = u;
                  for (int v = u; v <= m; v++) {
                    a[9] = v;
                    for (int w = 0; w < q; w++) {
                      if (a[t[w].b - 1 + 10 - n] - a[t[w].ai - 1 + 10 - n] ==
                          t[w].c) {
                        s = s + t[w].d;
                      }
                    }
                    if (max < s) {
                      max = s;
                    }
                    s = 0;
                  }
                  if (n <= 2) {
                    break;
                  }
                }
                if (n <= 3) {
                  break;
                }
              }
              if (n <= 4) {
                break;
              }
            }
            if (n <= 5) {
              break;
            }
          }
          if (n <= 6) {
            break;
          }
        }
        if (n <= 7) {
          break;
        }
      }
      if (n <= 8) {
        break;
      }
    }
    if (n <= 9) {
      break;
    }
  }

  cout << max << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef struct {
  int ai = 0, b = 0, c = 0, d = 0;
} ten;

int main(void) {
  int n = 0, m = 0, q = 0, s = 0, max = 0;
  cin >> n >> m >> q;
  int a[10];
  vector<ten> t(q);

  for (int i = 0; i < q; i++) {
    cin >> t[i].ai >> t[i].b >> t[i].c >> t[i].d;
  }

  for (int i = 1; i <= m; i++) {
    a[1] = i;
    for (int j = i; j <= m; j++) {
      a[2] = j;
      for (int k = j; k <= m; k++) {
        a[3] = k;
        for (int l = k; l <= m; l++) {
          a[4] = l;
          for (int o = l; o <= m; o++) {
            a[5] = o;
            for (int p = o; o <= m; p++) {
              a[6] = p;
              for (int r = p; r <= m; r++) {
                a[7] = r;
                for (int u = r; u <= m; u++) {
                  a[8] = u;
                  for (int v = u; v <= m; v++) {
                    a[9] = v;
                    for (int w = 0; w < q; w++) {
                      if (a[t[w].b - 1 + 10 - n] - a[t[w].ai - 1 + 10 - n] ==
                          t[w].c) {
                        s = s + t[w].d;
                      }
                    }
                    if (max < s) {
                      max = s;
                    }
                    s = 0;
                  }
                  if (n <= 2) {
                    break;
                  }
                }
                if (n <= 3) {
                  break;
                }
              }
              if (n <= 4) {
                break;
              }
            }
            if (n <= 5) {
              break;
            }
          }
          if (n <= 6) {
            break;
          }
        }
        if (n <= 7) {
          break;
        }
      }
      if (n <= 8) {
        break;
      }
    }
    if (n <= 9) {
      break;
    }
  }

  cout << max << endl;
  return 0;
}