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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (auto i = (o); (i) < (n); ++(i))
#define FOR1(i, n) FOR(i, 1, n + 1)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (auto i = (n)-1; (i) >= 0; --i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
#define fi first
#define se second
#define ll long long
#define int long long
#define V2(name, h, w) vector<vector<int>> name((h), vector<int>(w));
#define myCeil(dividend, divisor) ceil((double)(dividend) / (double)(divisor))
#define myFloor(dividend, divisor) floor((double)(dividend) / (double)(divisor))
#define calcDigits(num) floor(log10(num)) + 1

#define dl(line) cerr << "RUN: until " << (line) << "th line." << endl;
#define dv(name, value) cerr << (name) << " is " << (value) << endl;
#define da(v)                                                                  \\
  cerr << "[";                                                                 \\
  REPI(itr, v) { cerr << *itr << (itr != --v.end() ? ", " : ""); }             \\
  cerr << "]" << endl;
#define dpa(v)                                                                 \\
  cerr << "[ " << endl;                                                        \\
  REPI(itr, v) {                                                               \\
    cerr << "  [" << (*itr).first << ", " << (*itr).second << ']'              \\
         << (itr != --v.end() ? "," : "") << endl;                             \\
  }                                                                            \\
  cerr << "]" << endl;

signed main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<int> C(N);
  V2(A, N, M);
  REP(x, N) {
    REP(y, M + 1) {
      if (y == 0) {
        cin >> C.at(x);
      } else {
        cin >> A.at(x).at(y - 1);
      }
    }
  }

  vector<int> bits(N + 1);
  int minValue = __LONG_LONG_MAX__;
  REP(bit, (1 << N)) {
    REP(x, N) {
      if (bit & (1 << x))
        bits.at(x + 1) = true;
      else
        bits.at(x + 1) = false;
    }

    vector<int> tmpA(M);
    int valueSum = 0;
    REP(z, N) {
      if (bits.at(z + 1)) {
        REP(x, M) { tmpA.at(x) += A.at(z).at(x); }
        valueSum += C.at(z);
      }
    }
    bool isValid = true;
    REP(x, M) {
      if (tmpA.at(x) < X)
        isValid = false;
    }

    // da(bits);
    // da(tmpA);
    // dv("valueSum", valueSum);
    // cerr << endl;
    if (isValid) {
      minValue = min(minValue, valueSum);
    }
  }

  cout << (minValue != __LONG_LONG_MAX__ ? minValue : -1) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, o, n) for (auto i = (o); (i) < (n); ++(i))
#define FOR1(i, n) FOR(i, 1, n + 1)
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (auto i = (n)-1; (i) >= 0; --i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define REPIR(itr, v) for (auto itr = v.rbegin(); itr != v.rend(); ++itr)
#define fi first
#define se second
#define ll long long
#define int long long
#define V2(name, h, w) vector<vector<int>> name((h), vector<int>(w));
#define myCeil(dividend, divisor) ceil((double)(dividend) / (double)(divisor))
#define myFloor(dividend, divisor) floor((double)(dividend) / (double)(divisor))
#define calcDigits(num) floor(log10(num)) + 1

#define dl(line) cerr << "RUN: until " << (line) << "th line." << endl;
#define dv(name, value) cerr << (name) << " is " << (value) << endl;
#define da(v)                                                                  \\
  cerr << "[";                                                                 \\
  REPI(itr, v) { cerr << *itr << (itr != --v.end() ? ", " : ""); }             \\
  cerr << "]" << endl;
#define dpa(v)                                                                 \\
  cerr << "[ " << endl;                                                        \\
  REPI(itr, v) {                                                               \\
    cerr << "  [" << (*itr).first << ", " << (*itr).second << ']'              \\
         << (itr != --v.end() ? "," : "") << endl;                             \\
  }                                                                            \\
  cerr << "]" << endl;

signed main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<int> C(N);
  V2(A, N, M);
  REP(x, N) {
    REP(y, M + 1) {
      if (y == 0) {
        cin >> C.at(x);
      } else {
        cin >> A.at(x).at(y - 1);
      }
    }
  }

  vector<int> stairV(0);
  REP(x, N) stairV.push_back(x);

  int minPriceSum = __LONG_LONG_MAX__;
  do {
    int priceSum = 0;
    vector<int> tmpRikaidos(M);
    for (const auto &e : stairV) {
      bool isValid = true;
      REP(x, M) {
        if (tmpRikaidos.at(x) < X)
          isValid = false;
      }
      if (isValid)
        break;

      REP(x, M) tmpRikaidos.at(x) += A.at(e).at(x);
      priceSum += C.at(e);
    }
    bool isValid = true;
    REP(x, M) {
      if (tmpRikaidos.at(x) < X)
        isValid = false;
    }
    if (isValid) {
      minPriceSum = min(minPriceSum, priceSum);
    }
  } while (next_permutation(stairV.begin(), stairV.end()));

  cout << (minPriceSum != __LONG_LONG_MAX__ ? minPriceSum : -1) << endl;
}