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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vs> vvs;
typedef vector<vc> vvc;
typedef vector<vb> vvb;
using P = pair<ll, ll>;
#define REP(i, n) for (ll i = 0; i < (int)(n); i++)
#define RREP(i, n) for (ll i = (int)(n)-1; i > -1; i--)
#define IN(T, x)                                                               \\
  T x;                                                                         \\
  cin >> x;
#define AIN(T, a, n)                                                           \\
  vector<T> a(n);                                                              \\
  REP(i, n) { cin >> a[i]; }
#define A2IN(T1, a, T2, b, n)                                                  \\
  vector<T1> a(n);                                                             \\
  vector<T2> b(n);                                                             \\
  REP(i, n) { cin >> a[i] >> b[i]; }
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a)                                                               \\
  SORT(a);                                                                     \\
  reverse(ALL(a))
#define pb push_back
#define mp make_pair
#define PF first
#define PS second
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
#define OUT(x) cout << x << endl;
#define OUTD(x) cout << fixed << x << endl;
#define BOUT(b)                                                                \\
  if (b) {                                                                     \\
    OUT("Yes");                                                                \\
  } else {                                                                     \\
    OUT("No");                                                                 \\
  }
const ll MOD = 1000000007;
const ll INF = 10e9;
#define PI 3.14159265369;
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
ll ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
ll ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll ans = INF;
  IN(ll, N);
  IN(ll, M);
  IN(ll, X);
  vll C(N);
  vvll A(N, vll(M));
  REP(i, N) {
    cin >> C.at(i);
    REP(j, M) { cin >> A.at(i).at(j); }
  }
  REP(i, 1 << N) {
    ll money = 0;
    vll skill(M, 0);
    REP(j, N) {
      if (i >> j & 1) {
        bool isOK = true;
        money += C.at(j);
        REP(k, M) {
          skill.at(k) += A.at(j).at(k);
          if (skill.at(k) < X) {
            isOK = false;
          }
        }
        if (isOK) {
          chmin(ans, money);
          break;
        }
      }
    }
  }
  if (ans == INF) {
    OUT(-1);
  } else {
    OUT(ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
using str = string;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vs> vvs;
typedef vector<vc> vvc;
typedef vector<vb> vvb;
using P = pair<ll, ll>;
#define REP(i, n) for (ll i = 0; i < (int)(n); i++)
#define RREP(i, n) for (ll i = (int)(n)-1; i > -1; i--)
#define IN(T, x)                                                               \\
  T x;                                                                         \\
  cin >> x;
#define AIN(T, a, n)                                                           \\
  vector<T> a(n);                                                              \\
  REP(i, n) { cin >> a[i]; }
#define A2IN(T1, a, T2, b, n)                                                  \\
  vector<T1> a(n);                                                             \\
  vector<T2> b(n);                                                             \\
  REP(i, n) { cin >> a[i] >> b[i]; }
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define RSORT(a)                                                               \\
  SORT(a);                                                                     \\
  reverse(ALL(a))
#define pb push_back
#define mp make_pair
#define PF first
#define PS second
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
#define OUT(x) cout << x << endl;
#define OUTD(x) cout << fixed << x << endl;
#define BOUT(b)                                                                \\
  if (b) {                                                                     \\
    OUT("Yes");                                                                \\
  } else {                                                                     \\
    OUT("No");                                                                 \\
  }
const ll MOD = 1000000007;
const ll INF = 10e9;
#define PI 3.14159265369;
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, 1, -1};
ll ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
ll ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll ans = INF;
  vll perm;
  IN(ll, N);
  IN(ll, M);
  IN(ll, X);
  vll C(N);
  vvll A(N, vll(M));
  REP(i, N) {
    cin >> C.at(i);
    REP(j, M) { cin >> A.at(i).at(j); }
    perm.push_back(i);
  }
  vll sum(M, 0);
  REP(i, N) {
    REP(j, M) { sum.at(j) += A.at(i).at(j); }
  }
  bool sumans = true;
  REP(i, M) {
    if (sum.at(i) < X) {
      sumans = false;
    }
  }
  if (!sumans) {
    OUT(-1);
    return 0;
  }
  do {
    vll skill(M, 0LL);
    ll money = 0LL;
    REP(i, N) {
      bool isOK = true;
      REP(j, M) {
        skill.at(j) += A.at(perm.at(i)).at(j);
        if (skill.at(j) < X) {
          isOK = false;
        }
      }
      money += C.at(perm.at(i));
      if (isOK) {
        chmin(ans, money);
        sort(perm.begin() + i + 1, perm.end());
        reverse(perm.begin() + i + 1, perm.end());
        break;
      }
    }
  } while (next_permutation(ALL(perm)));
  OUT(ans);
}