Problem p02616 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;

vector<ll> as, bs;
ll na, nb;
ll N, K;

ll calProd(vector<ll> &cs) {
  ll res = cs[0];
  for (int i = 1; i < cs.size(); i++) {
    res *= cs[i];
    res %= MOD;
  }
  return (res + MOD) % MOD;
}

ll solve() {
  sort(as.begin(), as.end());
  sort(bs.begin(), bs.end());
  reverse(as.begin(), as.end());
  na = as.size();
  nb = bs.size();
  vector<ll> cs;
  if (K == N) {
    for (int a : as)
      cs.push_back(a);
    for (int b : bs)
      cs.push_back(b);
    return calProd(cs);
  }

  int i = 0, j = 0;
  while (cs.size() < (K / 2) * 2) {
    int lc = cs.size();
    if (i + 1 < na && j + 1 < nb) {
      if (as[i] * as[i + 1] > bs[j] * bs[j + 1]) {
        cs.push_back(as[i]);
        cs.push_back(as[i + 1]);
        i += 2;
      } else {
        cs.push_back(bs[j]);
        cs.push_back(bs[j + 1]);
        j += 2;
      }
    } else if (i + 1 < na) {
      cs.push_back(as[i]);
      cs.push_back(as[i + 1]);
      i += 2;
    } else if (j + 1 < nb) {
      cs.push_back(bs[j]);
      cs.push_back(bs[j + 1]);
      j += 2;
    }
    if (cs.size() == lc)
      break;
  }

  if (K % 2 && i < na) {
    cs.push_back(as[i]);
  }

  if (cs.size() == K)
    return calProd(cs);

  reverse(bs.begin(), bs.end());
  cs.clear();
  i = 0, j = 0;
  while (cs.size() < K) {
    while (cs.size() < K && j < nb) {
      cs.push_back(bs[j]);
      j++;
    }
    while (cs.size() < K && i < na) {
      cs.push_back(cs[i]);
      i++;
    }
  }

  return calProd(cs);
}

int main() {
  scanf("%lld%lld", &N, &K);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    if (a < 0)
      bs.push_back(a);
    else
      as.push_back(a);
  }

  cout << solve() << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ll MOD = 1e9 + 7;

vector<ll> as, bs;
ll na, nb;
ll N, K;

ll calProd(vector<ll> &cs) {
  ll res = cs[0];
  for (int i = 1; i < cs.size(); i++) {
    res *= cs[i];
    res %= MOD;
  }
  return (res + MOD) % MOD;
}

ll solve() {
  sort(as.begin(), as.end());
  sort(bs.begin(), bs.end());
  reverse(as.begin(), as.end());
  na = as.size();
  nb = bs.size();
  vector<ll> cs;
  int i = 0, j = 0;
  while (cs.size() < (K / 2) * 2) {
    if (i + 1 < na && j + 1 < nb) {
      if (as[i] * as[i + 1] > bs[j] * bs[j + 1]) {
        cs.push_back(as[i]);
        cs.push_back(as[i + 1]);
        i += 2;
      } else {
        cs.push_back(bs[j]);
        cs.push_back(bs[j + 1]);
        j += 2;
      }
    } else if (i + 1 < na) {
      cs.push_back(as[i]);
      cs.push_back(as[i + 1]);
      i += 2;
    } else if (j + 1 < nb) {
      cs.push_back(bs[j]);
      cs.push_back(bs[j + 1]);
      j += 2;
    }
  }
  if (K % 2 && i < na) {
    cs.push_back(as[i]);
  }

  if (cs.size() == K)
    return calProd(cs);

  reverse(bs.begin(), bs.end());
  cs.clear();
  i = 0, j = 0;
  while (cs.size() < K) {
    while (cs.size() < K && j < nb) {
      cs.push_back(bs[j]);
      j++;
    }
    while (cs.size() < K && i < na) {
      cs.push_back(cs[i]);
      i++;
    }
  }

  return calProd(cs);
}

int main() {
  scanf("%lld%lld", &N, &K);
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    if (a < 0)
      bs.push_back(a);
    else
      as.push_back(a);
  }

  cout << solve() << endl;
  return 0;
}