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

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef vector<double> Vec;
typedef vector<Vec> Mat;
typedef pair<ll, ll> P;
typedef pair<double, ll> Pd;
typedef pair<double, double> PD;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;
typedef priority_queue<Pd, vector<Pd>, greater<Pd>> Pd_queue;

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e15;
const double DEL = 1e-6;

#define _GLIBCXX_DEBUG
#define REP(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

void Add(ll &a, ll b) {
  a = (a + b) % mod;
  return;
}

void Pro(ll &a, ll b) {
  a = (a * b) % mod;
  return;
}

struct Kai {
  ll left;
  ll right;
  ll sa;
  ll cost;
  Kai(ll l, ll r, ll s, ll c) : left(l), right(r), sa(s), cost(c) {}
};

vector<Kai> D;

vec hen(vec x) {
  vec ret;
  ll Count = 0;
  rep(i, x.size()) {
    if (!x[i])
      Count++;
    else
      ret.pb(Count);
  }
  return ret;
}

int main() {
  ll N, M, Q;
  cin >> N >> M >> Q;
  rep(i, Q) {
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    Kai kari(a, b, c, d);
    D.pb(kari);
  }
  vec x = {};
  rep(i, M - 1) x.pb(0);
  rep(i, N) x.pb(1);

  ll ans = -INF;
  do {
    vec f = hen(x);
    // for(auto e: x) cout<<e<<' ';
    // cout<<endl;
    // for(auto e: f) cout<<e<<' ';
    ll Count = 0;
    for (auto e : D) {
      if (f[e.right] - f[e.left] == e.sa)
        Count += e.cost;
    }
    // cout<<':'<<Count<<endl;
    ans = max(Count, ans);
  } while (next_permutation(ALL(x)));
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll, ll> P;
typedef priority_queue<P, vector<P>, greater<P>> P_queue;

#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define U_ERASE(V) V.erase(unique(ALL(V)), V.end());

const ll MOD = 998244353;
const ll mod = 1000000007;
const ll INF = 1e10;
vec dx = {1, 0, -1, 0};
vec dy = {0, 1, 0, -1};

ll d[100][4];
ll N, M, Q;
ll ruijou(ll n, ll k) {
  if (k == 0)
    return 1;
  ll x = k / 2;
  long long t = ruijou(n, x);
  t = (t * t);
  if (k % 2 == 1)
    return (n * t);
  else
    return t;
}

int main() {
  cin >> M >> N >> Q;
  rep(i, Q) rep(j, 4) {
    cin >> d[i][j];
    if (j == 0 || j == 1)
      d[i][j]--;
  }
  ll K = ruijou(N, M);
  // cout<<K<<endl;
  ll ans = 0;
  rep(i, K) {
    vec ret;
    ll kari = i;
    bool ikeru = true;
    rep(j, M) {
      ret.pb(kari % N);
      kari /= N;
      if (j > 0)
        if (ret[j] < ret[j - 1])
          ikeru = false;
    }
    // rep(j,M) cout<<ret[j]<<' ';
    // cout<<ikeru<<endl;
    if (ikeru) {

      ll count = 0;
      rep(j, Q) {
        if (ret[d[j][1]] - ret[d[j][0]] == d[j][2])
          count += d[j][3];
      }
      ans = max(ans, count);
      // cout<<count<<endl;
    }
  }
  cout << ans << endl;
}