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

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int n, x, m;
int num[6], l[10], r[10], s[10], ans[6], sum;
bool done;

int Sum() {
  int res = 0;
  rep(i, 0, n) res += num[i];
  return res;
}

bool Check() {
  rep(i, 0, m) {
    int cnt = 0;
    rep(j, l[i], r[i]) cnt += num[j];
    if (cnt != s[i])
      return false;
  }
  done = true;
  return true;
}

void DFS(int i) {
  if (i == n) {
    int sum = 0;
    if (Check() && sum < Sum()) {
      rep(j, 0, n) ans[j] = num[j];
      sum = Sum();
    }
    return;
  }

  rep(j, 0, x + 1) {
    num[i] = j;
    DFS(i + 1);
  }
}

void Solve() {
  cin >> n >> x >> m;
  rep(i, 0, m) {
    cin >> l[i] >> r[i] >> s[i];
    --l[i];
  }

  DFS(0);
  if (done) {
    rep(i, 0, n) {
      cout << ans[i];
      if (i != n - 1)
        cout << ' ';
      else
        cout << endl;
    }
  } else
    cout << -1 << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()
#define SORT(x) sort((x).begin(), (x).end())
#define REP(i, x, y) for (int i = (x); i < (y); ++i)
#define MP(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int n, x, m, l[10], r[10], s[10], num[6];

bool DFS(int q) {
  if (q == m)
    return true;
  int sum = 0;
  REP(i, l[q], r[q] + 1) sum += num[i];
  if (sum >= s[q])
    return false;
  REP(i, l[q], r[q] + 1) {
    if (num[i] + 1 <= x) {
      ++num[i];
      if (s[q] == sum + 1 && DFS(q + 1))
        return true;
      else if (DFS(q))
        return true;
      --num[i];
    }
  }
  return false;
}

void Solve() {
  cin >> n >> x >> m;
  REP(i, 0, m) {
    cin >> l[i] >> r[i] >> s[i];
    --l[i];
    --r[i];
  }
  if (DFS(0))
    REP(i, 0, n) cout << num[i] << (i == n - 1 ? '\\n' : ' ');
  else
    cout << -1 << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}