Problem p02683 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, m, x, c[12] = {}, a[12][12] = {}, ans = 1e7;

void dfs(ll cur, vector<ll> u, ll cst) {
  if (cur < n - 1) {
    dfs(cur + 1, u, cst);
    rep(i, m) u[i] += a[cur][i];
    dfs(cur + 1, u, cst + c[cur]);
  } else {
    bool ok = true;
    rep(i, m) {
      if (u[i] < x)
        ok = false;
    }
    if (ok)
      ans = min(ans, cst);
    rep(i, m) u[i] += a[n - 1][i];
    ok = true;
    rep(i, m) {
      if (u[i] < x)
        ok = false;
    }
    if (ok)
      ans = min(ans, cst + c[n - 1]);
  }
}

int main() {
  cin >> n >> m >> x;
  vector<ll> u(m);
  rep(i, n) {
    cin >> c[i];
    rep(j, m) cin >> a[i][j];
  }
  dfs(0, u, 0);
  if (ans < 1e7) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll n, m, x, c[12] = {}, a[12][12] = {}, ans = 1e7;

void dfs(ll cur, vector<ll> u, ll cst) {
  if (cur < n - 1) {
    dfs(cur + 1, u, cst);
    rep(i, m) u[i] += a[cur][i];
    dfs(cur + 1, u, cst + c[cur]);
  } else {
    bool ok = true;
    rep(i, m) {
      if (u[i] < x)
        ok = false;
    }
    if (ok)
      ans = min(ans, cst);
    rep(i, n) u[i] += a[n - 1][i];
    ok = true;
    rep(i, m) {
      if (u[i] < x)
        ok = false;
    }
    if (ok)
      ans = min(ans, cst + c[n - 1]);
  }
}

int main() {
  cin >> n >> m >> x;
  vector<ll> u(n);
  rep(i, n) {
    cin >> c[i];
    rep(j, m) cin >> a[i][j];
  }
  dfs(0, u, 0);
  if (ans < 1e7) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}