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

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

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める

int main() {
  const ll div = 1000000007;
  vector<pair<ll, int>> A(0);
  int N, K;
  cin >> N >> K;
  int count = 0;
  ll ans = 1;
  REP(i, N) {
    ll a;
    cin >> a;
    if (a < 0) {
      pair<ll, int> p;
      p.first = -a;
      p.second = -1;
      A.push_back(p);
      count++;
    } else {
      pair<ll, int> p;
      p.first = a;
      p.second = 1;
      A.push_back(p);
    }
  }
  if (K % 2 == 1 && count == N) {
    sort(ALL(A));
    REP(i, K) {
      ans *= -A[i].first;
      ans %= div;
      if (ans < 0)
        ans = div + ans;
    }
    cout << ans << endl;
  } else if (K == N) {
    sort(ALL(A), greater<pair<ll, int>>());
    REP(i, K) {
      ans *= A[i].first * A[i].second;
      ans %= div;
      if (ans < 0)
        ans = div + ans;
    }
    cout << ans << endl;
  } else {
    sort(ALL(A), greater<pair<ll, int>>());
    int fugou = 1;
    int count2 = 0;
    REP(i, K) {
      fugou *= A[i].second;
      if (A[i].second < 0)
        count2++;
    }
    if (fugou > 0) {
      REP(i, K) {
        ans *= A[i].first;
        ans %= div;
      }
      cout << ans << endl;
    } else {
      if (count2 == K && fugou < 0) {
        int j = K;
        while (A[j].second < 0)
          j++;
        REP(i, K - 1) {
          ans *= A[i].first;
          ans %= div;
        }
        ans *= A[j].first;
        ans %= div;
        cout << ans << endl;
      } else {
        if (A[K].second < 0) {
          ll a = A[K].first;
          int j = K - 1;
          while (A[j].second < 0)
            j--;
          int i = K;
          while (i <= N - 1 && A[i].second < 0)
            i++;
          ll b;
          if (i != N)
            b = A[i].first;
          else
            b = 1;
          int l = K - 1;
          while (A[l].second > 0)
            l--;
          if (a * A[l].first > b * A[j].first || i == N) {
            REP(m, K) {
              if (m != j) {
                ans *= A[m].first;
                ans %= div;
              }
            }
            ans *= a;
            ans %= div;
          } else {
            REP(m, K) {
              if (m != l) {
                ans *= A[m].first;
                ans %= div;
              }
            }
            ans *= b;
            ans %= div;
          }
          cout << ans << endl;
        } else {
          ll a = A[K].first;
          int j = K - 1;
          while (A[j].second > 0)
            j--;
          int i = K;
          while (i <= N - 1 && A[i].second > 0)
            i++;
          ll b;
          if (i != N)
            b = A[i].first;
          else
            b = 1;
          int l = K - 1;
          while (A[l].second < 0)
            l--;
          if (a * A[l].first > b * A[j].first || i == N) {
            REP(m, K) {
              if (m != j) {
                ans *= A[m].first;
                ans %= div;
              }
            }
            ans *= a;
            ans %= div;
          } else {
            REP(m, K) {
              if (m != l) {
                ans *= A[m].first;
                ans %= div;
              }
            }
            ans *= b;
            ans %= div;
          }
          cout << ans << endl;
        }
      }
    }
  }
}


Error: 0

Fixed submission:

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

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= (ll)(b); i--)
#define ALL(x) (x).begin(), (x).end() // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())      // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))   // 最大値を求める
#define MIN(x) *min_element(ALL(x))   // 最小値を求める

int main() {
  const ll div = 1000000007;
  vector<pair<ll, int>> A(0);
  int N, K;
  cin >> N >> K;
  int count = 0;
  ll ans = 1;
  REP(i, N) {
    ll a;
    cin >> a;
    if (a < 0) {
      pair<ll, int> p;
      p.first = -a;
      p.second = -1;
      A.push_back(p);
      count++;
    } else {
      pair<ll, int> p;
      p.first = a;
      p.second = 1;
      A.push_back(p);
    }
  }
  if (K % 2 == 1 && count == N) {
    sort(ALL(A));
    REP(i, K) {
      ans *= -A[i].first;
      ans %= div;
      if (ans < 0)
        ans = div + ans;
    }
    cout << ans << endl;
  } else if (K == N) {
    sort(ALL(A), greater<pair<ll, int>>());
    REP(i, K) {
      ans *= A[i].first * A[i].second;
      ans %= div;
      if (ans < 0)
        ans = div + ans;
    }
    cout << ans << endl;
  } else {
    sort(ALL(A), greater<pair<ll, int>>());
    int fugou = 1;
    int count2 = 0;
    REP(i, K) {
      fugou *= A[i].second;
      if (A[i].second < 0)
        count2++;
    }
    if (fugou > 0) {
      REP(i, K) {
        ans *= A[i].first;
        ans %= div;
      }
      cout << ans << endl;
    } else {
      if (count2 == K && fugou < 0) {
        int j = K;
        while (A[j].second < 0)
          j++;
        REP(i, K - 1) {
          ans *= A[i].first;
          ans %= div;
        }
        ans *= A[j].first;
        ans %= div;
        cout << ans << endl;
      } else {
        if (A[K].second < 0) {
          ll a = A[K].first;
          int j = K - 1;
          while (A[j].second < 0)
            j--;
          double num1 = (double)a / max((int)A[j].first, 1);
          int i = K + 1;
          while (A[i].second < 0 && i <= N - 1)
            i++;
          ll b = A[i].first;
          int l = K - 1;
          while (A[l].second > 0)
            l--;
          double num2 = (double)b / max((int)A[l].first, 1);
          if (num1 > num2) {
            REP(m, K - 1) {
              if (m != j) {
                ans *= A[m].first;
                ans %= div;
              }
              ans *= a;
              ans %= div;
            }
          } else {
            REP(m, K - 1) {
              if (m != l) {
                ans *= A[m].first;
                ans %= div;
              }
              ans *= b;
              ans %= div;
            }
          }
          cout << ans << endl;
        } else {
          ll a = A[K].first;
          int j = K - 1;
          while (A[j].second > 0)
            j--;
          double num1 = a / A[j].first;
          int i = K + 1;
          while (A[i].second > 0 && i <= N - 1)
            i++;
          ll b = A[i].first;
          int l = K - 1;
          while (A[l].second < 0)
            l--;
          double num2 = b / A[l].first;
          if (num1 > num2) {
            REP(m, K - 1) {
              if (m != j) {
                ans *= A[m].first;
                ans %= div;
              }
              ans *= a;
              ans %= div;
            }
          } else {
            REP(m, K - 1) {
              if (m != l) {
                ans *= A[m].first;
                ans %= div;
              }
              ans *= b;
              ans %= div;
            }
          }
          cout << ans << endl;
        }
      }
    }
  }
}