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

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

#define mp make_pair
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define ins insert
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define sz(X) ((int)X.size())
#define ub upper_bound
#define lb lower_bound
#define rsz resize
#define minel(X) min_element(all(X))
#define maxel(X) max_element(all(X))
#define make_unique(X)                                                         \\
  sort(all(X));                                                                \\
  (X).resize(distance((X).begin(), unique(all(X))));

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto a : x)

// TYPES
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<db, db> pdb;
typedef pair<string, string> pss;

typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pss> vpss;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;

template <class T> using uset = unordered_set<T>;
template <class T> using mset = multiset<T>;
template <class T> using umset = unordered_multiset<T>;
template <class A, class B> using umap = unordered_map<A, B>;
template <class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <class T> using maxpq = priority_queue<T, vector<T>, less<T>>;

// FUNCTIONS
const int MOD = 1e9 + 7;
template <class T> void add(T &a, const T &b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
template <class T> void sub(T &a, const T &b) {
  a -= b;
  if (a >= MOD)
    a -= MOD;
}
template <class T> void mult(T &a, const T &b) {
  a *= b;
  if (a >= MOD)
    a -= MOD;
}
template <class T> bool amin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool amax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <typename T> T sqr(T x) { return x * x; }
template <typename T> T cdiv(T a, T b) {
  return a / b + !(a < 0 || a % b == 0);
}
template <typename T> T lcm(T a, T b) { return (a * b) / __gcd(a, b); }

// INPUT //
template <class A> void re(complex<A> &c);
template <class A, class B> void re(pair<A, B> &p);
template <class A> void re(vector<A> &v);
template <class A, size_t SZ> void re(array<A, SZ> &a);

template <class T> void re(T &x) { cin >> x; }
void re(db &d) {
  string t;
  re(t);
  d = stod(t);
}
void re(ld &d) {
  string t;
  re(t);
  d = stold(t);
}
template <class H, class... T> void re(H &h, T &...t) {
  re(h);
  re(t...);
}

template <class A> void re(complex<A> &c) {
  A a, b;
  re(a, b);
  c = {a, b};
}
template <class A, class B> void re(pair<A, B> &p) { re(p.ff, p.ss); }
template <class A> void re(vector<A> &x) { trav(&a, x) re(a); }
template <class A, size_t SZ> void re(array<A, SZ> &x) { trav(&a, x) re(a); }

// OUTPUT
template <class A> void pr(A x) { cout << x; }
template <class H, class... T> void pr(const H &h, const T &...t) {
  pr(h);
  pr(t...);
}
void pln() { pr("\\n"); }
template <class H, class... T> void pln(const H &h, const T &...t) {
  pr(h);
  pln(t...);
}
void ps() { pr("\\n"); } // print w/ spaces
template <class H, class... T> void ps(const H &h, const T &...t) {
  pr(h);
  if (sizeof...(t))
    pr(" ");
  ps(t...);
}

template <class A> void pr(vector<A> x) { trav(a, x) pr(a, " "); }
template <class A> void pln(vector<A> x) {
  trav(a, x) pr(a, " ");
  pln();
}

// TO_STRING
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(bool b) { return b ? "true" : "false"; }
string ts(const char *s) { return (string)s; }
string ts(string s) { return s; }
template <class A> string ts(complex<A> c) {
  stringstream ss;
  ss << c;
  return ss.str();
}
string ts(vector<bool> v) {
  string res = "{";
  F0R(i, sz(v)) res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ> string ts(bitset<SZ> b) {
  string res = "";
  F0R(i, SZ) res += char('0' + b[i]);
  return res;
}
template <class A, class B> string ts(pair<A, B> p);
template <class T> string ts(T v) { // containers with begin(), end()
  bool fst = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!fst)
      res += ", ";
    fst = 0;
    res += ts(x);
  }
  res += "}";
  return res;
}
template <class A, class B> string ts(pair<A, B> p) {
  return "(" + ts(p.ff) + ", " + ts(p.ss) + ")";
}

// FILE I/O
#pragma GCC diagnostic ignored "-Wunused-result"
void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void unsyncIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
void setIO(string s = "") {
  unsyncIO();
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

// DEBUG
template <class H> void DBG(const char *sdbg, H h) {
  cerr << sdbg << " = " << ts(h) << "]" << endl;
}
template <class H, class... T> void DBG(const char *sdbg, H h, T... t) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << " = " << ts(h) << ", ";
  DBG(sdbg + 1, t...);
}

#ifdef LOCAL // compile with -DLOCAL
#define dbg(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [", DBG(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) 0
#endif

// VARIABLES
const db pi = acos(-1.0);
const int INF = 1e9 + 7;
const ll llINF = ((ll)1e18) + 7LL;
const int mxN = 1e5 + 7;

// ************************************************************* CODE
// ********************************************************** //

vi costs;
vvi gain;
int n, m, target;

int rec(int pos, vi &algos) {
  if (*minel(algos) >= target)
    return 0;

  if (pos >= n)
    return -INF;

  vi new_algos = algos;
  F0R(j, m) { new_algos[j] += gain[pos][j]; }

  int choose, skip;
  choose = rec(pos + 1, new_algos) + costs[pos];
  skip = rec(pos + 1, algos);

  return min((choose < 0 ? INF : choose), (skip < 0 ? INF : skip));
}

int main() {
  setIO();

  re(n, m, target);

  costs.rsz(n);
  gain.rsz(n);

  F0R(i, n) {
    re(costs[i]);
    gain[i].rsz(m);
    F0R(j, m) { re(gain[i][j]); }
  }

  vi x(m, 0);
  int ans = rec(0, x);
  pln(ans != INF ? ans : -1);
  // you should actually read the stuff at the bottom
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smthn instead of nothing and stay organized
 * WRITE STUFF DOWN
 */


Error: TLE

Fixed submission:

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

#define mp make_pair
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define ins insert
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define sz(X) ((int)X.size())
#define ub upper_bound
#define lb lower_bound
#define rsz resize
#define minel(X) min_element(all(X))
#define maxel(X) max_element(all(X))
#define make_unique(X)                                                         \\
  sort(all(X));                                                                \\
  (X).resize(distance((X).begin(), unique(all(X))));

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i, 0, a)
#define trav(a, x) for (auto a : x)

// TYPES
typedef long long ll;
typedef long double ld;
typedef double db;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef pair<db, db> pdb;
typedef pair<string, string> pss;

typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<pss> vpss;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vb> vvb;

template <class T> using uset = unordered_set<T>;
template <class T> using mset = multiset<T>;
template <class T> using umset = unordered_multiset<T>;
template <class A, class B> using umap = unordered_map<A, B>;
template <class T> using minpq = priority_queue<T, vector<T>, greater<T>>;
template <class T> using maxpq = priority_queue<T, vector<T>, less<T>>;

// FUNCTIONS
const int MOD = 1e9 + 7;
template <class T> void add(T &a, const T &b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
}
template <class T> void sub(T &a, const T &b) {
  a -= b;
  if (a >= MOD)
    a -= MOD;
}
template <class T> void mult(T &a, const T &b) {
  a *= b;
  if (a >= MOD)
    a -= MOD;
}
template <class T> bool amin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool amax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template <typename T> T sqr(T x) { return x * x; }
template <typename T> T cdiv(T a, T b) {
  return a / b + !(a < 0 || a % b == 0);
}
template <typename T> T lcm(T a, T b) { return (a * b) / __gcd(a, b); }

// INPUT //
template <class A> void re(complex<A> &c);
template <class A, class B> void re(pair<A, B> &p);
template <class A> void re(vector<A> &v);
template <class A, size_t SZ> void re(array<A, SZ> &a);

template <class T> void re(T &x) { cin >> x; }
void re(db &d) {
  string t;
  re(t);
  d = stod(t);
}
void re(ld &d) {
  string t;
  re(t);
  d = stold(t);
}
template <class H, class... T> void re(H &h, T &...t) {
  re(h);
  re(t...);
}

template <class A> void re(complex<A> &c) {
  A a, b;
  re(a, b);
  c = {a, b};
}
template <class A, class B> void re(pair<A, B> &p) { re(p.ff, p.ss); }
template <class A> void re(vector<A> &x) { trav(&a, x) re(a); }
template <class A, size_t SZ> void re(array<A, SZ> &x) { trav(&a, x) re(a); }

// OUTPUT
template <class A> void pr(A x) { cout << x; }
template <class H, class... T> void pr(const H &h, const T &...t) {
  pr(h);
  pr(t...);
}
void pln() { pr("\\n"); }
template <class H, class... T> void pln(const H &h, const T &...t) {
  pr(h);
  pln(t...);
}
void ps() { pr("\\n"); } // print w/ spaces
template <class H, class... T> void ps(const H &h, const T &...t) {
  pr(h);
  if (sizeof...(t))
    pr(" ");
  ps(t...);
}

template <class A> void pr(vector<A> x) { trav(a, x) pr(a, " "); }
template <class A> void pln(vector<A> x) {
  trav(a, x) pr(a, " ");
  pln();
}

// TO_STRING
#define ts to_string
string ts(char c) { return string(1, c); }
string ts(bool b) { return b ? "true" : "false"; }
string ts(const char *s) { return (string)s; }
string ts(string s) { return s; }
template <class A> string ts(complex<A> c) {
  stringstream ss;
  ss << c;
  return ss.str();
}
string ts(vector<bool> v) {
  string res = "{";
  F0R(i, sz(v)) res += char('0' + v[i]);
  res += "}";
  return res;
}
template <size_t SZ> string ts(bitset<SZ> b) {
  string res = "";
  F0R(i, SZ) res += char('0' + b[i]);
  return res;
}
template <class A, class B> string ts(pair<A, B> p);
template <class T> string ts(T v) { // containers with begin(), end()
  bool fst = 1;
  string res = "{";
  for (const auto &x : v) {
    if (!fst)
      res += ", ";
    fst = 0;
    res += ts(x);
  }
  res += "}";
  return res;
}
template <class A, class B> string ts(pair<A, B> p) {
  return "(" + ts(p.ff) + ", " + ts(p.ss) + ")";
}

// FILE I/O
#pragma GCC diagnostic ignored "-Wunused-result"
void setIn(string s) { freopen(s.c_str(), "r", stdin); }
void setOut(string s) { freopen(s.c_str(), "w", stdout); }
void unsyncIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
void setIO(string s = "") {
  unsyncIO();
  if (sz(s)) {
    setIn(s + ".in"), setOut(s + ".out");
  } // for USACO
}

// DEBUG
template <class H> void DBG(const char *sdbg, H h) {
  cerr << sdbg << " = " << ts(h) << "]" << endl;
}
template <class H, class... T> void DBG(const char *sdbg, H h, T... t) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << " = " << ts(h) << ", ";
  DBG(sdbg + 1, t...);
}

#ifdef LOCAL // compile with -DLOCAL
#define dbg(...)                                                               \\
  cerr << "LINE(" << __LINE__ << ") -> [", DBG(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) 0
#endif

// VARIABLES
const db pi = acos(-1.0);
const int INF = 1e9 + 7;
const ll llINF = ((ll)1e18) + 7LL;
const int mxN = 1e5 + 7;

// ************************************************************* CODE
// ********************************************************** //

vi costs;
vvi gain;
int n, m, target;

int rec(vi &algos, uset<int> &used) {
  if (*minel(algos) >= target)
    return 0;

  if (sz(used) == n)
    return -INF;

  vi ret;
  F0R(i, n) {
    if (used.count(i))
      continue;

    vi new_algos = algos;
    F0R(j, m) { new_algos[j] += gain[i][j]; }
    uset<int> new_used = used;
    new_used.ins(i);
    ret.pb(rec(new_algos, new_used) + costs[i]);
  }
  return *minel(ret);
}

int main() {
  setIO();

  re(n, m, target);

  costs.rsz(n);
  gain.rsz(n);

  F0R(i, n) {
    re(costs[i]);
    gain[i].rsz(m);
    F0R(j, m) { re(gain[i][j]); }
  }

  vi x(m, 0);
  uset<int> used;
  int ans = rec(x, used);
  pln(ans >= 0 ? ans : -1);
  // you should actually read the stuff at the bottom
}
/* stuff you should look for
 * int overflow, array bounds
 * special cases (n=1?)
 * do smthn instead of nothing and stay organized
 * WRITE STUFF DOWN
 */