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

#include <bits/stdc++.h>
using namespace std;
int a[200005], n;
long long ans;
bool cmp(int s1, int s2) { return s1 > s2; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  // for(int i=1;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
  ans = a[1];
  for (int i = 3; i <= n; i++)
    ans += a[(i + 1) / 2];
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long a[200005], n;
long long ans;
bool cmp(int d, int b) { return a[d] > a[b]; }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  ans = a[1];
  for (int i = 3; i <= n; i++)
    ans += a[(i + 1) / 2];
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pb push_back
#define mp make_pair
#define FI first
#define SE second
#define maxn 200000
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef double db;

int a[maxn + 5];

int main() {
  int n;
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, greater<int>());
  ll ans = -a[1];
  rep(i, 1, n) ans += a[(i + 1) / 2];
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i <= n; i++)
using namespace std;
typedef long long ll;
int maxn = 50000;
int a[50005];
int main() {
  int n;
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]);
  sort(a + 1, a + n + 1, greater<int>());
  ll ans = -a[1];
  rep(i, 1, n) ans += a[(i + 1) / 2];
  printf("%lld\\n", ans);
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
// https://atcoder.jp/contests/abc173/tasks/abc173_e
ll mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(all(a));
  ll l = 0, r = n - 1;
  ll ans = 1;
  if (k & 1) {
    if (a[n - 1] < 0) {
      ans = 1;
      for (int i = n - 1; i >= n - k; i--) {
        ans *= a[i];
        ans %= mod;
      }
      if (ans < 0)
        cout << ans + mod;
      else
        cout << ans;
      return 0;
    }
    r--;
    k--;
  }

  while (k) {
    ll lv = a[l] * a[l + 1];
    ll rv = a[r] * a[r - 1];
    if (lv < rv)
      r -= 2;
    else
      l += 2;
    k -= 2;
  }
  ans = 1;
  for (int i = 0; i < l; i++) {
    ans *= a[i];
    ans %= mod;
  }
  for (int i = r + 1; i < n; i++) {
    ans *= a[i];
    ans %= mod;
  }
  if (ans < 0)
    cout << ans + mod;
  else
    cout << ans;

  return 0;
}


Error: WA

Fixed submission:

#pragma warning(disable : 4996)
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
// https://atcoder.jp/contests/abc173/tasks/abc173_e
ll mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  ll n, k;
  cin >> n >> k;
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(all(a));
  ll l = 0, r = n - 1;
  ll ans = 1;
  if (k & 1) {
    if (a[n - 1] < 0) {
      ans = 1;
      for (int i = n - 1; i >= n - k; i--) {
        ans *= a[i];
        ans %= mod;
      }
      if (ans < 0)
        cout << ans + mod;
      else
        cout << ans;
    }
    r--;
    k--;
  }

  while (k) {
    ll lv = a[l] * a[l + 1];
    ll rv = a[r] * a[r - 1];
    if (lv < rv)
      r -= 2;
    else
      l -= 2;
    k -= 2;
  }
  ans = 1;
  for (int i = 0; i < l; i++) {
    ans *= a[i];
    ans %= mod;
  }
  for (int i = r + 1; i < n; i++) {
    ans *= a[i];
    ans %= mod;
  }
  if (ans < 0)
    cout << ans + mod;
  else
    cout << ans;

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


// 'By Anki'

#include <bits/stdc++.h>
// #include<iostream>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define wr(i) cout << #i << " = " << i << ",  ";
#define wre(i) cout << #i << "  =  " << i << endl;
#define all(v) v.begin(), v.end()
typedef long long ll;
#define lim 1000000007LL
typedef unsigned long long ull;
#define M 10004
using u64 = uint64_t;

ll mod(ll n) {
  if (n < 0)
    return n + lim;
  else
    return n % lim;
}

int main() {
  fast ll t = 1LL;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> v(n);
    for (auto &x : v)
      cin >> x;
    sort(all(v));
    ll ans = 1LL;
    if (v[n - 1] < 0 && k & 1)
      for (ll i = 0; i < k; i++)
        ans = (ans * v[n - i - 1]) % lim;
    else {
      ll i = 0, j = n - 1;
      while (k) {
        if (k > 1) {
          if (v[i] * v[i + 1] > v[j] * v[j - 1])
            ans = (((v[i] * v[i + 1]) % lim) * ans) % lim, i += 2, k -= 2;
          else
            ans = (ans * v[j]) % lim, j--, k--;
        } else
          ans = (ans * v[j]) % lim, k--, j--;
        // wr(ans)
      }
      // cout<<(ans+lim)%lim;
    }
    cout << (ans + lim) % lim;
  }
}

Error: WA

Fixed submission:


// 'By Anki'

#include <bits/stdc++.h>
// #include<iostream>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define wr(i) cout << #i << " = " << i << ",  ";
#define wre(i) cout << #i << "  =  " << i << endl;
#define all(v) v.begin(), v.end()
typedef long long ll;
#define lim 1000000007LL
typedef unsigned long long ull;
#define M 10004
using u64 = uint64_t;

ll mod(ll n) {
  if (n < 0)
    return n + lim;
  else
    return n % lim;
}

int main() {
  fast ll t = 1LL;
  // cin>>t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<ll> v(n);
    for (auto &x : v)
      cin >> x;
    sort(all(v));
    ll ans = 1LL;
    if (v[n - 1] < 0 && k & 1)
      for (int i = 0; i < k; i++)
        ans = (ans * v[n - i - 1]) % lim;
    else {
      int i = 0, j = n - 1;
      while (k) {
        if (k > 1) {
          if (v[i] * v[i + 1] > v[j] * v[j - 1])
            ans = (ans * v[i] * v[i + 1]) % lim, i += 2, i++;
          else
            ans = (ans * v[j]) % lim, j--, k--;
        } else
          ans = (ans * v[j]) % lim, k--, j--;
        // wr(ans)
      }
      // cout<<(ans+lim)%lim;
    }
    cout << (ans + lim) % lim;
  }
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ft                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
const int N = 2e5 + 3;
ll M = 1e9 + 7;
ll a[N];
int main() {
  ft ll i, j, n, k, l, r, s = 1;
  cin >> n >> k;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  if (k % 2 == 1) {
    if (a[n - 1] == 0) {
      cout << "0\\n";
      return 0;
    } else if (a[n - 1] < 0) {
      for (i = n - 1; i >= (n - k); i--) {
        s = (s * (a[i] + M)) % M;
      }
      cout << s << "\\n";
      return 0;
    } else {
      s = (s * (a[n - 1] + M)) % M;
      n--, k--;
    }
  }
  l = 0, r = n - 1;
  while (k > 0) {
    if ((a[l] * a[l + 1]) > (a[r] * a[r - 1])) {
      s = (s * (a[l] + M)) % M;
      s = (s * (a[l + 1] + M)) % M;
      l += 2;
    } else {
      s = (s * (a[r] + M)) % M;
      s = (s * (a[r - 1] + M)) % M;
      r -= 2;
    }
    k -= 2;
  }
  cout << s << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ft                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define pi pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define B begin()
#define E end()
const ll N = 2e5 + 3;
ll M = 1e9 + 7;
ll a[N];
ll maxp(ll n, ll k) {
  ll mp = 1, r;
  for (ll i = 0; i < k; i++) {
    mp *= a[i];
    mp %= M;
  }

  ll pp = mp;
  for (ll i = 1; i <= n - k; i++) {
    ll cp = (pp / a[i - 1]) * a[i + k - 1];
    cp %= M;
    mp = max(mp, cp);
    pp = cp;
  }
  return mp;
}
int main() {
  ft ll t, i, j, n, m, l, r;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  l = maxp(n, m);
  if (l < 0) {
    r = abs(l * 1.0);
    r = ceil(r * 1.0 / M);
    l += r * M;
  }
  cout << l % M << "\\n";
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;

typedef long long LL;
const int N = 200010, mod = 1000000007;
int a[N];

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

  sort(a, a + n);

  LL res = 1;
  int l = 0, r = n - 1;
  int sign = 1;
  if (k % 2) {
    res = a[r];
    r--;
    k--;

    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;
      l += 2;
    } else {
      res = y % mod * res % mod;
      r -= 2;
    }
    k -= 2;
  }
  printf("%lld", (res % mod + mod) % mod);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;

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

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

  sort(a, a + n);

  LL res = 1;
  int l = 0, r = n - 1;
  int sign = 1;
  if (k % 2) {
    res = a[r];
    r--;
    k--;

    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;
      l += 2;
    } else {
      res = y % mod * res % mod;
      r -= 2;
    }
    k -= 2;
  }
  printf("%lld", (res % mod + mod) % mod);
  return 0;
}
Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  long long int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  long long int mod = 1e9 + 7;
  long long int ans = 1;
  vector<long long int> plus;
  vector<long long int> minus;
  for (int i = 0; i < n; i++) {
    if (a[i] < 0)
      minus.push_back(a[i]);
    else
      plus.push_back(a[i]);
  }
  if (n == k) {
    int t = 0;
    for (int i = 0; i < k; i++) {
      ans *= abs(a[i]);
      ans %= mod;
      if (a[i] < 0)
        t++;
    }
    if (t % 2 == 1 && ans != 0)
      cout << mod - ans;
    else
      cout << ans;
  } else if (a[n - 1] < 0 && k % 2 == 1) {
    for (int i = 0; i < k; i++) {
      ans *= abs(a[n - 1 - i]);
      ans %= mod;
    }
    cout << mod - ans;
  } else {
    int p;
    long unsigned int m;
    int count = 0;
    p = plus.size() - 1;
    m = 0;
    while (count < k) {
      while ((m + 1 < minus.size() && p - 1 >= 0) && count + 2 <= k) {
        if (minus[m] * minus[m + 1] >= plus[p] * plus[p - 1]) {
          ans *= ((minus[m] * minus[m + 1]) % mod);
          ans %= mod;
          m += 2;
          count += 2;
        } else
          break;
      }
      while (p < 0 && count + 1 <= k) {
        ans *= abs(minus[m]);
        ans %= mod;
        m++;
        count++;
      }
      if (p == 0 && (count < k && (k - count) % 2 == 0)) {
        while (count + 1 <= k) {
          ans *= abs(minus[m]);
          ans %= mod;
          m++;
          count++;
        }
      }
      if (count + 1 <= k && p >= 0) {
        ans *= plus[p];
        ans %= mod;
        p--;
        count++;
      }
    }
    cout << ans;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  long long int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  long long int mod = 1e9 + 7;
  long long int ans = 1;
  vector<long long int> plus;
  vector<long long int> minus;
  for (int i = 0; i < n; i++) {
    if (a[i] < 0)
      minus.push_back(a[i]);
    else
      plus.push_back(a[i]);
  }
  if (a[n - 1] < 0 && k % 2 == 1) {
    for (int i = 0; i < k; i++) {
      ans *= abs(a[n - 1 - i]);
      ans %= mod;
    }
    cout << mod - ans;
  } else {
    int p;
    long unsigned int m;
    int count = 0;
    p = plus.size() - 1;
    m = 0;
    while (count < k) {
      while ((m + 1 < minus.size() && p - 1 >= 0) && count + 2 <= k) {
        if (minus[m] * minus[m + 1] >= plus[p] * plus[p - 1]) {
          ans *= ((minus[m] * minus[m + 1]) % mod);
          ans %= mod;
          m += 2;
          count += 2;
        } else
          break;
      }
      while ((p < 0 && m + 1 < minus.size()) && count + 2 <= k) {
        ans *= ((minus[m] * minus[m + 1]) % mod);
        ans %= mod;
        m += 2;
        count += 2;
      }
      if (count + 1 <= k && p >= 0) {
        ans *= ((plus[p]) % mod);
        ans %= mod;
        p--;
        count++;
      }
    }
    cout << ans;
  }
}
Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
LL A[maxn];
int T, n, m, k;

int main() {
  cin >> n >> k;
  LL res = 1, sign = 1;
  for (int i = 1; i <= n; i++)
    cin >> A[i];
  sort(A + 1, A + 1 + n);
  int l = 1, r = n;
  if (k & 1) {
    res *= A[r--];
    k--;
    if (res < 0)
      sign = -1;
  }
  while (k > 0) {
    LL x = A[l] * A[l + 1], y = A[r] * A[r - 1];
    if (x * sign > y * sign) {
      res = x % mod * res % mod;
      l += 2;
    } else {
      res = y % mod * res % mod;
      r -= 2;
    }
    k -= 2;
  }
  cout << (res + mod) % mod << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 100;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int A[maxn];
int T, n, m, k;

int main() {
  cin >> n >> k;
  int cnt1 = 0, cnt2 = 0, cnt3 = 0;
  LL res = 1;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
    if (A[i] < 0)
      cnt1++;
    if (A[i] == 0)
      cnt2++;
    if (A[i] > 0)
      cnt3++;
  }
  sort(A + 1, A + 1 + n);
  LL res1 = 1;
  for (int i = n; i > n - k; i--)
    res1 = res1 * A[i] % mod;
  int l = 1, r = n;
  while (k) {
    if (k >= 2 && 1LL * A[l] * A[l + 1] >= 1LL * A[r] * A[r - 1]) {
      res = res * A[l] * A[l + 1] % mod;
      l += 2;
      k -= 2;
    } else {
      res = res * A[r] * A[r - 1] % mod;
      r -= 2;
      k -= 2;
    }
  }
  if (res < 0 && cnt2)
    res = 0;
  if (res < res1 && !cnt2)
    res = res1;
  cout << (res + mod) % mod << endl;
}
Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define SZ(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> Pii;
typedef pair<ll, int> Pli;
typedef unsigned long long ull;
const double eps = 1e-8;
const ll infll = 0x7fffffffffffffff;
const int mod = (int)(1e9) + 7;
const int inf = 0x7fffffff;
const int maxn = (int)(2e5) + 7;
const int maxm = 300;

int n, k, l1, l2, l3, z;
ll a[maxn], b[maxn], c[maxn];

int main() {

  // freopen("in.in", "r", stdin);
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    ll x;
    scanf("%lld", &x);
    if (x > 0)
      a[++l1] = x;
    if (x < 0)
      b[++l2] = x;
    if (x == 0)
      z++;
    c[++l3] = abs(x);
  }
  sort(a + 1, a + 1 + l1);
  sort(b + 1, b + 1 + l2, greater<ll>());
  ll res = 1;
  if (l1 + l2 / 2 * 2 >= k && (k % 2 == 0 || l1)) { // 正数
    if (k % 2) {
      res = res * a[l1] % mod;
      l1--;
      k--;
    }
    while (k) {
      ll v1 = (l1 > 1 ? a[l1] * a[l1 - 1] : 0);
      ll v2 = (l2 > 1 ? b[l2] * b[l2 - 1] : 0);
      if (v2 >= v1) {
        res = v2 % mod * res % mod;
        l2 -= 2;
      } else {
        res = v1 % mod * res % mod;
        l1 -= 2;
      }
      k -= 2;
    }
  } else if (z)
    res = 0; // 零
  else {     // 负数
    sort(c + 1, c + 1 + n);
    for (int i = 1; i <= k; i++)
      res = res * c[i] % mod;
    res = (-res + mod) % mod;
  }
  printf("%lld\\n", res);

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define SZ(a) (int)((a).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int> Pii;
typedef pair<ll, int> Pli;
typedef unsigned long long ull;
const double eps = 1e-8;
const ll infll = 0x7fffffffffffffff;
const int mod = (int)(1e9) + 7;
const int inf = 0x7fffffff;
const int maxn = (int)(2e5) + 7;
const int maxm = 300;

int n, k, l1, l2, l3, z;
ll a[maxn], b[maxn], c[maxn];

int main() {

  // freopen("in.in", "r", stdin);
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    ll x;
    scanf("%lld", &x);
    if (x > 0)
      a[++l1] = x;
    if (x < 0)
      b[++l2] = x;
    if (x == 0)
      z++;
    c[++l3] = abs(x);
  }
  sort(a + 1, a + 1 + l1);
  sort(b + 1, b + 1 + l2, greater<ll>());
  ll res = 1;
  if (l1 + l2 / 2 * 2 >= k && (k % 2 == 0 || l1)) { // 正数
    if (k % 2) {
      res = res * a[l1] % mod;
      l1--;
    }
    while (k) {
      ll v1 = (l1 > 1 ? a[l1] * a[l1 - 1] : 0);
      ll v2 = (l2 > 1 ? b[l2] * b[l2 - 1] : 0);
      if (v2 >= v1) {
        res = v2 % mod * res % mod;
        l2 -= 2;
      } else {
        res = v1 % mod * res % mod;
        l1 -= 2;
      }
      k -= 2;
    }
  } else if (z)
    res = 0; // 零
  else {     // 负数
    sort(c + 1, c + 1 + n);
    for (int i = 1; i <= k; i++)
      res = res * c[i] % mod;
    res = (-res + mod) % mod;
  }
  printf("%lld\\n", res);

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


#include <bits/stdc++.h>
using namespace std;
long M = 1e9 + 7, n, k, ans = 1, l, r, b = 1, a[1 << 18], x, y, i;
main() {
  cin >> n >> k;
  r = n - 1;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  if (k % 2)
    ans = a[r], r--, k--, b = (ans < 0 ? -1 : 1);
  while (k) {
    x = a[l] * a[l + 1], y = a[r] * a[r - 1];
    if (x * b > y * b)
      ans *= x % M, ans %= M, l += 2;
    else
      ans *= y % M, ans %= M, r -= 2;
    k -= 2;
  }
  cout << (ans + M) % M << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
#define pb push_back
#define mp make_pair
#define M 1000000007
#define vi vector<ll>
using namespace std;
ll power(ll x, ll y, ll m) { // power(x,y)=x^y  %m;
  ll res = 1 % m;
  while (y) {
    if (y & 1)
      res = (res * x) % m;
    x = (x * x) % m;
    y = (y >> 1) % m;
  }
  return res;
}
ll mod_inverse(ll x, ll m) { return power(x, m - 2, m); }
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k;
  ll a[n];
  ll b = 1;
  cin >> n >> k;
  ll r = n - 1;
  ll l = 0;
  ll ans = 0;
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  if (k % 2) {
    ans = a[r];
    r--;
    k--;
    b = (ans < 0 ? -1 : 1);
  }
  while (k) {
    ll x = a[l] * a[l + 1];
    ll y = a[r] * a[r - 1];
    if (x * b > y * b)
      ans *= x % M, ans %= M, l += 2;
    else
      ans *= y % M, ans %= M, r -= 2;
    k -= 2;
  }
  cout << (ans + M) % M << endl;

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

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

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

const int mod = 1e9 + 7;

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

  int n, k;
  cin >> n >> k;
  vi a(n);
  for (int &i : a)
    cin >> i;
  sort(all(a));

  int fpos = n, f0 = n;
  rep(i, n) if (a[i] == 0) {
    f0 = i;
    break;
  }
  rep(i, n) if (a[i] > 0) {
    fpos = i;
    if (f0 == n)
      f0 = i;
    break;
  }

  if (n - fpos + f0 < k || fpos == n && f0 != n && k & 1)
    return cout << 0, 0;

  ll ans = 1;
  if ((n - fpos + f0 == k && f0 & 1) || (fpos == n && k & 1)) {
    for (int &i : a)
      if (i < 0)
        i *= -1;
    sort(all(a));
    rep(i, k) ans = ans * a[i] % mod;
    return cout << (mod - ans) % mod, 0;
  }

  int l = min(f0, k) / 2 * 2, r = n - k + l;
  while (l > 1 && r > fpos + 1 &&
         (ll)a[l - 2] * a[l - 1] < (ll)a[r - 2] * a[r - 1]) {
    l -= 2, r -= 2;
  }

  rep(i, l) ans = ans * -a[i] % mod;
  for (int i = r; i < n; i++)
    ans = ans * a[i] % mod;
  cout << ans;
}


Error: WA

Fixed submission:

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

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

const int mod = 1e9 + 7;

ll iv(ll a, ll b) { return a > 1 ? b - iv(b % a, a) * b / a : 1; }

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

  int n, k;
  cin >> n >> k;
  vi a(n);
  for (int &i : a)
    cin >> i;
  sort(all(a));

  int fpos = n, f0 = n;
  rep(i, n) if (a[i] == 0) {
    f0 = i;
    break;
  }
  rep(i, n) if (a[i] > 0) {
    fpos = i;
    if (f0 == n)
      f0 = i;
    break;
  }

  if (n - fpos + f0 < k || fpos == n && f0 != n && k & 1)
    return cout << 0, 0;

  ll ans = 1;
  if ((n - fpos + f0 == k && f0 & 1) || (fpos == n && k & 1)) {
    for (int &i : a)
      if (i < 0)
        i *= -1;
    sort(all(a));
    rep(i, k) ans = ans * a[i] % mod;
    return cout << (mod - ans) % mod, 0;
  }

  int l = f0 / 2 * 2, r = n - k + l;
  while (l > 1 && r > fpos + 1 &&
         (ll)a[l - 2] * a[l - 1] < (ll)a[r - 2] * a[r - 1]) {
    l -= 2, r -= 2;
  }

  rep(i, l) ans = ans * -a[i] % mod;
  for (int i = r; i < n; i++)
    ans = ans * a[i] % mod;
  cout << ans;
}