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

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
using ll = long long;
using P = pair<ll, ll>;
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

/*
 string -> int : stoi(S);
 int -> string : to_string(N);
 Sのn文字以降をm文字取り出す : S.substr(n,m)
cout << fixed << setprecision(2) << 3.14 << endl; //3.1

 int: -2,147,483,648 ～ 2,147,483,647 (2×10^9)
 ll: -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 (9×10^18)

*/

ll n, m, x, count = 0;
ll MAX = 100000000000000;
ll ans = MAX;
vector<ll> c;
vector<vector<ll>> a;

void dps(vector<ll> A) {
  vector<ll> B = A;
  vector<ll> C = A;
  B.pb(0);
  C.pb(1);
  if (B.size() == n + 1) {
    ll cost = 0;
    vector<ll> result(m);

    rep(i, n) {
      if (B.at(i + 1)) {
        cost += c.at(i);
        rep(l, m) { result.at(l) += a.at(i).at(l); }
      }
    }

    rep(i, m) {
      if (result.at(i) < x)
        cost = MAX;
    }

    chmin(ans, cost);
  }

  if (C.size() == n + 1) {
    ll cost = 0;
    vector<ll> result(m, 0);

    rep(i, n) {
      if (C.at(i + 1)) {
        cost += c.at(i);
        rep(l, m) { result.at(l) += a.at(i).at(l); }
      }
    }

    rep(i, m) {
      if (result.at(i) < x)
        cost = MAX;
    }

    chmin(ans, cost);
  } else {
    dps(B);
    dps(C);
  }
}

int main() {

  string s;

  cin >> n >> m >> x;
  a.resize(n);
  c.resize(n);

  rep(i, n) {
    cin >> c.at(i);
    a.at(i).resize(m);
    rep(l, m) { cin >> a.at(i).at(l); }
  }

  dps({0});

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

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
using ll = long long;
using P = pair<ll, ll>;
#define pb push_back
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

/*
 string -> int : stoi(S);
 int -> string : to_string(N);
 Sのn文字以降をm文字取り出す : S.substr(n,m)
cout << fixed << setprecision(2) << 3.14 << endl; //3.1

 int: -2,147,483,648 ～ 2,147,483,647 (2×10^9)
 ll: -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 (9×10^18)

*/

ll n, m, x, ans;
ll MAX = 10000000000000;
vector<ll> c;
vector<vector<ll>> a;

void dps(vector<int> A) {
  if (A.size() == n + 1) {
    vector<ll> result(m + 1, 0);
    ll cost = 0;

    rep2(i, n) {
      if (A[i]) {
        cost += c[i - 1];
        rep(l, m) { result[i] += a[i - 1][l]; }
      }
    }
    rep2(i, m) {
      if (result[i] < x)
        return;
    }
    chmin(ans, cost);
  }

  A.pb(0);
  dps(A);
  A.pop_back();
  A.pb(1);
  dps(A);
}

int main() {
  cin >> n >> m >> x;
  c.resize(n);
  a.resize(n);
  rep(i, n) {
    cin >> c[i];
    rep(i, m) {
      int aaaa;
      cin >> aaaa;
      a[i].pb(aaaa);
    }
  }
  dps({0});
  if (ans == MAX)
    cout << -1 << endl;
  else
    cout << ans << endl;
}