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

#include "bits/stdc++.h"
#include <boost/dynamic_bitset.hpp>
#include <boost/multi_array.hpp>
#include <boost/operators.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
namespace adaptor = boost::adaptors;
using index_t = std::size_t;

namespace lib {
using graph_t = std::vector<std::set<index_t>>;
template <class T> using weighted_graph_t = std::vector<std::map<index_t, T>>;

struct max_t {
  template <class T> T operator()(T &&lhs, T &&rhs) const {
    return std::max(lhs, rhs);
  }
};
struct min_t {
  template <class T> T operator()(T &&lhs, T &&rhs) const {
    return std::min(lhs, rhs);
  }
};

template <class T> auto order_by(T func) {
  return [=](auto &&lhs, auto &&rhs) { return func(lhs) < func(rhs); };
}

template <class T> constexpr auto max_v = std::numeric_limits<T>::max();
template <class T> constexpr auto min_v = std::numeric_limits<T>::min();
} // namespace lib

bool next(std::array<int, 10> &ar, int M) {
  if (M == 1) {
    return false;
  }
  for (auto i : boost::irange(10) | adaptor::reversed) {
    if (ar[i] != M) {
      ++ar[i];
      for (auto j : boost::irange(i + 1, 10)) {
        ar[j] = ar[i];
      }
      break;
    }
  }
  return ar[0] == 1;
}

void Main() {
  std::array<int, 10> A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  int N, M, Q;
  std::cin >> N >> M >> Q;
  std::vector<std::tuple<int, int, int, int>> query;
  for (auto _ : boost::irange(Q)) {
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    query.emplace_back(a - 1, b - 1, c, d);
  }
  auto max = lib::min_v<int>;
  do {
    auto value = 0;
    for (auto [a, b, c, d] : query) {
      if (A[b] - A[a] == c) {
        value += d;
      }
    }
    max = std::max(max, value);
  } while (next(A, M));
  std::cout << max << std::endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <boost/dynamic_bitset.hpp>
#include <boost/multi_array.hpp>
#include <boost/operators.hpp>
#include <boost/optional.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/irange.hpp>
namespace adaptor = boost::adaptors;
using index_t = std::size_t;

namespace lib {
using graph_t = std::vector<std::set<index_t>>;
template <class T> using weighted_graph_t = std::vector<std::map<index_t, T>>;

struct max_t {
  template <class T> T operator()(T &&lhs, T &&rhs) const {
    return std::max(lhs, rhs);
  }
};
struct min_t {
  template <class T> T operator()(T &&lhs, T &&rhs) const {
    return std::min(lhs, rhs);
  }
};

template <class T> auto order_by(T func) {
  return [=](auto &&lhs, auto &&rhs) { return func(lhs) < func(rhs); };
}

template <class T> constexpr auto max_v = std::numeric_limits<T>::max();
template <class T> constexpr auto min_v = std::numeric_limits<T>::min();
} // namespace lib

bool next(std::array<int, 10> &ar, int N, int M) {
  for (auto i : boost::irange(N) | adaptor::reversed) {
    if (ar[i] != M) {
      ++ar[i];
      for (auto j : boost::irange(i + 1, N)) {
        ar[j] = ar[i];
      }
      break;
    }
  }
  return ar[0] == 1;
}

void Main() {
  std::array<int, 10> A = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  int N, M, Q;
  std::cin >> N >> M >> Q;
  std::vector<std::tuple<int, int, int, int>> query;
  for (auto i : boost::irange(Q)) {
    int a, b, c, d;
    std::cin >> a >> b >> c >> d;
    query.emplace_back(a - 1, b - 1, c, d);
  }
  auto max = lib::min_v<int>;
  do {
    auto value = 0;
    for (auto [a, b, c, d] : query) {
      if (A[b] - A[a] == c) {
        value += d;
      }
    }
    max = std::max(max, value);
  } while (next(A, N, M));
  std::cout << max << std::endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
}