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

#include <bits/stdc++.h>
#define nl '\\n'
#define dbg(a) cout << #a << "=" << a << endl
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
#define vl vector<ll>
#define pll pair<ll, ll>
#define ii pair<int, int>
#define vi vector<int>
#define md 1000000007

using namespace std;

ll n, k, ze;
vl v, p, neg;

int cmp(ll a, ll b) {
  //    if(abs(a)==abs(b))
  //        return a>b;
  //    else
  return abs(a) < abs(b);
}

ll s2() {
  if (ze > 0) {
    cout << 0 << endl;
    return 0;
  }
  vl v;
  for (auto it : p)
    v.push_back(it);
  for (auto it : neg)
    v.push_back(it);
  sort(v.begin(), v.end(), cmp);
  ll an = 1;
  for (int i = 0; i < k; i++)
    an = ((an * v[i]) % md + md) % md;

  an = (an + md) % md;
  cout << an << endl;
  return 0;
}

ll ps() {
  ll an = 1;
  ll f = 0;
  for (int i = k - 1; i >= 0; i--) {
    if (v[i] > 0 and f == 0) {
      f = 1;
    } else
      an = ((an * v[i]) % md + md) % md;
  }
  if (f == 0)
    return -1;
  for (int i = k; i < n; i++) {
    if (v[i] < 0) {
      an = ((an * v[i]) % md + md) % md;
      return an;
      break;
    }
  }
  return -1;
}

ll ng() {
  ll an = 1;
  ll f = 0;
  for (int i = k - 1; i >= 0; i--) {
    if (v[i] < 0 and f == 0) {
      f = 1;
    } else
      an = ((an * v[i]) % md + md) % md;
  }
  if (f == 0)
    return -1;
  for (int i = k; i < n; i++) {
    if (v[i] > 0) {
      an = ((an * v[i]) % md + md) % md;
      return an;
      break;
    }
  }
  return -1;
}

ll s1() {

  for (auto it : p)
    v.push_back(it);
  for (auto it : neg)
    v.push_back(it);
  if (v.size() < k) {
    cout << 0 << endl;
    return 0;
  }
  sort(v.begin(), v.end(), cmp);
  reverse(v.begin(), v.end());
  ll an = 1;
  ll ct = 0;
  for (int i = 0; i < k; i++) {
    an = ((an * v[i]) % md + md) % md;
    ct += v[i] < 0;
  }
  if (ct % 2 == 0)
    cout << an << endl;
  else {
    ll p1, p2, n1, n2;
    p1 = p2 = n1 = n2 = -1;
    for (int i = k - 1; i >= 0; i--) {
      if (v[i] < 0 and n1 == -1)
        n1 = i;
      if (v[i] > 0 and p1 == -1)
        p1 = i;
    }
    for (int i = k; i < n; i++) {
      if (v[i] < 0 and n2 == -1)
        n2 = i;
      if (v[i] > 0 and p2 == -1)
        p2 = i;
    }
    if ((p1 == -1 || n2 == -1) and (p2 == -1 || n1 == -1)) {
      cout << 0 << endl;
    } else if (p1 == -1 || n2 == -1) {
      swap(v[p2], v[n1]);
      ll an = 1;
      ll ct = 0;
      for (int i = 0; i < k; i++) {
        an = ((an * v[i]) % md + md) % md;
        ct += v[i] < 0;
      }
      cout << an << endl;
    } else if (p2 == -1 || n1 == -1) {
      swap(v[p1], v[n2]);
      ll an = 1;
      ll ct = 0;
      for (int i = 0; i < k; i++) {
        an = ((an * v[i]) % md + md) % md;
        ct += v[i] < 0;
      }
      cout << an << endl;
    } else if (abs(v[n1] * v[n2]) > abs(v[p1] * v[p2])) {
      swap(v[p1], v[n2]);
      ll an = 1;
      ll ct = 0;
      for (int i = 0; i < k; i++) {
        an = ((an * v[i]) % md + md) % md;
        ct += v[i] < 0;
      }
      cout << an << endl;
    } else {
      swap(v[p2], v[n1]);
      ll an = 1;
      ll ct = 0;
      for (int i = 0; i < k; i++) {
        an = ((an * v[i]) % md + md) % md;
        ct += v[i] < 0;
      }
      cout << an << endl;
    }
  }
  return 0;
}

int solve() {

  cin >> n >> k;
  ze = 0;
  p.clear();
  neg.clear();
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    if (x > 0)
      p.push_back(x);
    if (x < 0)
      neg.push_back(x);
    if (x == 0)
      ze++;
  }
  if (k == n or k % 2 == 1 and p.size() == 0) {
    s2();
  } else
    s1();

  return 0;
}

int main() {
  fast;
  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define nl '\\n'
#define dbg(a) cout << #a << "=" << a << endl
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ll long long
#define vl vector<ll>
#define pll pair<ll, ll>
#define ii pair<int, int>
#define vi vector<int>
#define md 1000000007

using namespace std;

ll n, k, ze;
vl v, p, neg;

int cmp(ll a, ll b) {
  //    if(abs(a)==abs(b))
  //        return a>b;
  //    else
  return abs(a) < abs(b);
}

ll s2() {
  if (ze > 0) {
    cout << 0 << endl;
    return 0;
  }
  vl v;
  for (auto it : p)
    v.push_back(it);
  for (auto it : neg)
    v.push_back(it);
  sort(v.begin(), v.end(), cmp);
  ll an = 1;
  for (int i = 0; i < k; i++)
    an = ((an * v[i]) % md + md) % md;

  an = (an + md) % md;
  cout << an << endl;
  return 0;
}

ll ps() {
  ll an = 1;
  ll f = 0;
  for (int i = k - 1; i >= 0; i--) {
    if (v[i] > 0 and f == 0) {
      f = 1;
    } else
      an = ((an * v[i]) % md + md) % md;
  }
  if (f == 0)
    return -1;
  for (int i = k; i < n; i++) {
    if (v[i] < 0) {
      an = ((an * v[i]) % md + md) % md;
      return an;
      break;
    }
  }
  return -1;
}

ll ng() {
  ll an = 1;
  ll f = 0;
  for (int i = k - 1; i >= 0; i--) {
    if (v[i] < 0 and f == 0) {
      f = 1;
    } else
      an = ((an * v[i]) % md + md) % md;
  }
  if (f == 0)
    return -1;
  for (int i = k; i < n; i++) {
    if (v[i] > 0) {
      an = ((an * v[i]) % md + md) % md;
      return an;
      break;
    }
  }
  return -1;
}

ll s1() {

  for (auto it : p)
    v.push_back(it);
  for (auto it : neg)
    v.push_back(it);
  if (v.size() < k) {
    cout << 0 << endl;
    return 0;
  }
  sort(v.begin(), v.end(), cmp);
  reverse(v.begin(), v.end());
  ll an = 1;
  ll ct = 0;
  for (int i = 0; i < k; i++) {
    an = ((an * v[i]) % md + md) % md;
    ct += v[i] < 0;
  }
  if (ct % 2 == 0)
    cout << an << endl;
  else {
    while (1) {
      ll a;
    }
    ll x = max(ps(), ng());
    if (x == -1) {
      while (1) {
        ll a;
      }
    }
    cout << x << endl;
  }
  return 0;
}

int solve() {

  cin >> n >> k;
  ze = 0;
  p.clear();
  neg.clear();
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    if (x > 0)
      p.push_back(x);
    if (x < 0)
      neg.push_back(x);
    if (x == 0)
      ze++;
  }
  if (k == n or k % 2 == 1 and p.size() == 0) {
    s2();
  } else
    s1();

  return 0;
}

int main() {
  fast;
  solve();

  return 0;
}