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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

signed main() {
  int N, K;
  vector<int> p, m, z;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    int in;
    cin >> in;
    if (in > 0)
      p.push_back(in);
    else if (in < 0)
      m.push_back(in);
    else
      z.push_back(in);
  }

  int ans = 1;
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());

  // allplus
  if (m.size() == 0) {
    if (p.size() >= K) {
      for (int i = 0; i < K; i++) {
        ans *= p[p.size() - 1 - i];
        ans %= MOD;
        while (ans < 0)
          ans += MOD;
      }
      ans %= MOD;
      while (ans < 0)
        ans += MOD;
      cout << ans % MOD << endl;
    } else {
      cout << "0" << endl;
    }
  }
  // allminus
  else if (p.size() == 0) {
    if (m.size() >= K) {
      if (K % 2 == 1) {
        if (z.size() > 0) {
          cout << "0" << endl;
        } else {
          for (int i = 0; i < K; i++) {
            ans *= m[m.size() - 1 - i];
            ans %= MOD;
            while (ans < 0)
              ans += MOD;
          }
          ans %= MOD;
          while (ans < 0)
            ans += MOD;
          cout << ans % MOD << endl;
        }
      } else {
        for (int i = 0; i < K; i++) {
          ans *= m[i];
          ans %= MOD;
          while (ans < 0)
            ans += MOD;
        }
        ans %= MOD;
        while (ans < 0)
          ans += MOD;
        cout << ans % MOD << endl;
      }
    } else {
      cout << "0" << endl;
    }
  }
  // all
  else if (N == K) {
    for (int i = 0; i < p.size(); i++) {
      ans *= p[i];
      ans %= MOD;
      while (ans < 0)
        ans += MOD;
    }
    for (int i = 0; i < m.size(); i++) {
      ans *= m[i];
      ans %= MOD;
      while (ans < 0)
        ans += MOD;
    }
    for (int i = 0; i < z.size(); i++) {
      ans *= z[i];
      ans %= MOD;
      while (ans < 0)
        ans += MOD;
    }
    ans %= MOD;
    while (ans < 0)
      ans += MOD;
    cout << ans % MOD << endl;
  }
  // other
  else {
    if (p.size() + m.size() >= K) {
      int ps = p.size() - 1;
      int ms = 0;
      int n = K;
      if (n % 2 == 1) {
        ans *= p[ps--];
        n--;
        ans %= MOD;
        while (ans < 0)
          ans += MOD;
      }
      while (n != 0) {
        if (ps - 1 < 0) {
          ans *= m[ms++];
          ans %= MOD;
          while (ans < 0)
            ans += MOD;
          ans *= m[ms++];
          ans %= MOD;
          while (ans < 0)
            ans += MOD;
        } else if (ms + 1 >= m.size()) {
          ans *= p[ps--];
          ans %= MOD;
          while (ans < 0)
            ans += MOD;
          ans *= p[ps--];
          ans %= MOD;
          while (ans < 0)
            ans += MOD;
        } else {
          if (m[ms] * m[ms + 1] > p[ps] * p[ps - 1]) {
            ans *= m[ms++];
            ans %= MOD;
            while (ans < 0)
              ans += MOD;
            ans *= m[ms++];
            ans %= MOD;
            while (ans < 0)
              ans += MOD;
          } else {
            ans *= p[ps--];
            ans %= MOD;
            while (ans < 0)
              ans += MOD;
            ans *= p[ps--];
            ans %= MOD;
            while (ans < 0)
              ans += MOD;
          }
        }
        n -= 2;
      }
      ans %= MOD;
      while (ans < 0)
        ans += MOD;
      cout << ans % MOD << endl;
      ;
    } else {
      cout << "0" << endl;
    }
  }
}


Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

signed main() {
  Bint N;
  int K;
  vector<Bint> p, m, z;

  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    Bint in;
    cin >> in;
    if (in > 0)
      p.push_back(in);
    else if (in < 0)
      m.push_back(in);
    else
      z.push_back(in);
  }

  Bint ans = 1;
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());

  // allplus
  if (m.size() == 0) {
    if (p.size() >= K) {
      for (int i = 0; i < K; i++) {
        ans *= p[p.size() - 1 - i];
      }
      ans %= MOD;
      while (ans < 0)
        ans += MOD;
      cout << ans % MOD << endl;
    } else {
      cout << "0" << endl;
    }
  }
  // allminus
  else if (p.size() == 0) {
    if (m.size() >= K) {
      if (K % 2 == 1) {
        if (z.size() > 0) {
          cout << "0" << endl;
        } else {
          for (int i = 0; i < K; i++) {
            ans *= m[m.size() - 1 - i];
          }
          ans %= MOD;
          while (ans < 0)
            ans += MOD;
          cout << ans % MOD << endl;
        }
      } else {
        for (int i = 0; i < K; i++) {
          ans *= m[i];
        }
        ans %= MOD;
        while (ans < 0)
          ans += MOD;
        cout << ans % MOD << endl;
      }
    } else {
      cout << "0" << endl;
    }
  }
  // all
  else if (N == K) {
    for (int i = 0; i < p.size(); i++) {
      ans *= p[i];
    }
    for (int i = 0; i < m.size(); i++) {
      ans *= m[i];
    }
    for (int i = 0; i < z.size(); i++) {
      ans *= z[i];
    }
    ans %= MOD;
    while (ans < 0)
      ans += MOD;
    cout << ans % MOD << endl;
  }
  // other
  else {
    if (p.size() + m.size() >= K) {
      int ps = p.size() - 1;
      int ms = 0;
      int n = K;
      if (n % 2 == 1) {
        ans *= p[ps--];
        n--;
      }
      while (n != 0) {
        if (ps - 1 < 0) {
          ans *= m[ms++];
          ans *= m[ms++];
        } else if (ms + 1 >= m.size()) {
          ans *= p[ps--];
          ans *= p[ps--];
        } else {
          if (m[ms] * m[ms + 1] > p[ps] * p[ps - 1]) {
            ans *= m[ms++];
            ans *= m[ms++];
          } else {
            ans *= p[ps--];
            ans *= p[ps--];
          }
        }
        n -= 2;
      }
      ans %= MOD;
      while (ans < 0)
        ans += MOD;
      cout << ans % MOD << endl;
      ;
    } else {
      cout << "0" << endl;
    }
  }
}

// 10 10
// 1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1