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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {

  int a = 0;
  int b = 0;
  int c = 0;

  int n;
  int k;
  cin >> n;
  cin >> k;

  VLL d, e;

  REP(i, n) {
    int j;
    cin >> j;
    if (j == 0)
      c++;
    else if (j > 0) {
      d.push_back(j);
      a++;
    } else {
      e.push_back(-j);
      b++;
    }
  }

  bool pos = true;

  if (a + b < k) {
    cout << 0 << endl;
    return 0;
  }

  if (k % 2 == 0) {
    int cnt = (b / 2) * 2;
    cnt = min(cnt, (k / 2) * 2);
    cnt += a;
    if (cnt < k)
      pos = false;
  }

  else {
    int cnt = (b / 2) * 2;
    cnt = min(cnt, (k / 2) * 2);
    cnt += a;
    if (cnt < k)
      pos = false;
  }

  if (!pos) {
    if (c != 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  if (pos) {

    sort(ALL(d));
    reverse(ALL(d));
    sort(ALL(e));
    reverse(ALL(e));
    VLL dd;

    ll ans = 1;

    if (k % 2 == 0) {
      for (int i = 0; i + 1 < a; i += 2) {
        dd.push_back(d[i] * d[i + 1]);
      }
      for (int i = 0; i + 1 < b; i += 2) {
        dd.push_back(e[i] * e[i + 1]);
      }
    } else {
      ans = d[0];
      for (int i = 1; i + 1 < a; i += 2) {
        dd.push_back(d[i] * d[i + 1]);
      }
      for (int i = 0; i + 1 < b; i += 2) {
        dd.push_back(e[i] * e[i + 1]);
      }
    }
    sort(ALL(dd));
    reverse(ALL(dd));
    REP(i, dd.size()) { dd[i] %= mod; }
    REP(i, k / 2) {
      if (i >= dd.size()) {
        cout << 0 << endl;
        return 0;
      }
      ans *= dd[i];
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }

  if (n == k) {
    if (c != 0) {
      cout << 0 << endl;
      return 0;
    }
    ll ans = 1;
    REP(i, a) {
      ans *= d[i];
      ans %= mod;
    }
    REP(i, b) {
      ans *= e[i];
      ans %= mod;
    }
    cout << (mod - ans) % mod << endl;
    return 0;
  }

  sort(ALL(d));
  sort(ALL(e));

  ll ans = e[0];

  VLL dd;

  if (k % 2 == 0) {
    for (int i = 1; i + 1 < a; i += 2) {
      dd.push_back(d[i] * d[i + 1]);
    }
    for (int i = 1; i + 1 < b; i += 2) {
      dd.push_back(e[i] * e[i + 1]);
    }
    if (a == 0) {
      cout << 0 << endl;
      return 0;
    }
    ans *= d[0];
    ans %= mod;
  } else {
    for (int i = 0; i + 1 < a; i += 2) {
      dd.push_back(d[i] * d[i + 1]);
    }
    for (int i = 1; i + 1 < b; i += 2) {
      dd.push_back(e[i] * e[i + 1]);
    }
  }
  if (k % 2 == 0) {
    k = (k - 2) / 2;
  } else
    k /= 2;
  sort(ALL(dd));

  REP(i, k) {
    if (i >= dd.size()) {
      cout << 0 << endl;
      return 0;
    }
    dd[i] %= mod;
    ans *= dd[i];
    ans %= mod;
  }
  cout << (mod - ans) % mod << endl;
}

Error: -6
c65bb0fc-b1fa-4ecf-9c4f-c90c80698e3f.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02616/C++/s762234836.cpp:88: int main(): Assertion `n == 0' failed.


Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define VI vector<int>
#define VP vector<pair<int, int>>
#define VPP vector<pair<int, pair<int, int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i, a) for (auto &i : a)
typedef pair<int, int> P;
template <typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 60;
const ll mod = 1000000007;

int main() {

  int a = 0;
  int b = 0;
  int c = 0;

  int n;
  int k;
  cin >> n;
  cin >> k;

  VLL d, e;

  REP(i, n) {
    int j;
    cin >> j;
    if (j == 0)
      c++;
    else if (j > 0) {
      d.push_back(j);
      a++;
    } else {
      e.push_back(-j);
      b++;
    }
  }

  bool pos = true;

  if (a + b < k) {
    cout << 0 << endl;
    return 0;
  }

  if (k % 2 == 0) {
    int cnt = (b / 2) * 2;
    cnt = min(cnt, (k / 2) * 2);
    cnt += a;
    if (cnt < k)
      pos = false;
  }

  else {
    int cnt = (b / 2) * 2;
    cnt = min(cnt, (k / 2) * 2);
    cnt += a;
    if (cnt < k)
      pos = false;
  }

  if (!pos) {
    if (c != 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  assert(n == 0);

  if (pos) {

    sort(ALL(d));
    reverse(ALL(d));
    sort(ALL(e));
    reverse(ALL(e));
    min_priority_queue<ll> q;
    VLL dd, ee;

    ll ans = 1;

    if (k % 2 == 0) {
      for (int i = 0; i + 1 < a; i += 2) {
        dd.push_back(d[i] * d[i + 1]);
      }
      for (int i = 0; i + 1 < b; i += 2) {
        dd.push_back(e[i] * e[i + 1]);
      }
    } else {
      ans = d[0];
      for (int i = 1; i + 1 < a; i += 2) {
        dd.push_back(d[i] * d[i + 1]);
      }
      for (int i = 0; i + 1 < b; i += 2) {
        dd.push_back(e[i] * e[i + 1]);
      }
    }
    sort(ALL(dd));
    reverse(ALL(dd));
    REP(i, dd.size()) { dd[i] %= mod; }
    REP(i, k / 2) {
      if (i >= dd.size()) {
        cout << 0 << endl;
        return 0;
      }
      ans *= dd[i];
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }

  if (n == k) {
    if (c != 0) {
      cout << 0 << endl;
      return 0;
    }
    ll ans = 0;
    REP(i, a) {
      ans *= d[i];
      ans %= mod;
    }
    REP(i, b) {
      ans *= e[i];
      ans %= mod;
    }
    cout << (mod - ans) % mod << endl;
    return 0;
  }

  sort(ALL(d));
  sort(ALL(e));

  ll ans = e[0];

  VLL dd;

  if (k % 2 == 0) {
    for (int i = 1; i + 1 < a; i += 2) {
      dd.push_back(d[i] * d[i + 1]);
    }
    for (int i = 1; i + 1 < b; i += 2) {
      dd.push_back(e[i] * e[i + 1]);
    }
    if (a == 0) {
      cout << 0 << endl;
      return 0;
    }
    ans *= d[0];
    ans %= mod;
  } else {
    for (int i = 0; i + 1 < a; i += 2) {
      dd.push_back(d[i] * d[i + 1]);
    }
    for (int i = 1; i + 1 < b; i += 2) {
      dd.push_back(e[i] * e[i + 1]);
    }
  }
  if (k % 2 == 0) {
    k = (k - 2) / 2;
  } else
    k /= 2;
  sort(ALL(dd));

  REP(i, k) {
    if (i >= dd.size()) {
      cout << 0 << endl;
      return 0;
    }
    ans *= dd[i];
    ans %= mod;
  }
  cout << (mod - ans) % mod << endl;
}