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

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
vector<ll> v1, v2, u;
ll n, k, a, cnt1, cnt2, N, MOD = 1000000007, ans = 1;
int main(void) {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a < 0) {
      cnt2++;
      v2.push_back(-a);
    } else {
      cnt1++;
      v1.push_back(a);
    }
  }
  if (cnt2 % 2 == 1)
    N = cnt2 - 1;
  else
    N = cnt2;
  if (N + cnt1 < k || ((cnt1 == 0) && (k % 2 == 1))) {
    for (int i = 0; i < cnt2; i++) {
      v1.push_back(v2[i]);
    }
    sort(v1.begin(), v1.end());
    for (int i = 0; i < k; i++) {
      ans *= v1[i];
      ans %= MOD;
    }
    ans = -ans;
    cout << (ans + MOD) % MOD << endl;
    return 0;
  } else {
    if (k % 2 == 0) {
      sort(v1.begin(), v1.end());
      sort(v2.begin(), v2.end());
      reverse(v1.begin(), v1.end());
      reverse(v2.begin(), v2.end());
      for (int i = 0; i < cnt1 / 2; i++) {
        u.push_back(v1[i * 2] * v1[i * 2 + 1]);
      }
      for (int i = 0; i < cnt2 / 2; i++) {
        u.push_back(v2[i * 2] * v2[i * 2 + 1]);
      }
      sort(u.begin(), u.end());
      reverse(u.begin(), u.end());
      for (int i = 0; i < k / 2; i++) {
        ans *= (u[i] % MOD);
        ans %= MOD;
      }
      cout << ans << endl;
      return 0;
    } else {
      sort(v1.begin(), v1.end());
      sort(v2.begin(), v2.end());
      reverse(v1.begin(), v1.end());
      reverse(v2.begin(), v2.end());
      ans *= v1[0];
      for (int i = 0; i < (cnt1 - 1) / 2; i++) {
        u.push_back(v1[i * 2 + 1] * v1[i * 2 + 2]);
      }

      for (int i = 0; i < cnt2 / 2; i++) {
        u.push_back(v2[i * 2] * v2[i * 2 + 1]);
      }
      sort(u.begin(), u.end());
      reverse(u.begin(), u.end());
      for (int i = 0; i < k / 2; i++) {
        ans *= (u[i] % MOD);
        ans %= MOD;
      }
      cout << ans << endl;
      return 0;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
vector<ll> v1, v2, u;
ll n, k, a, cnt1, cnt2, N, MOD = 1000000007, ans = 1;
int main(void) {
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a;
    if (a < 0) {
      cnt2++;
      v2.push_back(-a);
    } else {
      cnt1++;
      v1.push_back(a);
    }
  }
  if (cnt2 % 2 == 1)
    N = cnt2 - 1;
  else
    N = cnt2;
  if (N + cnt1 < k) {
    for (int i = 0; i < cnt2; i++) {
      v1.push_back(v2[i]);
    }
    sort(v1.begin(), v1.end());
    for (int i = 0; i < k; i++) {
      ans *= v1[i];
      ans %= MOD;
    }
    ans = -ans;
    cout << (ans + MOD) % MOD << endl;
    return 0;
  } else {
    if (k % 2 == 0) {
      sort(v1.begin(), v1.end());
      sort(v2.begin(), v2.end());
      reverse(v1.begin(), v1.end());
      reverse(v2.begin(), v2.end());
      for (int i = 0; i < cnt1 / 2; i++) {
        u.push_back(v1[i * 2] * v1[i * 2 + 1]);
      }
      for (int i = 0; i < cnt2 / 2; i++) {
        u.push_back(v2[i * 2] * v2[i * 2 + 1]);
      }
      sort(u.begin(), u.end());
      reverse(u.begin(), u.end());
      for (int i = 0; i < k / 2; i++) {
        ans *= (u[i] % MOD);
        ans %= MOD;
      }
      cout << ans << endl;
      return 0;
    } else {
      sort(v1.begin(), v1.end());
      sort(v2.begin(), v2.end());
      reverse(v1.begin(), v1.end());
      reverse(v2.begin(), v2.end());
      ans *= v1[0];
      for (int i = 0; i < (cnt1 - 1) / 2; i++) {
        u.push_back(v1[i * 2 + 1] * v1[i * 2 + 2]);
      }

      for (int i = 0; i < cnt2 / 2; i++) {
        u.push_back(v2[i * 2] * v2[i * 2 + 1]);
      }
      sort(u.begin(), u.end());
      reverse(u.begin(), u.end());
      for (int i = 0; i < k / 2; i++) {
        ans *= (u[i] % MOD);
        ans %= MOD;
      }
      cout << ans << endl;
      return 0;
    }
  }
}