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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

typedef long long ll;

const int MOD = 1e9 + 7;

int add(int a, int b) { return (a + b) % MOD; }

int mul(int a, int b) { return ((ll)a * b) % MOD; }

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

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &t : a) {
    cin >> t;
  }
  cerr << -1 << endl;
  vector<int> less;
  vector<int> gr;
  for (int i = 0; i < n; i++) {
    if (a[i] < 0) {
      less.push_back(a[i]);
    } else {
      gr.push_back(a[i]);
    }
  }
  cerr << less.size() << endl;
  if ((k % 2 == 1 && (int)less.size() == n) ||
      (k == n && less.size() % 2 == 1)) {
    sort(a.begin(), a.end(), [&](int i, int j) { return abs(i) < abs(j); });
    int ans = 1;
    for (int i = 0; i < k; i++) {
      ans = mul(ans, a[i] + MOD);
    }
    cout << ans << endl;
    exit(0);
  }
  cerr << "hr " << endl;
  sort(less.begin(), less.end());
  sort(gr.rbegin(), gr.rend());
  if ((int)less.size() & 1)
    less.pop_back();
  vector<pair<int, int>> tk;
  for (int i = 0; i + 1 < (int)less.size(); i += 2) {
    tk.push_back({less[i] * less[i + 1], -(i + 2)});
  }
  cerr << "hr " << endl;
  // if ((k & 1) && gr.size() % 2 == 0) gr.pop_back();
  for (int i = 0; i + 1 + (k & 1) < (int)gr.size(); i += 2) {
    tk.push_back({gr[i] * gr[i + 1], (i + 2)});
  }
  sort(tk.rbegin(), tk.rend());
  int mx = 0;
  int ans = 1;
  for (auto t : tk) {
    if (k > 1) {
      ans = mul(ans, t.first % MOD);
      mx = max(mx, t.second);
      k -= 2;
    }
  }
  if (k) {
    ans = mul(ans, gr[mx]);
  }
  cout << ans << endl;
}

Error: 0
-1
hr 
hr 


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long

typedef long long ll;

const int MOD = 1e9 + 7;

int add(int a, int b) { return (a + b) % MOD; }

int mul(int a, int b) { return ((ll)a * b) % MOD; }

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

  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (auto &t : a) {
    cin >> t;
  }
  cerr << -1 << endl;
  vector<int> less;
  vector<int> gr;
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] < 0) {
      less.push_back(a[i]);
    } else if (a[i] == 0) {
      c0++;
    } else {
      gr.push_back(a[i]);
    }
  }
  if ((k % 2 == 1 && (int)less.size() == n)) {
    sort(less.rbegin(), less.rend());
    int ans = 1;
    for (int i = 0; i < k; i++) {
      ans = mul(ans, a[i] + MOD);
    }
    cout << ans << endl;
    exit(0);
  }
  cerr << "hr " << endl;
  sort(less.begin(), less.end());
  if ((int)less.size() & 1)
    less.pop_back();
  vector<pair<int, int>> tk;
  for (int i = 0; i + 1 < (int)less.size(); i += 2) {
    tk.push_back({less[i] * less[i + 1], -(i + 2)});
  }
  cerr << "hr " << endl;
  for (int i = 0; i + 1 < (int)gr.size(); i += 2) {
    tk.push_back({gr[i] * gr[i + 1], (i + 2)});
  }
  sort(tk.rbegin(), tk.rend());
  int mx = 0;
  int ans = 1;
  for (auto t : tk) {
    if (k > 1) {
      ans = mul(ans, t.first % MOD);
      mx = max(mx, t.second);
      k -= 2;
    }
  }
  if (k) {
    ans = mul(ans, gr[mx]);
  }
  cout << ans << endl;
}