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

#include <bits/stdc++.h>

using namespace std;

#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
// #define int ll
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define pb push_back
#define B begin()
#define E end()
#define sz(A) (ll)(A).size()
#define F first
#define S second
#define loop(z, v) for (auto &z : v)
#define sp << ' ' <<
#define sps << ' '
#define nl "\\n"
// #define nl endl
#define fat 1000000007
// #define fat 998244353

int main() {
  boost;
  ll tt = 1, n, m, k = 0, i, j;
  ll q, l = 0, r = 0, x, y, z, h, a, b, c, d;

  bool poss = true;

  // cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    // cout << "Case #" << _ << ": ";
    cin >> n >> m >> x;

    vector<pair<int, vi>> books(n);
    for (i = 0; i < n; i++) {
      cin >> books[i].F;
      for (j = 0; j < m; j++) {
        cin >> a;

        books[i].S.pb(a);
      }
    }
    ll mask = 0;

    vi lvl(m, 0);
    ll ans = 1e9;
    for (mask = 0; mask < (1 << n); mask++) {
      ll cost = 0;
      fill(lvl.B, lvl.E, 0);

      for (i = 0; i < n; i++) {
        if (mask & (1 << i)) {
          cost += books[i].F;
          for (ll j = 0; j < sz(lvl); j++) {
            lvl[j] += books[i].S[j];
          }
        }
      }

      poss = true;
      for (ll i = 0; i < sz(lvl); i++) {
        if (lvl[i] < x) {
          poss = false;
          break;
        }
      }
      if (poss) {
        ans = min(ans, cost);
      }
    }

    if (ans > 1e8) {
      cout << -1 << nl;
    } else
      cout << ans << nl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define boost                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long int
// #define int ll
#define vi vector<ll>
#define vvi vector<vi>
#define pii pair<int, int>
#define vii vector<pii>
#define pb push_back
#define B begin()
#define E end()
#define sz(A) (ll)(A).size()
#define F first
#define S second
#define loop(z, v) for (auto &z : v)
#define sp << ' ' <<
#define sps << ' '
#define nl "\\n"
// #define nl endl
#define fat 1000000007
// #define fat 998244353

ll ans = 1e9;

void calc(vector<pair<int, vi>> &books, vi &lvl, ll n, ll m, ll x, ll cost,
          vi &vis) {

  bool poss = true;
  for (ll i = 0; i < sz(lvl); i++) {
    if (lvl[i] < x) {
      poss = false;
      break;
    }
  }
  if (poss) {
    ans = min(ans, cost);
  } else {
    for (ll i = 0; i < sz(books); i++) {
      if (vis[i])
        continue;

      for (ll j = 0; j < sz(lvl); j++) {
        lvl[j] += books[i].S[j];
      }

      vis[i] = 1;
      calc(books, lvl, n, m, x, cost + books[i].F, vis);
      vis[i] = 0;

      for (ll j = 0; j < sz(lvl); j++) {
        lvl[j] -= books[i].S[j];
      }
    }
  }
}

int main() {
  boost;
  ll tt = 1, n, m, k = 0, i, j;
  ll q, l = 0, r = 0, x, y, z, h, a, b, c, d;

  bool poss = true;

  // cin >> tt;
  for (int _ = 1; _ <= tt; _++) {
    // cout << "Case #" << _ << ": ";
    cin >> n >> m >> x;

    vector<pair<int, vi>> books(n);
    for (i = 0; i < n; i++) {
      cin >> books[i].F;
      for (j = 0; j < m; j++) {
        cin >> a;

        books[i].S.pb(a);
      }
    }

    vi lvl(m, 0);
    vi vis(n, 0);

    calc(books, lvl, n, m, x, 0, vis);
    if (ans > 1e8) {
      cout << -1 << nl;
    } else
      cout << ans << nl;
  }
  return 0;
}