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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;
  bool zero = 0;
  VI A, B;
  REP(i, N) {
    int a;
    cin >> a;
    if (a > 0)
      A.push_back(a);
    if (a < 0)
      B.push_back(-a);
    if (a == 0)
      zero = 1;
  }
  int tmp;
  double ans = -1;
  rSORT(A);
  rSORT(B);

  double asum[200010] = {0};
  double bsum[200010] = {0};
  int Asum[200010] = {0};
  int Bsum[200010] = {0};
  Asum[0] = Bsum[0] = 1;
  FOR(i, 0, A.size()) {
    asum[i + 1] += asum[i] + log(A[i]);
    Asum[i + 1] = Asum[i] * A[i] % mod;
  }
  FOR(i, 0, B.size()) {
    bsum[i + 1] += bsum[i] + log(B[i]);
    Bsum[i + 1] = Bsum[i] * B[i] % mod;
  }

  for (int i = 0; i <= B.size(); i += 2) {
    double cnt = bsum[i];
    int j = K - i;
    if (j > A.size() || j < 0)
      continue;
    cnt += asum[j];

    if (cnt > ans) {
      ans = cnt;
      tmp = Asum[j] * Bsum[i] % mod;
    }
  }

  // cout << ans << " " << tmp << endl;
  if (ans == -1) {
    if (zero) {
      cout << 0 << endl;
      return 0;
    }
    reverse(B.begin(), B.end());
    REP(i, B.size()) B[i] *= -1;

    tmp = 1;
    int j = 0;
    while (K--) {
      tmp *= B[j];
      tmp %= mod;
      j++;
      if (j >= B.size())
        break;
    }

    if (K != -1) {
      j = 0;
      SORT(A);
      while (K--) {
        tmp *= A[j];
        tmp %= mod;
        j++;
      }
    }
    tmp += mod;
    tmp %= mod;
  }

  cout << tmp << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, K;
  cin >> N >> K;
  bool zero = 0;
  VI A, B;
  REP(i, N) {
    int a;
    cin >> a;
    if (a > 0)
      A.push_back(a);
    if (a < 0)
      B.push_back(-a);
    if (a == 0)
      zero = 1;
  }
  int tmp;
  double ans = -1;
  rSORT(A);
  rSORT(B);

  double asum[200010] = {0};
  double bsum[200010] = {0};
  int Asum[200010] = {0};
  int Bsum[200010] = {0};
  Asum[0] = Bsum[0] = 1;
  FOR(i, 0, A.size()) {
    asum[i + 1] += asum[i] + log(A[i]);
    Asum[i + 1] = Asum[i] * A[i] % mod;
  }
  FOR(i, 0, B.size()) {
    bsum[i + 1] += bsum[i] + log(B[i]);
    Bsum[i + 1] = Bsum[i] * B[i] % mod;
  }

  for (int i = 0; i <= B.size(); i += 2) {
    double cnt = bsum[i];
    int j = K - i;
    if (j > A.size() || j < 0)
      continue;
    cnt += asum[j];

    if (cnt > ans) {
      ans = cnt;
      tmp = Asum[j] * Bsum[i] % mod;
    }
  }
  if (ans == -1) {
    if (zero) {
      cout << 0 << endl;
      return 0;
    }
    reverse(B.begin(), B.end());
    REP(i, B.size()) B[i] *= -1;
    tmp = 1;
    int j = 0;
    while (K--) {
      tmp *= B[j];
      j++;
      if (j >= B.size())
        break;
    }
    j = 0;
    SORT(A);
    while (K--) {
      tmp *= A[j];
      j++;
    }
    tmp += mod;
    tmp %= mod;
  }

  cout << tmp << endl;

  return 0;
}