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

#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

ll d1[200010];
ll d3[200010];

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v1, v2, v3;
  ll mx = -1000000007;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    if (a < 0)
      v1.pb(a);
    if (a == 0)
      v2.pb(a);
    if (a > 0)
      v3.pb(a);
    mx = max(mx, a);
  }
  sort(all(v1));
  sort(all(v3));
  reverse(all(v1));
  if (mx < 0) {
    ll ans = 1;
    if (k % 2 == 1) {
      rep(i, 0, k) {
        ans *= (MOD + v1[i]);
        ans %= MOD;
      }
    } else {
      reverse(all(v1));
      rep(i, 0, k) {
        ans *= (MOD + v1[i]);
        ans %= MOD;
      }
    }
    cout << ans << endl;
  } else {
    if (v1.sz + v3.sz < k) {
      cout << 0 << endl;
      return 0;
    }
    ll ans = 1;
    while (k > 1) {
      if (v1.sz > 1 && v3.sz > 1) {
        ll a1 = v1[v1.sz - 1];
        ll a2 = v1[v1.sz - 2];
        ll b1 = v3[v3.sz - 1];
        ll b2 = v3[v3.sz - 2];
        if (a1 * a2 >= b1 * b2) {
          ans *= (MOD + a1);
          ans %= MOD;
          ans *= (MOD + a2);
          ans %= MOD;
          v1.pop_back();
          v1.pop_back();
        } else {
          ans *= (MOD + b1);
          ans %= MOD;
          ans *= (MOD + b2);
          ans %= MOD;
          v3.pop_back();
          v3.pop_back();
        }
        k -= 2;
      } else if (v1.sz > 1) {
        ll a1 = v1[v1.sz - 1];
        ll a2 = v1[v1.sz - 2];
        ans *= (MOD + a1);
        ans %= MOD;
        ans *= (MOD + a2);
        ans %= MOD;
        v1.pop_back();
        v1.pop_back();
        k -= 2;
      } else if (v3.sz > 1) {
        ll b1 = v3[v3.sz - 1];
        ll b2 = v3[v3.sz - 2];
        ans *= (MOD + b1);
        ans %= MOD;
        ans *= (MOD + b2);
        ans %= MOD;
        v3.pop_back();
        v3.pop_back();
        k -= 2;
      } else {
        break;
      }
    }
    if (k == 1) {
      if (v3.sz > 0) {
        ans *= v3[v3.sz - 1];
        ans %= MOD;
      } else if (v2.sz > 0) {
        ans *= 0;
      } else {
        ans *= MOD + v1[v1.sz - 1];
        ans %= MOD;
      }
    }
    if (k == 2) {
      if (v2.sz > 0) {
        ans *= 0;
      } else {
        ans *= MOD + v1[v1.sz - 1];
        ans %= MOD;
        ans *= MOD + v3[v3.sz - 1];
        ans %= MOD;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

ll d1[200010];
ll d3[200010];

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> v1, v2, v3;
  ll mx = -1000000007;
  rep(i, 0, n) {
    ll a;
    cin >> a;
    if (a < 0)
      v1.pb(a);
    if (a == 0)
      v2.pb(a);
    if (a > 0)
      v3.pb(a);
    mx = max(mx, a);
  }
  sort(all(v1));
  sort(all(v3));
  reverse(all(v1));
  if (mx < 0) {
    ll ans = 1;
    if (k % 2 == 1) {
      rep(i, 0, k) {
        ans *= (MOD + v1[i]);
        ans %= MOD;
      }
    } else {
      reverse(all(v1));
      rep(i, 0, k) {
        ans *= (MOD + v1[i]);
        ans %= MOD;
      }
    }
    cout << ans << endl;
  } else {
    if (v1.sz + v3.sz < k) {
      cout << 0 << endl;
      return 0;
    }
    ll ans = 1;
    while (k > 1) {
      if (v1.sz > 1 && v3.sz > 1) {
        ll a1 = v1[v1.sz - 1];
        ll a2 = v1[v1.sz - 2];
        ll b1 = v3[v3.sz - 1];
        ll b2 = v3[v3.sz - 2];
        if (a1 * a2 >= b1 * b2) {
          ans *= (MOD + a1);
          ans %= MOD;
          ans *= (MOD + a2);
          ans %= MOD;
          v1.pop_back();
          v1.pop_back();
        } else {
          ans *= (MOD + b1);
          ans %= MOD;
          ans *= (MOD + b2);
          ans %= MOD;
          v3.pop_back();
          v3.pop_back();
        }
        k -= 2;
      } else if (v1.sz > 1) {
        ll a1 = v1[v1.sz - 1];
        ll a2 = v1[v1.sz - 2];
        ans *= (MOD + a1);
        ans %= MOD;
        ans *= (MOD + a2);
        ans %= MOD;
        v1.pop_back();
        v1.pop_back();
        k -= 2;
      } else if (v3.sz > 1) {
        ll b1 = v3[v3.sz - 1];
        ll b2 = v3[v3.sz - 2];
        ans *= (MOD + b1);
        ans %= MOD;
        ans *= (MOD + b2);
        ans %= MOD;
        v3.pop_back();
        v3.pop_back();
        k -= 2;
      }
    }
    if (k > 0) {
      if (v3.sz > 0) {
        ans *= v3[v3.sz - 1];
        ans %= MOD;
      } else if (v2.sz > 0) {
        ans *= 0;
      } else {
        ans *= MOD + v1[v1.sz - 1];
        ans %= MOD;
      }
    }
    cout << ans << endl;
  }
  return 0;
}