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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 998244353
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int fc[100001], in[100001];
vector<vector<int>> adj;
int pwmd(int a, int n) {
  if (!n)
    return 1;
  int pt = pwmd(a, n / 2);
  pt *= pt, pt %= mod;
  if (n & 1)
    pt *= a, pt %= mod;
  return pt;
}

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

  vector<vector<int>> a1(n);
  vector<int> vec;
  int ans = INT_MAX;
  for (int i = 0; i < n; i++) {
    int cost;
    cin >> cost;
    vec.push_back(cost);
    for (int j = 0; j < m; j++) {
      int x;
      cin >> x;
      a1[i].push_back(x);
    }
  }

  for (int i = 0; i < (1 << n); i++) {
    int cost = 0;
    vector<int> c(m);
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j)) > 0) {
        cost += vec[j];
        for (int k = 0; k < m; k++)
          c[k] += a1[j][k];
      }
    }
    int flag = 1;
    for (int i = 0; i < m; i++)
      if (c[i] < x)
        flag = 0;

    if (flag)
      ans = min(ans, cost);
  }
  if (ans != INT_MAX)
    cout << ans << endl;
  else
    cout << "-1" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 998244353
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;
int fc[100001], in[100001];
vector<vector<int>> adj;
int pwmd(int a, int n) {
  if (!n)
    return 1;
  int pt = pwmd(a, n / 2);
  pt *= pt, pt %= mod;
  if (n & 1)
    pt *= a, pt %= mod;
  return pt;
}

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

  vector<vector<int>> a1(n);
  int ans = 0;
  vector<int> c;

  for (int i = 0; i <= m; i++)
    c.push_back(0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a1[i].push_back(a);
    ans += a;
    for (int j = 0; j < m; j++) {
      int arr;
      cin >> arr;
      a1[i].push_back(arr);
      c[j + 1] += arr;
    }
  }

  // cout<<ans<<endl;
  sort(a1.begin(), a1.end());
  int flag = 1;
  for (int i = 1; i <= m; i++)
    if (c[i] < x)
      flag = 0;

  if (flag == 0) {
    cout << "-1" << endl;
    return 0;
  }

  for (int i = n - 1; i >= 0; i--) {
    int can = 1;
    for (int j = 1; j <= m; j++) {
      if ((c[j] - a1[i][j]) < x)
        can = 0;
    }

    if (can) {
      ans -= a1[i][0];
      for (int k = 1; k <= m; k++)
        c[i] -= a1[i][k];
    }
  }

  cout << ans << endl;
}