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


// Problem : C - Skill Up
// Contest : AtCoder - AtCoder Beginner Contest 167
// URL : https://atcoder.jp/contests/abc167/tasks/abc167_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define fio                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ull unsigned long long
#define ll long long
#define inf INT_MAX
#define mninf INT_MIN
#define infl 1e18 + 5
#define mninfl -1e18 - 5
#define Mod 1000000007
#define newmod 998244353
int ah = 911382323;
int bh = 972663749;
#define endl "\\n"
#define cp(x) cout << (x) << " "
#define cl(x) cout << (x) << endl
#define ct(x, y) cout << (x) << " " << (y) << endl;
#define nl cout << endl
// const long double pi=acos(-1);
// looping
#define int long long
#define For(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define test(t)                                                                \\
  int(t);                                                                      \\
  cin >> (t);                                                                  \\
  while ((t)--)
#define array(a, n)                                                            \\
  int(n);                                                                      \\
  cin >> (n);                                                                  \\
  int a[n];                                                                    \\
  for (int i = 0; i < (n); i++)                                                \\
    cin >> a[i];
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define pa(a, n)                                                               \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << ' ';                                                       \\
  cout << endl
#define pv(v)                                                                  \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cout << v[i] << " ";                                                       \\
  cout << endl
#define pm(a, n, m)                                                            \\
  for (int i = 0; i < n; i++) {                                                \\
    for (int j = 0; j < m; j++)                                                \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
#define deb(x) cerr << #x << " = " << x << endl;
#define deb2(x, y)                                                             \\
  cerr << #x << " = " << x << "   " << #y << " = " << y << endl;
// containers
#define F first
#define S second
#define pb emplace_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define all(v) (v).begin(), (v).end()
int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
/*/----------------------------Code begins----------------------------------/*/
int32_t main() {
  fio;
  int n, m, k;
  cin >> n >> m >> k;
  int a[n][m], c[n];
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  int cnt = inf;
  for (int mask = 0; mask < (1 << n); mask++) {
    int cost = 0;
    vi v(m);
    for (int i = 0; i < n; i++) {
      if (mask & (1 << i)) {
        cost += c[i];
        for (int j = 0; j < m; j++) {
          v[j] += a[i][j];
        }
      }
    }
    int f = 1;
    for (int i = 0; i < m; i++) {
      if (v[i] < k) {
        f = 0;
        break;
      }
    }
    if (f) {
      cnt = min(cnt, cost);
    }
  }
  if (cnt == inf)
    cnt = -1;
  cl(cnt);
}


Error: 0

Fixed submission:


// Problem : C - Skill Up
// Contest : AtCoder - AtCoder Beginner Contest 167
// URL : https://atcoder.jp/contests/abc167/tasks/abc167_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define fio                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ull unsigned long long
#define ll long long
#define inf INT_MAX
#define mninf INT_MIN
#define infl 1e18 + 5
#define mninfl -1e18 - 5
#define Mod 1000000007
#define newmod 998244353
int ah = 911382323;
int bh = 972663749;
#define endl "\\n"
#define cp(x) cout << (x) << " "
#define cl(x) cout << (x) << endl
#define ct(x, y) cout << (x) << " " << (y) << endl;
#define nl cout << endl
// const long double pi=acos(-1);
// looping
#define int long long
#define For(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define test(t)                                                                \\
  int(t);                                                                      \\
  cin >> (t);                                                                  \\
  while ((t)--)
#define array(a, n)                                                            \\
  int(n);                                                                      \\
  cin >> (n);                                                                  \\
  int a[n];                                                                    \\
  for (int i = 0; i < (n); i++)                                                \\
    cin >> a[i];
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define pa(a, n)                                                               \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << ' ';                                                       \\
  cout << endl
#define pv(v)                                                                  \\
  for (int i = 0; i < (v).size(); i++)                                         \\
    cout << v[i] << " ";                                                       \\
  cout << endl
#define pm(a, n, m)                                                            \\
  for (int i = 0; i < n; i++) {                                                \\
    for (int j = 0; j < m; j++)                                                \\
      cout << a[i][j] << " ";                                                  \\
    cout << endl;                                                              \\
  }                                                                            \\
  cout << endl
template <typename F, typename S>
ostream &operator<<(ostream &os, const pair<F, S> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "[";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename F, typename S>
ostream &operator<<(ostream &os, const map<F, S> &v) {
  os << "[";
  typename map<F, S>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin())
      os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
#define deb(x) cerr << #x << " = " << x << endl;
#define deb2(x, y)                                                             \\
  cerr << #x << " = " << x << "   " << #y << " = " << y << endl;
// containers
#define F first
#define S second
#define pb emplace_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define all(v) (v).begin(), (v).end()
int dx[8] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef pair<int, int> pii;
/*/----------------------------Code begins----------------------------------/*/
bool comp(pair<pii, int> a, pair<pii, int> b) {
  return (a.F.F / a.F.S) < (b.F.F / b.F.S);
}
int32_t main() {
  fio;
  int n, m, k;
  cin >> n >> m >> k;
  int a[n][m];
  int c[n], mn[n];
  For(i, 0, n) {
    cin >> c[i];
    mn[i] = inf;
    For(j, 0, m) {
      cin >> a[i][j];
      mn[i] = min(mn[i], a[i][j]);
    }
  }
  vector<pair<pair<int, int>, int>> v;
  for (int i = 0; i < n; i++) {
    v.pb(mp(mp(c[i], mn[i]), i));
  }
  sort(all(v), comp);
  vi sum(m);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += c[v[i].S];
    for (int j = 0; j < m; j++) {
      sum[j] += a[v[i].S][j];
    }
    int f = 1;
    for (int j = 0; j < m; j++) {
      if (sum[j] < k)
        f = 0;
    }
    if (f)
      break;
  }
  int f = 1;
  for (int j = 0; j < m; j++) {
    if (sum[j] < k)
      f = 0;
  }
  if (f)
    cl(cnt);
  else
    cl(-1);
}