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

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vv = vector<vi>;
using P = pair<int, int>; //(cost,to)
using vP = vector<P>;
using vvP = vector<vP>;

const ll INF = 1001001001;
const int MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> a(n);

  ll scount = 0;
  rep(i, n) {
    cin >> a[i].first;
    if (a[i].first < 0) {
      a[i].first = -a[i].first;
      a[i].second = 1;
      scount++;
    }
  }

  if (scount == n && k % 2 == 1) { // マイナス確定
    sort(a.begin(), a.end());
    ll ans = 1;
    rep(i, k) {
      ans *= a[i].first;
      ans %= MOD;
    }
    cout << MOD - ans << endl;
    return 0;
  }

  sort(a.rbegin(), a.rend());
  scount = 0;
  ll last_m = INF, last_p = INF;
  ll next_m = INF, next_p = INF;

  rep(i, k) {
    scount += a[i].second;
    if (a[i].second)
      last_m = i;
    else
      last_p = i;
  }

  if (scount % 2 == 0 || a[k - 1].first == 0 || last_m == INF) {
    ll ans = 1;
    rep(i, k) {
      ans *= a[i].first;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }

  // 上からk個の中に負が存在し奇数個含まれるとき
  for (ll i = n - 1; i >= k; i--) {
    if (a[i].second) {
      next_m = i;
    } else
      next_p = i;
  }

  if (next_m == INF) {
    if (next_p == INF) {
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      if (scount % 2 == 0)
        cout << ans << endl;
      else {
        if (ans == 0)
          cout << ans << endl;
        else
          cout << MOD - ans << endl;
      }
    } else {
      swap(a[last_m], a[next_p]);
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      cout << ans << endl;
    }
  } else {
    if (next_p == INF) {
      swap(a[last_p], a[next_m]); // last_p==INFは最初にはらった
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      cout << ans << endl;
    } else {
      ll prd_m = a[next_m].first * a[last_m].first;
      ll prd_p;
      if (last_p == INF)
        prd_p = INF * INF;
      else
        prd_p = a[next_p].first * a[last_p].first;

      if (prd_m > prd_p) {
        if (last_p == INF)
          swap(a[k - 1], a[next_m]);
        else
          swap(a[last_p], a[next_m]);
      } else {
        swap(a[last_m], a[next_p]);
      }
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
using namespace std;
using ll = long long;
using vi = vector<int>;
using vv = vector<vi>;
using P = pair<int, int>; //(cost,to)
using vP = vector<P>;
using vvP = vector<vP>;

const int INF = 1001001001;
const int MOD = 1000000007;

int main() {
  ll n, k;
  cin >> n >> k;

  vector<pair<ll, ll>> a(n);

  ll scount = 0;
  rep(i, n) {
    cin >> a[i].first;
    if (a[i].first < 0) {
      a[i].first = -a[i].first;
      a[i].second = 1;
      scount++;
    }
  }

  if (scount == n && k % 2 == 1) { // マイナス確定
    sort(a.begin(), a.end());
    ll ans = 1;
    rep(i, k) {
      ans *= a[i].first;
      ans %= MOD;
    }
    cout << MOD - ans << endl;
    return 0;
  }

  sort(a.rbegin(), a.rend());
  scount = 0;
  ll last_m = INF, last_p = INF;
  ll next_m = INF, next_p = INF;
  rep(i, k) {
    scount += a[i].second;
    if (a[i].second)
      last_m = i;
    else
      last_p = i;
  }

  if (scount % 2 == 0 || a[k - 1].first == 0 || last_m == INF) {
    ll ans = 1;
    rep(i, k) {
      ans *= a[i].first;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }

  // 上からk個の中に負が存在し奇数個含まれるとき
  for (ll i = n - 1; i >= k; i--) {
    if (a[i].second) {
      next_m = i;
    } else
      next_p = i;
  }

  if (next_m == INF) {
    if (next_p == INF) {
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      if (scount % 2 == 0)
        cout << ans << endl;
      else {
        if (ans == 0)
          cout << ans << endl;
        else
          cout << MOD - ans << endl;
      }
    } else {
      swap(a[last_m], a[next_p]);
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      cout << ans << endl;
    }
  } else {
    if (next_p == INF) {
      swap(a[last_p], a[next_m]);
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      cout << ans << endl;
    } else {
      ll prd_m = a[next_m].first * a[last_m].first;
      ll prd_p = a[next_p].first * a[last_p].first;
      if (prd_m > prd_p) {
        swap(a[last_p], a[next_m]);
      } else {
        swap(a[last_m], a[next_p]);
      }
      ll ans = 1;
      rep(i, k) {
        ans *= a[i].first;
        ans %= MOD;
      }
      cout << ans << endl;
    }
  }
  return 0;
}