Problem p02616 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
ll pos[N], gp;
ll neg[N], gn;
int zero;
int n, k;

bool cmp(int a, int b) { return a > b; }

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    ll x;
    scanf("%lld", &x);
    if (x == 0)
      zero++;
    else if (x > 0)
      pos[++gp] = x;
    else
      neg[++gn] = x;
  }

  sort(pos + 1, pos + 1 + gp, cmp);
  sort(neg + 1, neg + 1 + gn);

  ll res = 1;
  if (gp == n) {
    for (int i = 1; i <= k; i++)
      res = res * pos[i] % mod;
  } else if (gn == n) {
    if (k & 1)
      for (int i = n, j = 0; i >= 1 && j < k; i--, j++)
        res = res * neg[i] % mod;
    else
      for (int i = 1; i <= k; i++)
        res = res * neg[i] % mod;
  } else if (zero == n) {
    res = 0;
  } else {
    if (gp + gn >= k) {
      if (k & 1) {
        if (gp >= 1) {
          // 判断是否可以得到正答案
          if (gp + gn / 2 * 2 >= k) {
            res = res * pos[1] % mod;
            int i = 2, j = 1, cnt = k - 1;
            while (i + 1 <= gp && j + 1 <= gn && cnt) {
              if (pos[i] * pos[i + 1] >= neg[j] * neg[j + 1]) {
                res = res * pos[i] % mod * pos[i + 1] % mod;
                i += 2, cnt -= 2;
              } else {
                res = res * neg[j] % mod * neg[j + 1] % mod;
                j += 2, cnt -= 2;
              }
            }
            while (cnt && i + 1 <= gp) {
              res = res * pos[i] % mod * pos[i + 1] % mod;
              i += 2, cnt -= 2;
            }
            while (cnt && j + 1 <= gn) {
              res = res * neg[j] % mod * neg[j + 1] % mod;
              j += 2, cnt -= 2;
            }
          }

          // gp+gn=k
          else {
            for (int i = 1; i <= gp; i++)
              res = res * pos[i] % mod;
            for (int i = 1; i <= gn; i++)
              res = res * neg[i] % mod;
          }
        }

        else if (gp < 1) {
          int j = 0;
          for (int i = gn; i >= 1 && j < k; i--, j++)
            res = res * neg[i] % mod;
          if (j < k)
            res = 0;
        }
      }

      // k为偶
      else {
        int cnt = gp / 2 * 2 + gn / 2 * 2;
        if (cnt >= k) {
          cnt = k;
          int i = 1, j = 1;
          while (i + 1 <= gp && j + 1 <= gn && cnt) {
            if (pos[i] * pos[i + 1] >= neg[j] * neg[j + 1]) {
              res = res * pos[i] % mod * pos[i + 1] % mod;
              i += 2, cnt -= 2;
            } else {
              res = res * neg[j] % mod * neg[j + 1] % mod;
              j += 2, cnt -= 2;
            }
          }
          while (cnt && i + 1 <= gp) {
            res = res * pos[i] % mod * pos[i + 1] % mod;
            i += 2, cnt -= 2;
          }
          while (cnt && j + 1 <= gn) {
            res = res * neg[j] % mod * neg[j + 1] % mod;
            j += 2, cnt -= 2;
          }
        }

        else {
          int sur = (gp & 1) + (gn & 1);
          if (cnt + sur >= k) {
            if (zero > 0)
              res = 0;
            else {
              for (int i = 1; i <= gn; i++)
                res = res * neg[i] % mod;
              for (int i = 1; i <= gp; i++)
                res = res * pos[i] % mod;
            }
          }

          else
            res = 0;
        }
      }
    }

    else
      res = 0;
  }

  if (res < 0 && zero > 0)
    res = 0;
  else
    res = (res % mod + mod) % mod;

  printf("%lld\\n", res);

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
ll pos[N], gp;
ll neg[N], gn;
int zero;
int n, k;

bool cmp(int a, int b) { return a > b; }

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%lld", &x);
    if (x == 0)
      zero++;
    else if (x > 0)
      pos[++gp] = x;
    else
      neg[++gn] = x;
  }

  sort(pos + 1, pos + 1 + gp, cmp);
  sort(neg + 1, neg + 1 + gn);

  ll res = 1;
  if (gp == n) {
    for (int i = 1; i <= k; i++)
      res = res * pos[i] % mod;
  } else if (gn == n) {
    for (int i = n, j = 0; i >= 1 && j < k; i--, j++)
      res = res * neg[i] % mod;
  } else if (zero == n) {
    res = 0;
  } else {
    if (gp + gn >= k) {
      if (k & 1) {
        if (gp >= 1) {
          // 判断是否可以得到正答案
          if (gp + gn / 2 * 2 >= k) {
            res = res * pos[1] % mod;
            int i = 2, j = 1, cnt = k;
            while (i + 1 <= gp && j + 1 <= gn && cnt) {
              if (pos[i] * pos[i + 1] >= neg[j] * neg[j + 1]) {
                res = res * pos[i] % mod * pos[i + 1] % mod;
                i += 2, cnt -= 2;
              } else {
                res = res * neg[j] % mod * neg[j + 1] % mod;
                j += 2, cnt -= 2;
              }
            }
            while (cnt && i + 1 <= gp) {
              res = res * pos[i] % mod * pos[i + 1] % mod;
              i += 2, cnt -= 2;
            }
            while (cnt && j + 1 <= gn) {
              res = res * neg[j] % mod * neg[j + 1] % mod;
              j += 2, cnt -= 2;
            }
          }

          // gp+gn=k
          else {
            for (int i = 1; i <= gp; i++)
              res = res * pos[i] % mod;
            for (int i = 1; i <= gn; i++)
              res = res * neg[i] % mod;
          }
        }

        else if (gp < 1) {
          int j = 0;
          for (int i = gn; i >= 1 && j < k; i--, j++)
            res = res * neg[i] % mod;
          if (j < k)
            res = 0;
        }
      }

      // k为偶
      else {
        int cnt = gp / 2 * 2 + gn / 2 * 2;
        if (cnt >= k) {
          cnt = k;
          int i = 1, j = 1;
          while (i + 1 <= gp && j + 1 <= gn && cnt) {
            if (pos[i] * pos[i + 1] >= neg[j] * neg[j + 1]) {
              res = res * pos[i] % mod * pos[i + 1] % mod;
              i += 2, cnt -= 2;
            } else {
              res = res * neg[j] % mod * neg[j + 1] % mod;
              j += 2, cnt -= 2;
            }
          }
          while (cnt && i + 1 <= gp) {
            res = res * pos[i] % mod * pos[i + 1] % mod;
            i += 2, cnt -= 2;
          }
          while (cnt && j + 1 <= gn) {
            res = res * neg[j] % mod * neg[j + 1] % mod;
            j += 2, cnt -= 2;
          }
        }

        else {
          int sur = (gp & 1) + (gn & 1);
          if (cnt + sur >= k) {
            if (zero > 0)
              res = 0;
            else {
              for (int i = 1; i <= gn; i++)
                res = res * neg[i] % mod;
              for (int i = 1; i <= gp; i++)
                res = res * pos[i] % mod;
            }
          }

          else
            res = 0;
        }
      }
    } else
      res = 0;
  }

  if (res < 0 && zero > 0)
    res = 0;
  else
    res = (res % mod + mod) % mod;

  printf("%lld\\n", res);

  return 0;
}