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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
typedef vector<pair<long long, long long>> vii;
typedef stringstream ss;
ll mil() { return numeric_limits<long long>::min(); }
ll mal() { return numeric_limits<long long>::max(); }

#define R3(i, a, b) for (long long i = (a); i < (b); ++i)
#define R2(i, b) for (long long i = 0; i < (b); ++i)
#define R1(b) for (long long i = 0; i < (b); ++i)
#define RR(i, a, b) for (long long i = a; i > b; --i)
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)

// build: ctrl + f9

// run: shift + f10
// sanitize: shift + f11
// debug: shift + f9

void A() {
  string S;
  string T;
  cin >> S >> T;

  if (T.substr(0, S.size()) == S) {
    cout << "Yes" << endl;
    return;
  }

  cout << "No" << endl;
}

void B() {
  ll A, B, C, K;

  cin >> A >> B >> C >> K;

  ll tot = 0;
  R1(K) {
    if (i < A) {
      tot += 1;
    } else if (i >= (A + B)) {
      tot -= 1;
    }
  }

  cout << tot << endl;
}

void ps(ll k, ll l, ll r, vector<vector<long long>> &powerSets,
        vector<long long> &currSet) {
  if (k == 0) {
    powerSets.push_back(currSet);
  }

  for (int i = l; i < r; ++i) {
    currSet.push_back(i);
    ps(k - 1, i + 1, r, powerSets, currSet);
    currSet.pop_back();
  }
}
void C() {
  ll N, M, X;

  cin >> N >> M >> X;

  vi cis(N, 0);
  vector<vector<long long>> mij(N, vector<long long>(M));

  R1(N) {
    cin >> cis[i];
    R2(j, M) { cin >> mij[i][j]; }
  }

  vi prices;
  vector<vector<long long>> powerset;
  vector<long long> currSet;
  for (int i = 1; i <= N; ++i) {
    ps(i, 0, N, powerset, currSet);
  }
  ll minPrice = mal();
  for (auto s : powerset) {
    ll price = 0;
    vi av(M, 0);
    for (auto book : s) {
      price += cis[book];
      for (int i = 0; i < mij[book].size(); ++i) {
        av[i] += mij[book][i];
      }
    }
    bool lvlOk = true;
    for (auto lvl : av) {
      if (lvl < X) {
        lvlOk = false;
        break;
      }
    }
    if (lvlOk)
      minPrice = min(minPrice, price);
  }

  if (minPrice != mal())
    cout << minPrice << endl;
  else
    cout << -1 << endl;
}

void D() {}

void E() {}

int main() { C(); }

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef vector<long long> vi;
typedef pair<long long, long long> ii;
typedef vector<pair<long long, long long>> vii;
typedef stringstream ss;
ll mil() { return numeric_limits<long long>::min(); }
ll mal() { return numeric_limits<long long>::max(); }

#define R3(i, a, b) for (long long i = (a); i < (b); ++i)
#define R2(i, b) for (long long i = 0; i < (b); ++i)
#define R1(b) for (long long i = 0; i < (b); ++i)
#define RR(i, a, b) for (long long i = a; i > b; --i)
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)

// build: ctrl + f9

// run: shift + f10
// sanitize: shift + f11
// debug: shift + f9

void A() {
  string S;
  string T;
  cin >> S >> T;

  if (T.substr(0, S.size()) == S) {
    cout << "Yes" << endl;
    return;
  }

  cout << "No" << endl;
}

void B() {
  ll A, B, C, K;

  cin >> A >> B >> C >> K;

  ll tot = 0;
  R1(K) {
    if (i < A) {
      tot += 1;
    } else if (i >= (A + B)) {
      tot -= 1;
    }
  }

  cout << tot << endl;
}

void minPrices(ll N, ll M, ll X, vi ci, vector<bool> &availableBooks,
               vector<long long> &bookLevel, vector<vector<long long>> &mij,
               ll &price, vector<ll> &prices) {

  bool levelOk = true;
  for (auto bookLvl : bookLevel) {
    if (bookLvl < X) {
      levelOk = false;
      break;
    }
  }
  if (levelOk == true) {
    prices.push_back(price);
  }

  R1(M) {
    if (availableBooks[i] == false)
      continue;

    availableBooks[i] = false;
    for (int j = 0; j < mij[i].size(); ++j) {
      bookLevel[j] += mij[i][j];
    }
    price += ci[i];

    minPrices(N, M, X, ci, availableBooks, bookLevel, mij, price, prices);

    price -= ci[i];
    for (int j = 0; j < mij[i].size(); ++j) {
      bookLevel[j] -= mij[i][j];
    }
    availableBooks[i] = true;
  }
}

void C() {
  ll N, M, X;

  cin >> N >> M >> X;

  vi cis(N, 0);
  vector<vector<long long>> mij(N, vector<long long>(M));

  R1(N) {
    cin >> cis[i];
    R2(j, M) { cin >> mij[i][j]; }
  }

  vector<bool> availableBooks(N, true);
  vector<ll> bookLevel(N, 0);

  ll price = 0;
  vi prices;
  minPrices(N, M, X, cis, availableBooks, bookLevel, mij, price, prices);
  if (prices.empty()) {
    cout << -1 << endl;
    return;
  }

  sort(prices.begin(), prices.end());
  cout << prices.front() << endl;
}

void D() {}

void E() {}

int main() { C(); }