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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll ans = 0;
  ll N, M, Q;
  std::cin >> N >> M >> Q;

  vll a(Q), b(Q), c(Q), d(Q);
  vll p(Q + 1, 0);
  rep(i, 0, Q) {
    std::cin >> a[i] >> b[i] >> c[i] >> d[i];
    p[Q] += d[i];
  }
  rep(i, 0, Q) {
    p[i] = p[Q] - d[i];
    p[Q] = p[i];
  }

  vll A(N + 1, 1);
  while (A[0] == 1) {

    bool flg = true;
    ll idx = N;
    while (idx >= 0) {
      if (A[idx] == M) {

        ll j = 1;
        ll n = 0;
        while (idx > j) {
          if (idx == j) {
            flg = false;
            break;
          } else if (A[idx - j] != M) {
            n = A[idx - j];
            break;
          }
          j++;
        }

        A[idx] = n + 1;
        idx--;
      } else {
        A[idx]++;
        break;
      }
    }
    if (A[0] == 1) {
      rep(i, 1, N) {
        if (A[i] > A[i + 1]) {
          flg = false;
        }
      }
      if (flg) {
        ll tmp = 0;
        rep(i, 0, Q) {
          if (A[b[i]] - A[a[i]] == c[i]) {
            tmp += d[i];
          }
          if (tmp + p[i] < ans) {
            break;
          }
        }
        ans = max(ans, tmp);
      }
    }
    if (idx < 0) {
      A[0]++;
    }
  }

  std::cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll ans = 0;
  ll N, M, Q;
  std::cin >> N >> M >> Q;

  vll a(Q), b(Q), c(Q), d(Q);
  vector<pair<ll, ll>> o(Q);
  vll p(Q + 1, 0);
  rep(i, 0, Q) {
    std::cin >> a[i] >> b[i] >> c[i] >> d[i];
    o[i] = make_pair(d[i], i);
    p[Q] += d[i];
  }
  sort(o.begin(), o.end());
  rep(ii, 0, Q) {
    ll i = o[ii].second;
    p[i] = p[Q] - d[i];
    p[Q] = p[i];
  }

  vll A(N + 1, 1);
  while (A[0] == 1) {

    bool flg = true;
    ll idx = N;
    while (true) {
      if (A[idx] == M) {

        ll j = 1;
        ll n = 0;
        while (idx > j) {
          if (idx == j) {
            flg = false;
            break;
          } else if (A[idx - j] != M) {
            n = A[idx - j];
            break;
          }
          j++;
        }

        A[idx] = n + 1;
        idx--;
      } else {
        A[idx]++;
        break;
      }
    }
    if (A[0] == 1) {
      rep(i, 1, N) {
        if (A[i] > A[i + 1]) {
          flg = false;
        }
      }
      if (flg) {
        ll tmp = 0;
        rep(ii, 0, Q) {
          ll i = o[ii].second;
          if (A[b[i]] - A[a[i]] == c[i]) {
            tmp += d[i];
          }
          if (tmp + p[i] < ans) {
            break;
          }
        }
        ans = max(ans, tmp);
      }
    }
  }

  std::cout << ans << endl;

  return 0;
}