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

#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long int ll;
const int N = 13;
int cost[N];
int arr[N][N];
int brr[N][N];
int dp[N][N];
vector<int> total(N);

int n, m, x;
ll ans = 1e10;
string fun(int n) {
  int binaryNum[32];
  int i = 0;
  while (n > 0) {
    binaryNum[i] = n % 2;
    n = n / 2;
    i++;
  }

  string s;
  for (int j = i - 1; j >= 0; j--)
    s += binaryNum[j] + '0';
  return s;
}

void solve(vector<int> v) {
  int sz = v.size();
  ll temp = 0;
  // int brr[sz + 1][sz + 1];
  for (int i = 0; i < sz; ++i) {
    temp += cost[v[i]];
    for (int j = 0; j < m; ++j) {
      if (i == 0)
        brr[i][j] = arr[v[i]][j];
      else
        brr[i][j] = arr[v[i]][j] + brr[i - 1][j];
    }
    bool flag = true;
    // cout << "brr: ";
    for (int k = 0; k < m; ++k) {
      // cout << brr[i][k] << " ";
      if (brr[i][k] < x) {
        flag = false;
        break;
      }
    }
    // cout << "\\n";
    if (flag) {
      // cout << "found it: \\n";
      ans = min(ans, temp);
      // cout << "ans isssssssssssssss: " << ans << "\\n";
      return;
    }
  }
  // cout << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> x;
  memset(dp, 0, sizeof(dp));

  for (int i = 0; i < n; ++i) {
    cin >> cost[i];
    for (int j = 0; j < m; ++j) {
      cin >> arr[i][j];
      if (i == 0)
        dp[i][j] += arr[i][j];
      else
        dp[i][j] += arr[i][j] + dp[i - 1][j];
    }
  }
  for (int i = 0; i < m; ++i) {
    if (dp[n - 1][i] < x) {
      cout << "-1";
      return 0;
    }
  }

  ll all = pow(2, n);
  string s;
  for (int i = 0; i < all; ++i) {
    s = fun(i);
    // cout << "i is: " << i << "s is: " << s << "\\n";
    vector<int> v;
    for (int j = 0; j < s.size(); ++j) {
      if (s[j] == '1') {
        // cout << "j is: " << j << "\\n";
        v.pb(s.size() - j - 1);
      }
    }
    if (v.size())
      solve(v);
  }
  if (ans == 1e12)
    cout << "-1";
  else
    cout << ans << "\\n";

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long int ll;
const int N = 13;
int cost[N];
int arr[N][N];
int dp[N][N];
vector<int> total(N);

int n, m, x;
string fun(int n) {
  int binaryNum[32];
  int i = 0;
  while (n > 0) {
    binaryNum[i] = n % 2;
    n = n / 2;
    i++;
  }

  string s;
  for (int j = i - 1; j >= 0; j--)
    s += binaryNum[j] + '0';
  return s;
}

ll ans = 1e10;
void solve(vector<int> v) {
  int sz = v.size();
  ll temp = 0;
  int brr[sz + 1][sz + 1];
  for (int i = 0; i < v.size(); ++i) {
    temp += cost[v[i]];
    for (int j = 0; j < m; ++j) {
      if (i == 0)
        brr[i][j] = arr[v[i]][j];
      else
        brr[i][j] = arr[v[i]][j] + brr[i - 1][j];
    }
    bool flag = true;
    // cout << "brr: ";
    for (int k = 0; k < m; ++k) {
      // cout << brr[i][k] << " ";
      if (brr[i][k] < x) {
        flag = false;
        break;
      }
    }
    // cout << "\\n";
    if (flag) {
      // cout << "found it: \\n";
      ans = min(ans, temp);
      // cout << "ans isssssssssssssss: " << ans << "\\n";
      return;
    }
  }
  // cout << "\\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m >> x;
  memset(dp, 0, sizeof(dp));

  for (int i = 0; i < n; ++i) {
    cin >> cost[i];
    for (int j = 0; j < m; ++j) {
      cin >> arr[i][j];
      if (i == 0)
        dp[i][j] += arr[i][j];
      else
        dp[i][j] += arr[i][j] + dp[i - 1][j];
    }
  }
  for (int i = 0; i < m; ++i) {
    if (dp[n - 1][i] < x) {
      cout << "-1";
      return 0;
    }
  }

  ll all = pow(2, n);
  string s;
  for (int i = 0; i < all; ++i) {
    s = fun(i);
    // cout << "i is: " << i << "s is: " << s << "\\n";
    vector<int> v;
    for (int j = 0; j < s.size(); ++j) {
      if (s[j] == '1') {
        // cout << "j is: " << j << "\\n";
        v.pb(s.size() - j - 1);
      }
    }
    if (v.size())
      solve(v);
  }
  if (ans == 1e12)
    cout << "-1";
  else
    cout << ans << "\\n";
}