Problem p02683 (C++)

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

using namespace std;

const long long int INF = 1LL << 60;
const long long int mod = 1e9 + 7;

int main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<int> C(N + 1);
  vector<vector<int>> A(N + 1, vector<int>(N + 1, 0));
  vector<long long> sum_a(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
    for (int j = 1; j <= M; j++) {
      cin >> A[i][j];
      sum_a[j] += A[i][j];
    }
  }

  bool possible = true;
  for (int j = 1; j <= M; j++) {
    if (sum_a[j] < X) {
      possible = false;
      break;
    }
  }
  if (!possible) {
    cout << -1 << endl;
    return 0;
  }

  long long all_patterns = (long long)(pow(2, N + 1)) - 2;
  vector<vector<long long>> state(all_patterns + 1,
                                  vector<long long>(M + 1, 0));
  vector<long long> cumulative_cost(all_patterns + 1);
  vector<long long> ok(all_patterns + 1, false);

  long long pattern = 2;
  long long pos = 1;
  for (int i = 1; i <= N; i++) {
    long long prev_ref = pos - pattern / 2;
    for (int j = 1; j <= pattern; j++) {
      if (j % 2 == 0) {
        for (long long k = 1; k <= M; k++) {
          state[pos][k] = state[prev_ref][k];
          state[pos][k] += A[i][k];
        }

        cumulative_cost[pos] = cumulative_cost[prev_ref] + C[i];
        bool possible = true;
        for (int k = 1; k <= M; k++) {
          possible = possible && (state[pos][k] >= X);
        }
        ok[pos] = possible;
        prev_ref++;
      } else {
        for (int k = 1; k <= M; k++) {
          state[pos][k] = state[prev_ref][k];
        }
        cumulative_cost[pos] = cumulative_cost[prev_ref];
        ok[pos] = ok[prev_ref];
      }
      pos++;
    }
    pattern *= 2;
  }

  long long ans = 1200000 + 1;
  for (long long i = 1; i <= all_patterns; i++) {
    if (ok[i]) {
      ans = min(ans, cumulative_cost[i]);
    }
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

const long long int INF = 1LL << 60;
const long long int mod = 1e9 + 7;

int main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<int> C(N + 1);
  vector<vector<int>> A(N + 1, vector<int>(M + 1, 0));
  vector<long long> sum_a(M + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> C[i];
    for (int j = 1; j <= M; j++) {
      cin >> A[i][j];
      sum_a[j] += A[i][j];
    }
  }

  bool possible = true;
  for (int j = 1; j <= M; j++) {
    if (sum_a[j] < X) {
      possible = false;
      break;
    }
  }
  if (!possible) {
    cout << -1 << endl;
    return 0;
  }

  long long all_patterns = (long long)(pow(2, N + 1)) - 2;
  vector<vector<long long>> state(all_patterns + 1,
                                  vector<long long>(M + 1, 0));
  vector<long long> cumulative_cost(all_patterns + 1);
  vector<long long> ok(all_patterns + 1, false);

  long long pattern = 2;
  long long pos = 1;
  for (int i = 1; i <= N; i++) {
    long long prev_ref = pos - pattern / 2;
    for (int j = 1; j <= pattern; j++) {
      if (j % 2 == 0) {
        for (long long k = 1; k <= M; k++) {
          state[pos][k] = state[prev_ref][k];
          state[pos][k] += A[i][k];
        }

        cumulative_cost[pos] = cumulative_cost[prev_ref] + C[i];
        bool possible = true;
        for (int k = 1; k <= M; k++) {
          possible = possible && (state[pos][k] >= X);
        }
        ok[pos] = possible;
        prev_ref++;
      } else {
        for (int k = 1; k <= M; k++) {
          state[pos][k] = state[prev_ref][k];
        }
        cumulative_cost[pos] = cumulative_cost[prev_ref];
        ok[pos] = ok[prev_ref];
      }
      pos++;
    }
    pattern *= 2;
  }

  long long ans = 1200000 + 1;
  for (long long i = 1; i <= all_patterns; i++) {
    if (ok[i]) {
      ans = min(ans, cumulative_cost[i]);
    }
  }

  cout << ans << endl;

  return 0;
}