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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cassert>
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iostream> // cout, endl, cin
#include <limits>
#include <list>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
#define pno cout << "no" << endl
#define pyes cout << "yes" << endl
#define pYes cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pNO cout << "NO" << endl
#define pYES cout << "YES" << endl
#define endl "\\n"
#define pi 3.14159265358979
using namespace std;
using ll = long long;

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vi a(Q), b(Q), c(Q), d(Q);
  rep(i, Q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    --a[i];
    --b[i];
  }

  vector<vector<vector<int>>> comb(N + 1, vvi());
  for (int i = 1; i <= 1; i++) {
    comb[1].push_back({i});
  }
  for (int i = 1; i < N; i++) {
    for (auto &v : comb[i]) {
      // 単調増加なので、末尾の値以上であるものを全て試す
      int b = v.back();
      for (int a = b; a <= M; a++) {
        auto v2 = v;
        v2.push_back(a);
        comb[i + 1].push_back(v2);
      }
    }
  }

  int ans = 0;
  for (auto &v : comb[N]) {
    int res = 0;
    for (int i = 0; i < Q; i++)
      if (v[b[i]] - v[a[i]] == c[i])
        res += d[i];
    ans = max(ans, res);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cassert>
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cmath>
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iostream> // cout, endl, cin
#include <limits>
#include <list>
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ALL(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define int_INF 2147483647
#define pint_INF 2000000000
#define ll_INF 9223372036854775807
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vector<int>>
#define vvll vector<vector<long long>>
#define vvc vector<vector<char>>
#define vll vector<long long>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define sysp system("PAUSE")
#define pno cout << "no" << endl
#define pyes cout << "yes" << endl
#define pYes cout << "Yes" << endl
#define pNo cout << "No" << endl
#define pNO cout << "NO" << endl
#define pYES cout << "YES" << endl
#define endl "\\n"
#define pi 3.14159265358979
using namespace std;
using ll = long long;

class state {
public:
  int now;
  vector<int> cur;

  bool easy_check(int M) {
    for (auto &val : cur) {
      if (val > M) {
        return false;
      }
    }
    return true;
  }

  bool full_check(int M) {
    if (cur[0] > M) {
      return false;
    }
    for (int i = 1; i < cur.size(); i++) {
      if (cur[i] > M || cur[i - 1] > cur[i]) {
        return false;
      }
    }
    return true;
  }
};

int fullcheck(vi &vec, vi &a, vi &b, vi &c, vi &d) {
  int N = vec.size();
  int Q = d.size();
  int res = 0;

  for (int i = 1; i < N; i++) {
    if (vec[i - 1] > vec[i]) {
      return -1;
    }
  }

  for (int qq = 0; qq < Q; qq++) {
    if (vec[b[qq]] - vec[a[qq]] == c[qq]) {
      res += d[qq];
    }
  }
  return res;
}

int main() {
  int N, M, Q;
  cin >> N >> M >> Q;
  vi a(Q), b(Q), c(Q), d(Q);
  for (int i = 0; i < Q; i++) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
    --a[i];
    --b[i];
  }

  state s0;
  s0.now = -1;
  s0.cur = vector<int>(N, 1);
  int ans = fullcheck(s0.cur, a, b, c, d);
  stack<state> st;
  st.push(s0);
  while (!st.empty()) {
    state u = st.top();
    st.pop();
    ans = max(ans, fullcheck(u.cur, a, b, c, d));
    for (int indv = u.now + 1; indv < Q; indv++) {
      // query[indv]使う
      int Abi = u.cur[a[indv]] + c[indv];
      if (Abi > M) {
        // drop
        continue;
      } else {
        vector<int> curv = u.cur;
        curv[b[indv]] = Abi;
        state v;
        v.now = indv;
        v.cur = curv;
        st.push(v);
      }
    }
  }
  cout << ans << endl;
}