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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define int long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define fn for (int i = 0; i < n; i++)
#define fn1 for (int i = 1; i <= n; i++)
#define fm for (int j = 0; j < m; j++)
#define fm1 for (int j = 1; j <= m; j++)
#define fi first
#define se second
#define endl '\\n'
#define PI 3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int N = 1e2 + 5;
const int INF = 1e18L;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
int ar[N][N];
int dp[N];
// bc N nhi dekha :/
signed main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  IOS;
  int T = 1;
  // cin >> T;
  while (T--) {
    int n, m, x;
    cin >> n >> m >> x;
    int ans = INF;
    m++;
    fn {
      fm { cin >> ar[i][j]; }
    }
    for (int mask = 0; mask < (1ll << n); mask++) {
      int cur = 0;
      vector<int> cost(m - 1, 0);
      for (int i = 0; i < n; i++) {
        if (((mask >> i) & 1)) {
          cur += ar[i][0];
          for (int j = 1; j < m; j++) {
            cost[j - 1] += ar[i][j];
          }
        }
      }
      int f = 1;
      for (int j = 1; j < m; j++) {
        if (cost[j - 1] < x)
          f = 0;
      }
      if (f)
        ans = min(ans, cur);
    }
    if (ans == INF)
      ans = -1;
    cout << ans << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define int long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define fn for (int i = 0; i < n; i++)
#define fn1 for (int i = 1; i <= n; i++)
#define fm for (int j = 0; j < m; j++)
#define fm1 for (int j = 1; j <= m; j++)
#define fi first
#define se second
#define endl '\\n'
#define PI 3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
const int N = 2e6 + 5;
const int INF = 1e18L;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
int ar[15][15], c[15], val[15];
int ans = INF;
int n, m, k;
int dp[N][15];
int solve(int mid, int x) {
  bool bl = 1;
  if (x >= n)
    return INF;
  if (dp[mid][x] != -1)
    return dp[mid][x];
  for (int i = 0; i < m; i++) {
    if (val[i] < k) {
      bl = 0;
      break;
    }
  }
  if (bl) {
    ans = min(mid, ans);
  }
  for (int i = x; i < n; i++) {
    for (int j = 0; j < m; j++) {
      val[j] += ar[i][j];
    }
    int ha = solve(mid + c[i], x + 1);
    for (int j = 0; j < m; j++) {
      val[j] -= ar[i][j];
    }
  }
  dp[mid][x] = ans;
}
signed main() {
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  IOS;
  int T = 1;
  // cin >> T;
  while (T--) {
    memset(dp, -1, sizeof(dp));
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++) {
      cin >> c[i];
      for (int j = 0; j < m; j++) {
        cin >> ar[i][j];
      }
    }
    int a = solve(0, 0);
    if (ans == INF) {
      ans = -1;
    }
    cout << ans << '\\n';
  }
  return 0;
}