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

//
// Created by 10034 on 2020/7/28.
//

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 200010, mod = 1e9 + 7;
int a[N];
vector<int> v0, v1;
vector<int> v2, v3;
typedef long long LL;
int calc() {
  int res = 1;
  for (int i = 0; i < v0.size(); ++i) {
    res = ((LL)res * v0[i] % mod + mod) % mod;
  }
  for (int i = 0; i < v1.size(); ++i) {
    res = ((LL)res * v1[i] % mod + mod) % mod;
  }
  return res;
}
bool cmp(int a, int b) { return abs(a) < abs(b); }
int main() {

  int n, k;
  cin >> n >> k;
  int z = 0, f = 0, l = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] > 0)
      z++;
    else if (a[i] < 0)
      f++;
    else
      l++;
  }
  if (z >= k || ((k - z) % 2 == 0 && f / 2 * 2 >= k - z) ||
      (z && (k - z) % 2 == 1 && f / 2 * 2 >= k - z + 1)) {
    sort(a + 1, a + 1 + n, cmp);
    reverse(a + 1, a + 1 + n);
    int zz = 0, ff = 0;
    for (int i = 1, j = k; i <= n; ++i) {
      if (a[i] == 0)
        continue;
      if (j) {
        if (a[i] > 0)
          v0.push_back(a[i]);
        else
          v1.push_back(a[i]);
        --j;
        if (a[i] > 0)
          zz++;
        else
          ff++;
      } else if (a[i] > 0)
        v2.push_back(a[i]);
      else
        v3.push_back(a[i]);
    }
    if (ff % 2 == 0) {
      cout << calc() << endl;
    } else {
      if (v0.size())
        sort(v0.begin(), v0.end());
      if (v1.size())
        sort(v1.begin(), v1.end(), cmp);
      if (v2.size())
        sort(v2.begin(), v2.end(), greater<int>());
      if (v3.size())
        sort(v3.begin(), v3.end());
      if (v0.size() == 0) {
        v1[0] = v2[0];
      } else {
        if (v2.size() == 0) {
          v0[0] = v3[0];
        } else if (v3.size() == 0) {
          v1[0] = v2[0];
        } else {
          if ((LL)v0[0] * v2[0] > (LL)v1[0] * v3[0]) {
            v1[0] = v2[0];
          } else
            v0[0] = v3[0];
        }
      }
      cout << calc() << endl;
    }
  } else if (l) {
    puts("0");
  } else {
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= k; ++i) {
      v0.push_back(a[i]);
    }
    cout << calc() << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

//
// Created by 10034 on 2020/7/28.
//

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 200010, mod = 1e9 + 7;
int a[N];
vector<int> v;
vector<int> v2;
typedef long long LL;
int calc() {
  int res = 1;
  for (int i = 0; i < v.size(); ++i) {
    res = ((LL)res * v[i] % mod + mod) % mod;
  }
  return res;
}
bool cmp(int a, int b) { return abs(a) < abs(b); }
int main() {

  int n, k;
  cin >> n >> k;
  int z = 0, f = 0, l = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] > 0)
      z++;
    else if (a[i] < 0)
      f++;
    else
      l++;
  }
  if (z + f / 2 * 2 >= k) {
    sort(a + 1, a + 1 + n, cmp);
    reverse(a + 1, a + 1 + n);
    int zz = 0, ff = 0;
    for (int i = 1, j = k; i <= n; ++i) {
      if (a[i] == 0)
        continue;
      if (j) {
        v.push_back(a[i]);
        --j;
        if (a[i] > 0)
          zz++;
        else
          ff++;
      } else
        v2.push_back(a[i]);
    }
    if (ff % 2 == 0) {
      cout << calc() << endl;
    } else {
      int zs1 = 1e9, fs1 = -1e9, i1 = -1, j1 = -1;
      for (int i = 0; i < k; ++i) {
        if (v[i] > 0 && zs1 > v[i]) {
          zs1 = v[i];
          i1 = i;
        } else if (v[i] < 0 && fs1 < v[i]) {
          fs1 = v[i];
          j1 = i;
        }
      }
      int zs2 = 0, fs2 = 0;
      int i2 = -1, j2 = -1;
      for (int i = 0; i < v2.size(); ++i) {
        if (v2[i] > 0 && zs2 < v2[i]) {
          zs2 = v2[i];
          i2 = i;
        } else if (v2[i] < 0 && fs2 > v2[i]) {
          fs2 = v2[i];
          j2 = i;
        }
      }

      if ((i1 != -1 && j1 != -1 && i2 != -1 && j2 != -1 &&
           zs1 * zs2 > fs1 * fs2) ||
          j1 == -1 || j2 == -1)
        v[j1] = zs2;
      else
        v[i1] = fs2;
      cout << calc() << endl;
    }
  } else if (l) {
    puts("0");
  } else {
    sort(a + 1, a + 1 + n, cmp);
    //        reverse(a+1,a+1+n);
    for (int i = 1; i <= k; ++i) {
      v.push_back(a[i]);
    }
    cout << calc() << endl;
  }

  return 0;
}