prithivMLmods/Coder-Stat huggingface dataset:
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n, a[100010], ans, x;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ans += a[n - 1];
  for (long long i = n - 2; i > 0; i--) {
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, a[200010], ans, x;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  ans += a[n - 1];
  for (long long i = n - 2; i > 0; i--) {
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
    ans = ans + a[i];
    x++;
    if (x + 2 == n)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);

  ll ans = a[n - 1], k = n - 2, m = 0;
  for (i = n - 3; i >= 0; i--) {
    if (m < 2) {
      ans += a[k];
      m++;
    } else {
      ans += a[++k];
      m = 1;
    }
  }

  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  ll a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);

  ll ans = a[n - 1], k = n - 2, m = 0;
  for (i = n - 3; i >= 0; i--) {
    if (m < 2) {
      ans += a[k];
      m++;
    } else {
      ans += a[--k];
      m = 1;
    }
  }

  cout << ans;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Time Limit Exceeded):
#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>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n, 2);
  b[0] = 1;
  rep(i, n) cin >> a[i];

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

  if (n == 2) {
    cout << a[0] << endl;
    return 0;
  }

  ll ans = 0;
  rep(i, n - 1) rep(j, n) {
    if (b[j] > 0) {
      ans += a[j];
      b[j]--;
      break;
    }
  }

  cout << ans << endl;

  return 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>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n, 2);
  b[0] = 1;
  rep(i, n) cin >> a[i];

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

  if (n == 2) {
    cout << a[0] << endl;
    return 0;
  }

  ll ans = 0;
  int c = 0;
  rep(i, n - 1) {
    ans += a[c];
    b[c]--;
    if (b[c] <= 0)
      c++;
  }

  cout << ans << endl;

  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define N 100010
using namespace std;
ll ans;
int n;
int a[N];

bool cmp(int a, int b) { return a > b; }

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  int hal = n / 2;
  ans += a[1];
  for (int i = 2; i <= hal; i++) {
    ans += (long long)a[i];
    ans += (long long)a[i];
  }
  if (n % 2 == 1)
    ans += (long long)a[hal + 1];
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define N 200010
using namespace std;
ll ans;
int n;
int a[N];

bool cmp(int a, int b) { return a > b; }

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  int hal = n / 2;
  ans += a[1];
  for (int i = 2; i <= hal; i++) {
    ans += (long long)a[i];
    ans += (long long)a[i];
  }
  if (n % 2 == 1)
    ans += (long long)a[hal + 1];
  cout << ans;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <queue>
using namespace std;
int n, a[100010], max;
long long sum;
priority_queue<int> s1;
priority_queue<pair<int, pair<int, int>>> s2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    s1.push(a[i]);
  }
  s2.push(make_pair(s1.top(), make_pair(s1.top(), s1.top())));
  s1.pop();
  for (int i = 2; i <= n; ++i) {
    int x = s1.top();
    s1.pop();
    sum += s2.top().first;
    pair<int, pair<int, int>> k;
    k = s2.top();
    s2.pop();
    s2.push(make_pair(x, make_pair(k.second.first, x)));
    s2.push(make_pair(x, make_pair(k.second.second, x)));
  }
  printf("%lld\n", sum);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <queue>
using namespace std;
int n, a[200010], max;
long long sum;
priority_queue<int> s1;
priority_queue<pair<int, pair<int, int>>> s2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    s1.push(a[i]);
  }
  s2.push(make_pair(s1.top(), make_pair(s1.top(), s1.top())));
  s1.pop();
  for (int i = 2; i <= n; ++i) {
    int x = s1.top();
    s1.pop();
    sum += s2.top().first;
    pair<int, pair<int, int>> k;
    k = s2.top();
    s2.pop();
    s2.push(make_pair(x, make_pair(k.second.first, x)));
    s2.push(make_pair(x, make_pair(k.second.second, x)));
  }
  printf("%lld\n", sum);
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
const int inf = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
#define LOCAL

int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cout.tie(0), cin.tie(0);
  int t;
  cin >> t;
  rep(i, t) { cin >> a[i]; }
  sort(a, a + t, greater<int>());
  std::vector<int> v;
  v.push_back(a[0]);
  for (int i = 1; i < t; i++) {
    v.push_back(a[i]);
    v.push_back(a[i]);
  }
  ll ans = 0;
  rep(i, t - 1) { ans += v[i]; }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s, 0, sizeof(s))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
const int inf = 0x3f3f3f3f;
const int MAXN = 2e5 + 10;
#define LOCAL

int a[MAXN];

int main() {
  ios::sync_with_stdio(false);
  cout.tie(0), cin.tie(0);
  int t;
  cin >> t;
  rep(i, t) { cin >> a[i]; }
  sort(a, a + t, greater<int>());
  std::vector<int> v;
  v.push_back(a[0]);
  for (int i = 1; i < t; i++) {
    v.push_back(a[i]);
    v.push_back(a[i]);
  }
  ll ans = 0;
  rep(i, t - 1) { ans += v[i]; }
  cout << ans << endl;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
const int maxn = 1e5 + 100;
int a[maxn];
int cmp(int x, int y) { return x > y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  sort(a, a + n, cmp);
  // for (int i = 0; i < n; ++i)
  //     cout << a[i] << " "; cout << endl;
  int i = 0, now = 1;
  int t = 0;
  ll res = 0;
  while (t < n - 1) {
    res += a[i];
    now = (now + 1) % 2;
    if (now == 0)
      i++;
    t++;
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) 42
#endif

typedef long long ll;
const int maxn = 2e5 + 100;
int a[maxn];
int cmp(int x, int y) { return x > y; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  sort(a, a + n, cmp);
  // for (int i = 0; i < n; ++i)
  //     cout << a[i] << " "; cout << endl;
  int i = 0, now = 1;
  int t = 0;
  ll res = 0;
  while (t < n - 1) {
    res += a[i];
    now = (now + 1) % 2;
    if (now == 0)
      i++;
    t++;
  }
  cout << res << endl;
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
/* Author: topcoderme123
   Date: 5th july
*/

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define np next_permutation
using namespace std;
using vi = vector<int>;
#define lp(i, n) for (int i = 0; i < n; i++)
#define lps(i, j, n) for (int i = j; i < n; i++)
#define vii vector<vi>
#define vb vector<bool>
#define pr pair<int, int>
#define vl vector<ll>
#define vs vector<string>
#define us unordered_map<int, int>
#define Mpq priority_queue<int>
#define mpq priority_queue<int, vi, greater<int>>
#define eb emplace_back
#define prl pair<ll, ll>
#define vp vector<pr>
#define inf 1000000000000000008
#define ld long double
const ll N = 2e5 + 23;

ll n, sum, j;

ll a[N], dp[N + 6];

void solve() {
  cin >> n;
  lp(i, n) cin >> a[i], dp[a[i]] = 1;
  sort(a, a + n, greater<int>());

  lps(i, 1, n) {
    if (i == 1)
      sum += a[j++], dp[a[i]] <<= 1;
    else {
      sum += a[j];
      if (dp[a[i]] != N)
        dp[a[i]] = min(N, dp[a[i]] * 2);
      dp[a[j]]--;
      while (!dp[a[j]] and j < n)
        j++;
    }
  }
  cout << sum << "\n";
}

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

  // assert(freopen("input.txt", "r", stdin));
  // assert(freopen("output.txt", "w", stdout));

  solve();
  return 0;
}


Fixed submission:
/* Author: topcoderme123
   Date: 5th july
*/

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define F first
#define S second
#define all(v) (v).begin(), (v).end()
#define np next_permutation
using namespace std;
using vi = vector<int>;
#define lp(i, n) for (int i = 0; i < n; i++)
#define lps(i, j, n) for (int i = j; i < n; i++)
#define vii vector<vi>
#define vb vector<bool>
#define pr pair<int, int>
#define vl vector<ll>
#define vs vector<string>
#define us unordered_map<int, int>
#define Mpq priority_queue<int>
#define mpq priority_queue<int, vi, greater<int>>
#define eb emplace_back
#define prl pair<ll, ll>
#define vp vector<pr>
#define inf 1000000000000000008
#define ld long double
const ll N = 2e5 + 23;

ll n, sum, j;

ll a[N];
unordered_map<ll, ll> dp;

void solve() {
  cin >> n;
  lp(i, n) cin >> a[i], dp[a[i]] = 1;
  sort(a, a + n, greater<int>());

  lps(i, 1, n) {
    if (i == 1)
      sum += a[j++], dp[a[i]] <<= 1;
    else {
      sum += a[j];
      if (dp[a[i]] != N)
        dp[a[i]] = min(N, dp[a[i]] * 2);
      dp[a[j]]--;
      while (!dp[a[j]] and j < n)
        j++;
    }
  }
  cout << sum << "\n";
}

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

  // assert(freopen("input.txt", "r", stdin));
  // assert(freopen("output.txt", "w", stdout));

  solve();
  return 0;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;
int main() {
  int n, A[100000];
  scanf("%i", &n);
  for (int i = 0; i < n; i++) {
    scanf("%i", A + i);
  }
  sort(A, A + n, greater<int>());
  long long ans = A[0];
  n -= 2;
  for (int i = 1; n; i++) {
    if (n == 1) {
      ans += A[i];
      n = 0;
    } else {
      ans += A[i] << 1ll;
      n -= 2;
    }
  }
  printf("%lli", ans);
}


Fixed submission:
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;
int main() {
  int n, A[200000];
  scanf("%i", &n);
  for (int i = 0; i < n; i++) {
    scanf("%i", A + i);
  }
  sort(A, A + n, greater<int>());
  long long ans = A[0];
  n -= 2;
  for (int i = 1; n; i++) {
    if (n == 1) {
      ans += A[i];
      n = 0;
    } else {
      ans += A[i] << 1ll;
      n -= 2;
    }
  }
  printf("%lli", ans);
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
int t, n, a[100005], ans, pnt, tmp;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  t = 1;
  //  cin>>t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    ans = a[n - 1];
    pnt = n - 2;
    tmp = n - 2;
    while (1) {
      if (tmp == 0)
        break;
      tmp--;
      ans += a[pnt];
      if (tmp == 0)
        break;
      ans += a[pnt--];
      tmp--;
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
int t, n, a[200005], ans, pnt, tmp;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  t = 1;
  //  cin>>t;
  while (t--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    ans = a[n - 1];
    pnt = n - 2;
    tmp = n - 2;
    while (1) {
      if (tmp == 0)
        break;
      tmp--;
      ans += a[pnt];
      if (tmp == 0)
        break;
      ans += a[pnt--];
      tmp--;
    }
    cout << ans << endl;
  }
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll mod = 1000000007;

vector<ll> dp(200010);

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end(), greater<int>());
  rep(i, n) dp[i] = a[i];
  ll ans = dp[0];
  for (int i = 1; i < (n - 1); i++) {
    int num = i + n / 2 + n % 2 - 1;
    ans += dp[i];
    dp[num] = dp[i];
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <random>
#include <vector>
using namespace std;
using ll = long long int;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll mod = 1000000007;

vector<ll> dp(400010);

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  sort(a.begin(), a.end(), greater<int>());
  rep(i, n) dp[i] = a[i];
  ll ans = dp[0];
  for (int i = 1; i < (n - 1); i++) {
    int num = i + n / 2 + n % 2 - 1;
    ans += dp[i];
    dp[num] = dp[i];
  }
  cout << ans << endl;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
// author: hitch_hiker42;
#include "bits/stdc++.h"
using namespace std;

// solution:
#define int int64_t
#define span(a) begin(a), end(a)

void hike() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  sort(span(a), greater<int>());
  int cost = a.front(), j = 1;
  for (int i = n - 2; ~i; i -= 2, ++j) {
    cost += a[j] << 1;
  }
  if (n % 2 == 0)
    cout << cost << "\n";
  else
    cout << cost - a[j - 1] << "\n";
}

signed main() {
  int t = 1; // cin >> t;
  while (t--)
    hike();
  return 0;
} // farewell, until we meet again..

Fixed submission:
// author: hitch_hiker42;
#include "bits/stdc++.h"
using namespace std;

// solution:
#define int int64_t
#define span(a) begin(a), end(a)

void hike() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int &i : a)
    cin >> i;
  sort(span(a), greater<int>());
  int cost = a.front(), j = 1;
  for (int i = n - 2; i > 0; i -= 2, ++j) {
    cost += a[j] << 1;
  }
  if (n % 2 == 0)
    cout << cost << "\n";
  else
    cout << cost - a[j - 1] << "\n";
}

signed main() {
  int t = 1; // cin >> t;
  while (t--)
    hike();
  return 0;
} // farewell, until we meet again..
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define mod 1000000007
vector<ll> to[1000005];
int main() {
  ll a, b = 0;
  cin >> a;
  vector<ll> c(a * 2 - 1), d(a);
  rep(i, a) cin >> d[i];
  sort(d.begin(), d.end());
  reverse(d.begin(), d.end());
  c[0] = d[0];
  for (ll i = 1; i < a * 2 - 1; i++) {
    c[i] = d[i];
    i++;
    c[i] = d[i / 2];
  }
  rep(i, a - 1) { b += c[i]; }
  cout << b;
}


Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <cstdint> // int64_t, int*_t
#include <cstdio>  // printf
#include <deque>   // deque
#include <functional>
#include <iomanip> //setprecision
#include <iostream>
#include <map> // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define mod 1000000007
vector<ll> to[1000005];
int main() {
  ll a, b = 0;
  cin >> a;
  vector<ll> c(a * 2 - 1), d(a);
  rep(i, a) cin >> d[i];
  sort(d.begin(), d.end());
  reverse(d.begin(), d.end());
  c[0] = d[0];
  for (ll i = 1; i < a * 2 - 1; i++) {
    c[i] = d[i / 2 + 1];
    i++;
    c[i] = d[i / 2];
  }
  rep(i, a - 1) { b += c[i]; }
  cout << b;
}
Problem p02615 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
long long a[100010], ans = 0;
int main() {
  int i, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  long long num = n - 1;
  ans += a[n - 1];
  num--;
  for (i = n - 2; i >= 0; i--) {
    ans += min(2LL, num) * a[i];
    num -= min(2LL, num);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
long long a[200010], ans = 0;
int main() {
  int i, n;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  long long num = n - 1;
  ans += a[n - 1];
  num--;
  for (i = n - 2; i >= 0; i--) {
    ans += min(2LL, num) * a[i];
    num -= min(2LL, num);
  }
  cout << ans << endl;
}
Problem p02616 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

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

using namespace std;

const ll MOD = 1000000007;

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

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

  vector<ll> pos, neg;

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

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

  int rem = k;
  ll ans = 1;

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

  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

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

using namespace std;

const ll MOD = 1000000007;

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

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

  vector<ll> pos, neg;

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

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

  int rem = k;
  ll ans = 1;

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

  cout << ans;

  return 0;
}
Problem p02616 (C++)

Buggy submission (status: 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 (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;
}

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 (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;
}
Problem p02616 (Python)

Buggy submission (status: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10**9 + 7

plus = []
minus = []
zero = []
for i in range(n):
    if a[i] < 0:
        minus.append(abs(a[i]))
    elif a[i] > 0:
        plus.append(a[i])
    else:
        zero.append(0)


len_p = len(plus)
len_m = len(minus)
len_z = len(zero)

is_z = False
is_p = False
if len_z != 0:
    is_z = True

cnt = 2 * (len_m // 2) + len_p
if cnt >= k:
    is_p = True
if len_p == 0 and k % 2 == 1:
    is_p = False

if is_p:
    minus = sorted(minus, reverse=True)
    plus = sorted(plus, reverse=True)
    ans = 1
    if k % 2 == 1:
        k -= 1
        ans = plus[0]
        del plus[0]
    tmp = []
    for i in range(len(minus) // 2):
        tmp.append(minus[2 * i] * minus[2 * i + 1])
    for i in range(len(plus) // 2):
        tmp.append(plus[2 * i] * plus[2 * i + 1])
    tmp = sorted(tmp, reverse=True)
    for i in range(k // 2):
        ans *= tmp[i]
        ans %= MOD
    print(ans)
    exit()

elif is_z:
    print(0)
    exit()

else:
    minus = sorted(minus)
    ans = -1
    for i in range(k):
        ans *= minus[i]
        ans %= MOD
    print(ans)
    exit()


Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
MOD = 10**9 + 7

plus = []
minus = []
zero = []
for i in range(n):
    if a[i] < 0:
        minus.append(abs(a[i]))
    elif a[i] > 0:
        plus.append(a[i])
    else:
        zero.append(0)


len_p = len(plus)
len_m = len(minus)
len_z = len(zero)

is_z = False
is_p = False
if len_z != 0:
    is_z = True

cnt = 2 * (len_m // 2) + len_p
if cnt >= k:
    is_p = True
if len_p == 0 and k % 2 == 1:
    is_p = False

if is_p:
    minus = sorted(minus, reverse=True)
    plus = sorted(plus, reverse=True)
    ans = 1
    if k % 2 == 1:
        k -= 1
        ans = plus[0]
        del plus[0]
    tmp = []
    for i in range(len(minus) // 2):
        tmp.append(minus[2 * i] * minus[2 * i + 1])
    for i in range(len(plus) // 2):
        tmp.append(plus[2 * i] * plus[2 * i + 1])
    tmp = sorted(tmp, reverse=True)
    for i in range(k // 2):
        ans *= tmp[i]
        ans %= MOD
    print(ans)
    exit()

elif is_z:
    print(0)
    exit()

else:
    for i in plus:
        minus.append(i)
    minus = sorted(minus)
    ans = -1
    for i in range(k):
        ans *= minus[i]
        ans %= MOD
    print(ans)
    exit()
Problem p02616 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
A = tuple(map(int, input().split()))
MOD = 10**9 + 7

if K == N:
    ans = 1
    for x in A:
        ans = (ans * x) % MOD
    print(ans)
    exit()

plus, minus = [], []
for a in A:
    if a >= 0:
        plus.append(a)
    else:
        minus.append(a)

plus.sort(reverse=True)
minus.sort()

if not plus:
    ans = 1
    if K % 2:
        # 答えは負値になるので絶対値小さいのを取る
        for x in minus[-K:]:
            ans = (ans * x) % MOD
    else:
        # 答えは非負値になるので絶対値大きいのを取る
        for x in minus[:K]:
            ans = (ans * x) % MOD
    print(ans)
    exit()

idx = 0
for i in range(2, N, 2):
    if not len(plus) >= K - i + 2:
        idx += 2
        continue
    if len(minus) >= i:
        if minus[i - 2] * minus[i - 1] < plus[K - i + 1] * plus[K - i]:
            break
        else:
            idx += 2

ans = 1
for x in minus[:idx] + plus[: K - idx]:
    ans = (ans * x) % MOD
print(ans)


Fixed submission:
N, K = map(int, input().split())
A = tuple(map(int, input().split()))
MOD = 10**9 + 7

if K == N:
    ans = 1
    for x in A:
        ans = (ans * x) % MOD
    print(ans)
    exit()

plus, minus = [], []
for a in A:
    if a >= 0:
        plus.append(a)
    else:
        minus.append(a)

plus.sort(reverse=True)
minus.sort()

if not plus:
    ans = 1
    if K % 2:
        # 答えは負値になるので絶対値小さいのを取る
        for x in minus[-K:]:
            ans = (ans * x) % MOD
    else:
        # 答えは非負値になるので絶対値大きいのを取る
        for x in minus[:K]:
            ans = (ans * x) % MOD
    print(ans)
    exit()

idx = 0
for i in range(2, N, 2):
    if K - i < 0:
        break
    if not len(plus) >= K - i + 2:
        idx += 2
        continue
    if len(minus) >= i:
        if minus[i - 2] * minus[i - 1] < plus[K - i + 1] * plus[K - i]:
            break
        else:
            idx += 2

ans = 1
for x in minus[:idx] + plus[: K - idx]:
    ans = (ans * x) % MOD
print(ans)
Problem p02616 (Python)

Buggy submission (status: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 1
mod = 10**9 + 7
c = 0
z = 0
for i in a:
    if i < 0:
        c += 1
    elif i == 0:
        z += 1
if c == n and k & 1:
    a.reverse()
    for i in range(k):
        ans *= -a[i]
        ans %= mod
    print(-ans % mod)
elif c == n:
    for i in range(k):
        ans *= -a[i]
        ans %= mod
    print(ans)
else:
    plus = [i for i in a if i > 0]
    minus = [i for i in a if i <= 0]
    plus.sort(reverse=1)
    minus.sort()
    x = len(plus)
    y = len(minus)
    plus += [0] * z
    p, m = 0, 0
    for i in range(k // 2):
        if x - 1 <= p:
            ans *= minus[m] * minus[m + 1]
            m += 2
        elif y - 1 <= m:
            ans *= plus[p] * plus[p + 1]
            p += 2
        else:
            if minus[m] * minus[m + 1] >= plus[p] * plus[p + 1]:
                ans *= minus[m] * minus[m + 1]
                m += 2
            else:
                ans *= plus[p] * plus[p + 1]
                p += 2
        ans %= mod
    if k & 1:
        if p < x:
            ans *= plus[p]
        else:
            ans *= minus[-1]
        ans %= mod

    print(ans % mod)


Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 1
mod = 10**9 + 7
c = 0
z = 0
for i in a:
    if i < 0:
        c += 1
    elif i == 0:
        z += 1
if c == n and k & 1:
    a.reverse()
    for i in range(k):
        ans *= -a[i]
        ans %= mod
    print(-ans % mod)
elif c == n:
    for i in range(k):
        ans *= -a[i]
        ans %= mod
    print(ans)
elif k == n:
    for i in a:
        ans *= abs(i)
        ans %= mod
    print(((-1) ** c * ans) % mod)
else:
    plus = [i for i in a if i > 0]
    minus = [i for i in a if i <= 0]
    plus.sort(reverse=1)
    minus.sort()
    x = len(plus)
    y = len(minus)
    plus += [0] * z
    p, m = 0, 0
    for i in range(k // 2):
        if x - 1 <= p:
            ans *= minus[m] * minus[m + 1]
            m += 2
        elif y - 1 <= m:
            ans *= plus[p] * plus[p + 1]
            p += 2
        else:
            if minus[m] * minus[m + 1] >= plus[p] * plus[p + 1]:
                ans *= minus[m] * minus[m + 1]
                m += 2
            else:
                ans *= plus[p] * plus[p + 1]
                p += 2
        ans %= mod
    if k & 1:
        if p < x:
            ans *= plus[p]
        else:
            ans *= minus[-1]
        ans %= mod

    print(ans % mod)
Problem p02616 (Python)

Buggy submission (status: Runtime Error):
MOD = 10**9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort(key=lambda x: abs(x), reverse=True)

    res = 1

    if max(a) < 0:
        if k % 2 == 1:
            for i in range(n - k, n):
                res *= a[i]
                res %= MOD
        else:
            for i in range(k):
                res *= a[i]
                res %= MOD
    else:
        plus_a = [e for e in a if e >= 0]
        minus_a = [e for e in a if e < 0]

        minus_cnt = min(k, len(minus_a))
        minus_cnt -= minus_cnt % 2
        plus_cnt = k - minus_cnt

        minus_a = minus_a[:minus_cnt]
        minus_a.reverse()

        max_change_cnt = min(minus_cnt, len(plus_a) - plus_cnt)
        max_change_cnt -= max_change_cnt % 2
        change_cnt = 0
        while change_cnt < max_change_cnt:
            if (
                plus_a[plus_cnt + change_cnt] * plus_a[plus_cnt + change_cnt + 1]
                <= minus_a[change_cnt] * minus_a[change_cnt + 1]
            ):
                break
            change_cnt += 2

        for i in range(change_cnt, minus_cnt):
            res *= minus_a[i]
            res %= MOD

        for i in range(plus_cnt + change_cnt):
            res *= plus_a[i]
            res %= MOD

    print(res)


main()


Fixed submission:
MOD = 10**9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort(key=lambda x: abs(x), reverse=True)

    res = 1

    if k == n:
        for e in a:
            res *= e
            res %= MOD
    elif max(a) < 0:
        if k % 2 == 1:
            for i in range(n - k, n):
                res *= a[i]
                res %= MOD
        else:
            for i in range(k):
                res *= a[i]
                res %= MOD
    else:
        plus_a = [e for e in a if e >= 0]
        minus_a = [e for e in a if e < 0]

        minus_cnt = min(k, len(minus_a))
        minus_cnt -= minus_cnt % 2
        plus_cnt = k - minus_cnt

        minus_a = minus_a[:minus_cnt]
        minus_a.reverse()

        max_change_cnt = min(minus_cnt, len(plus_a) - plus_cnt)
        max_change_cnt -= max_change_cnt % 2
        change_cnt = 0
        while change_cnt < max_change_cnt:
            if (
                plus_a[plus_cnt + change_cnt] * plus_a[plus_cnt + change_cnt + 1]
                <= minus_a[change_cnt] * minus_a[change_cnt + 1]
            ):
                break
            change_cnt += 2

        for i in range(change_cnt, minus_cnt):
            res *= minus_a[i]
            res %= MOD

        for i in range(plus_cnt + change_cnt):
            res *= plus_a[i]
            res %= MOD

    print(res)


main()
Problem p02616 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

X = sorted([a for a in A if a > 0])
Y = sorted([a for a in A if a < 0], key=lambda x: abs(x))

if 2 * (min(K, len(Y)) // 2) + len(X) >= K:
    ans = 1
    if K % 2 == 1:
        ans *= X.pop()
        K -= 1

    XX = [(x1 * x2) for x1, x2 in zip(*[iter(X[::-1])] * 2)]
    YY = [(y1 * y2) for y1, y2 in zip(*[iter(Y[::-1])] * 2)]
    ZZ = sorted(XX + YY)

    for i in range(K // 2):
        ans *= ZZ.pop()
        ans %= MOD

    print(ans)

else:
    ans = 1
    Z = sorted(X + Y, key=lambda x: abs(x), reverse=True)
    for i in range(K):
        ans *= Z.pop()
        ans %= MOD
    print(ans)


Fixed submission:
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10**9 + 7

X = sorted([a for a in A if a >= 0])
Y = sorted([a for a in A if a < 0], key=lambda x: abs(x))

if 2 * (min(K, len(Y)) // 2) + len(X) >= K:
    ans = 1
    if K % 2 == 1:
        ans *= X.pop()
        K -= 1

    XX = [(x1 * x2) for x1, x2 in zip(*[iter(X[::-1])] * 2)]
    YY = [(y1 * y2) for y1, y2 in zip(*[iter(Y[::-1])] * 2)]
    ZZ = sorted(XX + YY)

    for i in range(K // 2):
        ans *= ZZ.pop()
        ans %= MOD

    print(ans)

else:
    ans = 1
    Z = sorted(X + Y, key=lambda x: abs(x), reverse=True)
    for i in range(K):
        ans *= Z.pop()
        ans %= MOD
    print(ans)
Problem p02616 (Python)

Buggy submission (status: Runtime Error):
N, K = map(int, input().split())
X = list(map(int, input().split()))

MOD = 10**9 + 7

if K % 2 == 1 and all(v < 0 for v in X):
    # Minimize abs
    X.sort(key=lambda x: -x)
    ans = 1
    for i in range(K):
        ans = ans * X[i] % MOD
    print(ans)
else:
    pos = sorted(v for v in X if v >= 0)
    neg = sorted(-v for v in X if v < 0)

    ans = 1
    if K % 2 == 1:
        ans *= pos.pop()

    cand = []
    while len(pos) >= 2:
        tmp = pos.pop() * pos.pop()
        cand.append(tmp)

    while len(neg) >= 2:
        tmp = neg.pop() * neg.pop()
        cand.append(tmp)

    cand.sort(reverse=True)
    for i in range(K // 2):
        ans = ans * cand[i] % MOD

    print(ans)


Fixed submission:
N, K = map(int, input().split())
X = list(map(int, input().split()))

MOD = 10**9 + 7

if K % 2 == 1 and all(v < 0 for v in X):
    # Minimize abs
    X.sort(key=lambda x: -x)
    ans = 1
    for i in range(K):
        ans = ans * X[i] % MOD
    print(ans)
elif K == N:
    ans = 1
    for i in range(K):
        ans = ans * X[i] % MOD
    print(ans)
else:
    pos = sorted(v for v in X if v >= 0)
    neg = sorted(-v for v in X if v < 0)

    ans = 1
    if K % 2 == 1:
        ans *= pos.pop()

    cand = []
    while len(pos) >= 2:
        tmp = pos.pop() * pos.pop()
        cand.append(tmp)

    while len(neg) >= 2:
        tmp = neg.pop() * neg.pop()
        cand.append(tmp)

    cand.sort(reverse=True)
    for i in range(K // 2):
        ans = ans * cand[i] % MOD

    print(ans)
Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#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;
}

Fixed submission:
#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;
}
Problem p02616 (C++)

Buggy submission (status: 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--;
    }
    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;
}

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--;
      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;
}
Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll mod = 1000000007;

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

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

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

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

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

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


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

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

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

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

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

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

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