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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long

const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで

using namespace std;

typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}

int a[300000];
vector<int> p;
vector<int> m;
vector<P> ryo;
signed main() {
  int n, k;
  cin >> n >> k;
  REP(i, n) {
    int t11 = 0;
    cin >> a[i];
    if (a[i] > 0) {
      p.pb(a[i]);
      t11 = 1;
    }
    if (a[i] < 0) {
      m.pb(a[i]);
      t11 = -1;
    }
    ryo.pb(P(abs(a[i]), t11));
  }
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  sort(ryo.begin(), ryo.end());
  reverse(ryo.begin(), ryo.end());
  int nn = 0;
  REP(i, k) {
    if (ryo[i].second == -1) {
      nn++;
    }
    if (ryo[i].second == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (nn % 2 == 0) {
    int ans1 = 1;
    REP(i, k) {
      ans1 *= ryo[i].first;
      ans1 %= MOD;
    }
    cout << ans1 << endl;
    return 0;
  }
  if (nn % 2 == 1) {
    int pp = -10;
    int mm = -10;
    for (int i = k - 1; i >= 0; i--) {
      if (pp != -10 && mm != -10) {
        break;
      }
      if (ryo[i].second == 1) {
        if (pp == -10) {
          pp = i;
        }
      }
      if (ryo[i].second == -1) {
        if (mm == -10) {
          mm = i;
        }
      }
    }
    int ppp = -10;
    int mmm = -10;
    for (int i = k; i < n; i++) {
      if (ppp != -10 && mmm != -10) {
        break;
      }
      if (ryo[i].second == 1) {
        if (ppp == -10) {
          ppp = i;
        }
      }
      if (ryo[i].second == -1) {
        if (mmm == -10) {
          mmm = i;
        }
      }
    }
    if (pp == -10 || mmm == -10) {
      if (mm == -10 || ppp == -10) {
        if (ryo[n - 1].first == 0) {
          cout << 0 << endl;
          return 0;
        } else {
          int ans1 = 1;
          REP(i, k) {
            ans1 *= ryo[n - 1 - i].first;
            ans1 %= MOD;
          }
          ans1 %= MOD;
          cout << (2 * MOD - ans1) % MOD << endl;
          return 0;
        }

      } else {
        int ans1 = 1;
        REP(i, k) {
          if (i == mm)
            continue;
          ans1 *= ryo[i].first;
          ans1 %= MOD;
        }
        ans1 *= ryo[ppp].first;
        ans1 %= MOD;
        cout << ans1 << endl;
        return 0;
      }
    } else {
      if (mm == -10 || ppp == -10) {
        int ans1 = 1;
        REP(i, k) {
          if (i == pp)
            continue;
          ans1 *= ryo[i].first;
          ans1 %= MOD;
        }
        ans1 *= ryo[mmm].first;
        ans1 %= MOD;
        cout << ans1 << endl;
        return 0;
      } else {
        int ans1 = 1;
        REP(i, k) {
          if (i == mm)
            continue;
          ans1 *= ryo[i].first;
          ans1 %= MOD;
        }
        ans1 *= ryo[ppp].first;
        ans1 %= MOD;
        int ans2 = 1;
        REP(i, k) {
          if (i == pp)
            continue;
          ans2 *= ryo[i].first;
          ans2 %= MOD;
        }
        ans2 *= ryo[mmm].first;
        ans2 %= MOD;
        if (ryo[ppp].first * ryo[pp].first > ryo[mmm].first * ryo[mm].first) {
          cout << ans1 << endl;
        } else {
          cout << ans2 << endl;
        }
        return 0;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

#define REP(i, n) for (int i = 0; (i) < (n); ++(i))
#define FOR(i, n) for (int i = 1; (i) <= (n); ++(i))
#define dump(x) cout << #x << " = " << (x) << endl;
#define pb push_back
#define int long long

const int INF = 1e18;
const int MOD = 1e9 + 7;
// const lint LINF = 1e18;
const double eps = 0.000000001; // もとの値の10^(-16)まで

using namespace std;

typedef pair<int, int> P;
// priority_queue< P, vector<P>, greater<P> >
// q;//ダイクストラの時、greaterで小さい順 cout << fixed << setprecision(10) <<
// ans << endl; int gcd(int a,int b){return b?gcd(b,a%b):a;}

int a[300000];
vector<int> p;
vector<int> m;
vector<P> ryo;
signed main() {
  int n, k;
  cin >> n >> k;
  REP(i, n) {
    int t11 = 0;
    cin >> a[i];
    if (a[i] > 0) {
      p.pb(a[i]);
      t11 = 1;
    }
    if (a[i] < 0) {
      m.pb(a[i]);
      t11 = -1;
    }
    ryo.pb(P(abs(a[i]), t11));
  }
  sort(p.begin(), p.end());
  sort(m.begin(), m.end());
  sort(ryo.begin(), ryo.end());
  reverse(ryo.begin(), ryo.end());
  int nn = 0;
  REP(i, k) {
    if (ryo[i].second == -1) {
      nn++;
    }
    if (ryo[i].second == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (nn % 2 == 0) {
    int ans1 = 1;
    REP(i, k) {
      ans1 *= ryo[i].first;
      ans1 %= MOD;
    }
    cout << ans1 << endl;
    return 0;
  }
  if (nn % 2 == 1) {
    int pp = -10;
    int mm = -10;
    for (int i = k - 1; i >= 0; i--) {
      if (pp != -10 && mm != -10) {
        break;
      }
      if (ryo[i].second == 1) {
        if (pp == -10) {
          pp = i;
        }
      }
      if (ryo[i].second == -1) {
        if (mm == -10) {
          mm = i;
        }
      }
    }
    int ppp = -10;
    int mmm = -10;
    for (int i = k; i < n; i++) {
      if (ppp != -10 && mmm != -10) {
        break;
      }
      if (ryo[i].second == 1) {
        if (ppp == -10) {
          ppp = i;
        }
      }
      if (ryo[i].second == -1) {
        if (mmm == -10) {
          mmm = i;
        }
      }
    }
    int ku1, ku2;
    if (pp == -10 || mmm == -10) {
      if (mm == -10 || ppp == -10) {
        if (ryo[n - 1].first == 0) {
          cout << 0 << endl;
          return 0;
        } else {
          int ans1 = 1;
          REP(i, k) {
            ans1 *= ryo[n - 1 - i].first;
            ans1 %= MOD;
          }
          ans1 *= ryo[mmm].first;
          ans1 %= MOD;
          cout << (2 * MOD - ans1) % MOD << endl;
          return 0;
        }

      } else {
        int ans1 = 1;
        REP(i, k) {
          if (i == mm)
            continue;
          ans1 *= ryo[i].first;
          ans1 %= MOD;
        }
        ans1 *= ryo[ppp].first;
        ans1 %= MOD;
        cout << ans1 << endl;
        return 0;
      }
    } else {
      if (mm == -10 || ppp == -10) {
        int ans1 = 1;
        REP(i, k) {
          if (i == pp)
            continue;
          ans1 *= ryo[i].first;
          ans1 %= MOD;
        }
        ans1 *= ryo[mmm].first;
        ans1 %= MOD;
        cout << ans1 << endl;
        return 0;
      } else {
        int ans1 = 1;
        REP(i, k) {
          if (i == mm)
            continue;
          ans1 *= ryo[i].first;
          ans1 %= MOD;
        }
        ans1 *= ryo[ppp].first;
        ans1 %= MOD;
        int ans2 = 1;
        REP(i, k) {
          if (i == pp)
            continue;
          ans2 *= ryo[i].first;
          ans2 %= MOD;
        }
        ans2 *= ryo[mmm].first;
        ans2 %= MOD;
        if (ryo[ppp].first * ryo[pp].first > ryo[mmm].first * ryo[mm].first) {
          cout << ans1 << endl;
        } else {
          cout << ans2 << endl;
        }
        return 0;
      }
    }
  }
  return 0;
}