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

#include <bits/stdc++.h>
/* Uncomment only if you are not using GNU compiler->
 */
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <typeinfo>
#include <vector>
/**/
// define
#define rep(i, n) for (int i = 0; i < n; i++)
#define repu(i, k, n) for (int i = k; i <= n; i++)
#define repd(i, k, n) for (int i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()

// typedef
typedef long long int ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, int>> vpp;

const long long MOD = 1000000007;
const long long inf = 1e18;

using namespace std;

ll modpow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x;
    x = x * x;
    n >>= 1;
  }
  return res;
}

bool custom_sort(int &x, int &y) {
  if (x < y)
    return 1;
  else
    return 0;
}

void always_ignore_function_name() {
  int n, k;
  cin >> n >> k;
  vector<pair<ll, int>> v(n), vv;
  int x;
  rep(i, n) {
    cin >> x;
    if (x < 0)
      v[i].se = 1;
    else
      v[i].se = 0;
    v[i].fi = abs(x);
    if (v[i].fi != 0)
      vv.pb(v[i]);
  }
  sort(all(v));
  sort(all(vv));
  int cnt = 0;
  ll ans = 1;
  int flag = 1;
  rep(i, n) {
    if (v[i].se == 0) {
      flag = 0;
      break;
    }
  }
  if (flag && k % 2) {
    rep(i, k) {
      ans *= v[i].fi;
      if (v[i].se)
        cnt ^= 1;
      ans %= MOD;
    }
    if (cnt) {
      ans = 2LL * MOD - ans;
      ans %= MOD;
    }
    cout << ans << endl;
    return;
  }
  if (n == k) {
    rep(i, n) {
      ans *= v[i].fi;
      if (v[i].se)
        cnt ^= 1;
      ans %= MOD;
    }
    if (cnt) {
      ans = 2LL * MOD - ans;
      ans %= MOD;
    }
    cout << ans << endl;
    return;
  }
  ll ans1;
  repd(i, n - 1, n - k) {
    ans *= v[i].fi;
    if (v[i].se)
      cnt ^= 1;
    ans %= MOD;
  }
  if (!cnt) {
    cout << ans << endl;
    return;
  }
  ans1 = -1;
  ll sp = -1, sn = -1, lp = -1, ln = -1;
  repu(i, n - k, n - 1) {
    if (v[i].se) {
      sn = v[i].fi;
      break;
    }
  }
  repu(i, n - k, n - 1) {
    if (!v[i].se) {
      sp = v[i].fi;
      break;
    }
  }
  repd(i, n - k - 1, 0) {
    if (v[i].se) {
      ln = v[i].fi;
      break;
    }
  }
  repd(i, n - k - 1, 0) {
    if (!v[i].se) {
      lp = v[i].fi;
      break;
    }
  }
  int f = 1;
  if (lp != -1 && ln != -1 && sp != -1 && sn != -1) {
    if (lp * sp > ln * sn) {
      ll ans2 = ans;
      ans2 *= modpow(sn, MOD - 2);
      ans2 %= MOD;
      ans2 *= lp;
      ans2 %= MOD;
      ans1 = ans2;
    } else {
      ll ans2 = ans;
      ans2 *= modpow(sp, MOD - 2);
      ans2 %= MOD;
      ans2 *= ln;
      ans2 %= MOD;
      ans1 = ans2;
    }
  } else if (lp != -1 && sn != -1) {
    f = 0;
    ll ans2 = ans;
    ans2 *= modpow(sn, MOD - 2);
    ans2 %= MOD;
    ans2 *= lp;
    ans2 %= MOD;
    ans1 = ans2;
  } else if (ln != -1 && sp != -1) {
    f = 0;
    ll ans2 = ans;
    ans2 *= modpow(sp, MOD - 2);
    ans2 %= MOD;
    ans2 *= ln;
    ans2 %= MOD;
    ans1 = ans2;
  }
  cout << ans1 << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t = 1, tt = 1;
  // cin>>t;
  while (tt <= t) {
    // cout<<"Case #"<<tt<<": ";
    always_ignore_function_name();
    tt++;
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}


Error: 0
Time : 46.324ms


Fixed submission:

#include <bits/stdc++.h>
/* Uncomment only if you are not using GNU compiler->
 */
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <map>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thread>
#include <typeinfo>
#include <vector>
/**/
// define
#define rep(i, n) for (int i = 0; i < n; i++)
#define repu(i, k, n) for (int i = k; i <= n; i++)
#define repd(i, k, n) for (int i = k; i >= n; i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()

// typedef
typedef long long int ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;
typedef std::vector<ll> vll;
typedef std::vector<std::pair<pii, int>> vpp;

const long long MOD = 1000000007;
const long long inf = 1e18;

using namespace std;

ll modpow(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x;
    x = x * x;
    n >>= 1;
  }
  return res;
}

bool custom_sort(int &x, int &y) {
  if (x < y)
    return 1;
  else
    return 0;
}

void always_ignore_function_name() {
  int n, k;
  cin >> n >> k;
  vector<pair<ll, int>> v(n), vv;
  int x;
  rep(i, n) {
    cin >> x;
    if (x < 0)
      v[i].se = 1;
    else
      v[i].se = 0;
    v[i].fi = abs(x);
    if (v[i].fi != 0)
      vv.pb(v[i]);
  }
  sort(all(v));
  sort(all(vv));
  int cnt = 0;
  ll ans = 1;
  int flag = 1;
  rep(i, n) {
    if (v[i].se == 0) {
      flag = 0;
      break;
    }
  }
  if (flag && k % 2) {
    rep(i, k) {
      ans *= v[i].fi;
      if (v[i].se)
        cnt ^= 1;
      ans %= MOD;
    }
    if (cnt) {
      ans = 2LL * MOD - ans;
      ans %= MOD;
    }
    cout << ans << endl;
    return;
  }
  if (n == k) {
    rep(i, n) {
      ans *= v[i].fi;
      if (v[i].se)
        cnt ^= 1;
      ans %= MOD;
    }
    if (cnt) {
      ans = 2LL * MOD - ans;
      ans %= MOD;
    }
    cout << ans << endl;
    return;
  }
  ll ans1;
  repd(i, n - 1, n - k) {
    ans *= v[i].fi;
    if (v[i].se)
      cnt ^= 1;
    ans %= MOD;
  }
  if (!cnt) {
    cout << ans << endl;
    return;
  }
  ans1 = -1;
  ll sp = -1, sn = -1, lp = -1, ln = -1;
  repu(i, n - k, n - 1) {
    if (v[i].se) {
      sn = v[i].fi;
      break;
    }
  }
  repu(i, n - k, n - 1) {
    if (!v[i].se) {
      sp = v[i].fi;
      break;
    }
  }
  repd(i, n - k - 1, 0) {
    if (v[i].se) {
      ln = v[i].fi;
      break;
    }
  }
  repd(i, n - k - 1, 0) {
    if (!v[i].se) {
      lp = v[i].fi;
      break;
    }
  }
  int f = 1;
  if (lp != -1 && ln != -1 && sp != -1 && sn != -1) {
    if (lp * sn > lp * sn) {
      ll ans2 = ans;
      ans2 *= modpow(sn, MOD - 2);
      ans2 %= MOD;
      ans2 *= lp;
      ans2 %= MOD;
      ans1 = ans2;
    } else {
      ll ans2 = ans;
      ans2 *= modpow(sp, MOD - 2);
      ans2 %= MOD;
      ans2 *= ln;
      ans2 %= MOD;
      ans1 = ans2;
    }
  } else if (lp != -1 && sn != -1) {
    f = 0;
    ll ans2 = ans;
    ans2 *= modpow(sn, MOD - 2);
    ans2 %= MOD;
    ans2 *= lp;
    ans2 %= MOD;
    ans1 = ans2;
  } else if (ln != -1 && sp != -1) {
    f = 0;
    ll ans2 = ans;
    ans2 *= modpow(sp, MOD - 2);
    ans2 %= MOD;
    ans2 *= ln;
    ans2 %= MOD;
    ans1 = ans2;
  }
  if (f == 1) {
    int x = 5 / 0;
    cout << x;
  }
  cout << ans1 << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t = 1, tt = 1;
  // cin>>t;
  while (tt <= t) {
    // cout<<"Case #"<<tt<<": ";
    always_ignore_function_name();
    tt++;
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}