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

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

#define MOD 1000000007

struct solve {
  solve() {}
  ll operator()(int k, vll &a) {
    int n = a.size();
    vll ap, an;

    for (auto m : a) {
      if (m >= 0)
        ap.push_back(m);
      else
        an.push_back(-m);
    }

    if (n == k) {
      ll ans = 1;
      for (auto m : ap)
        ans = (ans * m) % MOD;
      for (auto m : an)
        ans = (ans * m) % MOD;
      if (an.size() & 1) {
        return (MOD - ans) % MOD;
      }
      return ans;
    }

    if (ap.size() == 0) {
      ll ans = 1;
      if (k & 1) {
        sort(an.begin(), an.end());
        for (int i = 0; i < k; i++)
          ans = (ans * an[i]) % MOD;
        return MOD - ans;
      } else {
        sort(an.rbegin(), an.rend());
        for (int i = 0; i < k; i++)
          ans = (ans * an[i]) % MOD;
        return ans;
      }
    }

    sort(ap.begin(), ap.end());
    sort(an.begin(), an.end());
    int api = (int)ap.size() - 1, ani = (int)an.size() - 1;
    ll ans = 1;
    for (; k >= 2;) {
      if (api < 1 || ani < 1)
        break;
      if (ap[api] * ap[api - 1] > an[ani] * an[ani - 1]) {
        ans = (ans * ap[api]) % MOD;
        api -= 1;
        k--;
      } else {
        ans = (((an[ani] * an[ani - 1]) % MOD) * ans) % MOD;
        ani -= 2;
        k -= 2;
      }
    }
    for (; k >= 2 && ani >= 1;) {
      ans = (((an[ani] * an[ani - 1]) % MOD) * ans) % MOD;
      ani -= 2;
      k -= 2;
    }
    for (; k >= 1 && api >= 0;) {
      ans = (ans * ap[api]) % MOD;
      api -= 1;
      k--;
    }

    return ans;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n, k;
    cin >> n >> k;
    if (cin.fail())
      break;
    vll a(n);
    for (auto &m : a)
      cin >> m;
    cout << solve()(k, a) << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

#define PI 3.1415926535897932384626433832795

template <typename X> bool max_u(X &m, X v) {
  if (m < v) {
    m = v;
    return true;
  }
  return false;
}

template <typename X> bool min_u(X &m, X v) {
  if (m > v) {
    m = v;
    return true;
  }
  return false;
}

#define MOD 1000000007

struct solve {
  solve() {}
  ll operator()(int k, vll &a) {
    int n = a.size();
    vll ap, an;

    for (auto m : a) {
      if (m >= 0)
        ap.push_back(m);
      else
        an.push_back(-m);
    }

    if (n == k) {
      ll ans = 1;
      for (auto m : ap)
        ans = (ans * m) % MOD;
      for (auto m : an)
        ans = (ans * m) % MOD;
      if (an.size() & 1) {
        return (MOD - ans) % MOD;
      }
      return ans;
    }

    if (ap.size() == 0) {
      ll ans = 1;
      if (k & 1) {
        sort(an.begin(), an.end());
        for (int i = 0; i < k; i++)
          ans = (ans * an[i]) % MOD;
        return MOD - ans;
      } else {
        sort(an.rbegin(), an.rend());
        for (int i = 0; i < k; i++)
          ans = (ans * an[i]) % MOD;
        return ans;
      }
    }

    sort(ap.begin(), ap.end());
    sort(an.begin(), an.end());
    int api = (int)ap.size() - 1, ani = (int)an.size() - 1;
    ll ans = 1;
    for (; k >= 2;) {
      if (ap[api] * ap[api - 1] > an[ani] * an[ani - 1]) {
        ans = (ans * ap[api]) % MOD;
        api -= 1;
        k--;
        if (api < 1)
          break;
      } else {
        ans = (((an[ani] * an[ani - 1]) % MOD) * ans) % MOD;
        ani -= 2;
        k -= 2;
        if (ani < 1)
          break;
      }
    }
    for (; k >= 2 && ani >= 1;) {
      ans = (((an[ani] * an[ani - 1]) % MOD) * ans) % MOD;
      ani -= 2;
      k -= 2;
    }
    for (; k >= 1 && api >= 0;) {
      ans = (ans * ap[api]) % MOD;
      api -= 1;
      k--;
    }

    return ans;
  }
};

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  for (;;) {
    int n, k;
    cin >> n >> k;
    if (cin.fail())
      break;
    vll a(n);
    for (auto &m : a)
      cin >> m;
    cout << solve()(k, a) << "\\n";
  }
  return 0;
}