Problem p02683 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;

#define FOR(i, a, b) for (int i = a; i < (b); i++)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

#define trav(container, it)                                                    \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)

const int MOD = 1000000007;
const char nl = '\n';

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m, x;
  cin >> n >> m >> x;

  vvi v(n);
  vi c(n);

  int temp;

  FOR(i, 0, n) {
    cin >> c[i];

    FOR(j, 0, m) {
      cin >> temp;
      v[j].pb(temp);
    }
  }

  ll count, sum, res = INT_MAX;
  bool flag;

  FOR(mask, 1, pow(2, n)) {
    count = 0;
    sum = 0;
    flag = true;

    FOR(j, 0, n) {
      if (mask & (1 << j))
        count += v[0][j], sum += c[j];
    }
    if (count >= x) {
      FOR(l, 1, m) {
        count = 0;
        FOR(j, 0, n) {
          if (mask & (1 << j)) {
            count += v[l][j];
          }
        }
        if (count < x) {
          flag = false;
          break;
        }
      }
      if (flag)
        res = min(res, sum);
    }
  }

  if (res == INT_MAX)
    cout << -1;
  else
    cout << res;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;

#define FOR(i, a, b) for (int i = a; i < (b); i++)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()

template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

#define trav(container, it)                                                    \
  for (typeof(container.begin()) it = container.begin();                       \
       it != container.end(); it++)

const int MOD = 1000000007;
const char nl = '\n';

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, m, x;
  cin >> n >> m >> x;

  vvi v;
  if (n > m)
    v.resize(n);
  else
    v.resize(m);

  vi c(n);

  int temp;

  FOR(i, 0, n) {
    cin >> c[i];

    FOR(j, 0, m) {
      cin >> temp;
      v[j].pb(temp);
    }
  }

  ll count, sum, res = INT_MAX;
  bool flag;

  FOR(mask, 1, pow(2, n)) {
    count = 0;
    sum = 0;
    flag = true;

    FOR(j, 0, n) {
      if (mask & (1 << j))
        count += v[0][j], sum += c[j];
    }
    if (count >= x) {
      FOR(l, 1, m) {
        count = 0;
        FOR(j, 0, n) {
          if (mask & (1 << j)) {
            count += v[l][j];
          }
        }
        if (count < x) {
          flag = false;
          break;
        }
      }
      if (flag)
        res = min(res, sum);
    }
  }

  if (res == INT_MAX)
    cout << -1;
  else
    cout << res;
}