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

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 500'000 + 5;

i64 T;

i64 n, k;
i64 a[MAXN];

vector<i64> pos;
vector<i64> neg;
i64 zero = 0;

i64 calc_pos() {
  i64 R = 1;

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  i64 i = pos.size() - 1;
  i64 j = 0;

  if (k % 2) {
    k -= 1;
    R *= pos[i];
    R %= mod;
    i--;
  }

  while (k > 0 && i > 0 && j < (i64)neg.size() - 1) {
    i64 pn = neg[j] * neg[j + 1];
    i64 pp = pos[i] * pos[i - 1];
    if (pn > pp) {
      R *= pn % mod;
      j += 2;
    } else {
      R *= pp % mod;
      i -= 2;
    }
    k -= 2;
    R %= mod;
  }

  while (k > 0 && i > 0) {
    i64 pp = pos[i] * pos[i - 1];
    R *= pp % mod;
    R %= mod;
    k -= 2;
    i -= 2;
  }

  while (k > 0 && j < (i64)neg.size() - 1) {
    i64 pn = neg[j] * neg[j + 1];
    R *= pn % mod;
    R %= mod;
    k -= 2;
    j += 2;
  }

  if (k != 0) {
    throw 1;
  }

  return R;
}

i64 calc_neg() {
  i64 R = 1;

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  i64 i = 0;
  i64 j = neg.size() - 1;

  k -= 1;
  j -= 1;
  /*
  R *= neg.back();
  R %= mod;
  */

  while (k > 0 && i < (i64)pos.size() - 1 && j > 0) {
    i64 pn = neg[j] * neg[j - 1];
    i64 pp = pos[i] * pos[i + 1];
    if (pn > pp) {
      R *= pn % mod;
      j -= 2;
    } else {
      R *= pp % mod;
      i += 2;
    }
    k -= 2;
    R %= mod;
  }

  while (k > 0 && i < (i64)pos.size()) {
    i64 pp = pos[i];
    R *= pp % mod;
    R %= mod;
    k -= 1;
    i += 1;
  }

  while (k > 0 && j > 0) {
    i64 pn = neg[j] * neg[j - 1];
    R *= pn % mod;
    R %= mod;
    k -= 2;
    j -= 2;
  }

  if (k != 0) {
    throw 1;
  }

  R *= (mod + neg.back()) % mod;
  R %= mod;

  return R;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  T = 1;
  for (i64 tt = 1; tt <= T; tt++) {
    cin >> n >> k;
    for (i64 i = 0; i < n; i++) {
      cin >> a[i];
    }

    for (i64 i = 0; i < n; i++) {
      if (a[i] < 0) {
        neg.push_back(a[i]);
      } else if (a[i] > 0) {
        pos.push_back(a[i]);
      } else {
        zero += 1;
      }
    }

    i64 R = 0;
    if ((i64)neg.size() - (i64)neg.size() % 2 + (i64)pos.size() >= k &&
        (i64)pos.size() >= k % 2) {
      R = calc_pos();
    } else if (zero > 0) {

    } else {
      R = calc_neg();
    }
    cout << R << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 5'000 + 5;

i64 T;

i64 n, k;
i64 a[MAXN];

vector<i64> pos;
vector<i64> neg;
i64 zero = 0;

i64 calc_pos() {
  i64 R = 1;

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  i64 i = pos.size() - 1;
  i64 j = 0;

  if (k % 2) {
    k -= 1;
    R *= pos[i];
    R %= mod;
    i--;
  }

  while (k > 0 && i > 0 && j < (i64)neg.size() - 1) {
    i64 pn = neg[j] * neg[j + 1];
    i64 pp = pos[i] * pos[i - 1];
    if (pn > pp) {
      R *= pn % mod;
      j += 2;
    } else {
      R *= pp % mod;
      i -= 2;
    }
    k -= 2;
    R %= mod;
  }

  while (k > 0 && i > 0) {
    i64 pp = pos[i] * pos[i - 1];
    R *= pp % mod;
    R %= mod;
    k -= 2;
    i -= 2;
  }

  while (k > 0 && j < (i64)neg.size() - 1) {
    i64 pn = neg[j] * neg[j + 1];
    R *= pn % mod;
    R %= mod;
    k -= 2;
    j += 2;
  }

  return R;
}

i64 calc_neg() {
  i64 R = 1;

  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());

  i64 i = 0;
  i64 j = neg.size() - 1;

  k -= 1;
  j -= 1;
  /*
  R *= neg.back();
  R %= mod;
  */

  while (k > 0 && i < (i64)pos.size() - 1 && j > 0) {
    i64 pn = neg[j] * neg[j - 1];
    i64 pp = pos[i] * pos[i + 1];
    if (pn > pp) {
      R *= pn % mod;
      j -= 2;
    } else {
      R *= pp % mod;
      i += 2;
    }
    k -= 2;
    R %= mod;
  }

  while (k > 0 && i < (i64)pos.size() - 1) {
    i64 pp = pos[i] * pos[i + 1];
    R *= pp % mod;
    R %= mod;
    k -= 2;
    i += 2;
  }

  while (k > 0 && j > 0) {
    i64 pn = neg[j] * neg[j - 1];
    R *= pn % mod;
    R %= mod;
    k -= 2;
    j -= 2;
  }

  R *= (mod + neg.back()) % mod;
  R %= mod;

  return R;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  T = 1;
  for (i64 tt = 1; tt <= T; tt++) {
    cin >> n >> k;
    for (i64 i = 0; i < n; i++) {
      cin >> a[i];
    }

    for (i64 i = 0; i < n; i++) {
      if (a[i] < 0) {
        neg.push_back(a[i]);
      } else if (a[i] > 0) {
        pos.push_back(a[i]);
      } else {
        zero += 1;
      }
    }

    i64 R = 0;
    if ((i64)neg.size() - (i64)neg.size() % 2 + (i64)pos.size() >= k &&
        (i64)pos.size() >= k % 2) {
      R = calc_pos();
    } else if (zero > 0) {

    } else {
      R = calc_neg();
    }
    cout << R << endl;
  }

  return 0;
}