Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define vvi vector<vi>
#define str string
#define vs vector<str>
#define vb vector<bool>
#define slli set<lli>
#define mii map<int, int>
#define mlli map<int, lli>
#define msi map<str, int>
#define mp make_pair
#define pii pair<int, int>
#define pll pair<lli, lli>
#define vpii vector<pii>
#define vpll vector<pll>
#define pb push_back
#define loop(i, s, l, j) for (int i = s; i < l; i += j)
#define rloop(i, s, l, j) for (int i = s; i >= l; i -= j)
#define rep(i, n) loop(i, 0, n, 1)
#define rrep(i, n) rloop(i, n - 1, 0, 1)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ft first
#define sd second
#define mit mii::iterator
#define vit vi::iterator
#define all(a) a.begin(), a.end()
#define min3(a, b, c) min(a, min(b, c))
#define mod 1000000007
#define inf 1000000000
#define limit 200001
/**********************************************************************************************************************************************/
int dx4[4] = {0, -1, 0, 1};
int dy4[4] = {1, 0, -1, 0};
int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int Power(int base, int exp) {
  int ans = 1;
  while (exp > 0) {
    if (exp & 1)
      ans = (1LL * ans * base) % mod;
    exp = exp >> 1;
    base = (1LL * base * base) % mod;
  }
  return ans;
}
/**********************************************************************************************************************************************/

/*
5p 5n


ppppnnnppppnnnpppnnn

ppppnnnppp_nnn
ppppnnnppppn_p


5p 5n,  drop smallest p, include largest n nr/pl

5p 5n   drop smallest n, include largest p pr/nl

 => (p, n)

    100p 9o o == 9
            e = 100
            k = 109
            n = 109

    0p 9o p == 0
*/
bool comp(int p1, int p2) { return abs(p1) > abs(p2); }
void input() {
  int n, k;
  cin >> n >> k;
  vlli a(n);
  rep(i, n) cin >> a[i];

  sort(all(a), comp);
  vb take(n, 0);

  int nn = 0;
  rep(i, k) {
    take[i] = 1;
    if (a[i] < 0)
      nn++;
  }

  if (nn % 2 == 1) {
    int inr = -1, ipl = -1, ipr = -1, inl = -1;
    rep(i, k) {
      if (a[i] >= 0)
        ipl = i;
      else
        inl = i;
    }
    loop(i, k, n, 1) if (a[i] >= 0) {
      ipr = i;
      break;
    }
    loop(i, k, n, 1) if (a[i] < 0) {
      inr = i;
      break;
    }

    bool c1 = (ipl != -1 && inr != -1);
    bool c2 = (inl != -1 && ipr != -1);
    if (c1 && c2) {
      if (a[ipl] * a[ipr] > a[inl] * a[inr])
        take[ipr] = 1, take[inl] = 0;
      else
        take[inr] = 1, take[ipl] = 0;
    } else if (c1)
      take[inr] = 1, take[ipl] = 0;
    else if (c2)
      take[ipr] = 1, take[inl] = 0;
    else {
      take.clear();
      take.resize(n, 0);
      loop(i, n - k, n, 1) take[i] = 1;
    }
  }

  lli ans = 1;
  rep(i, n) if (take[i]) ans = (ans * ((a[i] + mod) % mod)) % mod;
  cout << ans << '\\n';
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fastio();
  int q = 1;
  // cin >> q;
  while (q--)
    input();
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ld long double
#define lli long long int
#define vi vector<int>
#define vlli vector<lli>
#define vvi vector<vi>
#define str string
#define vs vector<str>
#define vb vector<bool>
#define slli set<lli>
#define mii map<int, int>
#define mlli map<int, lli>
#define msi map<str, int>
#define mp make_pair
#define pii pair<int, int>
#define pll pair<lli, lli>
#define vpii vector<pii>
#define vpll vector<pll>
#define pb push_back
#define loop(i, s, l, j) for (int i = s; i < l; i += j)
#define rloop(i, s, l, j) for (int i = s; i >= l; i -= j)
#define rep(i, n) loop(i, 0, n, 1)
#define rrep(i, n) rloop(i, n - 1, 0, 1)
#define fastio()                                                               \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ft first
#define sd second
#define mit mii::iterator
#define vit vi::iterator
#define all(a) a.begin(), a.end()
#define min3(a, b, c) min(a, min(b, c))
#define mod 1000000007
#define inf 1000000000
#define limit 200001
/**********************************************************************************************************************************************/
int dx4[4] = {0, -1, 0, 1};
int dy4[4] = {1, 0, -1, 0};
int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int Power(int base, int exp) {
  int ans = 1;
  while (exp > 0) {
    if (exp & 1)
      ans = (1LL * ans * base) % mod;
    exp = exp >> 1;
    base = (1LL * base * base) % mod;
  }
  return ans;
}
/**********************************************************************************************************************************************/

/*
5p 5n


ppppnnnppppnnnpppnnn

ppppnnnppp_nnn
ppppnnnppppn_p


5p 5n,  drop smallest p, include largest n nr/pl

5p 5n   drop smallest n, include largest p pr/nl

 => (p, n)

    100p 9o o == 9
            e = 100
            k = 109
            n = 109

    0p 9o p == 0
*/
bool comp(int p1, int p2) {
  return (abs(p1) > abs(p2) || (abs(p1) == abs(p2) && p1 > 0));
}
void input() {
  int n, k;
  cin >> n >> k;
  vlli a(n);
  rep(i, n) cin >> a[i];

  sort(all(a), comp);
  vb take(n, 0);

  int nn = 0;
  rep(i, k) {
    take[i] = 1;
    if (a[i] < 0)
      nn++;
  }

  if (nn % 2 == 1) {
    lli inr = -1, ipl = -1, ipr = -1, inl = -1;
    rep(i, k) {
      if (a[i] >= 0)
        ipl = i;
      else
        inl = i;
    }
    loop(i, k, n, 1) if (a[i] >= 0) {
      ipr = i;
      break;
    }
    loop(i, k, n, 1) if (a[i] < 0) {
      inr = i;
      break;
    }

    bool c1 = (ipl != -1 && inr != -1);
    bool c2 = (inl != -1 && ipr != -1);
    if (c1 && c2) {
      if (a[ipl] * a[ipr] > a[inl] * a[inr])
        take[ipr] = 1, take[inl] = 0;
      else
        take[inr] = 1, take[ipl] = 0;
    } else if (c1)
      take[inr] = 1, take[ipl] = 0;
    else if (c2)
      take[ipr] = 1, take[inl] = 0;
    else {
      take.clear();
      take.resize(n, 0);
      loop(i, n - k, n, 1) take[i] = 1;
    }
  }
  lli ans = 1;
  rep(i, n) if (take[i]) ans = (ans * ((a[i] + mod) % mod)) % mod;
  cout << ans << '\\n';
}

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  fastio();
  int q = 1;
  // cin >> q;
  while (q--)
    input();
}