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

#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> v(n);
  int cnt = 0;
  REP(i, n) {
    cin >> v[i];
    if (v[i] < 0)
      cnt++;
  }

  if (cnt == n and k % 2 == 1) {
    sort(ALL(v), greater<llong>());
    llong anss = 1;
    REP(i, k) anss = anss * (MOD + v[i]) % MOD;
    cout << anss << endl;
    return 0;
  }

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

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

    // debug(ans);
    // debug(k);
  }

  cout << ans << endl;
}

Error: TLE

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> v(n);
  int cnt = 0;
  REP(i, n) {
    cin >> v[i];
    if (v[i] < 0)
      cnt++;
  }

  if (cnt == n and k % 2 == 1) {
    sort(ALL(v), greater<llong>());
    llong anss = 1;
    REP(i, k) anss = anss * (MOD + v[i]) % MOD;
    cout << anss << endl;
    return 0;
  }

  priority_queue<llong, vector<llong>, greater<llong>> a;
  priority_queue<llong> b;
  REP(i, n) {
    if (v[i] < 0)
      a.push(v[i]);
    else
      b.push(v[i]);
  }

  llong ans = 1;
  while (k > 0) {
    llong aa[2] = {1, 1};
    llong bb[2] = {-1, -1};
    REP(i, 2) {
      if (!a.empty()) {
        aa[i] = a.top();
        a.pop();
      }
      if (!b.empty()) {
        bb[i] = b.top();
        b.pop();
      }
    }

    if (k == 1) {
      if (bb[0] == -1)
        ans = ans * (MOD + aa[0]) % MOD;
      else
        ans = ans * bb[0] % MOD;
      k--;
    } else {
      if (aa[0] == 1) {
        ans = ans * bb[0] % MOD;
        b.push(bb[1]);
        k--;
      } else if (bb[0] == -1) {
        ans = ans * (MOD + aa[0]) % MOD;
        a.push(aa[1]);
        k--;
      } else if (bb[0] * bb[1] > aa[0] * aa[1]) {
        ans = ans * bb[0] % MOD;
        REP(i, 2) a.push(aa[i]);
        b.push(bb[1]);
        k--;
      } else {
        REP(i, 2) ans = ans * (MOD + aa[i]) % MOD;
        REP(i, 2) b.push(bb[i]);
        k -= 2;
      }
    }

    debug(ans);
    debug(k);
  }

  cout << ans << endl;
}