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

#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define lnd '\\n'
// #define reverse(i, n) for(ll i = n-1; i >= 0; --i)
#define from(i, n) for (ll i = 0; i < n; ++i)
#define loop(i, n) for (ll i = 0; i < n; i++)
#define REPN(i, n) for (ll i = 1; i <= n; ++i)
#define ALL(s) (s).begin(), (s).end()
#define rALL(s) (s).rbegin(), (s).rend()
#define sz(s) (int)(s).size()
#define mk make_pair
#define pb push_back
#define sqr(s) ((s) * (s))

using namespace std;

typedef long long ll;
typedef long long int lli;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int ui;

template <class T> T GCD(T a, T b) {
  if (a == 0)
    return b;
  return GCD(b % a, a);
}

template <class T> T LCM(T a, T b) { return a * b / GCM(a, b); }

template <class T> T modpow(T x, T p) {
  T res = 1;
  while (p > 0) {
    if (p & 1)
      res *= x;
    x *= x;
    p /= 2;
  }
  return res;
}

template <class T> T modpow(T x, T p, ll mod) {
  T res = 1;
  while (p > 0) {
    if (p & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p /= 2;
  }
  return res % mod;
}

template <class T> void print(T a[], ll n) {
  for (ll i = 0; i < n; i++)
    std::cout << a[i] << ' ';
  cout << endl;
}

template <class T> void printc(T c) {
  for (auto const &v : c)
    std::cout << v << ' ';
  cout << endl;
}

struct HASH {
  size_t operator()(const pair<long, long> &x) const {
    return hash<long long>()(((long long)x.first) ^
                             (((long long)x.second) << 32));
  }
};

bool pair_sort(const pair<int, int> &p1, const pair<int, int> &p2) {
  if (p1.first != p2.first)
    return p1.first < p2.first;
  else
    return p1.second < p2.second;
}

const int INF = std::numeric_limits<int>::max();
const ll M_INF = std::numeric_limits<ll>::min();
const ll LL_INF = std::numeric_limits<ll>::max();

void solution();
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solution();
  return 0;
}

ll result[200005] = {0};
void fact(ll n) {
  if (n >= 0) {
    result[0] = 1;
    for (ll i = 1; i <= n; ++i) {
      result[i] = i * result[i - 1];
    }
  }
}

void dfs(ll n, ll m, ll i, vector<ll> &v) {
  if (i == n) {
  } else {
    for (int k = 1; k <= m; k++) {
      v.push_back(k);
      dfs(n, m, i + 1, v);
      v.pop_back();
    }
  }
}

void solution() {
  ll n, m, x;
  cin >> n >> m >> x;
  vector<vector<ll>> a(n, vector<ll>(m + 1));
  loop(i, n) {
    loop(j, m + 1) { cin >> a[i][j]; }
  }

  // sort(ALL(a), [](vector<ll> x, vector<ll>y)->bool{
  //   if(x[0] < y[0]) return true;
  //   else return false;
  // });

  ll ans = LL_INF;
  function<ll(int, set<int>, vector<ll>, ll)> dfs =
      [&](int cur, set<int> seen, vector<ll> counts, ll cost) -> ll {
    ll count = 0;
    for (int j = 1; j <= m; j++) {
      if (counts[j] >= x)
        count++;
    }

    if (count == m) {
      ans = min(ans, cost);
      return cost;
    }

    if (seen.size() == n) {
      return -1;
    }

    for (int i = cur; i < n; i++) {
      if (!seen.count(i)) {
        seen.insert(i);
        for (int j = 1; j <= m; j++) {
          counts[j] += a[i][j];
        }
        dfs(i + 1, seen, counts, cost + a[i][0]);
        for (int j = 1; j <= m; j++) {
          counts[j] -= a[i][j];
        }
        seen.erase(i);
      }
    }
    return -1;
  };

  set<int> seen;
  vector<ll> counts(m + 1, 0);
  dfs(0, seen, counts, 0);
  if (ans == LL_INF) {
    ans = -1;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define lnd '\\n'
// #define reverse(i, n) for(ll i = n-1; i >= 0; --i)
#define from(i, n) for (ll i = 0; i < n; ++i)
#define loop(i, n) for (ll i = 0; i < n; i++)
#define REPN(i, n) for (ll i = 1; i <= n; ++i)
#define ALL(s) (s).begin(), (s).end()
#define rALL(s) (s).rbegin(), (s).rend()
#define sz(s) (int)(s).size()
#define mk make_pair
#define pb push_back
#define sqr(s) ((s) * (s))

using namespace std;

typedef long long ll;
typedef long long int lli;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int ui;

template <class T> T GCD(T a, T b) {
  if (a == 0)
    return b;
  return GCD(b % a, a);
}

template <class T> T LCM(T a, T b) { return a * b / GCM(a, b); }

template <class T> T modpow(T x, T p) {
  T res = 1;
  while (p > 0) {
    if (p & 1)
      res *= x;
    x *= x;
    p /= 2;
  }
  return res;
}

template <class T> T modpow(T x, T p, ll mod) {
  T res = 1;
  while (p > 0) {
    if (p & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p /= 2;
  }
  return res % mod;
}

template <class T> void print(T a[], ll n) {
  for (ll i = 0; i < n; i++)
    std::cout << a[i] << ' ';
  cout << endl;
}

template <class T> void printc(T c) {
  for (auto const &v : c)
    std::cout << v << ' ';
  cout << endl;
}

struct HASH {
  size_t operator()(const pair<long, long> &x) const {
    return hash<long long>()(((long long)x.first) ^
                             (((long long)x.second) << 32));
  }
};

bool pair_sort(const pair<int, int> &p1, const pair<int, int> &p2) {
  if (p1.first != p2.first)
    return p1.first < p2.first;
  else
    return p1.second < p2.second;
}

const int INF = std::numeric_limits<int>::max();
const ll M_INF = std::numeric_limits<ll>::min();
const ll LL_INF = std::numeric_limits<ll>::max();

void solution();
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solution();
  return 0;
}

ll result[200005] = {0};
void fact(ll n) {
  if (n >= 0) {
    result[0] = 1;
    for (ll i = 1; i <= n; ++i) {
      result[i] = i * result[i - 1];
    }
  }
}

void dfs(ll n, ll m, ll i, vector<ll> &v) {
  if (i == n) {
  } else {
    for (int k = 1; k <= m; k++) {
      v.push_back(k);
      dfs(n, m, i + 1, v);
      v.pop_back();
    }
  }
}

void solution() {
  ll n, m, x;
  cin >> n >> m >> x;
  vector<vector<ll>> a(n, vector<ll>(m + 1));
  loop(i, n) {
    loop(j, m + 1) { cin >> a[i][j]; }
  }

  // sort(ALL(a), [](vector<ll> x, vector<ll>y)->bool{
  //   if(x[0] < y[0]) return true;
  //   else return false;
  // });

  ll ans = LL_INF;
  function<ll(set<int>, vector<ll>, ll)> dfs =
      [&](set<int> seen, vector<ll> counts, ll cost) -> ll {
    ll count = 0;
    for (int j = 1; j <= m; j++) {
      if (counts[j] >= x)
        count++;
    }

    if (count == m) {
      ans = min(ans, cost);
      return cost;
    }

    if (seen.size() == n) {
      return -1;
    }

    for (int i = 0; i < n; i++) {
      if (!seen.count(i)) {
        seen.insert(i);
        for (int j = 1; j <= m; j++) {
          counts[j] += a[i][j];
        }
        dfs(seen, counts, cost + a[i][0]);
        for (int j = 1; j <= m; j++) {
          counts[j] -= a[i][j];
        }
        seen.erase(i);
      }
    }
    return -1;
  };

  set<int> seen;
  vector<ll> counts(m + 1, 0);
  dfs(seen, counts, 0);
  if (ans == LL_INF) {
    ans = -1;
  }
  cout << ans << endl;
}