alexjercan/bugnet huggingface dataset:
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ll long long

using namespace std;

// memset(dp, 0, sizeof(dp));

void work(int T) {

  ll n, k, res = 1, M = 1e9 + 7, nn = 0, nz = 0, np = 0;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > 0) {
      np++;
    } else if (a[i] < 0) {
      nn++;
    } else {
      nz++;
    }
  }
  sort(a.begin(), a.end());
  if (k % 2 == 1 && a[n - 1] == 0) {
    cout << 0 << endl;
    return;
  }
  if (k % 2 == 1 && a[n - 1] < 0) {
    while (k) {
      res = res * a[--n] % M;
      k--;
    }
    cout << (res + M) % M << endl;
    return;
  }
  int l = 0, t = n - 1, z = lower_bound(a.begin(), a.end(), 0) - a.begin(),
      r = upper_bound(a.begin(), a.end(), 0) - a.begin();
  while (k) {
    if (k >= 2 && n - l >= 2 &&
        (n - l < 2 || a[n - 1] * a[n - 2] <= (a[l] * a[l + 1]))) {
      res = res * a[l] % M;
      res = res * a[l + 1] % M;
      l += 2;
      k -= 2;
    } else {
      res = res * a[n - 1] % M;
      k--;
      n--;
    }
  }

  cout << (res + M) % M << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  // int t;
  // for(cin >> t; t > 0; t--) {
  // work(t);
  // }
  work(0);
  return 0;
};

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ll long long

using namespace std;

// memset(dp, 0, sizeof(dp));

void work(int T) {

  ll n, k, res = 1, M = 1e9 + 7, nn = 0, nz = 0, np = 0;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > 0) {
      np++;
    } else if (a[i] < 0) {
      nn++;
    } else {
      nz++;
    }
  }
  sort(a.begin(), a.end());

  if (np + nn / 2 * 2 < k) {
    cout << 0 << endl;
    return;
  }
  if (k % 2 == 1 && a[n - 1] < 0) {
    while (k) {
      res = res * a[--n] % M;
      k--;
    }
    cout << (res + M) % M << endl;
    return;
  }
  int l = 0, z = lower_bound(a.begin(), a.end(), 0) - a.begin(),
      r = upper_bound(a.begin(), a.end(), 0) - a.begin();
  while (k >= 2 && z - l >= 2 &&
         (n - r < 2 || a[r] * a[r + 1] < (a[l] * a[l + 1]))) {
    res = res * a[l] % M * a[l + 1] % M;
    l += 2;
    k -= 2;
  }
  while (k) {
    res = res * a[r++] % M;
    k--;
  }

  cout << (res + M) % M << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  // int t;
  // for(cin >> t; t > 0; t--) {
  // work(t);
  // }
  work(0);
  return 0;
};
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using p = pair<ll, ll>;
constexpr ll INF = 1LL << 62;
constexpr ll MOD = 1e9 + 7;

int main(void) {
  ll N, K;
  cin >> N >> K;

  vector<ll> A(N);
  for (auto &&a : A) {
    cin >> a;
  }

  if (K == N) {
    cout << accumulate(A.begin(), A.end(), 1,
                       [](ll l, ll r) { return ((l * r) % MOD + MOD) % MOD; });
    return 0;
  }

  vector<ll> pos, neg;
  for (auto a : A) {
    if (a >= 0) {
      pos.emplace_back(a);
    } else {
      neg.emplace_back(a);
    }
  }

  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end());

  if (pos.size() == 0 && (K % 2)) {
    ll ans = 1;
    for (ll i = 0; i < K; i++) {
      ans *= neg[N - 1 - i];
      ans = (ans + MOD) % MOD;
    }

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

  ll pptr = 0, mptr = 0;
  ll ans = 1;

  while (pptr + mptr < K) {
    if (pptr + mptr + 2 <= K) {
      if (pptr >= pos.size()) {
        ans *= (neg[mptr] * neg[mptr + 1]) % MOD;
        mptr += 2;
      } else if (mptr + 1 >= neg.size()) {
        ans *= pos[pptr++];
      } else {
        ll ppair = pos[pptr] * pos[pptr + 1];
        ll mpair = neg[mptr] * neg[mptr + 1];
        if (ppair > mpair) {
          ans *= pos[pptr++];
        } else {
          ans *= mpair % MOD;
          mptr += 2;
        }
      }
    } else {
      ans *= pos[pptr++];
    }
    ans = (ans + MOD) % MOD;
  }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using p = pair<ll, ll>;
constexpr ll INF = 1LL << 62;
constexpr ll MOD = 1e9 + 7;

int main(void) {
  ll N, K;
  cin >> N >> K;

  vector<ll> A(N);
  for (auto &&a : A) {
    cin >> a;
  }

  if (K == N) {
    cout << accumulate(A.begin(), A.end(), 1,
                       [](ll l, ll r) { return ((l * r) % MOD + MOD) % MOD; });
    return 0;
  }

  vector<ll> pos, neg;
  for (auto a : A) {
    if (a >= 0) {
      pos.emplace_back(a);
    } else {
      neg.emplace_back(a);
    }
  }

  sort(pos.begin(), pos.end(), greater<ll>());
  sort(neg.begin(), neg.end());

  if (pos.size() == 0 && (K % 2)) {
    ll ans = 1;
    for (ll i = 0; i < K; i++) {
      ans *= neg[N - 1 - i];
      ans = (ans + MOD) % MOD;
    }

    cout << "hoge" << endl;

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

  ll pptr = 0, mptr = 0;
  ll ans = 1;

  while (pptr + mptr < K) {
    if (pptr + mptr + 2 <= K) {
      if (pptr >= pos.size()) {
        ans *= (neg[mptr] * neg[mptr + 1]) % MOD;
        mptr++;
      } else if (mptr + 1 >= neg.size()) {
        ans *= pos[pptr++];
      } else {
        ll ppair = pos[pptr] * pos[pptr + 1];
        ll mpair = neg[mptr] * neg[mptr + 1];
        if (ppair > mpair) {
          ans *= pos[pptr++];
        } else {
          ans *= mpair % MOD;
          mptr += 2;
        }
      }
    } else {
      ans *= pos[pptr++];
    }
    ans = (ans + MOD) % MOD;
  }

  cout << ans << endl;

  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;

const ll mod = 1e9 + 7;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N), p, n;
  rep(i, N) {
    cin >> A[i];
    if (A[i] >= 0)
      p.push_back(A[i]);
    else
      n.push_back(A[i]);
  }

  int pn = p.size();
  int nn = n.size();
  bool ok = false;
  if (pn > 0) {
    if (N == K)
      ok = (nn % 2 == 0);
    else
      ok = true;
  } else {
    ok = (K % 2 == 0);
  }

  ll ans = 1;
  if (ok) {
    sort(p.begin(), p.end());
    sort(n.rbegin(), n.rend());
    if (K % 2 == 1) {
      ans *= p.back();
      p.pop_back();
    }
    vector<ll> t;
    while (p.size() >= 2) {
      ll x = p.back();
      p.pop_back();
      x *= p.back();
      p.pop_back();
      t.push_back(x);
    }
    while (n.size() >= 2) {
      ll x = n.back();
      n.pop_back();
      x *= n.back();
      n.pop_back();
      t.push_back(x);
    }
    sort(t.rbegin(), t.rend());
    rep(i, K / 2) { ans = ((ans % mod) * (t[i] % mod)) % mod; }
  } else {
    sort(A.begin(), A.end(), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, K) {
      ans = (ans * A[i]) % mod;
      if (ans < 0)
        ans += mod;
    }
  }

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> a(n);

  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end());

  int pn = 0, mn = 0;
  vector<int> plus, minus;
  rep(i, n) {
    if (a[i] > 0)
      pn++, plus.push_back(a[i]);
    if (a[i] <= 0)
      mn--, minus.push_back(a[i]);
  }

  vector<int> t(n);
  rep(i, n) t[i] = abs(a[i]);

  sort(t.begin(), t.end());
  sort(minus.begin(), minus.end(), greater<int>());

  ll ans = 1;
  if (pn >= k || (pn - k) % 2 == 0) {
    for (int i = 0; i < k; i++) {
      ans *= t[n - 1 - i];
      ans %= mod;
    }
  } else {
    for (int i = 0; i < pn; i++) {
      ans *= plus[i];
    }
    for (int i = pn; i < k; i++) {
      ans *= minus[i];
      ans %= mod;
    }
  }

  if (ans < 0) {
    ans += mod;
  }
  cout << ans << endl;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int N = 2E5 + 10, mod = 1000000007;
typedef long long LL;
int a[N];
LL res = 1;

int main() {
  int n, k;
  cin >> n >> k;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);

  int sign = 1;
  if (n == k) {
    for (int i = 0; i < n; i++)
      (res *= a[i]) %= mod;
  } // n==k时
  else {
    int l = 0, r = n - 1;
    if (k % 2) // 奇数情况
    {
      res = a[r--], k--;
      if (res < 0)
        sign = -1; // 唯一的全负状况 取最小
    }
    while (k) // 转化为偶数状态处理
    {
      LL ll = (LL)a[l] * a[l + 1], rr = (LL)a[r] * a[r - 1];
      if (ll * sign > rr * sign) {
        res = ll % mod * res % mod;
        l += 2;
      } // 左右两边哪个绝对值大就选哪个 全负数状态相反
      else {
        res = rr % mod * res % mod;
        r -= 2;
      }
      k -= 2;
    }
  }

  cout << (res + mod) % mod;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;

const int N = 1E5 + 10, mod = 1000000009;
typedef long long LL;
int a[N];
LL res = 1;

int main() {
  int n, k;
  cin >> n >> k;

  for (int i = 0; i < n; i++)
    cin >> a[i];

  sort(a, a + n);

  int sign = 1;
  if (n == k) {
    for (int i = 0; i < n; i++)
      res *= a[i];
  } // n==k时
  else {
    int l = 0, r = n - 1;
    if (k % 2) // 奇数情况
    {
      res = a[r--], k--;
      if (res < 0)
        sign = -1; // 唯一的全负状况 取最小
    }
    while (k) // 转化为偶数状态处理
    {
      LL ll = (LL)a[l] * a[l + 1], rr = (LL)a[r] * a[r - 1];
      if (ll * sign > rr * sign) {
        res = ll % mod * res % mod;
        l += 2;
      } // 左右两边哪个绝对值大就选哪个 全负数状态相反
      else {
        res = rr % mod * res % mod;
        r -= 2;
      }
      k -= 2;
    }
  }

  cout << res;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> a, b;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x >= 0)
      a.push_back(x);
    else
      b.push_back(x);
  }

  sort(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());

  int i, j;
  i = j = 0;
  ll k = 1;
  ll ans = 1;

  if (N == K) {
    for (ll i : a) {
      ans *= i;
      ans %= mod;
    }
    for (ll j : b) {
      ans *= j;
      ans += mod * ((ans * -1 + mod - 1) / mod);
    }
    cout << ans << endl;
    return 0;
  }

  if (K % 2 == 1) {
    if (a.size() == 0) {
      for (int i = 0; i < K; i++) {
        ans *= b[i];
        if (ans >= 0)
          ans %= mod;
        else {
          ans += mod * ((ans * -1 + mod - 1) / mod);
        }
      }
      cout << ans << endl;
      return 0;
    } else {
      ans *= a[0];
      K--;
      i++;
    }
  }

  sort(b.begin(), b.end());
  while (K > 0) {
    if (b.size() <= j + 1 ||
        (a.size() > i + 1 && a[i] * a[i + 1] >= b[j] * b[j + 1])) {
      ans *= a[i];
      ans %= mod;
      ans *= a[i + 1];
      ans %= mod;
      i += 2;
      K -= 2;
    } else {
      ans *= abs(b[j]);
      ans %= mod;
      ans *= abs(b[j + 1]);
      ans %= mod;
      j += 2;
      K -= 2;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1000000007;

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> a, b;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x >= 0)
      a.push_back(x);
    else
      b.push_back(x);
  }

  sort(a.begin(), a.end(), greater<ll>());
  sort(b.begin(), b.end(), greater<ll>());

  int i, j;
  i = j = 0;
  ll k = 1;
  ll ans = 1;

  if (N == K) {
    for (ll i : a) {
      ans *= i;
      ans %= mod;
    }
    for (ll j : b) {
      ans *= j;
      ans += mod * ((ans * -1 + mod - 1) / mod);
    }
    cout << ans << endl;
    return 0;
  }

  if (K % 2 == 1) {
    if (a.size() == 0) {
      for (int i = 0; i < K; i++) {
        ans *= b[i];
        if (ans >= 0)
          ans %= mod;
        else {
          ans += mod * ((ans * -1 + mod - 1) / mod);
        }
      }
      cout << ans << endl;
      return 0;
    } else {
      ans *= a[0];
      K--;
      i++;
    }
  }

  sort(b.begin(), b.end());
  while (K > 0) {
    if (a.size() > i + 1 && a[i] * a[i + 1] >= b[j] * b[j + 1]) {
      ans *= a[i];
      ans %= mod;
      ans *= a[i + 1];
      ans %= mod;
      i += 2;
      K -= 2;
    } else {
      ans *= abs(b[j]);
      ans %= mod;
      ans *= abs(b[j + 1]);
      ans %= mod;
      j += 2;
      K -= 2;
    }
  }
  cout << ans << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 2e5 + 10, mod = 1e9 + 7;
ll n, k, a[N], ans, flag;

int main() {
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a, a + n, greater<ll>());
  ll l = 0, r = n - 1;
  ans = 1;
  flag = 1; // 注意如果开始k是偶数，flag要初始化
  if (k % 2)
    ans = a[l], l++, flag = (ans >= 0 ? 1 : -1), k--;
  // cout<<ans<<" f"<<flag<<endl;
  while (k) {
    // cout<<l<<" "<<r<<endl;
    ll x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (flag * x > flag * y)
      ans = ans * (x % mod) % mod, l += 2;
    else
      ans = ans * (y % mod) % mod, r -= 2;
    k -= 2;
    // cout<<x<<" "<<y<<" "<<ans<<endl;
  }
  cout << (ans + mod) % mod << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N = 2e5 + 10, mod = 1e9 + 7;
ll n, k, a[N], ans, flag;

int main() {
  cin >> n >> k;
  for (ll i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
  }
  sort(a, a + n, greater<ll>());
  ll l = 0, r = n - 1;
  ans = 1;
  if (k % 2)
    ans = a[l], l++, flag = (ans >= 0 ? 1 : -1);
  while (k) {
    if (flag * a[l] * a[l + 1] > flag * a[r] * a[r - 1])
      ans = ans * a[l] % mod * a[l + 1] % mod, l += 2;
    else
      ans = ans * a[r] % mod * a[r - 1] % mod, r -= 2;
    k -= 2;
  }
  cout << ans << endl;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;
//~ const int MOD=998244353;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

ll a[300000];

int main() {
  int n, K;
  cin >> n >> K;
  bool b = 0;
  rep(i, n) {
    scanf("%lld", &a[i]);
    if (a[i] >= 0)
      b = true;
  }
  if (n == K) {
    ll ans = 1;
    rep(i, n) { (ans *= a[i]) %= MOD; }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  if (!b && (K % 2 == 1)) {
    sort(a, a + n, greater<>());
    ll ans = 1;
    rep(i, K) { (ans *= a[i]) %= MOD; }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  priority_queue<ll> A, B;
  rep(i, n) {
    if (a[i] >= 0)
      A.push(a[i]);
    else
      B.push(-a[i]);
  }
  priority_queue<ll, vector<ll>, greater<ll>> usedA, usedB;
  rep(i, K) {
    if (!A.empty() && (B.empty() || A.top() > B.top()))
      usedA.push(A.top()), A.pop();
    else
      usedB.push(B.top()), B.pop();
  }
  if (!usedA.empty() && usedA.top() == 0) {
    puts("0");
    return 0;
  }
  if (usedB.size() % 2 == 1) {
    if (!B.empty() && !usedA.empty() &&
        (A.empty() || B.top() * usedB.top() > A.top() * usedA.top())) {
      usedA.pop();
      usedB.push(B.top());
    } else {
      usedB.pop();
      usedA.push(A.top());
    }
  }
  ll ans = 1;
  while (!usedA.empty())
    (ans *= usedA.top()) %= MOD, usedA.pop();
  while (!usedB.empty())
    (ans *= usedB.top()) %= MOD, usedB.pop();
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const int MOD = 1000000007;
//~ const int MOD=998244353;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3f;

ll a[300000];

int main() {
  int n, K;
  cin >> n >> K;
  bool b = 0;
  rep(i, n) {
    scanf("%lld", &a[i]);
    if (a[i] >= 0)
      b = true;
  }
  if (!b && (K % 2 == 1)) {
    sort(a, a + n, greater<>());
    ll ans = 1;
    rep(i, K) { (ans *= a[i]) %= MOD; }
    cout << (ans + MOD) % MOD << endl;
    return 0;
  }
  priority_queue<ll> A, B;
  rep(i, n) {
    if (a[i] >= 0)
      A.push(a[i]);
    else
      B.push(-a[i]);
  }
  priority_queue<ll, vector<ll>, greater<ll>> usedA, usedB;
  rep(i, K) {
    if (!A.empty() && (B.empty() || A.top() > B.top()))
      usedA.push(A.top()), A.pop();
    else
      usedB.push(B.top()), B.pop();
  }
  if (!usedA.empty() && usedA.top() == 0) {
    puts("0");
    return 0;
  }
  if (usedB.size() % 2 == 1) {
    if (!B.empty() &&
        (A.empty() || B.top() * usedB.top() > A.top() * usedA.top())) {
      usedA.pop();
      usedB.push(B.top());
    } else {
      usedB.pop();
      usedA.push(A.top());
    }
  }
  ll ans = 1;
  while (!usedA.empty())
    (ans *= usedA.top()) %= MOD, usedA.pop();
  while (!usedB.empty())
    (ans *= usedB.top()) %= MOD, usedB.pop();
  cout << ans << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
#define pans (((long long)pow(10, 9)) + (long long)7)
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  long long n, k;
  long long ans = 1;
  cin >> n >> k;
  int s = 0, e = n - 1;
  long long t;
  bool f = false;
  vector<long long> A(n);
  rep(i, n) {
    cin >> t;
    A[i] = t % pans;
  }
  sort(A.rbegin(), A.rend());
  if (k % 2 != 0) {
    if (A[s] < 0)
      f = true;
    ans *= A[s];
    s++;
  }
  rep(i, int(k / 2)) {
    if (k == 1)
      break;
    if (A[s] * A[s + 1] > A[e] * A[e - 1] || f == true) {
      ans *= (A[s] * A[s + 1]) % pans;
      s += 2;
    } else {
      ans *= (A[e] * A[e - 1]) % pans;
      e -= 2;
    }
    ans %= pans;
  }
  if (ans < 0)
    ans += pans;
  printf("%lld", ans);
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int main() {
  long long n, k;
  long long ans = 1;
  int s = 0, e = n - 1;
  cin >> n >> k;
  vector<long long> A(n);
  rep(i, n) cin >> A[i];
  sort(A.rbegin(), A.rend());
  if (k % 2 != 0) {
    ans *= A[s];
    s++;
  }
  rep(i, k / 2) {
    if (k == 1)
      break;
    if (A[s] * A[s + 1] > A[e] * A[e - 1]) {
      ans *= A[s] * A[s + 1];
      s += 2;
    } else {
      ans *= A[e] * A[e - 1];
      e -= 2;
    }
  }
  printf("%d", ans % int(((pow(10, 9)) + 1)));
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define in(x, l, r) (ll)(l) <= (x) && (x) < (ll)(r)

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> A(N);
  vector<ll> B(0), C(0);
  rep(i, 0, N) {
    int n;
    cin >> n;
    A[i].second = n;
    if (n >= 0) {
      A[i].first = n;
      B.push_back(n);
    } else {
      A[i].first = -n;
      C.push_back(n);
    }
  }
  rep(i, 0, N) A[i].first = A[i].second > 0 ? A[i].second : -A[i].second;
  sort(all(A));
  sort(all(B));
  sort(all(C));

  ll ans = 1;
  int m = C.size();
  int M = 1000000007;

  if ((m == N && K % 2) || (K == N && m % 2)) {
    // 積は負
    rep(i, 0, K) {
      ans *= A[i].second;
      ans %= M;
    }
    if (ans < 0)
      ans += M;
  } else {
    // 正
    reverse(all(B));
    //        reverse(all(C));
    int m_cur = min(K, m);
    if (m_cur % 2)
      m_cur--;
    int p_cur = K - m_cur;
    while (m_cur > 1 && p_cur + 2 <= N - m) {
      if (B[p_cur] * B[p_cur + 1] < C[m_cur - 1] * C[m_cur - 2])
        break;
      m_cur -= 2;
      p_cur += 2;
    }

    rep(i, 0, p_cur) {
      ans *= B[i];
      ans %= M;
    }
    rep(i, 0, m_cur) {
      ans *= C[i];
      ans %= M;
    }
    if (ans < 0)
      ans += M;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define in(x, l, r) (ll)(l) <= (x) && (x) < (ll)(r)

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> A(N);
  vector<ll> B(0), C(0);
  rep(i, 0, N) {
    int n;
    cin >> n;
    A[i].second = n;
    if (n >= 0) {
      A[i].first = n;
      B.push_back(n);
    } else {
      A[i].first = -n;
      C.push_back(n);
    }
  }
  rep(i, 0, N) A[i].first = A[i].second > 0 ? A[i].second : -A[i].second;
  sort(all(A));
  sort(all(B));
  sort(all(C));

  ll ans = 1;
  int m = C.size();
  int M = 1000000007;

  if ((m == N && K % 2) || (K == N && m % 2)) {
    // 積は負
    rep(i, 0, K) {
      ans *= A[i].second;
      ans %= M;
    }
    if (ans < 0)
      ans += M;
  } else {
    // 正
    reverse(all(B));
    reverse(all(C));
    int m_cur = min(K, m);
    if (m % 2)
      m_cur--;
    int p_cur = K - m_cur;
    while (m_cur > 1 && p_cur + 2 <= N - m) {
      if (B[p_cur] * B[p_cur + 1] < C[m_cur - 1] * C[m_cur - 2])
        break;
      m_cur -= 2;
      p_cur += 2;
    }

    rep(i, 0, p_cur) {
      ans *= B[i];
      ans %= M;
    }
    rep(i, 0, m_cur) {
      ans *= C[i];
      ans %= M;
    }
    if (ans < 0)
      ans += M;
  }
  cout << ans << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;
#define fr first
#define sc second
#define pii pair<int, int>
#define all(v) v.begin(), v.end()

using namespace std;

const ll MOD = 1000000007;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;

  vector<ll> pos, neg;

  for (int i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    if (temp >= 0) {
      pos.push_back(temp);
    } else {
      neg.push_back(-1 * temp);
    }
  }
  sort(all(pos));
  sort(all(neg));

  int i = (int)pos.size() - 1;
  int j = (int)neg.size() - 1;

  int rem = k;
  ll ans = 1;

  if (i == -1 && k % 2 == 1) {
    for (i = 0; i < k; i++) {
      ans = (ans * neg[i]) % MOD;
    }
    ans = (ans * (MOD - 1)) % MOD;
  } else {
    while (rem > 0) {
      if (rem > 1 && j > 0 &&
          (i < 1 || neg[j] * neg[j - 1] > pos[i] * pos[i - 1])) {
        // if  {
        ans = (ans * neg[j]) % MOD;
        j--;
        ans = (ans * neg[j]) % MOD;
        j--;
        rem -= 2;
        // }
      } else {
        if (i >= 0) {
          ans = (ans * pos[i]) % MOD;
          i--;
        } else {
          ans = (ans * neg[j]) % MOD;
          ans = (ans * (MOD - 1)) % MOD;
          j--;
        }
        rem--;
      }
    }
  }

  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;
#define fr first
#define sc second
#define pii pair<int, int>
#define all(v) v.begin(), v.end()

using namespace std;

const ll MOD = 1000000007;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, k;
  cin >> n >> k;

  vector<ll> pos, neg;

  for (int i = 0; i < n; i++) {
    ll temp;
    cin >> temp;
    if (temp >= 0) {
      pos.push_back(temp);
    } else {
      neg.push_back(-1 * temp);
    }
  }
  sort(all(pos));
  sort(all(neg));

  int i = (int)pos.size() - 1;
  int j = (int)neg.size() - 1;

  int rem = k;
  ll ans = 1;

  if (i == -1 && k % 2 == 1) {
    for (i = 0; i < k; i++) {
      ans = (ans * neg[i]) % MOD;
    }
    ans = (ans * (MOD - 1)) % MOD;
  } else {
    while (rem > 0) {
      if (rem > 1 && j > 0) {
        if (i < 1 || neg[j] * neg[j - 1] > pos[i] * pos[i - 1]) {
          ans = (ans * neg[j]) % MOD;
          j--;
          ans = (ans * neg[j]) % MOD;
          j--;
          rem -= 2;
        }
      } else {
        if (i >= 0) {
          ans = (ans * pos[i]) % MOD;
          i--;
        } else {
          ans = (ans * neg[j]) % MOD;
          ans = (ans * (MOD - 1)) % MOD;
          j--;
        }
        rem--;
      }
    }
  }

  cout << ans;

  return 0;
}