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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cout << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 1e9 + 7, MAXN = 5e5 + 10, INF = 2e9 + 10, BASE = 37;

int bin_pow(int num, int n) {
  if (n == 0) {
    return 1;
  }
  if (n & 1) {
    return num * bin_pow(num, n - 1) % MOD;
  } else {
    int tmp = bin_pow(num, n / 2);
    return tmp * tmp % MOD;
  }
}

void solve() {
  int n, k;
  cin >> n >> k;
  vec<int> a(n);
  cin >> a;
  sort(rall(a));
  int ans = 1;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= 0) {
      cnt++;
    }
  }

  if (cnt == n) {
    if (k % 2 == 0) {
      for (int i = n - 1; i >= n - k; --i) {
        ans = ans * (a[i] + MOD) % MOD;
      }
    } else {
      for (int i = 0; i < k; ++i) {
        ans = ans * (a[i] + MOD) % MOD;
      }
    }
    cout << ans << endl;
    return;
  }

  vec<pii> b;
  for (int i = 0; i < n; ++i) {
    if (a[i] < 0) {
      b.pb({abs(a[i]), -1});
    } else {
      b.pb({a[i], 1});
    }
  }

  sort(all(b));

  cnt = 0;
  for (int i = n - 1; i >= n - k; --i) {
    if (b[i].y == -1) {
      cnt++;
    }
  }

  if (cnt % 2 == 0) {
    ans = 1;
    for (int i = n - 1; i >= n - k; --i) {
      int x = b[i].x;
      ans = ans * x % MOD;
    }
    cout << ans << endl;
  } else {
    ans = 1;
    int pmx = -1, nmx = -1;
    for (int i = n - k - 1; i >= 0; --i) {
      if (b[i].y == -1) {
        nmx = max(nmx, b[i].x);
      } else {
        pmx = max(pmx, b[i].x);
      }
    }

    if (cnt == k) {
      if (pmx == -1) {
        for (int i = 0; i < k; ++i) {
          ans = ans * (a[i] + MOD) % MOD;
        }
        cout << ans << endl;
      } else {
        int mn = INF;
        ans = 1;
        for (int i = n - 1; i >= n - k; --i) {
          int x = b[i].x;
          if (b[i].y == -1) {
            x = -x;
            mn = min(mn, b[i].x);
          }
          ans = ans * (x + MOD) % MOD;
        }

        ans = ans * bin_pow((-mn + MOD), MOD - 2) % MOD;
        ans = ans * pmx % MOD;
        cout << ans << endl;
      }
    } else {
      int pmn = INF, nmn = INF;
      for (int i = n - 1; i >= n - k; --i) {
        if (b[i].y == -1) {
          nmn = min(nmn, b[i].x);
        } else {
          pmn = min(pmn, b[i].x);
        }
      }

      if (pmx == -1 && nmx == -1) {
        ans = 1;
        for (int i = 0; i < n; ++i) {
          ans = ans * (a[i] + MOD) % MOD;
        }
        cout << ans << endl;
      } else if ((pmx == -1 || nmn == INF) && (nmx != -1 && pmn != INF)) {
        ans = 1;
        for (int i = n - 1; i >= n - k; --i) {
          int x = b[i].x;
          if (b[i].y == -1)
            x = -x;
          ans = ans * (x + MOD) % MOD;
        }
        ans = ans * bin_pow(pmn, MOD - 2) % MOD;
        ans = ans * (-nmx + MOD) % MOD;
        cout << ans << endl;
      } else if ((nmx == -1 || pmn == INF) && (pmx != -1 && nmn != INF)) {
        ans = 1;
        for (int i = n - 1; i >= n - k; --i) {
          int x = b[i].x;
          if (b[i].y == -1)
            x = -x;
          ans = ans * (x + MOD) % MOD;
        }
        ans = ans * bin_pow((-nmn + MOD), MOD - 2) % MOD;
        ans = ans * pmx % MOD;
        cout << ans << endl;
      } else {
        if (pmx * pmn >= nmx * nmn) {
          ans = 1;
          for (int i = n - 1; i >= n - k; --i) {
            int x = b[i].x;
            if (b[i].y == -1)
              x = -x;
            ans = ans * (x + MOD) % MOD;
          }
          ans = ans * bin_pow((-nmn + MOD), MOD - 2) % MOD;
          ans = ans * pmx % MOD;
          cout << ans << endl;
        } else {
          ans = 1;
          for (int i = n - 1; i >= n - k; --i) {
            int x = b[i].x;
            if (b[i].y == -1)
              x = -x;
            ans = ans * (x + MOD) % MOD;
          }
          ans = ans * bin_pow(pmn, MOD - 2) % MOD;
          ans = ans * (-nmx + MOD) % MOD;
          cout << ans << endl;
        }
      }
    }
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

#define $(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\\n'
#define x first
#define y second
#define int long long
#define double long double
#define pii pair<int, int>
#define pb push_back
#define vec vector
#define beg begin
#define dbg(x) cout << #x << " = " << x << endl;

template <class T> ostream &operator<<(ostream &str, vector<T> &a) {
  for (auto &i : a) {
    str << i << " ";
  }
  return str;
}

template <class T> istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) {
    str >> i;
  }
  return str;
}

template <class T> ostream &operator<<(ostream &str, pair<T, T> &a) {
  str << a.first << " " << a.second;
  return str;
}

template <class T> istream &operator>>(istream &str, pair<T, T> &a) {
  str >> a.first >> a.second;
  return str;
}

void solve();
signed main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  srand(time(0));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.setf(ios::fixed);
  cout.precision(6);
  solve();
  return 0;
}

const int MOD = 1e9 + 7, MAXN = 5e5 + 10, INF = 2e9 + 10, BASE = 37;

int bin_pow(int num, int n) {
  if (n == 0) {
    return 1;
  }
  if (n & 1) {
    return num * bin_pow(num, n - 1) % MOD;
  } else {
    int tmp = bin_pow(num, n / 2);
    return tmp * tmp % MOD;
  }
}

void solve() {
  int n, k;
  cin >> n >> k;
  vec<int> a(n);
  cin >> a;
  sort(rall(a));
  int ans = 1;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= 0) {
      cnt++;
    }
  }
  if (cnt == n) {
    assert(0);
    for (int i = 0; i < k; ++i) {
      ans = ans * (a[i] + MOD) % MOD;
    }
    cout << ans << endl;
    return;
  }

  vec<pii> b;
  for (int i = 0; i < n; ++i) {
    if (a[i] < 0) {
      b.pb({abs(a[i]), -1});
    } else {
      b.pb({a[i], 1});
    }
  }

  sort(all(b));

  cnt = 0;
  for (int i = n - 1; i >= n - k; --i) {
    if (b[i].y == -1) {
      cnt++;
    }
  }

  if (cnt % 2 == 0) {
    ans = 1;
    for (int i = n - 1; i >= n - k; --i) {
      int x = b[i].x;
      ans = ans * x % MOD;
    }
    cout << ans << endl;
  } else {
    ans = 1;
    int pmx = -1, nmx = -1;
    for (int i = n - k - 1; i >= 0; --i) {
      if (b[i].y == -1) {
        nmx = max(nmx, b[i].x);
      } else {
        pmx = max(pmx, b[i].x);
      }
    }

    if (cnt == k) {
      if (pmx == -1) {
        for (int i = 0; i < k; ++i) {
          ans = ans * (a[i] + MOD) % MOD;
        }
        cout << ans << endl;
      } else {
        int mn = INF;
        ans = 1;
        for (int i = n - 1; i >= n - k; --i) {
          int x = b[i].x;
          if (b[i].y == -1) {
            x = -x;
            mn = min(mn, b[i].x);
          }
          ans = ans * (x + MOD) % MOD;
        }

        ans = ans * bin_pow((-mn + MOD), MOD - 2) % MOD;
        ans = ans * pmx % MOD;
        cout << ans << endl;
      }
    } else {
      int pmn = INF, nmn = INF;
      for (int i = n - 1; i >= n - k; --i) {
        if (b[i].y == -1) {
          nmn = min(nmn, b[i].x);
        } else {
          pmn = min(pmn, b[i].x);
        }
      }

      if (pmx == -1 && nmx == -1) {
        ans = 1;
        for (int i = 0; i < n; ++i) {
          ans = ans * (a[i] + MOD) % MOD;
        }
        cout << ans << endl;
      } else if ((pmx == -1 || nmn == INF) && (nmx != -1 && pmn != INF)) {
        ans = 1;
        for (int i = n - 1; i >= n - k; --i) {
          int x = b[i].x;
          if (b[i].y == -1)
            x = -x;
          ans = ans * (x + MOD) % MOD;
        }
        ans = ans * bin_pow(pmn, MOD - 2) % MOD;
        ans = ans * (-nmx + MOD) % MOD;
        cout << ans << endl;
      } else if ((nmx == -1 || pmn == INF) && (pmx != -1 && nmn != INF)) {
        ans = 1;
        for (int i = n - 1; i >= n - k; --i) {
          int x = b[i].x;
          if (b[i].y == -1)
            x = -x;
          ans = ans * (x + MOD) % MOD;
        }
        ans = ans * bin_pow((-nmn + MOD), MOD - 2) % MOD;
        ans = ans * pmx % MOD;
        cout << ans << endl;
      } else {
        if (pmx * pmn >= nmx * nmn) {
          ans = 1;
          for (int i = n - 1; i >= n - k; --i) {
            int x = b[i].x;
            if (b[i].y == -1)
              x = -x;
            ans = ans * (x + MOD) % MOD;
          }
          ans = ans * bin_pow((-nmn + MOD), MOD - 2) % MOD;
          ans = ans * pmx % MOD;
          cout << ans << endl;
        } else {
          ans = 1;
          for (int i = n - 1; i >= n - k; --i) {
            int x = b[i].x;
            if (b[i].y == -1)
              x = -x;
            ans = ans * (x + MOD) % MOD;
          }
          ans = ans * bin_pow(pmn, MOD - 2) % MOD;
          ans = ans * (-nmx + MOD) % MOD;
          cout << ans << endl;
        }
      }
    }
  }
}