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

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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

vector<lli> A;
lli N, Q, M;
vector<lli> a, b, c, d;

lli ans;

void calc(lli now, lli maxNum) {
  LOG2(now, maxNum);
  if (now == N) {
    lli score = 0;
    REP(i, 0, Q) {
      if (A[a[i]] + c[i] == A[b[i]])
        score += d[i];
    }
    chmax(ans, score);
    return;
  }
  REP(i, maxNum, M + 1) {
    A[now] = i;
    calc(now + 1, i);
  }
}

void func() {

  ans = 0;
  A.resize(N);

  calc(0, 1);
  cout << ans << endl;
}

int main() {
  // cout << fixed << setprecision(5);

  scanf("%lld", &N);
  scanf("%lld", &M);
  scanf("%lld", &Q);
  a.resize(Q);
  b.resize(Q);
  c.resize(Q);
  d.resize(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
    a[i]--, b[i]--;
    scanf("%lld", &c[i]);
    scanf("%lld", &d[i]);
  }
  func();
  return 0;
}


Error: TLE

Fixed submission:

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

#define lli long long int
#define REP(i, s, n) for (int i = s; i < n; i++)
#define NUM 2520
#define INF (1LL << 50)
#define DEBUG 0
#define mp(a, b) make_pair(a, b)
#define SORT(V) sort(V.begin(), V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) #VariableName
#define LOG(x)                                                                 \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << endl;
#define LOG2(x, y)                                                             \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << endl;
#define LOG3(x, y, z)                                                          \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(x) << "=" << x << " " << TO_STRING(y) << "=" << y << " " \\
         << TO_STRING(z) << "=" << z << endl;
#define LOG4(w, x, y, z)                                                       \\
  if (DEBUG)                                                                   \\
    cout << TO_STRING(w) << "=" << w << " " << TO_STRING(x) << "=" << x << " " \\
         << TO_STRING(y) << "=" << y << " " << TO_STRING(z) << "=" << z        \\
         << endl;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

void func(long long N, long long M, long long Q, std::vector<long long> a,
          std::vector<long long> b, std::vector<long long> c,
          std::vector<long long> d) {

  vector<pair<lli, lli>> v;
  REP(i, 0, Q) v.push_back({a[i], b[i]});

  // sort(v.begin(),v.end());

  lli ans = 0;
  for (lli bit = 0; bit < (1LL << Q); bit++) {

    vector<lli> num(N + 1, INF);
    lli maxNum = 1;
    bool ok = true;
    lli sum = 0;
    for (lli i = 0; i < Q; i++) {
      if (!(bit & (1LL << i)))
        continue;
      sum += d[i];
      LOG3(i, v[i].first, v[i].second);
      if (num[v[i].first] == INF) {
        lli tmp = 1;
        REP(i, 0, v[i].first) {
          if (num[i] == INF)
            continue;
          chmax(tmp, num[i]);
        }
        num[v[i].first] = tmp;
      }
      if (num[v[i].second] == INF) {
        num[v[i].second] = num[v[i].first] + c[i];
      } else {
        if (num[v[i].second] != num[v[i].first] + c[i]) {
          ok = false;
          break;
        };
      }
    }

    lli tmp = 1;

    LOG3(tmp, sum, ok);
    REP(i, 0, N) {
      if (num[i] == INF)
        continue;
      LOG3(i, tmp, num[i]);
      if (tmp > num[i])
        ok = false;
      chmax(tmp, num[i]);
    }
    // LOG2(tmp,sum);
    /*if(tmp>M){
        LOG2(tmp,sum);
        ok=false;
    }*/
    LOG3(tmp, sum, ok);
    if (!ok)
      continue;
    chmax(ans, sum);
  }

  cout << ans << endl;
}

int main() {
  // cout << fixed << setprecision(5);

  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  long long Q;
  scanf("%lld", &Q);
  std::vector<long long> a(Q);
  std::vector<long long> b(Q);
  std::vector<long long> c(Q);
  std::vector<long long> d(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &a[i]);
    scanf("%lld", &b[i]);
    a[i]--, b[i]--;
    scanf("%lld", &c[i]);
    scanf("%lld", &d[i]);
  }
  func(N, M, Q, std::move(a), std::move(b), std::move(c), std::move(d));
  return 0;
}