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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 2e5 + 5, M = 1e6 + 6, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
int tc;
ll arr[N];
int main() {
  FIO int n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    // cout<<arr[i]<<endl;
    if (arr[i] >= 0)
      pos.push_back(arr[i]);
    else
      neg.push_back(arr[i]);
  }
  // cout<<(int) pos.size() <<" "<<(int) neg.size()<<endl;
  // for(auto x : pos)
  //     cout<<x<<" ";
  // cout<<endl;
  sort(arr, arr + n);
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  if (k == 1)
    return cout << arr[n - 1], 0;
  if (k == n) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ans = ((ans * llabs(arr[i]))) % MOD;
    }
    if ((int)neg.size() % 2)
      cout << MOD - ans << endl;
    else
      cout << ans << endl;
    return 0;
  }
  if (pos.empty()) {
    // cout<<"HERE";
    ll res = 1;
    if (k % 2) {
      reverse(neg.begin(), neg.end());
      for (int i = 0; i < k; i++) {
        res = ((res * -neg[i])) % MOD;
      }
      cout << MOD - res << endl;
    } else {
      for (int i = 0; i < k; i++) {
        res = ((res * -neg[i])) % MOD;
        // cout<<res<<endl;
      }
      cout << res << endl;
    }
  } else if (neg.empty()) {
    reverse(pos.begin(), pos.end());
    ll res = 1;
    for (int i = 0; i < k; i++) {
      // cout<<pos[i] <<" "<<res<<endl;
      res = ((res * pos[i]) + MOD) % MOD;
    }
    cout << res << endl;
    return 0;
  } else {
    // sort(arr, arr+n, [](const ll &a, const ll &b){
    //     return abs(a) < abs(b);
    // });
    vector<ll> vec;
    int np = 0, rp = (int)pos.size() - 1;
    while ((int)vec.size() < k) {
      if (np == (int)neg.size()) {
        vec.push_back(pos[rp]);
        rp--;
        continue;
      }
      if (rp == -1) {
        vec.push_back(neg[np]);
        np++;
        continue;
      }
      if (-neg[np] > pos[rp]) {
        vec.push_back(neg[np]);
        np++;
      } else {
        vec.push_back(pos[rp]);
        rp--;
      }
    }
    int cntneg = 0;
    bool isZ = false;
    for (auto x : vec) {
      isZ == (x == 0);
      cntneg += x < 0;
      if (isZ)
        break;
    }
    if (isZ)
      return cout << 0, 0;
    if (cntneg % 2 == 0) {
      ll ans = 1;
      for (auto x : vec) {
        ans = (ans * abs(x)) % MOD;
      }
      cout << ans << endl;
    } else {
      ll ans = 1;
      ll y2 = np == (int)neg.size()
                  ? 1
                  : neg[np];           // largest negative element not taken
      ll y1 = rp == -1 ? -1 : pos[rp]; // largest postivie element not taken
      ll x1 = 1, x2 = -1; // smallest negative value taken (absoulute), smallest
                          // pos value taken
      reverse(vec.begin(), vec.end());
      for (auto x : vec) {
        if (x < 0) {
          x1 = x;
          break;
        }
      }
      for (auto x : vec) {
        if (x > 0) {
          x2 = x;
          break;
        }
      }
      // cout<<"x1: "<<x1<<" "<<"x2: "<<x2<<" "<<"y1: "<<y1<<" "<<"y2:
      // "<<y2<<endl;
      if (x1 == 1 || y1 == -1) {
        for (int i = 0; i < (int)vec.size(); i++) {
          if (vec[i] == x2) {
            vec[i] = 1;
            break;
          }
        }
        vec.push_back(y2);
        // cout<<"DONE"<<endl;
        for (auto x : vec)
          ans = (ans * abs(x)) % MOD;

        cout << ans << endl;
        return 0;
      }
      if (x2 == (ll)-1 || y2 == (ll)1) {
        // cout<<x2<<" "<<y2<<endl;
        for (int i = 0; i < (int)vec.size(); i++) {
          if (vec[i] == x1) {
            vec[i] = 1;
            break;
          }
        }
        vec.push_back(y1);

        for (auto x : vec)
          ans = (ans * abs(x)) % MOD;

        cout << ans << endl;
        return 0;
      } else {
        if (llabs(x2 * y1) <= llabs(x1 * y2)) {
          // cout<<1<<endl;
          for (int i = 0; i < (int)vec.size(); i++) {
            if (vec[i] == x2) {
              vec[i] = 1;
              break;
            }
          }
          vec.push_back(y2);

          for (auto x : vec)
            ans = (ans * abs(x)) % MOD;

          cout << ans << endl;
          return 0;
        } else {
          // cout<<2<<endl;
          for (int i = 0; i < (int)vec.size(); i++) {
            if (vec[i] == x1) {
              vec[i] = 1;
              break;
            }
          }
          vec.push_back(y1);

          for (auto x : vec)
            ans = (ans * abs(x)) % MOD;

          cout << ans << endl;
          return 0;
        }
      }
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);

const int N = 2e5 + 5, M = 1e6 + 6, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
int tc;
ll arr[N];
int main() {
  FIO int n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] >= 0)
      pos.push_back(arr[i]);
    else
      neg.push_back(arr[i]);
  }
  sort(arr, arr + n);
  sort(pos.begin(), pos.end());
  sort(neg.begin(), neg.end());
  if (k == 1)
    return cout << arr[n - 1], 0;
  if (pos.empty()) {
    ll res = 1;
    if (k % 2)
      for (int i = 0; i < k; i++)
        res = ((res * neg[n - i - 1]) + MOD) % MOD;
    else
      for (int i = 0; i < k; i++)
        res = ((res * neg[i]) + MOD) % MOD;

    cout << res << endl;
  } else if (neg.empty()) {
    ll res = 1;
    for (int i = 0; i < k; i++)
      res = ((res * pos[n - i - 1])) % MOD;
    cout << res << endl;
  } else {
    vector<int> vec;
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      vec.push_back(arr[n - i - 1]);
      ans *= vec.back();
    }
    reverse(vec.begin(), vec.end());
    for (int i = 0; i < n - k - 1; i++) {
      ll tmpans = ans;
      tmpans /= (vec[i] * vec[i + 1]);
      tmpans *= (arr[i] * arr[i + 1]);
      if (tmpans > ans)
        ans = tmpans;
      else
        break;
    }
    cout << ans << endl;
  }

  return 0;
}