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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++) // 0-indexed昇順
#define MOD 1000000007LL

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A;
  vector<int> B;
  rep(i, N) {
    int j;
    cin >> j;
    ((j < 0) ? B : A).push_back(j);
  }
  if (N == K) {
    ll res = 1;
    if (A.size())
      rep(i, A.size()) res = res * A.at(i) % MOD;
    if (B.size())
      rep(i, B.size()) res = res * B.at(i) % MOD;
    if (res < 0)
      res += MOD;
    cout << res << endl;
    return 0;
  }
  sort(A.begin(), A.end());                 // ASC
  sort(B.begin(), B.end(), greater<int>()); // DSC
  vector<int> C;
  if (B.size() < 2) { // All positive
    rep(i, K) C.emplace_back(A.back()), A.pop_back();
  } else if (A.size() == 0) { // All negative
    if (K % 2 == 0) {         // Result must be positive
      rep(i, K) C.emplace_back(B.back()), B.pop_back();
    } else { // Result must be negative or zero
      rep(i, K) C.emplace_back(B[i]);
    }
  } else { // Use both positve and negative
    while (K > 0) {
      ll p1, p2;
      // There must be at least two elements of B
      ll n1, n2;
      if (K > 1 && B.size() > 1) {
        n1 = B.back();
        n2 = B.at(B.size() - 2);
        int AS = A.size();
        if (AS > 1)
          p1 = A.back(), p2 = A.at(AS - 2);
        if (AS < 2 || (p1 * p2 <= n1 * n2)) {
          C.emplace_back(n1);
          B.pop_back();
          C.emplace_back(n2);
          B.pop_back();
          K -= 2;
          continue;
        }
      } // Fall through
      p1 = A.back();
      A.pop_back();
      C.emplace_back(p1);
      K--;
    }
  }

  ll res = 1;
  rep(i, C.size()) {
    res *= (ll)C[i];
    res %= MOD;
    if (res < 0)
      res += MOD;
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  rep(i, N) { cin >> A.at(i); }
  sort(A.begin(), A.end(), greater<int>());
  vector<int> C;
  if ((A[0] >= 0 && A[N - 1] >= 0) || (A[0] <= 0 && A[N - 1] <= 0)) {
    int i = 0, c = 0;
    while (c < K) {
      if (A[i] != 0) {
        C.push_back(A[i]);
        c++;
      }
      i++;
    }
  } else {
    int i = 0, j = N - 1;
    while (C.size() < K) {
      while (A[j] == 0)
        j--;
      while (A[i] == 0)
        i++;
      if (A[j] < 0 && abs(A[j]) > A[i]) {
        C.push_back(A[j--]);
      } else {
        C.push_back(A[i++]);
      }
    }
    if ((N - 1 - j) % 2 == 1) {
      auto x = find(C.begin(), C.end(), A[j + 1]);
      *x = 1;
      C.push_back(A[i++]);
    }
  }
  ll res = 1;
  rep(i, C.size()) {
    res *= C[i];
    res %= 1000000007;
  }
  if (res < 0)
    res += 1000000007;
  cout << res << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define all(x) x.begin(), x.end()
#define puts(x) cout << (x) << "\\n"
constexpr int mod = 1000000007;
using namespace std;
int input() {
  int t;
  cin >> t;
  return t;
}
vector<int> v1, v2;
signed main() {
  int n, k;
  cin >> n >> k;
  for1(i, n) {
    int t;
    cin >> t;
    if (t < 0)
      v1.push_back(t); //-
    else
      v2.push_back(t); //+
  }
  sort(all(v1));
  sort(all(v2));
  if (n == k || (v2.size() == 0 && k % 2 == 1)) {
    int ans = 1;
    for0(i, v2.size()) {
      ans *= v2[i];
      if (ans >= mod)
        ans %= mod;
    }
    for1(i, min((int)v1.size(), k)) {
      ans *= v1[v1.size() - i];
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    puts(ans);
    return 0;
  }
  int p = k - v2.size();
  if (p < 0)
    p = 0;
  if (p % 2 == 1)
    p++;
  while (1) {
    if (p + 2 > v1.size() || p + 2 > k)
      break;
    else if (v1[p] * v1[p + 1] >=
             v2[v2.size() - k + p] * v2[v2.size() - k + p + 1])
      p += 2;
    else
      break;
  }
  int ans = 1;
  for1(i, k - p) {
    ans *= v2[v2.size() - i];
    if (ans >= mod)
      ans %= mod;
  }
  for0(i, p) {
    ans *= v1[i];
    ans %= mod;
  }
  if (ans < 0)
    ans += mod;
  puts(ans);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
#define int long long
#define for0(i, n) for (int i = 0; i < (n); i++)
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define all(x) x.begin(), x.end()
#define puts(x) cout << (x) << "\\n"
constexpr int mod = 1000000007;
using namespace std;
int input() {
  int t;
  cin >> t;
  return t;
}
vector<int> v1, v2;
signed main() {
  int n, k;
  cin >> n >> k;
  for1(i, n) {
    int t;
    cin >> t;
    if (t < 0)
      v1.push_back(t);
    else
      v2.push_back(t);
  }
  sort(all(v1));
  sort(all(v2));
  if (n == k || v2.size() == 0) {
    int ans = 1;
    for0(i, v2.size()) {
      ans *= v2[i];
      if (ans >= mod)
        ans %= mod;
    }
    for1(i, min(v1.size(), (size_t)k)) {
      ans *= v1[v1.size() - i];
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    puts(ans);
    return 0;
  }
  int p = 0;
  while (1) {
    if (v1.size() + p < k)
      p += 2;
    else if (p + 2 > v2.size())
      break;
    else if (p + 2 > k)
      break;
    else if (v1[p] * v1[p + 1] > v2[v2.size() - k + p] * v2[v2.size() - k + p])
      p += 2;
    else
      break;
  }
  int ans = 1;
  for1(i, k - p) {
    ans *= v2[v2.size() - i];
    if (ans >= mod)
      ans %= mod;
  }
  for0(i, p) {
    ans *= v1[i];
    ans %= mod;
  }
  if (ans < 0)
    ans += mod;
  puts(ans);
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
int main() {
  long long int N, K, A, M, pI, mI, ans = 1;
  vector<long long int> p, m;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A;
    if (A < 0)
      m.push_back(-A);
    else
      p.push_back(A);
  }
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  if (p.size() == 0 && K % 2) {
    for (int i = 0; i < K; i++)
      ans = ((-ans * m[i]) % MOD + MOD) % MOD;
  } else {
    pI = p.size() - 1;
    mI = m.size() - 1;
    if (K % 2) {
      ans = p[pI] % MOD;
      pI--;
      K--;
    }
    for (int i = 0; i < K / 2; i++) {
      if (mI - 1 < 0 && pI - 1 < 0) {
        M = -p[0] * m[0] % MOD;
      } else if (mI - 1 < 0) {
        M = p[pI] * p[pI - 1] % MOD;
        pI -= 2;
      } else if (pI - 1 < 0) {
        M = m[mI] * m[mI - 1] % MOD;
        mI -= 2;
      } else if (p[pI] * p[pI - 1] >= m[mI] * m[mI - 1]) {
        M = p[pI] * p[pI - 1] % MOD;
        pI -= 2;
      } else {
        M = m[mI] * m[mI - 1] % MOD;
        mI -= 2;
      }
      ans = (ans * M % MOD + MOD) % MOD;
    }
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long int MOD = 1000000007;
int main() {
  long long int pI, mI;
  long long int N, K, A, M;
  long long int ans = 1;
  vector<long long int> p, m;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A;
    if (A < 0)
      m.push_back(-A);
    else
      p.push_back(A);
  }
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  if (p.size() == 0 && K % 2) {
    for (int i = 0; i < K; i++)
      ans = ((-ans * m[i]) % MOD + MOD) % MOD;
  } else if (N == K) {
    for (int i = 0; i < m.size(); i++)
      ans = ((-ans * m[i]) % MOD + MOD) % MOD;
    for (int i = 0; i < p.size(); i++)
      ans = ((ans * p[i]) % MOD + MOD) % MOD;
  } else {
    if (K % 2) {
      pI = p.size() - 2;
      ans = ((p[p.size() - 1] * ans) % MOD + MOD) % MOD;
    } else {
      pI = p.size() - 1;
      mI = m.size() - 1;
    }
    for (int i = 0; i < K / 2; i++) {
      if (mI - 1 < 0) {
        M = p[pI] * p[pI - 1] % MOD;
        pI -= 2;
      } else if (pI - 1 < 0) {
        M = m[mI] * m[mI - 1] % MOD;
        mI -= 2;
      } else if (p[pI] * p[pI - 1] > m[mI] * m[mI - 1]) {
        M = p[pI] * p[pI - 1] % MOD;
        pI -= 2;
      } else {
        M = m[mI] * m[mI - 1] % MOD;
        mI -= 2;
      }
      ans = ((ans * M) % MOD + MOD) % MOD;
    }
  }
  cout << ans;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
#define int long long
  const int mod = 1000000007;
  int n, k;
  cin >> n >> k;
  int a[n];
  vector<int> sei;
  vector<int> fu;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      sei.push_back(a[i]);
    else
      fu.push_back(-a[i]);
  }
  sort(sei.begin(), sei.end(), greater<int>());
  sort(fu.begin(), fu.end(), greater<int>());
  long long x = 1;
  if (sei.size() == 0) { // コーナーケース
    if (k % 2) {
      for (int i = fu.size() - k; i < fu.size(); i++) {
        x *= fu[i];
        x %= mod;
      }
      cout << mod - x << endl;
    } else {
      for (int i = 0; i < k; i++) {
        x *= fu[i];
        x %= mod;
      }
      cout << x << endl;
    }
    return 0;
  }
  if (n == k) {
    for (int i = 0; i < fu.size(); i++) {
      x *= fu[i];
      x %= mod;
    }
    for (int i = 0; i < sei.size(); i++) {
      x *= sei[i];
      x %= mod;
    }
    if (fu.size() % 2)
      cout << mod - x << endl;
    else
      cout << x << endl;
    return 0;
  }
  // 負を偶数個選ぶことで、必ず非負になる
  int best = 0; // 最も良い、負を選ぶ個数
  for (int i = 2; i <= k; i += 2) {
    if (i > fu.size() || k - i > sei.size())
      continue;
    if (fu[i - 2] * fu[i - 1] > sei[k - i] * sei[k - i + 1])
      best = i;
  }
  for (int i = 0; i < best; i++) {
    x *= fu[i];
    x %= mod;
  }
  for (int i = 0; i < k - best; i++) {
    x *= sei[i];
    x %= mod;
  }
  cout << x << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
#define int long long
  const int mod = 1000000007;
  int n, k;
  cin >> n >> k;
  int a[n];
  vector<int> sei;
  vector<int> fu;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] >= 0)
      sei.push_back(a[i]);
    else
      fu.push_back(-a[i]);
  }
  sort(sei.begin(), sei.end(), greater<int>());
  sort(fu.begin(), fu.end(), greater<int>());
  long long x = 1;
  if (sei.size() == 0 && k % 2) { // コーナーケース
    for (int i = fu.size() - k; i < fu.size(); i++) {
      x *= fu[i];
      x %= mod;
    }
    cout << mod - x << endl;
    return 0;
  }
  if (n == k) {
    for (int i = 0; i < fu.size(); i++) {
      x *= fu[i];
      x %= mod;
    }
    for (int i = 0; i < sei.size(); i++) {
      x *= sei[i];
      x %= mod;
    }
    if (fu.size() % 2)
      cout << mod - x << endl;
    else
      cout << x << endl;
    return 0;
  }
  // 負を偶数個選ぶことで、必ず非負になる
  int best = 0; // 最も良い、負を選ぶ個数
  for (int i = 2; i <= k; i += 2) {
    if (i > fu.size() || k - i > sei.size())
      continue;
    if (fu[i - 2] * fu[i - 1] > sei[k - i] * sei[k - i + 1])
      best = i;
  }
  for (int i = 0; i < best; i++) {
    x *= fu[i];
    x %= mod;
  }
  for (int i = 0; i < k - best; i++) {
    x *= sei[i];
    x %= mod;
  }
  cout << x << endl;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 200010;
ll a[N];
int n, m;
int main() {
  int t;
  // while(){
  int k;
  scanf("%d%d", &n, &k);
  vector<ll> v, v1;
  ll mod = 1e9 + 7;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] >= 0) {
      v.push_back(a[i]);
    } else {
      v1.push_back(-a[i]);
    }
  }
  sort(v.begin(), v.end());
  sort(v1.begin(), v1.end());
  ll ans = 1;
  if (n == k) {
    for (int i = 0; i < n; ++i) {
      ans *= a[i];
      ans %= mod;
    }
    if (ans < 0)
      ans += mod;
    cout << ans << endl;
    return 0;
  }
  if (k % 2) {
    if (v.size() == 0) {
      for (int i = 0; i < k; ++i) {
        ans *= -v1[i];
        ans %= mod;
      }
      if (ans < 0)
        ans += mod;
      cout << ans << endl;
      return 0;
    }
    --k;
    ans = v.back();
    v.pop_back();
  }
  while (k > 0) {
    ll tmp1 = -1, tmp2 = -1;
    if (v.size() >= 2) {
      tmp1 = v.back() * v[((int)v.size()) - 2];
    }
    if (v1.size() >= 2) {
      tmp2 = v1.back() * v1[((int)v1.size()) - 2];
    }
    assert(tmp1 >= 0 || tmp2 >= 0);
    if (tmp1 > tmp2) {
      tmp1 %= mod;
      ans *= tmp1;
      ans %= mod;
      v.pop_back();
      v.pop_back();
    } else {
      tmp2 %= mod;
      ans *= tmp2;
      ans %= mod;
      v1.pop_back();
      v1.pop_back();
    }
    k -= 2;
  }
  ans %= mod;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 200010;
ll a[N];
int n, m;
int main() {
  int t;
  // while(){
  int k;
  scanf("%d%d", &n, &k);
  vector<ll> v, v1;
  ll mod = 1e9 + 7;
  for (int i = 0; i < n; ++i) {
    scanf("%lld", a + i);
    if (a[i] >= 0) {
      v.push_back(a[i]);
    } else {
      v1.push_back(-a[i]);
    }
  }
  sort(v.begin(), v.end());
  sort(v1.begin(), v1.end());
  ll ans = 1;
  if (k % 2) {
    if (v.size() == 0) {
      for (int i = 0; i < k; ++i) {
        ans *= -v1[i];
        ans %= mod;
      }
      if (ans < 0)
        ans += mod;
      cout << ans << endl;
      return 0;
    }
    --k;
    ans = v.back();
    v.pop_back();
  }
  while (k > 0) {
    ll tmp1 = -1, tmp2 = -1;
    if (v.size() >= 2) {
      tmp1 = v.back() * v[((int)v.size()) - 2];
    }
    if (v1.size() >= 2) {
      tmp2 = v1.back() * v1[((int)v1.size()) - 2];
    }
    assert(tmp1 >= 0 || tmp2 >= 0);
    if (tmp1 > tmp2) {
      tmp1 %= mod;
      ans *= tmp1;
      ans %= mod;
      v.pop_back();
      v.pop_back();
    } else {
      tmp2 %= mod;
      ans *= tmp2;
      ans %= mod;
      v1.pop_back();
      v1.pop_back();
    }
    k -= 2;
  }
  ans %= mod;
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> se, fu;
  int zero = 0;

  if (n == k) {
    ll ans = 1;
    for (int i = 0; i < n; i++) {
      ll t;
      cin >> t;
      ans *= t;
      ans %= mod;
    }
    cout << (ans % mod + mod) % mod << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (t > 0)
      se.emplace_back(t);
    if (t <= 0)
      fu.emplace_back(-t);
    if (t == 0)
      zero++;
  }

  sort(se.begin(), se.end());
  sort(fu.begin(), fu.end());

  if (n - zero < k) {
    cout << 0 << endl;
    return 0;
  }

  // 負 or ゼロになる
  if ((int)fu.size() == n && k % 2) {
    ll ans = 1;
    for (int i = 0; i < k; i++)
      ans *= -fu[i], ans %= mod;
    cout << (ans % mod + mod) % mod << endl;
    return 0;
  }

  ll ans = 1;
  if (k % 2) {
    ans = se.back();
    se.pop_back();
  }
  k /= 2;

  for (int i = 0; i < k; i++) {
    ll pl = (se.size() >= 2 ? se.back() * *(se.end() - 2) : 1);
    ll mi = (fu.size() >= 2 ? fu.back() * *(fu.end() - 2) : 1);

    if (pl >= mi && se.size() >= 2) {
      ans *= pl % mod;
      ans %= mod;
      se.pop_back(), se.pop_back();
    } else if (mi > pl && fu.size() >= 2) {
      ans *= mi % mod;
      ans %= mod;
      fu.pop_back(), fu.pop_back();
    } else {
      while (true)
        ;
      // cerr << fu.size() << " : " << se.size() << endl;
      // ans *= fu.back() * se.back();
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<ll> se, fu;
  int zero = 0;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    if (t > 0)
      se.emplace_back(t);
    if (t <= 0)
      fu.emplace_back(-t);
    if (t == 0)
      zero++;
  }

  sort(se.begin(), se.end());
  sort(fu.begin(), fu.end());

  if (n - zero < k) {
    cout << 0 << endl;
    return 0;
  }

  // 負 or ゼロになる
  if ((int)fu.size() == n && k % 2) {
    ll ans = 1;
    for (int i = 0; i < k; i++)
      ans *= -fu[i], ans %= mod;
    cout << (ans % mod + mod) % mod << endl;
    return 0;
  }

  ll ans = 1;
  if (k % 2) {
    ans = se.back();
    se.pop_back();
  }
  k /= 2;

  for (int i = 0; i < k; i++) {
    ll pl = (se.size() >= 2 ? se.back() * *(se.end() - 2) : 1);
    ll mi = (fu.size() >= 2 ? fu.back() * *(fu.end() - 2) : 1);

    if (pl >= mi && se.size() >= 2) {
      ans *= pl % mod;
      ans %= mod;
      se.pop_back(), se.pop_back();
    } else if (mi > pl && fu.size() >= 2) {
      ans *= mi % mod;
      ans %= mod;
      fu.pop_back(), fu.pop_back();
    } else {
      while (true)
        ;
      // cerr << fu.size() << " : " << se.size() << endl;
    }
  }
  cout << ans << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 5;
const long long inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
long long a[maxn], ans = 1, w = 1;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  long long n, k;
  cin >> n >> k;
  long long l = 1, r = n;
  for (long long i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1);
  long long ans = 1;
  if (k & 1) {
    ans = a[r--];
    k--;
    w = (ans < 0 ? -1 : 1);
  }
  while (k) {
    long long x = a[l] * a[l + 1];
    long long y = a[r] * a[r - 1];
    if (x * w > y * w) {
      ans = ans * (x % mod) % mod;
      l += 2;
    } else {
      ans = ans * (y % mod) % mod;
      r -= 2;
    }
    k -= 2;
  }
  cout << (ans % mod + mod) % mod << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e5 + 5;
const long long inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
long long a[maxn], ans = 1, maxv = -inf, w;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  long long n, k;
  cin >> n >> k;
  long long l = 1, r = n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    maxv = max(maxv, a[i]);
  }
  sort(a + 1, a + n + 1);
  if (k % 2 == 1) {
    ans = maxv;
    k--;
    r--;
    w = 1;
  }
  while (k) {
    long long x = a[l] * a[l + 1];
    long long y = a[r] * a[r - 1];
    if (x * w > y * w) {
      ans = (ans * x + mod) % mod;
      l += 2;
    } else {
      ans = (ans * y + mod) % mod;
      r -= 2;
    }
    k -= 2;
  }
  cout << (ans + mod) % mod;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long LL;
const int N = 1000010, mod = 1000000007;
int a[N];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  sort(a, a + n);

  LL res = 1;           // 乘积初始化
  int l = 0, r = n - 1; // 双指针初始化
  int sign = 1;         // 符号初始化

  // 由于4种情况除了 k 是奇数且 k < n 的时候需要特判一下处理一下符号
  // ,其他的时候都可以转化为双指针做法 k 是奇数是先选出最大的数, k--
  // 就是偶数,两边再同时取对,转化成相同的双指针做法
  if (k % 2) {
    res = (a[r]) % mod; // 取出最大的一个数
    r--;                // 右指针移动
    k--;                // 个数减1

    if (res < 0)
      sign = -1; // 如果最大值都是负数,就证明全是负数,那么符号要发生改变
  }
  while (k) // 双指针做法
  {
    LL x = (LL)a[l] * a[l + 1], y = (LL)a[r] * a[r - 1]; // 两边同时取对
    // 选择更大的一对,和归并排序思路相近
    if (x * sign > y * sign) {
      res = (x % mod * res % mod) %
            mod; // 需要注意的是 :不可以写成(x * res) % mod ,也不可以写成是 res
                 // % mod * x % mod
      // 因为x最大是 10^10，如果不先取模的话，和res相乘的结果最大是
      // 10^19,会暴long long。
      l += 2; // 指针移动
    } else {
      res = (y % mod * res % mod) % mod;
      r -= 2;
    }
    k -= 2;
  }
  cout << (res + mod) % mod << endl;
  return 0;
}


Error: WA

Fixed submission:

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

typedef long long LL;
const int N = 100010, mod = 1000000009;
int a[N];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  sort(a, a + n);

  LL res = 1;           // 乘积初始化
  int l = 0, r = n - 1; // 双指针初始化
  int sign = 1;         // 符号初始化

  // 由于4种情况除了 k 是奇数且 k < n 的时候需要特判一下处理一下符号
  // ,其他的时候都可以转化为双指针做法 k 是奇数是先选出最大的数, k--
  // 就是偶数,两边再同时取对,转化成相同的双指针做法
  if (k % 2) {
    res = a[r]; // 取出最大的一个数
    r--;        // 右指针移动
    k--;        // 个数减1

    if (res < 0)
      sign = -1; // 如果最大值都是负数,就证明全是负数,那么符号要发生改变
  }
  while (k) // 双指针做法
  {
    LL x = (LL)a[l] * a[l + 1], y = (LL)a[r] * a[r - 1]; // 两边同时取对
    // 选择更大的一对,和归并排序思路相近
    if (x * sign > y * sign) {
      res = x % mod * res % mod; // 需要注意的是 :不可以写成(x * res) % mod
                                 // ,也不可以写成是 res % mod * x % mod
      // 因为x最大是 10^10，如果不先取模的话，和res相乘的结果最大是
      // 10^19,会暴long long。
      l += 2; // 指针移动
    } else {
      res = y % mod * res % mod;
      r -= 2;
    }
    k -= 2;
  }
  cout << (res + mod) % mod;
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 1e6 + 5;
const ll mod = 1e9 + 7;
vector<ll> neg, pos;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    int tem;
    cin >> tem;
    if (tem > 0)
      pos.push_back(tem);
    if (tem < 0)
      neg.push_back(tem);
  }
  bool ok = 0;
  int mn = 0;
  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end(), greater<int>());
  for (int i = 0; i <= neg.size() && i <= k; i += 2)
    if (pos.size() + i >= k) {
      ok = 1;
      mn = i;
      break;
    }
  ll ans = 1;
  if (!ok) {
    if (pos.size() + neg.size() < n) {
      cout << 0 << endl;
      return 0;
    }
    sort(neg.begin(), neg.end(), greater<int>());
    for (int i = 0; i < min(k, (int)neg.size()); i++)
      (ans *= neg[i]) %= mod;
    sort(pos.begin(), pos.end());
    for (int i = 0; i < k - (int)neg.size(); i++)
      (ans *= pos[i]) %= mod;
  } else {
    for (int i = mn + 2; i <= neg.size() && i <= k; i += 2) {
      if (neg[i - 1] * neg[i - 2] > pos[k - i] * pos[k - i + 1])
        mn = i;
      else
        break;
    }
    for (int i = 0; i < mn; i++)
      (ans *= neg[i]) %= mod;
    for (int i = 0; i < k - mn; i++)
      (ans *= pos[i]) %= mod;
  }
  (ans += mod) %= mod;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int maxn = 1e6 + 5;
int pre[maxn];
const ll mod = 1e9 + 7;
vector<int> neg, pos;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    int tem;
    cin >> tem;
    if (tem > 0)
      pos.push_back(tem);
    if (tem < 0)
      neg.push_back(tem);
  }
  bool ok = 0;
  int mn = 0;
  sort(neg.begin(), neg.end());
  sort(pos.begin(), pos.end(), greater<int>());
  for (int i = 0; i < neg.size(); i += 2)
    if (pos.size() + i >= k) {
      ok = 1;
      mn = i;
      break;
    }
  ll ans = 1;
  if (!ok) {

    // if(pos.size()+neg.size()<n) {cout<<0<<endl;return 0;}
    // sort(neg.begin(),neg.end(),greater<int>());
    // for(int i=0;i<min(k,(int)neg.size());i++) (ans*=neg[i])%=mod;
    // sort(pos.begin(),pos.end());
    // for(int i=0;i<k-(int)neg.size();i++) (ans*=pos[i])%=mod;
    ans = 0;
  } else {
    for (int i = mn + 2; i <= neg.size() && i <= k; i += 2) {
      if (pos[i - 1] * pos[i - 2] < neg[k - i] * neg[k - i + 1])
        mn = i;
      else
        break;
    }
    for (int i = 0; i < mn; i++)
      (ans *= neg[i]) %= mod;
    for (int i = 0; i < k - mn; i++)
      (ans *= pos[i]) %= mod;
  }
  (ans += mod) %= mod;
  cout << ans << endl;
  return 0;
}