alexjercan/bugnet huggingface dataset:
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using Graph = vector<vector<int>>;
using Grid = vector<vector<char>>;
using vin = vector<int>;
using ll = long long;
using vll = vector<ll>;
using vbl = vector<bool>;
using vch = vector<char>;
using pinin = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rell(i, n) for (ll i = 0; i < (ll)(n); i++)
#define sort(x) sort(x.begin(), x.end());
#define rese(x) reverse(x.begin(), x.end());
#define vnn(x, y, name) vector<vector<int>> name(x, vector<int>(y));

string yes = "Yes", no = "No";

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  vin c(n);
  vnn(n, m, a);
  rep(i, n) {
    cin >> c[i];
    rep(j, m) { cin >> a[i][j]; }
  }
  bool ok = false;
  int ans = 1001001001;
  rep(i, 4096) {
    bitset<12> hant(i);
    vin cnt(m, 0);
    int ansa = 0;
    rep(j, n) {
      if (hant[j]) {
        rep(k, m) { cnt[k] += a[j][k]; }
        ansa += c[j];
      }
    }
    bool allok = true;
    rep(j, m) {
      if (cnt[j] < x)
        allok = false;
    }
    if (allok) {
      ok = true;
      ans = min(ans, ansa);
    }
  }
  if (ok)
    cout << ans << endl;
  else
    cout << -1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  vector<vector<int>> data(n, vector<int>(m + 1));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m + 1; j++) {
      cin >> data.at(i).at(j);
    }
  }
  int ans = 3000000;
  for (int tmp = 0; tmp < (1 << n); tmp++) {
    bitset<12> s(tmp);

    vector<int> vec(n);
    int a = 0;
    int b = 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == 1) {
        a += data.at(i).at(0);
        for (int j = 0; j < m; j++) {
          vec.at(j) += data.at(i).at(j + 1);
        }
      }
    }
    for (int i = 0; i < m; i++) {
      if (vec.at(i) < x) {
        b = 0;
        break;
      }
    }
    if (b == 1 && ans > a) {
      ans = a;
    }
  }
  if (ans == 3000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }
}
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int ar[13][13], cost[13], check[13];

int main() {
  // freopen("inputfile.txt","r",stdin);
  ll n, m, h;
  cin >> n >> m >> h;
  for (ll i = 0; i < n; i++) {
    cin >> cost[i];
    for (ll j = 0; j < m; j++) {
      ll k;
      cin >> k;
      ar[i][j] = k;
    }
  }
  ll net = 1 << n;
  ll mi = 10000000;
  for (ll i = 0; i < net; i++) {
    ll sum = 0;
    ll dup[m];
    memset(dup, 0, sizeof(dup));
    for (ll j = 0; j < n; j++) {
      ll f = 1 << j;
      if (f & i) {
        sum += cost[j];
        for (ll k = 0; k < m; k++) {
          dup[k] += ar[j][k];
        }
      }
    }
    bool flag = true;
    for (ll i = 0; i < m; i++) {
      if (dup[i] < h) {
        flag = false;
        break;
      }
    }
    if (flag == true)
      mi = min(mi, sum);
  }
  if (mi == 10000000) {
    cout << -1 << endl;
  } else {
    cout << mi << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
int ar[13][13], cost[13], check[13];

int main() {
  freopen("inputfile.txt", "r", stdin);
  int n, m, h;
  cin >> n >> m >> h;

  for (int i = 0; i < n; i++) {
    cin >> cost[i];
    for (int j = 0; j < m; j++) {
      int k;
      cin >> k;
      ar[i][j] = k;

      check[j] += ar[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    if (check[i] < h) {
      cout << "-1" << endl;
      return 0;
    }
  }
  int net = 1 << n;
  int mi = 1e6;
  for (int i = 1; i < net; i++) {
    int sum = 0;
    int dup[m];
    memset(dup, 0, sizeof(dup));
    for (int j = 0; j < n; j++) {
      int f = 1 << j;
      if (f & i) {

        for (int k = 0; k < m; k++) {
          dup[k] += ar[j][k];
        }
        sum += cost[j];
      }
    }
    bool flag = true;
    for (int i = 0; i < m; i++) {
      if (dup[i] < h) {
        flag = false;
        break;
      }
    }
    if (flag == true)
      mi = min(mi, sum);
  }
  cout << mi << endl;
  return 0;
}
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N, M;
  cin >> N >> M;
  ll X;
  cin >> X;
  vector<vector<ll>> a(N, vector<ll>());
  vector<ll> c;
  rep(i, N) {
    ll C;
    cin >> C;
    c.push_back(C);
    rep(j, M) {
      ll A;
      cin >> A;
      a[i].push_back(A);
    }
  }

  ll ans = INF;
  rep(i, pow(2, N)) {
    ll total = 0;
    vector<ll> tmp(M, 0);
    rep(j, N) {
      int bit = (i >> j);
      if ((bit & 1) == 1) {
        total += c[j];
        rep(k, M) { tmp[k] += a[j][k]; }
      }
    }
    bool isOK = true;
    rep(j, M) {
      if (tmp[j] < X)
        isOK = false;
    }
    if (isOK) {
      ans = min(ans, total);
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

int main() {
  int N, M;
  cin >> N >> M;
  ll X;
  cin >> X;
  vector<vector<ll>> a(N, vector<ll>());
  vector<ll> c;
  rep(i, N) {
    ll C;
    cin >> C;
    c.push_back(C);
    rep(j, M) {
      ll A;
      cin >> A;
      a[i].push_back(A);
    }
  }

  ll ans = INF;
  rep(i, pow(2, N)) {
    ll total = 0;
    vector<ll> tmp(N, 0);
    rep(j, N) {
      int bit = (i >> j);
      if ((bit & 1) == 1) {
        total += c[j];
        rep(k, M) { tmp[k] += a[j][k]; }
      }
    }
    bool isOK = true;
    rep(j, M) {
      if (tmp[j] < X)
        isOK = false;
    }
    if (isOK) {
      ans = min(ans, total);
    }
  }

  if (ans == INF)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<vector<int>> A(N, vector<int>(M));
  vector<int> MAX(M, 0);
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    for (int j = 0; j < M; j++) {
      cin >> A[i][j];
    }
  }

  int ans = 2000000000;
  for (int tmp = 0; tmp < (1 << 12); tmp++) {
    bitset<13> s(tmp);
    if (s.test(N) && N < 12)
      break;
    int cost = 0;
    for (int i = 0; i < N; i++) {
      if (s.test(i)) {
        cost += c[i];
        for (int j = 0; j < M; j++) {
          MAX[j] += A[i][j];
        }
      }
    }
    bool b = true;
    for (int i = 0; i < M; i++) {
      if (MAX[i] < X)
        b = false;
    }
    if (b)
      ans = min(ans, cost);
    MAX.assign(M, 0);
  }

  cout << (ans == 2000000000 ? -1 : ans) << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<vector<int>> A(N, vector<int>(M));
  vector<int> MAX(M, 0);
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    for (int j = 0; j < M; j++) {
      cin >> A[i][j];
    }
  }

  int ans = 2000000000;
  for (int tmp = 0; tmp < (1 << 12); tmp++) {
    bitset<12> s(tmp);
    if (s.test(N) && N < 12)
      break;
    int cost = 0;
    for (int i = 0; i < N; i++) {
      if (s.test(i)) {
        cost += c[i];
        for (int j = 0; j < M; j++) {
          MAX[j] += A[i][j];
        }
      }
    }
    bool b = true;
    for (int i = 0; i < M; i++) {
      if (MAX[i] < X)
        b = false;
    }
    if (b)
      ans = min(ans, cost);
    MAX.assign(M, 0);
  }

  cout << (ans == 2000000000 ? -1 : ans) << endl;
}
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

const int MAX = 1001001001;

int main() {

  int N, M, X;
  std::cin >> N >> M >> X;
  std::vector<int> C(N);
  std::vector<std::vector<int>> A(N, std::vector<int>(M));
  for (int i = 0; i < N; i++) {
    std::cin >> C[i];
    for (int j = 0; j < M; j++) {
      std::cin >> A[i][j];
    }
  }

  int answer = MAX;
  for (int i = 0; i < 1 << N; i++) {
    int cost = 0;
    std::vector<int> skill(M);
    for (int j = 0; j < N; j++) {
      if (i >> j & 1) {
        cost += C[j];
        for (int k = 0; k < M; k++)
          skill[k] += A[j][k];
      }
    }
    int count_error = 0;
    for (int j = 0; j < M; j++) {
      if (skill[j] < X)
        count_error = 1;
    }
    if (count_error == 0)
      answer = std::min(cost, answer);
  }

  if (answer == MAX)
    std::cout << -1 << std::endl;
  else
    std::cout << answer << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

const int MAX = 1001001001;

int main() {

  int N, M, X;
  std::cin >> N >> M >> X;
  std::vector<int> C(N);
  std::vector<std::vector<int>> A(N, std::vector<int>(N));
  for (int i = 0; i < N; i++) {
    std::cin >> C[i];
    for (int j = 0; j < M; j++) {
      std::cin >> A[i][j];
    }
  }

  int answer = MAX;
  for (int i = 0; i < 1 << N; i++) {
    int cost = 0;
    std::vector<int> skill(M);
    for (int j = 0; j < N; j++) {
      if (i >> j & 1) {
        cost += C[j];
        for (int k = 0; k < M; k++)
          skill[k] += A[j][k];
      }
    }
    int count_error = 0;
    for (int j = 0; j < M; j++) {
      if (skill[j] < X)
        count_error = 1;
    }
    if (count_error == 0)
      answer = std::min(cost, answer);
  }

  if (answer == MAX)
    std::cout << -1 << std::endl;
  else
    std::cout << answer << std::endl;
  return 0;
}
Problem p02683 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define rep(i, j) for (int i = 0; i < j; i++)
using namespace std;

int main() {
  int n, m, x;
  cin >> n >> m >> x;
  vector<int> c(n);
  vector<vector<int>> a(n, vector<int>(m));

  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }

  int ans = -1;
  int flag = 0;
  rep(s, 1 << n) {
    int cost = 0;
    vector<int> d(m, 0);
    rep(i, n) {
      if (s >> i & 1) {
        cost += c[i];
        rep(j, m) { d[j] += a[i][j]; }
      }
    }
    bool ok = true;
    rep(i, m) if (d[i] < x) ok = false;
    if (ok) {
      if (flag == 1)
        ans = min(ans, cost);
      else {
        ans = cost;
        flag = 1;
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, j) for (long long i = j; i > 0; i--)
using namespace std;

int n, m, x;
long long ans_money = -1;
int flag = 0;

void dfs(int now_num, vector<int> book_kari, vector<int> need, long long money,
         vector<int> nokori, vector<vector<int>> book) {
  vector<int> ok(m, (0));
  int can = 0;
  if (now_num != 0) {
    money += book[book_kari[book_kari.size() - 1]][0];
    for (int j = 1; j <= m; j++) {
      if (ok[j - 1] == 0) {
        need[j - 1] += book[book_kari[book_kari.size() - 1]][j];
        if (need[j - 1] >= x) {
          ok[j - 1] = 1;
          can += 1;
        }
      }
    }
    if (can == m) {
      if (flag == 0) {
        ans_money = money;
        flag = 1;
        return;
      } else {
        ans_money = min(ans_money, money);
        return;
      }
    }
  }

  if (now_num == n)
    return;

  vector<int> book_zitu(now_num + 1);
  for (int i = 0; i < now_num; i++) {
    book_zitu[i] = book_kari[i];
  }

  for (int i = 0; i < n - now_num; i++) {
    book_zitu[now_num] = nokori[i];
    vector<int> nokori_zitu(0);
    for (int j = 0; j < n - now_num; j++) {
      if (i != j)
        nokori_zitu.emplace_back(nokori[j]);
    }
    dfs(now_num + 1, book_zitu, need, money, nokori_zitu, book);
  }
}

int main() {
  cin >> n >> m >> x;
  vector<vector<int>> book(n, vector<int>(m + 1));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m + 1; j++) {
      cin >> book[i][j];
    }
  }

  vector<int> book_zitu;
  vector<int> need(m, (0));
  vector<int> nokori(n);
  for (int i = 0; i < n; i++)
    nokori[i] = i;

  dfs(0, book_zitu, need, 0, nokori, book);

  cout << ans_money << endl;
  return 0;
}
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
// static const ll MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  int n, m, x;
  cin >> n >> m >> x;

  vector<vector<int>> a(n, vector<int>(m, 0));
  vector<int> c(n);
  for (int in = 0; in < n; in++) {
    cin >> c.at(in);
    for (int im = 0; im < m; im++) {
      cin >> a.at(in).at(im);
    }
  }

  bool flg;
  int ans = 1e9, anstmp;
  for (int ib = 0; ib < (1 << n); ib++) {
    vector<int> algo(m);
    anstmp = 0;
    flg = true;
    for (int in = 0; in < n; in++) {
      if (((ib >> in) & 1)) {
        for (int im = 0; im < m; im++) {
          algo.at(im) += a.at(in).at(im);
        }
        anstmp += c.at(in);
      }
    }
    for (int im = 0; im < m; im++) {
      if (algo.at(im) < x) {
        flg = false;
      }
    }
    if (flg)
      ans = min(ans, anstmp);
  }

  if (ans == 1e9)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
// static const ll MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

int main(void) {
  int n, m, x;
  cin >> n >> m >> x;

  vector<vector<int>> a(n, vector<int>(n, 0));
  vector<int> c(n);
  for (int in = 0; in < n; in++) {
    cin >> c.at(in);
    for (int im = 0; im < m; im++) {
      cin >> a.at(in).at(im);
    }
  }

  bool flg;
  int ans = 1e9, anstmp;
  for (int ib = 0; ib < (1 << n); ib++) {
    vector<int> algo(m);
    anstmp = 0;
    flg = true;
    for (int in = 0; in < n; in++) {
      if (((ib >> in) & 1)) {
        for (int im = 0; im < m; im++) {
          algo.at(im) += a.at(in).at(im);
        }
        anstmp += c.at(in);
      }
    }
    for (int im = 0; im < m; im++) {
      if (algo.at(im) < x) {
        flg = false;
      }
    }
    if (flg)
      ans = min(ans, anstmp);
  }

  if (ans == 1e9)
    ans = -1;
  cout << ans << endl;
  return 0;
}
Problem p02683 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define mod 1000000007
typedef long long ll;

int n, m, x;
vector<vector<int>> vec;
ll smallest = 1000000000000;

void dfs(vector<int> v, vector<bool> flag, int length) {
  if (v.size() == length) {
    vector<int> xVec(m, 0);
    ll sum = 0;
    rep(i, v.size()) {
      rep(j, m + 1) {
        if (j == 0) {
          sum += vec[v[i]][j];
        } else {
          xVec[j - 1] += vec[v[i]][j];
        }
      }
    }
    bool overX = true;
    rep(i, m) {
      if (xVec[i] < x)
        overX = false;
    }
    if (overX) {
      smallest = min(smallest, sum);
    }
  } else {
    for (int i = v.back() + 1; i < n; i++) {
      if (!flag[i]) {
        v.push_back(i);
        flag[i] = true;
        dfs(v, flag, length);
        v.pop_back();
        flag[i] = false;
      }
    }
  }
}

int main() {
  cin >> n >> m >> x;
  rep(i, n) {
    vector<int> tmp;
    int c;
    cin >> c;
    tmp.push_back(c);
    rep(j, m) {
      int num;
      cin >> num;
      tmp.push_back(num);
    }
    vec.push_back(tmp);
  }
  for (int i = 1; i <= n; i++) {
    rep(j, n) {
      vector<int> tmp;
      vector<bool> flagVec(n, false);
      tmp.push_back(j);
      flagVec[j] = true;
      dfs(tmp, flagVec, i);
      tmp.pop_back();
      flagVec[j] = false;
    }
  }
  if (smallest == 1000000000000)
    cout << -1 << endl;
  else
    cout << smallest << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define mod 1000000007
typedef long long ll;

int n, m, x;
vector<vector<int>> vec;
ll smallest = 1000000000000;

void dfs(vector<int> v, vector<bool> flag, int length) {
  if (v.size() == length) {
    vector<int> xVec(m, 0);
    ll sum = 0;
    rep(i, v.size()) {
      rep(j, m + 1) {
        if (j == 0) {
          sum += vec[v[i]][j];
        } else {
          xVec[j - 1] += vec[v[i]][j];
        }
      }
    }
    bool overX = true;
    rep(i, m) {
      if (xVec[i] < x)
        overX = false;
    }
    if (overX) {
      smallest = min(smallest, sum);
    }
  } else {
    rep(i, n) {
      if (!flag[i]) {
        v.push_back(i);
        flag[i] = true;
        dfs(v, flag, length);
        v.pop_back();
        flag[i] = false;
      }
    }
  }
}

int main() {
  cin >> n >> m >> x;
  rep(i, n) {
    vector<int> tmp;
    int c;
    cin >> c;
    tmp.push_back(c);
    rep(j, m) {
      int num;
      cin >> num;
      tmp.push_back(num);
    }
    vec.push_back(tmp);
  }
  for (int i = 1; i <= n; i++) {
    rep(j, n) {
      vector<int> tmp;
      vector<bool> flagVec(n, false);
      tmp.push_back(j);
      flagVec[j] = true;
      dfs(tmp, flagVec, i);
      tmp.pop_back();
      flagVec[j] = false;
    }
  }
  if (smallest == 1000000000000)
    cout << -1 << endl;
  else
    cout << smallest << endl;
  return 0;
}
Problem p02683 (C++) — buggy submission, verdict: Runtime Error

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

int main() {

  int n, m, x;
  cin >> n >> m >> x;

  vector<int> c(n);
  vector<vector<int>> a(n, vector<int>(m));
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (int j = 0; j < m; j++)
      cin >> a[i][j];
  }

  int num = 0b0000'0000'0000;
  int stop = int(pow(2, n));

  int ans = 1200008;
  int nowAns = 0;
  vector<int> ar(m, 0);
  int okCount = 0;

  for (int bit = num; bit < stop; bit++) {
    // nこの参考書の01の組み合わせで足してい
    okCount = 0;
    nowAns = 0;
    for (int t = 0; t < m; t++)
      ar[t] = 0;
    // cout << "bit:" << bitset<5>(bit) << endl;
    // 参考書jのそれぞれの習熟度を足していく
    for (int j = 0; j < n; j++) {
      if (bit & (1 << j)) {
        // cout << bitset<5>(1<<j) << endl;
        // cout << "入った" << endl;
        nowAns += c[j];
        for (int k = 0; k < m; k++) {
          ar[k] += a[j][k];
        }
      }
    }
    for (int f = 0; f < m; f++) {
      if (ar[f] >= x)
        okCount++;
      // cout << ar[f] << ",";
    } // cout << endl;
    // cout << "okCount:" << okCount << endl;
    if (okCount == m) {
      // cout << "入った01" << endl;
      ans = min(ans, nowAns);
    }
    // cout << "ans:" << ans << endl << endl;
  }

  if (ans == 1200008)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {

  int n, m, x;
  int ans = 12 * 100001;
  cin >> n >> m >> x;

  vector<vector<int>> a(n, vector<int>(m));
  vector<int> c(n);

  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }

  vector<int> ar(m);
  vector<int> points(m, 0);
  int count = 0;
  int nowAns;

  for (int i = 0; i < m; i++)
    ar[i] = i;
  do {
    nowAns = 0;
    /*cout << endl;
    for(int t=0;t<m;t++) cout << ar[t];
    cout << endl;*/

    for (int k = 0; k < m; k++)
      points[k] = 0;
    for (int i = 0; i < n; i++) {
      nowAns += c[ar[i]];
      count = 0;
      for (int j = 0; j < m; j++) {
        points[j] += a[ar[i]][j];
        if (points[j] >= x)
          count++;
      }

      /*for(int t=0;t<m;t++){
        cout << points[t] <<" ";
      }cout << endl;*/

      if (count == m) {
        ans = min(ans, nowAns);
        break;
      }
    }
  } while (next_permutation(ar.begin(), ar.end()));

  if (ans == 12 * 100001)
    cout << "-1" << endl;
  else
    cout << ans << endl;
}
Problem p02683 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;
int n, m, x;
int c[2000], a[2000][2000], vis[2000], ans = INT_MAX;
int temp[2000] = {0};
void dfs(int temp_ans, int pos) {
  if (temp_ans >= ans)
    return;
  int flag = 1;
  for (int i = 1; i <= m; i++) {
    if (temp[i] < x) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    ans = min(ans, temp_ans);
    return;
  }
  for (int i = pos + 1; i <= n; i++) {
    if (vis[i] == 0) {
      for (int j = 1; j <= m; j++) {
        temp[j] += a[i][j];
      }
      vis[i] = 1;
      dfs(c[i] + temp_ans, i);
      vis[i] = 0;
      for (int j = 1; j <= m; j++) {
        temp[j] -= a[i][j];
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> x;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  }
  dfs(0, 0);
  if (ans == INT_MAX)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, x;
int c[2000], a[2000][2000], vis[2000], ans = INT_MAX;
int temp[2000] = {0};
void dfs(int temp_ans) {
  if (temp_ans >= ans)
    return;
  int flag = 1;
  for (int i = 1; i <= m; i++) {
    if (temp[i] < x) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    ans = min(ans, temp_ans);
    return;
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      for (int j = 1; j <= m; j++) {
        temp[j] += a[i][j];
      }
      vis[i] = 1;
      dfs(c[i] + temp_ans);
      vis[i] = 0;
      for (int j = 1; j <= m; j++) {
        temp[j] -= a[i][j];
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m >> x;
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
    for (int j = 1; j <= m; j++)
      cin >> a[i][j];
  }
  dfs(0);
  if (ans == INT_MAX)
    cout << -1 << endl;
  else
    cout << ans << endl;
  return 0;
}