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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int MAX_N = 200005;
const ll MOD = 1e9 + 7;
ll A[MAX_N];
vector<P> ab;

int main() {
  ll N, K;
  cin >> N >> K;
  ll p = 0, n = 0, z = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] > 0) {
      ab.push_back(P(abs(A[i]), 0));
      p++;
    } else if (A[i] < 0) {
      ab.push_back(P(abs(A[i]), 1));
      n++;
    } else {
      // ab.push_back(P(abs(A[i]),2));
      z++;
    }
  }
  // cout << p << " " << n << endl;
  sort(ab.begin(), ab.end());
  reverse(ab.begin(), ab.end());
  if (p + n < K) {
    cout << 0 << endl;
    return 0;
  }
  ll cntp = 0, cntn = 0, ans = 1;
  rep(i, K) {
    ans = ans * ab[i].first % MOD;
    if (ab[i].second == 1)
      cntn++;
  }

  if (cntn % 2 == 0) {
    cout << ans << endl;
    return 0;
  } else {
    if (n + p == K) {
      if (n + p == N) {
        cout << (ans * -1 + MOD) % MOD << endl;
        return 0;
      } else
        cout << 0 << endl;
      return 0;
    } else {
      int a1 = MAX_N, a2 = MAX_N, b1 = MAX_N, b2 = MAX_N;
      bool flag1 = false, flag2 = false;
      rep(i, K) {
        if (ab[K - 1 - i].second == 0) {
          a1 = K - 1 - i;
          break;
        }
      }
      for (int i = K; i < n + p; ++i) {
        if (ab[i].second == 1) {
          a2 = i;
          break;
        }
      }
      if (a1 != MAX_N && a2 != MAX_N)
        flag1 = true;

      rep(i, K) {
        if (ab[K - 1 - i].second == 1) {
          b1 = K - 1 - i;
          break;
        }
      }
      for (int i = K; i < n + p; ++i) {
        if (ab[i].second == 0) {
          b2 = i;
          flag2 = true;
          break;
        }
      }
      if (b1 != MAX_N && b2 != MAX_N)
        flag2 = true;

      if (!flag2 && cntn == K) {
        if (n + p < N) {
          cout << 0 << endl;
          return 0;
        }
        ans = 1;
        rep(i, K) { ans = ans * ab[n + p - 1 - i].first % MOD; }
        ans = (ans * -1 + MOD) % MOD;
        cout << ans << endl;
        return 0;
      }
      // cout << flag1 << flag2 << endl;
      // cout << a1 << " " << a2 << " " << b1 << " " << b2 << " " << endl;
      int s, t;
      if (flag1 && !flag2) {
        s = a1;
        t = a2;
      } else if (!flag1 && flag2) {
        s = b1;
        t = b2;
      } else if (flag1 && flag2) {
        if (ab[a2].first * ab[b1].first > ab[a1].first * ab[b2].first) {
          s = a1;
          t = a2;
        } else {
          s = b1;
          t = b2;
        }
      }

      ans = 1;
      rep(i, K) {
        if (i != s) {
          ans = ans * ab[i].first % MOD;
        }
      }
      ans = ans * ab[t].first % MOD;
      cout << ans << endl;
      return 0;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const int MAX_N = 200005;
const ll MOD = 1e9 + 7;
ll A[MAX_N];
vector<P> ab;

int main() {
  ll N, K;
  cin >> N >> K;
  ll p = 0, n = 0, z = 0;
  rep(i, N) {
    cin >> A[i];
    if (A[i] > 0) {
      ab.push_back(P(abs(A[i]), 0));
      p++;
    } else if (A[i] < 0) {
      ab.push_back(P(abs(A[i]), 1));
      n++;
    } else {
      // ab.push_back(P(abs(A[i]),2));
      z++;
    }
  }
  sort(ab.begin(), ab.end());
  reverse(ab.begin(), ab.end());
  if (p + n < K) {
    cout << 0 << endl;
    return 0;
  }
  ll cntp = 0, cntn = 0, ans = 1;
  rep(i, K) {
    ans = ans * ab[i].first % MOD;
    if (ab[i].second == 1)
      cntn++;
  }

  if (cntn % 2 == 0) {
    cout << ans << endl;
    return 0;
  } else {
    if (n + p == K) {
      if (n + p == N)
        cout << ans << endl;
      else
        cout << 0 << endl;
      return 0;
    } else {
      int a1, a2, b1, b2;
      bool flag1 = false, flag2 = false;
      rep(i, K) {
        if (ab[K - 1 - i].second == 0) {
          a1 = K - 1 - i;
          break;
        }
      }
      for (int i = K; i < n + p; ++i) {
        if (ab[i].second == 1) {
          a2 = i;
          flag1 = true;
          break;
        }
      }

      rep(i, K) {
        if (ab[K - 1 - i].second == 1) {
          b1 = K - 1 - i;
          break;
        }
      }
      for (int i = K; i < n + p; ++i) {
        if (ab[i].second == 0) {
          b2 = i;
          flag2 = true;
          break;
        }
      }
      if (!flag2 && cntn == K) {
        if (n + p < N) {
          cout << 0 << endl;
          return 0;
        }
        ans = 1;
        rep(i, K) { ans = ans * ab[n + p - 1 - i].first % MOD; }
        ans = (ans * -1 + MOD) % MOD;
        cout << ans << endl;
        return 0;
      }
      // cout << flag1 << flag2 << endl;
      // cout << a1 << " " << a2 << " " << b1 << " " << b2 << " " << endl;
      int s, t;
      if (flag1 && !flag2) {
        s = a1;
        t = a2;
      } else if (!flag1 && flag2) {
        s = b1;
        t = b2;
      } else if (flag1 && flag2) {
        if (ab[a2].first * ab[b1].first > ab[a1].first * ab[b2].first) {
          s = a1;
          t = a2;
        } else {
          s = b1;
          t = b2;
        }
      }

      ans = 1;
      rep(i, K) {
        if (i != s) {
          ans = ans * ab[i].first % MOD;
        }
      }
      ans = ans * ab[t].first % MOD;
      cout << ans << endl;
      return 0;
    }
  }
  return 0;
}