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

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;

const long long mod = 1000000007;
vector<long long> pos, neg;
int zero;

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (x > 0)
      pos.push_back(x);
    else if (x < 0)
      neg.push_back(-x);
    else
      zero++;
  }

  if (pos.size() + neg.size() < k) {
    puts("0");
    return 0;
  }

  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());

  int i = 0, j = 0;
  while (i + j < k) {
    if (j == neg.size() || i < pos.size() && pos[i] > neg[j])
      i++;
    else
      j++;
  }

  if (j % 2) {
    long long u1 = 0, v1 = 1;
    long long u2 = 0, v2 = 1;

    if (i < pos.size() && j > 0)
      u1 = pos[i], v1 = neg[j - 1];
    if (i > 0 && j < neg.size())
      u2 = neg[j], v2 = pos[i - 1];
    if (u1 || u2) {
      if (u1 * v2 > v1 * u2)
        i++, j--;
      else
        i--, j++;
    }
  }

  long long a = 1;
  if (j % 2) {
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end());
  }
  for (int p = 0; p < i; p++)
    a = a * pos[p] % mod;
  for (int n = 0; n < j; n++)
    a = a * (mod - neg[n]) % mod;
  if (j % 2 && zero)
    a = 0;

  printf("%lld\\n", a);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;

const long long mod = 1000000007;
vector<long long> pos, neg;
int zero;

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (x > 0)
      pos.push_back(x);
    else if (x < 0)
      neg.push_back(-x);
    else
      zero++;
  }

  if (pos.size() + neg.size() < k) {
    puts("0");
    return 0;
  }

  if (pos.empty()) {
    long long a = 1;
    if (k % 2) {
      sort(neg.begin(), neg.end());
      for (int i = 0; i < k; i++)
        a = a * (mod - neg[i]) % mod;
      if (zero)
        a = 0;
    } else {
      sort(neg.rbegin(), neg.rend());
      for (int i = 0; i < k; i++)
        a = a * (mod - neg[i]) % mod;
    }
    printf("%lld\\n", a);
    return 0;
  }

  sort(pos.rbegin(), pos.rend());
  sort(neg.rbegin(), neg.rend());

  int i = 0, j = 0;
  while (i + j < k) {
    if (i < pos.size() && pos[i] > neg[j])
      i++;
    else
      j++;
  }

  long long a = 1;
  if (j % 2 == 0 || pos.size() + neg.size() == k) {
    for (int p = 0; p < i; p++)
      a = a * pos[p] % mod;
    for (int n = 0; n < j; n++)
      a = a * (mod - neg[n]) % mod;
  } else if (i < pos.size() && j > 0 && pos[i] > neg[j - 1]) {
    for (int p = 0; p < i + 1; p++)
      a = a * pos[p] % mod;
    for (int n = 0; n < j - 1; n++)
      a = a * neg[n] % mod;
  } else {
    for (int p = 0; p < i - 1; p++)
      a = a * pos[p] % mod;
    for (int n = 0; n < j + 1; n++)
      a = a * neg[n] % mod;
  }

  printf("%lld\\n", a);
  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;
typedef pair<int, int> P;
typedef vector<int> vi;

const int m = 1000000007;

int print(ll ans) {
  ans %= m;
  if (ans < 0)
    ans += m;
  cout << ans << "\\n";
  return 0;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  sort(a.rbegin(), a.rend());
  ll ans = 1;
  if (a[0] < 0) {
    if (k % 2 == 1) {
      rep(i, k) {
        ans *= a[i];
        ans %= m;
      }
      print(ans);
      return 0;
    } else {
      rep(i, k) {
        ans *= a[n - 1 - i];
        ans %= m;
      }
      print(ans);
      return 0;
    }
  }
  int index1 = 0, index2 = n - 1;
  if (k % 2 == 1) {
    ans *= a[0];
    k--;
    index1++;
  }
  rep(i, k / 2) {
    if (a[index1] * a[index1 + 1] > a[index2] * a[index2 - 1]) {
      ans *= a[index1];
      ans %= m;
      ans *= a[index1 + 1];
      ans %= m;
      index1 += 2;
    } else {
      ans *= a[index2];
      ans %= m;
      ans *= a[index2 - 1];
      ans %= m;
      index2 -= 2;
    }
  }
  print(ans);
  return 0;
}

Error: WA

Fixed submission:

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

const int m = 1000000007;

int main() {
  int t, k;
  cin >> t >> k;
  vi p, n, pp, np, pair;
  int rp = 0, rn = 0;
  int zero = 0;
  rep(i, t) {
    int x;
    cin >> x;
    if (x > 0)
      p.push_back(x);
    if (x == 0)
      zero++;
    if (x < 0)
      n.push_back(x);
  }
  if (k > t - zero) {
    cout << 0;
    return 0;
  }
  int pn = p.size();
  int nn = n.size();
  sort(p.rbegin(), p.rend());
  sort(n.begin(), n.end());
  ll tmp = 1;
  rep(i, pn) {
    tmp *= p[i];
    if (i % 2 == 1) {
      pp.push_back(tmp);
      tmp = 1;
    }
  }
  if (pn % 2 == 1)
    rp = n[pn - 1];
  tmp = 1;
  rep(i, nn) {
    tmp *= n[i];
    if (i % 2 == 1) {
      np.push_back(tmp);
      tmp = 1;
    }
  }
  if (nn % 2 == 1)
    rn = n[nn - 1];
  rep(i, pn / 2) pair.push_back(pp[i]);
  rep(i, nn / 2) pair.push_back(np[i]);
  sort(pair.rbegin(), pair.rend());
  int z = pair.size();
  ll ans = 1;
  if (nn + zero == t) {
    if (zero > 0) {
      cout << 0;
      return 0;
    }
    rep(i, k) {
      ans *= n[i];
      ans %= m;
    }
    cout << ans;
    return 0;
  }
  if (t == k) {
    rep(i, nn) {
      ans *= n[i];
      ans %= m;
    }
    rep(i, zero) { ans *= 0; }
    rep(i, pn) {
      ans *= p[i];
      ans %= m;
    }
    cout << ans;
    return 0;
  }

  rep(i, k / 2) {
    if (i >= z) {
      if (rp != 0 and rn != 0) {
        ans *= rp;
        ans %= m;
        ans *= rn;
        ans %= m;
        cout << ans;
        return 0;
      } else {
        cout << 0;
        return 0;
      }
    }
    ans *= pair[i];
    ans %= m;
  }
  if (k % 2 == 1) {
    ans *= rp;
    ans %= m;
    cout << ans;
    return 0;
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &e : a)
    cin >> e;

  vector<ll> ne, po;
  for (auto e : a) {
    if (e < 0) {
      ne.push_back(e);
    } else if (e > 0) {
      po.push_back(e);
    }
  }

  ll r = 1;

  if (k == n) {
    bool b = false;
    for (auto e : a) {
      (r *= abs(e)) %= MOD;
      if (e < 0)
        b = !b;
    }
    if (b)
      r = (MOD - r) % MOD;
    cout << r << endl;
    return 0;
  }

  if (ne.size() - ne.size() % 2 + po.size() >= k &&
      (po.size() > 0 || k % 2 == 0)) {
    sort(ne.begin(), ne.end());
    sort(po.rbegin(), po.rend());
    int i = 0, j = 0;
    if (k % 2 == 1) {
      r = po[0];
      j++;
    }
    while (i + j < k) {
      if (i + 1 < ne.size() &&
          (j + 1 >= po.size() || ne[i] * ne[i + 1] > po[j] * po[j + 1])) {
        r *= ne[i] * ne[i + 1] % MOD;
        i += 2;
      } else {
        r *= po[j] * po[j + 1] % MOD;
        j += 2;
      }
      r %= MOD;
    }
  } else if (n - (ne.size() + po.size()) > 0) {
    r = 0;
  } else {
    sort(ne.rbegin(), ne.rend());
    for (int i = 0; i < k; i++)
      (r *= -ne[i]) %= MOD;
    r = (MOD - r) % MOD;
  }
  cout << r << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int MOD = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (auto &e : a)
    cin >> e;

  vector<ll> ne, po;
  for (auto e : a) {
    if (e < 0) {
      ne.push_back(e);
    } else if (e > 0) {
      po.push_back(e);
    }
  }

  ll r = 1;

  if (k == n) {
    bool b = false;
    for (auto e : a) {
      (r *= abs(e)) %= MOD;
      if (e < 0)
        b = !b;
    }
    if (b)
      r = (MOD - r) % MOD;
    cout << r << endl;
    return 0;
  }

  if (ne.size() - ne.size() % 2 + po.size() >= k &&
      (po.size() > 0 || k % 2 == 0)) {
    sort(ne.begin(), ne.end());
    sort(po.rbegin(), po.rend());
    for (int i = 0, j = 0; i + j < k;) {
      if (i + 1 << ne.size() &&
          (j >= po.size() || (i + j + 2 == k && j + 1 == po.size()) ||
           ne[i] * ne[i + 1] > po[j] * po[j + 1])) {
        r *= ne[i] * ne[i + 1] % MOD;
        i += 2;
      } else {
        r *= po[j];
        j++;
      }
      r %= MOD;
    }
  } else if (n - (ne.size() + po.size()) > 0) {
    r = 0;
  } else {
    sort(ne.rbegin(), ne.rend());
    for (int i = 0; i < k; i++)
      (r *= -ne[i]) %= MOD;
    r = (MOD - r) % MOD;
  }
  cout << r << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;

int main() {
  ll n, k;
  cin >> n >> k;
  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (a[n - 1] == 0 && k % 2) {
    cout << 0 << '\\n';
    return 0;
  }
  ll sol = 1;
  if (a[n - 1] <= 0 && k % 2) {
    for (int i = n - 1; i >= n - k; i--) {
      sol = (sol * 1LL * a[i] + MOD) % MOD;
    }
    cout << sol << '\\n';
    return 0;
  }

  ll i = 0, j = n - 1;
  if (k % 2) {
    sol = (sol * 1LL * a[j]) % MOD;
    j--;
    k--;
  }
  k /= 2;
  for (ll p = 0; p < k; p++) {
    ll neg = (a[i] * 1LL * a[i + 1]);
    ll pos = (a[j] * 1LL * a[j - 1]);
    if (neg >= pos) {
      sol = (sol * 1LL * (neg % MOD)) % MOD;
      i += 2;
    } else {
      sol = (sol * 1LL * (pos % MOD)) % MOD;
      j -= 2;
    }
  }
  cout << (sol + MOD) % MOD << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MOD = 1e9 + 7;

void fn() { fn(); }
int main() {
  int n, k;
  cin >> n >> k;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (a[n - 1] == 0 && k % 2) {
    cout << 0 << '\\n';
    return 0;
  }
  ll sol = 1;
  if (a[n - 1] <= 0 && k % 2) {
    for (int i = n - 1; i >= n - k; i--) {
      sol = (sol * 1LL * a[i] + MOD) % MOD;
    }
    cout << sol << '\\n';
    return 0;
  }
  int i = 0, j = n - 1;
  if (k % 2) {
    sol = (sol * 1LL * a[j]) % MOD;
    j--;
    k--;
  }
  k /= 2;
  fn();
  for (int p = 0; p < k; p++) {
    ll neg = (a[i] * 1LL * a[i + 1]);
    ll pos = (a[j] * 1LL * a[j - 1]);
    if (neg > pos) {
      sol = (sol * 1LL * (neg % MOD)) % MOD;
      i += 2;
    } else {
      sol = (sol * 1LL * (pos % MOD)) % MOD;
      j -= 2;
    }
  }
  cout << sol << '\\n';
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const int mod = 1e9 + 7;
int N, K;

bool cmp(long long x, long long y) { return abs(x) < abs(y); }

int main() {
  scanf("%d%d", &N, &K);
  vector<long long> A(N);
  int pn = 0, nn = 0;
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
    if (A[i] < 0)
      nn++;
    else
      pn++;
  }
  long long ans = 1;
  if (pn == 0 && K % 2) {
    sort(A.begin(), A.end(), cmp);
    for (int i = 0; i < K; i++)
      ans = ans * A[i] % mod;
  } else {
    sort(A.begin(), A.end(), greater<long long>());
    // for (int i=0; i<N; i++) printf("%lld ", A[i]);
    int l = 0, r = N - 1;
    while (K) {
      if (K % 2) {
        ans = ans * A[l] % mod;
        l++;
        K--;
      } else {
        if (A[l] * A[l + 1] >= A[r] * A[r - 1]) {
          ans = (ans * A[l] % mod * A[l + 1] % mod);
          l += 2, K -= 2;
        } else {
          ans = (ans * A[r] % mod * A[r - 1] % mod);
          r -= 2, K -= 2;
        }
      }
    }
  }
  ans = (ans % mod + mod) % mod;
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long
const ll mod = 1e9 + 7;
const int maxn = 2e5 + 111;
int N, K;
ll A, ans = 1;
vector<ll> a, b;

void solve() {
  int p = a.size() - 1, q = b.size() - 1;
  while (K) {
    ll t;
    if (K == 1) {
      if (p >= 0)
        t = a[p], p--;
      else
        t = a[q], q--;
      K--;
    } else {
      ll x = a[p], y = 0;
      if (q >= 1)
        y = b[q] * b[q - 1];
      if (x >= y)
        t = x, p--, K--;
      else
        t = y, q -= 2, K -= 2;
    }
    // printf("%lld %d\\n", t, K);
    ans = (ans * t % mod + mod) % mod;
  }
}

int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &A);
    if (A < 0)
      b.push_back(A);
    else
      a.push_back(A);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  solve();
  printf("%lld\\n", ans);
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

#define MOD 1000000007

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<long long> b, c;
  for (int i = 0; i < n; i++) {
    long long a;
    scanf("%lld", &a);
    if (a >= 0)
      b.emplace_back(a);
    else
      c.emplace_back(a);
  }
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  long long r = 1;
  if (c.size() == n && k % 2 == 1) {
    for (int i = 0; i < k; i++) {
      r = r * (c[n - 1 - i] + MOD) % MOD;
    }
  } else if (k == n) {
    for (auto &i : b)
      r = r * i % MOD;
    for (auto &i : c)
      r = r * (i + MOD) % MOD;
  } else {
    int p = b.size() - 1;
    int q = 0;
    if (k % 2 == 1) {
      r = b[p];
      p--;
      k--;
    }
    while (k) {
      long long x = -1;
      long long y = -1;
      if (p > 0)
        x = (b[p] * b[p - 1]);
      if (q + 1 < c.size())
        y = (c[q] * c[q + 1]);
      if (x > y) {
        r = x % MOD * r % MOD;
        p -= 2;
      } else {
        r = y % MOD * r % MOD;
        q += 2;
      }
      k -= 2;
    }
  }
  printf("%lld\\n", r);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

#define MOD 1000000007

int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  vector<long long> b, c;
  for (int i = 0; i < n; i++) {
    long long a;
    scanf("%lld", &a);
    if (a >= 0)
      b.emplace_back(a);
    else
      c.emplace_back(a);
  }
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  long long r = 1;
  if (c.size() == n && k % 2 == 1) {
    for (int i = 0; i < k; i++) {
      r = r * (c[n - 1 - i] + MOD) % MOD;
    }
  } else {
    int p = b.size() - 1;
    int q = 0;
    if (k % 2 == 1) {
      r = b[p];
      p--;
      k--;
    }
    while (k) {
      long long x = -1;
      long long y = -1;
      if (p > 0)
        x = (b[p] * b[p - 1]);
      if (q + 1 < c.size())
        y = (c[q] * c[q + 1]);
      if (x < 0 && y < 0)
        abort();
      if (x > y) {
        r = x % MOD * r % MOD;
        p -= 2;
      } else {
        r = y % MOD * r % MOD;
        q += 2;
      }
      k -= 2;
    }
  }
  printf("%lld\\n", r);
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int mod = 1e9 + 7;
const int N = 2e5 + 100;
ll a[N];
int f[5];
bool cmp(int x, int y) { return abs(x) < abs(y); }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      f[1]++;
    else
      f[2]++;
  }
  int l = 1, r = n;
  ll ans = 1;
  if (!f[1] && m % 2 == 1) {
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= m; i++) {
      ans = a[i] * ans % mod;
    }
  } else {
    sort(a + 1, a + 1 + n, greater<int>());
    while (m > 0) {
      if (m % 2)
        ans = ans * a[l] % mod, l++, m--;
      else if (m >= 2) {
        if (a[l] * a[l + 1] >= a[r] * a[r - 1]) {
          ans = ans * a[l] % mod * a[l + 1] % mod;
          l += 2;
          m -= 2;
        } else {
          ans = ans * a[r - 1] % mod * a[r] % mod;
          r -= 2;
          m -= 2;
        }
      }
    }
  }
  cout << (ans % mod + mod) % mod << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 13;
int a[maxn];
int b[maxn];
const int mod = 1e9 + 7;
int c[maxn];
bool cmp(int x, int y) { return abs(x) < abs(y); }
int main() {
  int n, k;
  int cnt1 = 0, cnt2 = 0;
  scanf("%d%d", &n, &k);
  ll ans = 1ll;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &c[i]);
    if (c[i] > 0)
      a[++cnt1] = c[i];
    else if (c[i] <= 0)
      b[++cnt2] = -c[i];
  }
  sort(a + 1, a + cnt1 + 1);
  sort(b + 1, b + cnt2 + 1);
  int check = 1;
  if ((cnt1 == 0 && k & 1) || (n == k && cnt2 & 1))
    check = 0;
  if (check) {
    int now2 = cnt2;
    int now1 = cnt1;
    if (k & 1)
      ans = 1ll * ans * a[cnt1] % mod;
    cnt1--;
    int now = 0;
    while (now != k) {
      if (now1 == 0 || (now2 >= 2 && 1ll * b[now2] * b[now2 - 1] >
                                         1ll * a[now1] * a[now1 - 1])) {
        ans = ((ans * 1ll * b[now2]) % mod) * b[now2 - 1] % mod;
        now2 -= 2;
      } else if (now2 == 0 || (now1 >= 2 && 1ll * b[now2] * b[now2 - 1] <=
                                                1ll * a[now1] * a[now1 - 1]))
        ans = ((ans * 1ll * a[now1]) % mod) * a[now1 - 1] % mod, now1 -= 2;
      now += 2;
    }
  } else {
    sort(c + 1, c + 1 + n, cmp);
    for (int i = 1; i <= k; i++) {
      ans *= 1ll * c[i];
      while (ans < 0)
        ans += mod;
    }
  }
  printf("%lld", ans);
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define debug(x) cerr << "[(" << __LINE__ << ") " << (#x) << "]: " << x << endl;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ll n, k;
  cin >> n >> k;

  ll a[n];
  int neg = 0;
  for (ll &e : a) {
    cin >> e;
    if (e < 0)
      neg++;
  }
  sort(a, a + n);

  int p = 0, p2 = n - 1;
  if (k == 1) {
    cout << a[n - 1] << "\\n";
    return 0;
  }
  ll ans = 1;
  ll MOD = 1e9 + 7;
  if ((neg == n && k % 2) || (neg % 2 && n == k)) {
    for (ll &e : a)
      e = abs(e);
    sort(a, a + n);
    for (int i = 0; i < k; i++) {
      ans = ans * a[i] % MOD;
    }
    ans *= -1;
    if (ans < 0)
      ans += MOD;
  } else {
    while (k > 0) {
      if (k > 3 || k == 2) {
        if (a[p] * a[p + 1] > a[p2] * a[p2 - 1] ||
            (k % 2 && p2 > 1 && a[p2 - 2] <= 0)) {
          ans = ans * a[p] % MOD;
          ans = ans * a[p + 1] % MOD;
          p += 2;
        } else {
          ans = ans * a[p2] % MOD;
          ans = ans * a[p2 - 1] % MOD;
          p2 -= 2;
        }
        k -= 2;
      } else {
        __int128 duh, duh2, duh3, duh4;
        duh = a[p] * a[p + 1];
        duh2 = duh;
        duh3 = a[p];
        duh4 = a[p2] * a[p2 - 1];

        duh *= a[p + 2];
        duh2 *= a[p2];
        duh3 *= a[p2] * a[p2 - 1];
        duh4 *= a[p2 - 2];

        __int128 mx = max({duh, duh2, duh3, duh4});
        mx %= MOD;
        ans = ans * mx % MOD;
        k -= 3;
      }
    }
  }
  cout << ans << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define debug(x) cerr << "[(" << __LINE__ << ") " << (#x) << "]: " << x << endl;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

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

  ll a[n];
  int neg = 0;
  for (ll &e : a) {
    cin >> e;
    if (e < 0)
      neg++;
  }
  sort(a, a + n);

  int p = 0, p2 = n - 1;
  if (k == 1) {
    cout << a[0] << "\\n";
    return 0;
  }
  ll ans = 1;
  ll MOD = 1e9 + 7;
  if ((neg == n && k % 2) || (neg % 2 && n == k)) {
    for (ll &e : a)
      e = abs(e);
    sort(a, a + n);
    for (int i = 0; i < k; i++) {
      ans = ans * a[i] % MOD;
    }
    ans *= -1;
    if (ans < 0)
      ans += MOD;
    assert(0);
  } else {
    while (k > 0) {
      if (k > 3 || k == 2) {
        if (a[p] * a[p + 1] > a[p2] * a[p2 - 1] ||
            (k % 2 && p2 > 1 && a[p2 - 2] <= 0)) {
          ans = ans * a[p] % MOD;
          ans = ans * a[p + 1] % MOD;
          p += 2;
        } else {
          ans = ans * a[p2] % MOD;
          ans = ans * a[p2 - 1] % MOD;
          p2 -= 2;
        }
        k -= 2;
      } else {
        __int128 duh, duh2, duh3, duh4;
        duh = a[p] * a[p + 1];
        duh2 = duh;
        duh3 = a[p];
        duh4 = a[p2] * a[p2 - 1];

        duh *= a[p + 2];
        duh2 *= a[p2];
        duh3 *= a[p2] * a[p2 - 1];
        duh4 *= a[p2 - 2];

        __int128 mx = max({duh, duh2, duh3, duh4});
        mx %= MOD;
        ans = ans * mx % MOD;
        k -= 3;
      }
    }
  }
  cout << ans << "\\n";
}
Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<ll> a(n);
  for (int i = 0; i < n; i++)
    scanf("%lld", &a[i]);
  sort(a.begin(), a.end());
  ll ans = 1, sign = 1;
  int l = 0, r = n - 1;
  if (k % 2) {
    ans = a[r--];
    --k;
    if (ans < 0)
      sign = -1;
  }
  while (k) {
    ll x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (x * sign > y * sign) {
      ans = x % mod * ans % mod;
      l += 2;
    } else {
      ans = y % mod * ans % mod;
      r -= 2;
    }
    k -= 2;
  }
  printf("%lld", (ans + mod) % mod);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, k;
long long ans = 1;
int l_neg = -1, l_pos = -1, r_neg = -1, r_pos = -1;
const ll M = 1e9 + 7;

ll f(ll a, ll b) {
  ll k = 0;
  while (b) {
    if (b & 1)
      k = (k + a) % M;
    a = (a + a) % M;
    b >>= 1;
  }
  return k;
}

bool cmp(ll a, ll b) { return abs(a) < abs(b); }

ll calc(vector<ll> a) {
  ll temp = 1;
  for (int i = n - k; i < n; i++)
    temp = f(temp, a[i]) % M;
  return temp;
}

bool v_cmp(vector<ll> a, vector<ll> b) {
  ll sum1 = 0, sum2 = 0;
  for (int i = n - k; i < n; i++) {
    sum1 += abs(a[i]);
    sum2 += abs(b[i]);
  }
  return sum1 > sum2;
}

int main() {
  cin >> n >> k;
  vector<ll> a(n, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a.begin(), a.end(), cmp);

  for (int i = n - k; i < n; i++) {
    ans = (ans * a[i]) % M;
    if (r_pos == -1 && a[i] > 0)
      r_pos = i;
    if (r_neg == -1 && a[i] < 0)
      r_neg = i;
  }
  for (int i = n - k - 1; i >= 0; i--) {
    if (l_pos == -1 && a[i] > 0)
      l_pos = i;
    if (l_neg == -1 && a[i] < 0)
      l_neg = i;
  }

  if (ans < 0 && n != k) {
    if (r_pos == -1) {
      if (l_pos == -1) {
        ans = 1;
        for (int i = 0; i < k; i++)
          ans = (ans * a[i]) % M;
      } else {
        swap(a[l_pos], a[r_neg]);
        ans = calc(a);
      }
    } else {
      if (l_neg == -1) {
        swap(a[l_pos], a[r_neg]);
        ans = calc(a);
      } else if (l_pos == -1) {
        swap(a[l_neg], a[r_pos]);
        ans = calc(a);
      } else {
        vector<ll> c1 = a;
        vector<ll> c2 = a;
        swap(c1[l_neg], c1[r_pos]);
        swap(c2[l_pos], c2[r_neg]);
        ans = v_cmp(c1, c2) == true ? calc(c1) : calc(c2);
      }
    }
  }
  while (ans < 0)
    ans += M;
  cout << ans % M << endl;
  return 0;
}