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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int M = 1e9 + 7;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<ll> pos, neg;
  ll zer = 0, a;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a);
    if (a > 0)
      pos.push_back(a);
    else if (a < 0)
      neg.push_back(-a);
    else
      zer++;
  }
  if ((neg.size() / 2) * 2 + pos.size() >= k && (k % 2 == 0 || pos.size())) {
    // positive
    ll ans = 1;
    sort(pos.begin(), pos.end(), greater<>());
    sort(neg.begin(), neg.end(), greater<>());
    int pi = 0, ni = 0, cnt = 0;
    while (cnt < k) {
      bool tneg = (k - cnt >= 2) && (ni + 1 < neg.size());
      if (tneg && pi + 1 < pos.size()) {
        tneg = (pos[pi] * pos[pi + 1]) <= (neg[ni] * neg[ni + 1]);
      }
      if (tneg) {
        ans = ans * neg[ni] % M;
        ans = ans * neg[ni + 1] % M;
        ni += 2, cnt += 2;
      } else {
        ans = ans * pos[pi] % M;
        pi++, cnt++;
      }
    }
    cout << ans << "\\n";
  } else if (zer) {
    // zero
    printf("0\\n");
  } else {
    // negative, either all negative or k == n
    ll ans = 1;
    if (k == n) {
      for (auto e : pos)
        ans = ans * e % M;
      for (auto e : neg)
        ans = ans * e % M;
    } else {
      sort(neg.begin(), neg.end());
      for (int i = 0; i < k; i++)
        ans = ans * neg[i] % M;
    }
    ans = (M - ans) % M;
    cout << ans << "\\n";
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int M = 1e9 + 7;

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<ll> pos, neg;
  ll zer, a;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a);
    if (a > 0)
      pos.push_back(a);
    else if (a < 0)
      neg.push_back(-a);
    else
      zer++;
  }
  sort(pos.begin(), pos.end(), greater<>());
  sort(neg.begin(), neg.end(), greater<>());
  if (zer && (neg.size() / 2) * 2 + pos.size() < k) {
    printf("0\\n");
    return 0;
  } else if (k % 2 == 1 && zer + pos.size() == 0) {
    ll ans = 1;
    reverse(neg.begin(), neg.end());
    for (int i = 0; i < k; i++)
      ans = ans * neg[i] % M;
    ans = (M - ans) % M;
    cout << ans << "\\n";
    return 0;
  }
  ll ans = 1;
  int pi = 0, ni = 0, cnt = 0;
  while (cnt < k) {
    bool tneg = (k - cnt >= 2) && (ni + 1 < neg.size());
    if (tneg && pi + 1 < pos.size()) {
      tneg = (pos[pi] * pos[pi + 1]) <= (neg[ni] * neg[ni + 1]);
    }
    if (tneg) {
      ans = ans * neg[ni] % M;
      ans = ans * neg[ni + 1] % M;
      ni += 2, cnt += 2;
    } else {
      ans = ans * pos[pi] % M;
      pi++, cnt++;
    }
  }
  cout << ans << "\\n";
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  bool found = 0;
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (k == n)
      ans = ans * (x + mod) % mod;
    if (x > 0)
      pos.push_back(x);
    else if (x < 0)
      neg.push_back(-x);
    else
      found = 1;
  }
  if (k == n)
    return cout << ans, 0;
  n = pos.size();
  int m = neg.size();
  if (n + m < k || n + m - m % 2 < k)
    return cout << 0, 0;
  if (n == 0 && k & 1) {
    if (found)
      return cout << 0, 0;
    sort(neg.begin(), neg.end());
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      ans = ans * (-neg[i] + mod) % mod;
    }
    return cout << ans, 0;
  }
  sort(neg.rbegin(), neg.rend());
  sort(pos.rbegin(), pos.rend());
  int p1 = 0, p2 = 0;
  if (k & 1)
    ans = pos[0], p1 = 1;
  for (int i = k & 1; i < k; i += 2) {
    ll best_pos = 0, best_neg = 0;
    if (p1 + 1 < n)
      best_pos = pos[p1] * pos[p1 + 1];
    if (p2 + 1 < m)
      best_neg = neg[p2] * neg[p2 + 1];
    if (best_pos >= best_neg) {
      ans = ans * pos[p1] % mod;
      ans = ans * pos[p1 + 1] % mod;
      p1 += 2;
    } else {
      ans = ans * (-neg[p2] + mod) % mod;
      ans = ans * (-neg[p2 + 1] + mod) % mod;
      p2 += 2;
    }
  }
  cout << ans;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<ll> pos, neg;
  bool found = 0;
  ll ans = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (k == n)
      ans = ans * (x + mod) % mod;
    if (x > 0)
      pos.push_back(x);
    else if (x < 0)
      neg.push_back(-x);
    else
      found = 1;
  }
  if (k == n)
    return cout << ans, 0;
  n = pos.size();
  int m = neg.size();
  if (n + m < k || n + m - m % 2 < k)
    return cout << 0, 0;
  if (n == 0 && k & 1) {
    if (found)
      return cout << 0, 0;
    sort(neg.begin(), neg.end());
    ll ans = 1;
    for (int i = 0; i < k; i++) {
      ans = ans * (-neg[i] + mod) % mod;
    }
    return cout << ans, 0;
  }
  int kam = 0;
  sort(neg.rbegin(), neg.rend());
  sort(pos.rbegin(), pos.rend());
  int p1 = 0, p2 = 0;
  if (k & 1)
    ans = pos[0], p1 = 1;
  for (int i = k & 1; i < k; i += 2) {
    int best_pos = 0, best_neg = 0;
    if (p1 + 1 < n)
      best_pos = pos[p1] * pos[p1 + 1];
    if (p2 + 1 < m)
      best_neg = neg[p2] * neg[p2 + 1];
    if (best_pos >= best_neg) {
      ans = ans * pos[p1] % mod;
      ans = ans * pos[p1 + 1] % mod;
      p1 += 2;
    } else {
      ans = ans * (-neg[p2] + mod) % mod;
      ans = ans * (-neg[p2 + 1] + mod) % mod;
      p2 += 2;
    }
  }
  cout << ans;

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

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> plus, minus, total;
  int mx = -mod;
  for (int i = 0, num; i < n; i++) {
    cin >> num;
    if (num > 0) {
      plus.push_back(num);
    } else {
      minus.push_back(-num);
    }
    mx = max(mx, num);
    total.push_back(num);
  }
  if (mx <= 0 || n == k) {
    sort(total.begin(), total.end());
    if (k % 2) {
      reverse(total.begin(), total.end());
    }
    int res = 1;
    for (int i = 0; i < k; i++) {
      if (total[i] < 0) {
        res = (res * (total[i] + mod) % mod) % mod;
      } else {
        res = (res * total[i]) % mod;
      }
    }
    cout << res << endl;
  } else {
    sort(plus.begin(), plus.end());
    sort(minus.begin(), minus.end());
    int res = 1;
    if (k % 2) {
      res *= plus.back();
      plus.pop_back();
      k--;
    }
    k /= 2;
    vector<int> ans;
    reverse(plus.begin(), plus.end());
    reverse(minus.begin(), minus.end());
    for (int i = 0; i + 1 < plus.size(); i += 2) {
      ans.push_back(plus[i] * plus[i + 1]);
    }
    for (int i = 0; i + 1 < minus.size(); i += 2) {
      ans.push_back(minus[i] * minus[i + 1]);
    }
    while ((int)ans.size() < k) {
      ans.push_back(0);
    }
    sort(ans.begin(), ans.end());
    reverse(ans.begin(), ans.end());
    for (int i = 0; i < k; i++) {
      res = (res * (ans[i] % mod)) % mod;
    }
    cout << res << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> store;
  int countZ = 0;
  for (int i = 0, num; i < n; i++) {
    cin >> num;
    if (num > 0) {
      store.push_back({num, 1});
    } else if (num < 0) {
      store.push_back({-num, 0});
    } else {
      countZ++;
    }
  }
  sort(store.begin(), store.end());
  vector<int> ansP, ansN;
  int i, K;
  for (i = n - 1, K = k; K > 0 && i >= 0; i--) {
    if (store[i].second)
      ansP.push_back(store[i].first);
    else
      ansN.push_back(-store[i].first);
    K--;
  }
  if ((int)ansN.size() & 1) {
    int posValue = -1;
    if (countZ > 0)
      posValue = 0;
    for (int j = i; j >= 0; j--) {
      if (store[j].second) {
        posValue = store[j].first;
        break;
      }
    }
    if (posValue == -1) {
      int toBePushed = ansN.size();
      ansN.clear();
      for (int i = 0; toBePushed > 0 && i < n; i++) {
        if (!store[i].second) {
          ansN.push_back(-store[i].first);
          toBePushed--;
        }
      }
    } else {
      if (ansN.size() > 0)
        ansN.pop_back();
      ansP.push_back(posValue);
    }
  }
  int ans = 1;
  for (auto v : ansP) {
    ans = (ans * 1LL * (v % mod)) % mod;
  }
  for (auto v : ansN) {
    ans = (ans * 1LL * ((v + mod) % mod)) % mod;
  }
  cout << ans;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define MAXN 200005
#define mod 1000000007
using namespace std;
long long a[MAXN];
int zs = 0, fs = 0;
bool cmp(int a, int b) { return a < b; }
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0)
      fs++;
    else
      zs++;
  }
  if (fs == n) {
    long long sum = 1;
    if (k % 2)
      for (int i = n; i >= n - k + 1; i--)
        sum = (sum % mod * a[i] % mod) % mod;
    else
      for (int i = 1; i <= k; i++)
        sum = (sum % mod * a[i] % mod) % mod;
    printf("%lld", (sum % mod + mod) % mod);
    return 0;
  } else if (n == k) {
    long long sum = 1;
    for (int i = 1; i <= n; i++)
      sum = (sum % mod * a[i] % mod) % mod;
    printf("%lld", (sum % mod + mod) % mod);
    return 0;
  } else {
    long long sum = 1;
    int l = 1, r = n, tot = 0;
    while (tot < k) {
      if (tot != k - 1 && a[l] * a[l + 1] >= a[r] * a[r - 1]) {
        sum = (sum % mod * a[l] % mod * a[l + 1] % mod) % mod;
        l += 2;
        tot += 2;
      } else {
        if (a[r] >= 0) {
          sum = (sum % mod * a[r] % mod) % mod;
          r--;
          tot++;
        } else {
          sum = (sum % mod * a[r] % mod * a[r - 1] % mod) % mod;
          r -= 2;
          tot += 2;
        }
      }
    }
    printf("%lld", (sum % mod + mod) % mod);
    return 0;
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define MAXN 200005
#define mod 1000000007
using namespace std;
int a[MAXN], zs = 0, fs = 0;
bool cmp(int a, int b) { return a < b; }
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    if (a[i] < 0)
      fs++;
    else
      zs++;
  }
  if (fs == n) {
    long long sum = 1;
    if (k % 2)
      for (int i = n; i >= n - k + 1; i--)
        sum = (sum * a[i]) % mod;
    else
      for (int i = 1; i <= k; i++)
        sum = (sum * a[i]) % mod;
    printf("%lld", (sum + mod) % mod);
    return 0;
  } else if (n == k) {
    long long sum = 1;
    for (int i = 1; i <= n; i++)
      sum = (sum * a[i]) % mod;
    printf("%lld", (sum + mod) % mod);
    return 0;
  } else {
    long long sum = 1;
    int l = 1, r = n, tot = 0;
    while (tot < k) {
      if (tot != k - 1 && a[l] * a[l + 1] >= a[r] * a[r - 1]) {
        sum = (sum * a[l] * a[l + 1]) % mod;
        l += 2;
        tot += 2;
      } else {
        sum = (sum * a[r]) % mod;
        r++;
        tot++;
      }
    }
    printf("%lld", (sum + mod) % mod);
    return 0;
  }
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
using ll = long long;

int main() {
  const ll mod = 1000000007;
  ll x, k, ans = 1;
  bool plus = false;
  cin >> x >> k;
  if (x == k) {
    ll a;
    rep(i, x) {
      cin >> a;
      ans = ans * a % mod;
      if (ans < 0)
        ans += mod;
    }
    cout << ans << endl;
    return 0;
  }
  if (k % 2 == 0)
    plus = true;
  vector<ll> p, n;
  rep(i, x) {
    ll a;
    cin >> a;
    if (a >= 0)
      p.push_back(a);
    else
      n.push_back(a);
    if (a >= 0 && !plus)
      plus = true;
  }
  sort(p.rbegin(), p.rend());
  sort(n.begin(), n.end());
  if (plus) {
    int ip = 0, in = 0;
    if (k % 2) {
      ans *= p[0];
      ip++;
      k--;
    }
    while (k) {
      ll pos = 0, neg = 0;
      if (ip + 1 < (int)p.size()) {
        pos = p[ip] * p[ip + 1];
      }
      if (in + 1 < (int)n.size()) {
        neg = n[in] * n[in + 1];
      }
      if (pos >= neg) {
        ans = ans * (pos % mod) % mod;
        ip += 2;
        k -= 2;
      } else {
        ans = ans * (neg % mod) % mod;
        in += 2;
        k -= 2;
      }
    }
  } else {
    ll i = n.size() - 1;
    while (k) {
      ans = ans * n[i] % mod;
      i--;
      k--;
    }
  }
  if (ans < 0)
    ans += mod;
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

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

bool check(int x, int y) { return abs(x) < abs(y); }

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  vector<int> s, t;
  rep(i, n) {
    int a[n];
    cin >> a[i];
    if (a[i] < 0)
      t.push_back(a[i]);
    else
      s.push_back(a[i]);
  }
  int S = s.size();
  int T = t.size();
  bool sei = false;
  if (S > 0) {
    if (n == k)
      sei = (T % 2 == 0);
    else
      sei = true;
  } else {
    sei = (k % 2 == 0);
  }
  ll ans = 1;
  if (!sei) {
    sort(a.begin(), a.end(), check);
    rep(i, k) {
      ans *= a[i];
      ans %= MOD
    }
  } else {
    vector<ll> p;
    sort(s.begin(), s.end());
    sort(t.rbegin(), t.rend());
    if (k % 2 == 1) {
      ans *= s.back();
      s.pop_back();
    }
    while (s.size() >= 2) {
      ll x = s.back();
      s.pop_back();
      x *= s.back();
      x %= MOD;
      s.pop_back();
      p.push_back(x);
    }
    while (t.size() >= 2) {
      ll x = t.back();
      t.pop_back();
      x *= t.back();
      x %= MOD;
      t.pop_back();
      t.push_back(x);
    }
    sort(p.rbegin(), p.rend());
    rep(i, k / 2) {
      ans *= p[i];
      ans %= 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;
using P = pair<int, int>;
ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  vector<int> pos, neg;
  rep(i, n) {
    cin >> a.at(i);
    if (a.at(i) < 0)
      neg.push_back(a.at(i));
    else
      pos.push_back(a.at(i));
  }

  // 正にできる組み合わせがあるか調べる
  bool ok = true;
  int posSize = pos.size();
  int negSize = neg.size();
  if (posSize == 0) {
    ok = (k % 2 == 0);
  } else {
    if (n == k)
      ok = (negSize % 2 == 0);
  }

  ll ans = 1;
  if (!ok) {
    // できない場合は絶対値の小さい順に取る
    sort(a.begin(), a.end(), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, k) ans = ans * a.at(i) % mod;
    ans = mod + ans;
  } else {
    // できる場合は積を大きくできる組み合わせを選ぶ
    sort(pos.begin(), pos.end());
    sort(neg.rbegin(), neg.rend());
    if (k % 2 == 1) {
      ans = ans * pos.back() % mod;
      pos.pop_back();
    }

    vector<ll> p;
    while (pos.size() > 1) {
      ll tmp = pos.back();
      pos.pop_back();
      tmp *= pos.back();
      pos.pop_back();
      p.push_back(tmp);
    }
    while (neg.size() > 1) {
      ll tmp = neg.back();
      neg.pop_back();
      tmp *= neg.back();
      neg.pop_back();
      p.push_back(tmp);
    }
    sort(p.rbegin(), p.rend());
    rep(i, k / 2) ans = ans * (p.at(i) % mod) % 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;
using P = pair<int, int>;
ll mod = 1e9 + 7;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n, 0);
  vector<int> pos, neg;
  rep(i, n) {
    cin >> a.at(i);
    if (a.at(i) < 0)
      neg.push_back(a.at(i));
    else
      pos.push_back(a.at(i));
  }

  // 正にできる組み合わせがあるか調べる
  bool ok = true;
  int posSize = (int)pos.size();
  if (posSize == 0) {
    ok = (k % 2 == 0);
  } else {
    if (n == k)
      ok = (k % 2 == 0);
  }

  ll ans = 1;
  if (!ok) {
    // できない場合は絶対値の小さい順に取る
    sort(a.begin(), a.end(), [](int x, int y) { return abs(x) < abs(y); });
    rep(i, k) ans = ans * a.at(i) % mod;
    ans = mod + ans;
  } else {
    // できる場合は積を大きくできる組み合わせを選ぶ
    sort(pos.begin(), pos.end());
    sort(neg.rbegin(), neg.rend());
    if (k % 2 == 1) {
      ans = ans * pos.back() % mod;
      pos.pop_back();
    }

    vector<ll> p;
    while (pos.size() > 1) {
      ll tmp = pos.back();
      pos.pop_back();
      tmp *= pos.back();
      pos.pop_back();
      p.push_back(tmp);
    }
    while (neg.size() > 1) {
      ll tmp = neg.back();
      neg.pop_back();
      tmp *= neg.back();
      neg.pop_back();
      p.push_back(tmp);
    }
    sort(p.rbegin(), p.rend());
    rep(i, k / 2) ans = ans * (p.at(i) % 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)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

// 解説参考. 解法2.
int main() {
  int n, k;
  cin >> n >> k;

  vector<llong> a, b;
  REP(i, n) {
    llong in;
    cin >> in;
    if (in < 0)
      a.push_back(in);
    else
      b.push_back(in);
  }
  sort(ALL(a));
  sort(ALL(b), greater<llong>());

  if (k == n) {
    llong anss = 1;
    REP(i, a.size()) anss = anss * (MOD + a[i]) % MOD;
    REP(i, b.size()) anss = anss * b[i] % MOD;
    cout << anss << endl;
    return 0;
  }

  if (a.size() == n and k % 2 == 1) {
    llong anss = 1;
    for (int i = a.size() - 1; i >= a.size() - k; --i)
      anss = anss * (MOD + a[i]) % MOD;
    cout << anss << endl;
    return 0;
  }

  llong ans = 1;
  int pa = 0, pb = 0;
  while (k > 0) {
    if (k <= 1) {
      ans = ans * b[pb++] % MOD;
      k--;
    } else {
      if (a.size() - pa >= 2 and
          (b.size() - pb <= 1 or
           (b.size() - pb >= 2 and a[pa] * a[pa + 1] >= b[pb] * b[pb + 1]))) {
        REP(i, 2) ans = ans * (MOD + a[pa++]) % MOD;
        k -= 2;
      } else {
        ans = ans * b[pb++] % MOD;
        k--;
      }
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

// 解説参考. 解法2.
int main() {
  int n, k;
  cin >> n >> k;

  vector<llong> a, b;
  REP(i, n) {
    llong in;
    cin >> in;
    if (in < 0)
      a.push_back(in);
    else
      b.push_back(in);
  }
  sort(ALL(a));
  sort(ALL(b), greater<llong>());

  if (a.size() == n and k % 2 == 1) {
    llong anss = 1;
    for (int i = a.size() - 1; i >= a.size() - k; --i)
      anss = anss * (MOD + a[i]) % MOD;
    cout << anss << endl;
    return 0;
  }

  llong ans = 1;
  int pa = 0, pb = 0;
  while (k > 0) {
    if (k <= 1) {
      ans = ans * b[pb++] % MOD;
      k--;
    } else {
      if (a.size() - pa >= 2 and
          (b.size() - pb <= 1 or
           (b.size() - pb >= 2 and a[pa] * a[pa + 1] >= b[pb] * b[pb + 1]))) {
        REP(i, 2) ans = ans * (MOD + a[pa++]) % MOD;
        k -= 2;
      } else {
        ans = ans * b[pb++] % MOD;
        k--;
      }
    }
  }

  cout << ans << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

const int MOD = 1e+9 + 7;

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

  sort(p.begin(), p.end());
  sort(n.begin(), n.end());

  int ps = p.size();
  int ns = n.size();
  long long ans = 1;
  if (N == K) {
    REP(i, ps) ans = (ans * p[i]) % MOD;
    REP(i, ns) ans = (ans * n[i]) % MOD;
  } else if (ns == N) {
    if (K % 2 == 0)
      REP(i, K) ans = (ans * n[i]) % MOD;
    if (K % 2 == 1)
      REP(i, K) ans = (ans * n[N - 1 - i]) % MOD;
  } else {
    int i = ps - 1, j = 0;
    if (K % 2 == 1) {
      ans *= p[i];
      ans %= MOD;
      --i, --K;
    }
    while (K) {
      if ((0 <= i - 1 and j + 1 < ns and p[i - 1] * p[i] >= n[j] * n[j + 1]) or
          j >= ns - 1) {
        ans *= p[i - 1] * p[i] % MOD;
        ans %= MOD;
        i -= 2;
      } else {
        ans *= n[j] * n[j + 1] % MOD;
        ans %= MOD;
        j += 2;
      }
      K -= 2;
    }
  }
  cout << (ans + MOD) % MOD << endl;

  return 0;
}

Error: 0

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)

const int MOD = 1e+9 + 7;

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

  sort(p.begin(), p.end());
  sort(n.begin(), n.end());

  long long ans = 1;
  if (N == K) {
    REP(i, p.size()) ans = (ans * p[i]) % MOD;
    REP(i, n.size()) ans = (ans * n[i]) % MOD;
  } else if (n.size() == N) {
    if (K % 2 == 0)
      REP(i, K) ans = (ans * n[i]) % MOD;
    if (K % 2 == 1)
      REP(i, K) ans = (ans * n[N - 1 - i]) % MOD;
  } else {
    int i = p.size() - 1, j = 0;
    if (K % 2 == 1) {
      ans *= p[i];
      ans %= MOD;
      --i, --K;
    }
    while (K) {
      if ((0 <= i - 1 and j + 1 < n.size() and
           p[i - 1] * p[i] >= n[j] * n[j + 1]) or
          j >= n.size() - 1) {
        ans *= p[i - 1] * p[i] % MOD;
        ans %= MOD;
        i -= 2;
      } else {
        ans *= n[j] * n[j + 1] % MOD;
        ans %= MOD;
        j += 2;
      }
      K -= 2;
    }
  }
  cout << (ans + MOD) % MOD << endl;

  return 0;
}