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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
// #include"testlib.h"
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define RT(s) return cout << s, 0
#define sz(s) (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x) << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("street.in", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
int mod = 1e9 + 7;
int n, k;
vector<ll> v1;
ll gt() {
  ll ret = 1;
  for (int i = 0; i < k; i++) {
    ret *= v1[i];
    ret %= mod;
  }
  ret += mod;
  return ret % mod;
}

int main() {
  file();
  fast();
  cin >> n >> k;
  v1 = vector<ll>(n);
  for (auto &it : v1)
    cin >> it;
  if (n == k)
    RT(gt() << endl);
  auto cmp = [](const ll &a, const ll &b) -> bool { return abs(a) > abs(b); };
  sort(all(v1), cmp);
  int ok = 0, neg = -1, pos = -1;
  for (int i = 0; i < k; i++) {
    if (v1[i] < 0)
      ok = 1 - ok, neg = i;
    else
      pos = i;
  }

  if (!ok)
    RT(gt() << endl);
  if (pos == -1) {
    bool s = 0;
    for (int i = k; i < n; i++)
      if (v1[i] >= 0) {
        s = 1;
        swap(v1[neg], v1[i]);
        break;
      }
    if (!s)
      reverse(all(v1));
    RT(gt() << endl);
  }
  ok = 0;
  for (int i = k; i < n; i++) {
    if (v1[i] >= 0) {
      ok = 1;
      break;
    }
  }
  if (!ok) {
    swap(v1[pos], v1[k]);
    RT(gt() << endl);
  }
  ll onePos = INT_MIN, oneNeg = INT_MIN;
  for (int i = k; i < n; i++)
    if (v1[i] >= 0) {
      onePos = v1[i];
      break;
    }
  for (int i = k; i < n; i++) {
    if (v1[i] < 0) {
      oneNeg = v1[i];
      break;
    }
  }
  if (oneNeg == INT_MIN) {
    v1[neg] = onePos;
    RT(gt() << endl);
  }
  if (v1[pos] * onePos > v1[neg] * oneNeg)
    v1[neg] = onePos;
  else
    v1[pos] = oneNeg;
  RT(gt() << endl);
  return 0;
}


Error: WA

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
// #include"testlib.h"
#define endl "\\n"
#define all(v) v.begin(), v.end()
#define allr(s) s.rbegin(), s.rend()
#define RT(s) return cout << s, 0
#define sz(s) (int)(s.size())
#define PI acos(-1)
#define EPS 1e-8
#define watch(x) cout << (#x) << " = " << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
void file() {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#else
  // freopen("street.in", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
}
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
}
int mod = 1e9 + 7;
int n, k;
vector<ll> v1;
ll gt() {
  ll ret = 1;
  for (int i = 0; i < k; i++) {
    ret *= v1[i];
    ret %= mod;
  }
  ret += mod;
  return ret % mod;
}
int main() {
  file();
  fast();
  cin >> n >> k;
  v1 = vector<ll>(n);
  for (auto &it : v1)
    cin >> it;
  auto cmp = [](const ll &a, const ll &b) -> bool { return abs(a) > abs(b); };
  sort(all(v1), cmp);
  int ok = 0, neg = -1, pos = -1;
  for (int i = 0; i < k; i++) {
    if (v1[i] < 0)
      ok = 1 - ok, neg = i;
    else
      pos = i;
  }

  if (!ok)
    RT(gt() << endl);
  if (pos == -1) {
    bool s = 0;
    for (int i = k; i < n; i++)
      if (v1[i] >= 0) {
        s = 1;
        swap(v1[neg], v1[pos]);
        break;
      }
    if (!s)
      reverse(all(v1));
    RT(gt() << endl);
  }
  ok = 0;
  for (int i = k; i < n; i++) {
    if (v1[i] >= 0) {
      ok = 1;
      break;
    }
  }
  if (!ok)
    RT(gt() << endl);
  ll onePos = INT_MIN, oneNeg = INT_MIN;
  for (int i = k; i < n; i++)
    if (v1[i] >= 0) {
      onePos = v1[i];
      break;
    }
  for (int i = k; i < n; i++) {
    if (v1[i] < 0) {
      oneNeg = v1[i];
      break;
    }
  }
  if (v1[pos] * onePos > v1[neg] * oneNeg)
    v1[neg] = onePos;
  else
    v1[pos] = oneNeg;
  RT(gt() << endl);
  return 0;
}