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

#include <algorithm>
#include <iostream>

#define SIZE 200001
long long int M = 1000000007;

using namespace std;

int main() {
  long long int A, B, C, D, i, j, k, K, Q, R, K2;
  long long int X, N, Y, Z, N_POS, N_NEG, N_NUL, i_POS, i_NEG, PP, PN;
  long long int POS[SIZE], NEG[SIZE];

  cin >> N >> K;

  if (N == K) {
    R = 1;
    for (i = 0; i < N; i++) {
      scanf("%lld", &A);
      if (A == 0) {
        R = 0;
        i = N;
      } else
        R = (R * (A + M)) % M;
    }
    R = (R + M) % M;
    cout << R << endl;
  } else {
    N_NUL = 0;
    N_POS = 0;
    N_NEG = 0;
    for (i = 0; i < N; i++) {
      //  cin >> A;
      scanf("%lld", &A);
      if (A > 0) {
        POS[N_POS] = A;
        N_POS++;
      } else if (A == 0)
        N_NUL++;
      else {
        NEG[N_NEG] = -A;
        N_NEG++;
      }
    }

    if ((N_NUL > 0) || (N_POS > 0) || ((K % 2) == 0)) {
      if (N_POS + N_NEG < K) {
        cout << 0 << endl;
      } else {

        // on regarde quand le max est forcement positif
        sort(POS, POS + N_POS);
        sort(NEG, NEG + N_NEG);
        i_POS = N_POS - 1;
        i_NEG = N_NEG - 1;
        K2 = K;
        R = 1;
        if (i_POS >= 1)
          PP = POS[i_POS] * POS[i_POS - 1];
        if (i_NEG >= 1)
          PN = NEG[i_NEG] * NEG[i_NEG - 1];
        while (K2 > 1) {
          if (i_NEG >= 1) {
            if (i_POS >= 1) {
              if (PN >= PP) {
                B = PN % M;
                R = (R * B) % M;
                i_NEG -= 2;
                K2 -= 2;
                if (i_NEG >= 1)
                  PN = NEG[i_NEG] * NEG[i_NEG - 1];
              } else {
                B = PP % M;
                R = (R * B) % M;
                i_POS -= 2;
                K2 -= 2;
                if (i_POS >= 1)
                  PP = POS[i_POS] * POS[i_POS - 1];
              }
            } else {
              B = PN % M;
              R = (R * B) % M;
              i_NEG -= 2;
              K2 -= 2;
              if (i_NEG >= 1)
                PN = NEG[i_NEG] * NEG[i_NEG - 1];
            }
          } else {
            if (i_POS >= 1) {
              // PP = POS[i_POS]*POS[i_POS-1];
              B = PP % M;
              R = (R * B) % M;
              i_POS -= 2;
              K2 -= 2;
              if (i_POS >= 1)
                PP = POS[i_POS] * POS[i_POS - 1];
            } else {
              if (N_NEG > 0) {
                R = 0;
                K2 = 0;
              }
            }
          }
        } // end WHILE
        if (K2 == 1) {
          if (i_POS >= 0)
            R = (R * POS[i_POS]) % M;
          else if (N_NEG > 0)
            R = 0;
          else if (i_NEG >= 0) {
            R = (-R * NEG[i_NEG]) % M;
            R = (R + M) % M;
          }
        }
        cout << R << endl;
      }
    } else {
      //  cerr << "RESULTAT < 0" << endl;
      // le resultat est < 0 donc il faut minimiser
      sort(NEG, NEG + N_NEG);
      i_NEG = 0;
      K2 = K;
      R = (-NEG[0] + M) % M;
      for (i = 1; i < K; i++)
        R = (-R * NEG[i]) % M;

      R = (R + M) % M;
      /*
      while (K2 > 1) {
        PN = NEG[i_NEG]*NEG[i_NEG+1];
        B = PN % M;
        R = (R*B) % M;
        i_NEG += 2;
        K2 -= 2;
      }
      if (K2 == 1) {
        R = (-R*NEG[i_NEG]) % M;
        R = (R+M) % M;
      }
      */
      cout << R << endl;
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

#define SIZE 200001
long long int M = 1000000007;

using namespace std;

int main() {
  long long int A, B, C, D, i, j, k, K, Q, R, K2;
  long long int X, N, Y, Z, N_POS, N_NEG, N_NUL, i_POS, i_NEG, PP, PN;
  long long int POS[SIZE], NEG[SIZE];

  cin >> N >> K;

  if (N == K) {
    R = 1;
    for (i = 0; i < N; i++) {
      scanf("%lld", &A);
      R = (R * (A + M)) % M;
    }
    R = (R + M) % M;
    cout << R << endl;
  } else {
    N_NUL = 0;
    N_POS = 0;
    N_NEG = 0;
    for (i = 0; i < N; i++) {
      //  cin >> A;
      scanf("%lld", &A);
      if (A == 0)
        N_NUL++;
      else if (A > 0) {
        POS[N_POS] = A;
        N_POS++;
      } else {
        NEG[N_NEG] = -A;
        N_NEG++;
      }
    }

    if ((N_NUL > 0) || (N_POS > 0) || ((K % 2) == 0)) {
      // on regarde quand le max est forcement positif
      sort(POS, POS + N_POS);
      sort(NEG, NEG + N_NEG);
      i_POS = N_POS - 1;
      i_NEG = N_NEG - 1;
      K2 = K;
      R = 1;
      if (i_POS >= 1)
        PP = POS[i_POS] * POS[i_POS - 1];
      if (i_NEG >= 1)
        PN = NEG[i_NEG] * NEG[i_NEG - 1];
      while (K2 > 1) {
        if (i_NEG >= 1) {
          if (i_POS >= 1) {
            if (PN >= PP) {
              B = PN % M;
              R = (R * B) % M;
              i_NEG -= 2;
              K2 -= 2;
              if (i_NEG >= 1)
                PN = NEG[i_NEG] * NEG[i_NEG - 1];
            } else {
              B = PP % M;
              R = (R * B) % M;
              i_POS -= 2;
              K2 -= 2;
              if (i_POS >= 1)
                PP = POS[i_POS] * POS[i_POS - 1];
            }
          } else {
            B = PN % M;
            R = (R * B) % M;
            i_NEG -= 2;
            K2 -= 2;
            if (i_NEG >= 1)
              PN = NEG[i_NEG] * NEG[i_NEG - 1];
          }
        } else {
          if (i_POS >= 1) {
            // PP = POS[i_POS]*POS[i_POS-1];
            B = PP % M;
            R = (R * B) % M;
            i_POS -= 2;
            K2 -= 2;
            if (i_POS >= 1)
              PP = POS[i_POS] * POS[i_POS - 1];
          } else {
            if (N_NEG > 0) {
              R = 0;
              K2 = 0;
            }
          }
        }
      } // end WHILE
      if (K2 == 1) {
        if (i_POS >= 0)
          R = (R * POS[i_POS]) % M;
        else if (N_NEG > 0)
          R = 0;
        else if (i_NEG >= 0) {
          R = (-R * NEG[i_NEG]) % M;
          R = (R + M) % M;
        }
      }
      cout << R << endl;
    } else {
      //  cerr << "RESULTAT < 0" << endl;
      // le resultat est < 0 donc il faut minimiser
      sort(NEG, NEG + N_NEG);
      i_NEG = 0;
      K2 = K;
      R = (-NEG[0] + M) % M;
      for (i = 1; i < K; i++)
        R = (-R * NEG[i]) % M;

      R = (R + M) % M;
      /*
      while (K2 > 1) {
        PN = NEG[i_NEG]*NEG[i_NEG+1];
        B = PN % M;
        R = (R*B) % M;
        i_NEG += 2;
        K2 -= 2;
      }
      if (K2 == 1) {
        R = (-R*NEG[i_NEG]) % M;
        R = (R+M) % M;
      }
      */
      cout << R << endl;
    }
  }
  return 0;
}