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

/* aman36 - Aman Raj */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define fast_io                                                                \\
  ios ::sync_with_stdio(false);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define AfterDecimal(n) cout << fixed << setprecision(n);
#define pb push_back
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define F first
#define S second
#define bits(x) __builtin_popcount(x)
#define fori(n) for (ll i = 0; i < n; i++)
#define forj(n) for (ll j = 0; j < n; j++)
#define fork(n) for (ll k = 0; k < n; k++)
#define forlp(k, x, n) for (ll k = x; k <= n; k++)
#define vll vector<ll>
#define mll map<ll, ll>
#define vpll vector<pll>
#define vi vector<int>
#define mem(x, v) memset(x, v, sizeof(x));
#define int ll
#define endl "\\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;  // Min Heap

// template <typename T> using ordered_set =
//  tree<T, null_type, less<T>, rb_tree_tag,
//  tree_order_statistics_node_update>;

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << "# " << name << " = " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout << "# ";
  cout.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...) 36
#endif

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}

const ll INF = (ll)(8e18);
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const int M = 3000 + 5;
const int N = 3e5 + 5;
const int maxn = 1e5 + 5;

void solve() {
  int n, k;
  cin >> n >> k;

  int f = 0;
  int a[n];
  fori(n) {
    cin >> a[i];
    if (a[i] > 0)
      f = 1;
  }

  auto cmp = [](int x, int b) { return abs(x) > abs(b); };

  sort(a, a + n, cmp);

  vll pos, neg;

  if (f == 0) {
    if (k % 2 == 0) {
      for (int i = 0; i < k; i++) {
        neg.pb(a[i]);
      }
    } else {
      int c = 0;
      for (int i = n - 1; i >= 0 && c < k; i--, c++) {
        neg.pb(a[i]);
      }
    }

    int ans = 1;

    for (auto x : neg) {
      ans *= x;
      ans %= MOD;
      if (ans < 0)
        ans += MOD;
    }

    cout << ans << endl;
    return;
  }

  int i = 0;
  for (; i < k; i++) {
    if (a[i] < 0)
      neg.pb(a[i]);
    else
      pos.pb(a[i]);
  }

  if (i != n && (int)neg.size() % 2 != 0) {
    int r = 0;
    int index = i;
    for (int j = index; j < n; j++) {
      if (a[j] < 0) {
        r = a[j];
        break;
      }
    }

    int s = 0;
    int j = index;
    for (; j < n; j++) {
      if (a[j] > 0) {
        s = a[j];
        break;
      }
    }

    int p1 = r * neg[(int)neg.size() - 1];
    int t = -1;

    if (pos.empty()) {
      neg.pop_back();
      pos.pb(s);
    } else {
      t = pos[(int)pos.size() - 1];
      int p2 = s * t;

      if (p1 > p2) {
        pos.pop_back();
        neg.pb(r);
      } else {
        neg.pop_back();
        pos.pb(s);
      }
    }
  }

  int ans = 1;
  for (auto x : pos) {
    ans *= x;
    ans %= MOD;
  }
  for (auto x : neg) {
    ans *= x;
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
  }

  cout << ans << endl;
}

signed main() {
  fast_io;
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif

  int tc = 1;
  // cin >> tc;

  for (int i = 1; i <= tc; i++) {
    // cout << "Case #" << i << ": " ;
    solve();
  }

#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif

  return 0;
}

// read the question correctly
// read code carefully
// corner cases // int vs ll // cin vs scanf // clear structures // maths
// check if max, min set to INT_MAX instead of INF
// check constraint

Error: 0

Fixed submission:

/* aman36 - Aman Raj */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define fast_io                                                                \\
  ios ::sync_with_stdio(false);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define AfterDecimal(n) cout << fixed << setprecision(n);
#define pb push_back
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define pll pair<ll, ll>
#define pii pair<int, int>
#define F first
#define S second
#define bits(x) __builtin_popcount(x)
#define fori(n) for (ll i = 0; i < n; i++)
#define forj(n) for (ll j = 0; j < n; j++)
#define fork(n) for (ll k = 0; k < n; k++)
#define forlp(k, x, n) for (ll k = x; k <= n; k++)
#define vll vector<ll>
#define mll map<ll, ll>
#define vpll vector<pll>
#define vi vector<int>
#define mem(x, v) memset(x, v, sizeof(x));
#define int ll
#define endl "\\n"

// priority_queue<ll, vector<ll>, less<ll>>pq;     // Max Heap
// priority_queue<ll, vector<ll>, greater<ll>>pq;  // Min Heap

// template <typename T> using ordered_set =
//  tree<T, null_type, less<T>, rb_tree_tag,
//  tree_order_statistics_node_update>;

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << "# " << name << " = " << arg1 << '\\n';
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout << "# ";
  cout.write(names, comma - names) << " = " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define debug(...) 36
#endif

ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  return true;
}

const ll INF = (ll)(8e18);
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const int M = 3000 + 5;
const int N = 3e5 + 5;
const int maxn = 1e5 + 5;

void solve() {
  int n, k;
  cin >> n >> k;

  int f = 0;
  int a[n];
  fori(n) {
    cin >> a[i];
    if (a[i] > 0)
      f = 1;
  }

  auto cmp = [](int x, int b) { return abs(x) > abs(b); };

  sort(a, a + n, cmp);

  vll pos, neg;

  if (f == 0) {
    if (k % 2 == 0) {
      for (int i = 0; i < k; i++) {
        neg.pb(a[i]);
      }
    } else {
      int c = 0;
      for (int i = n - 1; i >= 0 && c < k; i--, c++) {
        neg.pb(a[i]);
      }
    }

    int ans = 1;

    for (auto x : neg) {
      ans *= x;
      ans %= MOD;
      if (ans < 0)
        ans += MOD;
    }

    cout << ans << endl;
    return;
  }

  int i = 0;
  for (; i < k; i++) {
    if (a[i] < 0)
      neg.pb(a[i]);
    else
      pos.pb(a[i]);
  }

  if ((int)neg.size() % 2 != 0) {
    int r = 0;
    int index = i;
    for (int j = index; j < n; j++) {
      if (a[j] < 0) {
        r = a[j];
        break;
      }
    }

    int s = 0;
    for (int j = index; j < n; j++) {
      if (a[j] > 0) {
        s = a[j];
        break;
      }
    }

    int p1 = r * neg[(int)neg.size() - 1];
    int p2 = s * pos[(int)pos.size() - 1];

    if (p1 > p2) {
      pos.pop_back();
      neg.pb(r);
    } else {
      neg.pop_back();
      pos.pb(s);
    }
  }

  int ans = 1;
  for (auto x : pos) {
    ans *= x;
    ans %= MOD;
  }
  for (auto x : neg) {
    ans *= x;
    ans %= MOD;
    if (ans < 0)
      ans += MOD;
  }

  cout << ans << endl;
}

signed main() {
  fast_io;
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif

  int tc = 1;
  // cin >> tc;

  for (int i = 1; i <= tc; i++) {
    // cout << "Case #" << i << ": " ;
    solve();
  }

#ifdef LOCAL
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif

  return 0;
}

// read the question correctly
// read code carefully
// corner cases // int vs ll // cin vs scanf // clear structures // maths
// check if max, min set to INT_MAX instead of INF
// check constraint