Problem p02912 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define int long long
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define itr(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
#define All(x) (x).begin(), (x).end()
#define rAll(x) (x).rbegin(), (x).rend()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int mod = 1000000007;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x * y / gcd(x, y); }
int roundup(int a, int b) { return (a - 1) / b + 1; }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }
struct rational {
  long long p, q;
  long long gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
  void normalize() { // keep q positive
    if (q < 0)
      p *= -1, q *= -1;
    long long d = gcd(p < 0 ? -p : p, q);
    if (d == 0)
      p = 0, q = 1;
    else
      p /= d, q /= d;
  }
  rational(long long p, long long q = 1) : p(p), q(q) { normalize(); }
  rational &operator+=(const rational &a) {
    p = a.q * p + a.p * q;
    q = a.q * q;
    normalize();
    return *this;
  }
  rational &operator-=(const rational &a) {
    p = a.q * p - a.p * q;
    q = a.q * q;
    normalize();
    return *this;
  }
  rational &operator*=(const rational &a) {
    p *= a.p;
    q *= a.q;
    normalize();
    return *this;
  }
  rational &operator/=(const rational &a) {
    p *= a.q;
    q *= a.p;
    normalize();
    return *this;
  }
  rational &operator-() {
    p *= -1;
    return *this;
  }
  friend rational operator+(const rational &a, const rational &b) {
    return rational(a) += b;
  }
  friend rational operator*(const rational &a, const rational &b) {
    return rational(a) *= b;
  }
  friend rational operator-(const rational &a, const rational &b) {
    return rational(a) -= b;
  }
  friend rational operator/(const rational &a, const rational &b) {
    return rational(a) /= b;
  }
  friend bool operator<(const rational &a,
                        const rational &b) { // avoid overflow
    return (long double)a.p * b.q < (long double)a.q * b.p;
  }
  friend bool operator<=(const rational &a, const rational &b) {
    return !(b < a);
  }
  friend bool operator>(const rational &a, const rational &b) { return b < a; }
  friend bool operator>=(const rational &a, const rational &b) {
    return !(a < b);
  }
  friend bool operator==(const rational &a, const rational &b) {
    return !(a < b) && !(b < a);
  }
  friend bool operator!=(const rational &a, const rational &b) {
    return (a < b) || (b < a);
  }
  friend std::ostream &operator<<(std::ostream &os, const rational &x) {
    printf("%.16f", (double)x.p / (double)x.q);
    return os;
  }
  friend std::istream &operator>>(std::istream &is, rational &x) {
    is >> x.p >> x.q;
    x.normalize();
    return is;
  }
};
signed main() {
  int n, m;
  cin >> n >> m;
  multiset<rational, greater<rational>> ms;
  rep(i, n) {
    int a;
    cin >> a;
    ms.insert(rational(a, 1));
  }
  rep(i, m) {
    rational tar = *ms.begin();
    ms.erase(ms.begin());
    tar /= rational(2, 1);
    ms.insert(tar);
  }
  int ans = 0;
  for (auto c : ms)
    ans += c.p / c.q;
  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#define int long long
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define itr(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
#define All(x) (x).begin(), (x).end()
#define rAll(x) (x).rbegin(), (x).rend()
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
const int mod = 1000000007;
int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
int lcm(int x, int y) { return x * y / gcd(x, y); }
int roundup(int a, int b) { return (a - 1) / b + 1; }
void YN(bool flg) { cout << (flg ? "YES" : "NO") << endl; }
void Yn(bool flg) { cout << (flg ? "Yes" : "No") << endl; }
void yn(bool flg) { cout << (flg ? "yes" : "no") << endl; }
struct rational {
  long long p, q;
  long long gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
  void normalize() { // keep q positive
    if (q < 0)
      p *= -1, q *= -1;
    long long d = gcd(p < 0 ? -p : p, q);
    if (d == 0)
      p = 0, q = 1;
    else
      p /= d, q /= d;
  }
  rational(long long p, long long q = 1) : p(p), q(q) { normalize(); }
  rational &operator+=(const rational &a) {
    p = a.q * p + a.p * q;
    q = a.q * q;
    normalize();
    return *this;
  }
  rational &operator-=(const rational &a) {
    p = a.q * p - a.p * q;
    q = a.q * q;
    normalize();
    return *this;
  }
  rational &operator*=(const rational &a) {
    p *= a.p;
    q *= a.q;
    normalize();
    return *this;
  }
  rational &operator/=(const rational &a) {
    p *= a.q;
    q *= a.p;
    normalize();
    return *this;
  }
  rational &operator-() {
    p *= -1;
    return *this;
  }
  friend rational operator+(const rational &a, const rational &b) {
    return rational(a) += b;
  }
  friend rational operator*(const rational &a, const rational &b) {
    return rational(a) *= b;
  }
  friend rational operator-(const rational &a, const rational &b) {
    return rational(a) -= b;
  }
  friend rational operator/(const rational &a, const rational &b) {
    return rational(a) /= b;
  }
  friend bool operator<(const rational &a,
                        const rational &b) { // avoid overflow
    return (long double)a.p * b.q < (long double)a.q * b.p;
  }
  friend bool operator<=(const rational &a, const rational &b) {
    return !(b < a);
  }
  friend bool operator>(const rational &a, const rational &b) { return b < a; }
  friend bool operator>=(const rational &a, const rational &b) {
    return !(a < b);
  }
  friend bool operator==(const rational &a, const rational &b) {
    return !(a < b) && !(b < a);
  }
  friend bool operator!=(const rational &a, const rational &b) {
    return (a < b) || (b < a);
  }
  friend std::ostream &operator<<(std::ostream &os, const rational &x) {
    printf("%.16f", (double)x.p / (double)x.q);
    return os;
  }
  friend std::istream &operator>>(std::istream &is, rational &x) {
    is >> x.p >> x.q;
    x.normalize();
    return is;
  }
};
signed main() {
  int n, m;
  cin >> n >> m;
  multiset<rational, greater<rational>> ms;
  rep(i, n) {
    int a;
    cin >> a;
    ms.insert(rational(a, 1));
  }
  rep(i, m) {
    rational tar = *ms.begin();
    ms.erase(ms.begin());
    if (tar.p / tar.q < 1)
      break;
    tar /= rational(2, 1);
    ms.insert(tar);
  }
  int ans = 0;
  for (auto c : ms)
    ans += c.p / c.q;
  cout << ans << endl;
}