Problem p02912 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(a) int((a).size())
#define pb push_back
#define eb emplace_back
#define FOR(var, from, to) for (int var = (from); var <= (to); ++var)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define rep1(var, n) for (int var = 1; var <= (n); ++var)
#define repC2(vari, varj, n)                                                   \
  for (int vari = 0; vari < (n)-1; ++vari)                                     \
    for (int varj = vari + 1; varj < (n); ++varj)
#define repC3(vari, varj, vark, n)                                             \
  for (int vari = 0; vari < (n)-2; ++vari)                                     \
    for (int varj = vari + 1; varj < (n)-1; ++varj)                            \
      for (int vark = varj + 1; vark < (n); ++vark)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define tr(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define mset(arr, val) memset(arr, val, sizeof(arr))
#define mid(x, y) ((x) + ((y) - (x)) / 2)
#define IN(x, a, b) ((a) <= (x) && (x) <= (b))
using namespace std;
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
  if (f)
    Out(y);
  else
    Out(n);
}

int main() {
  int B, C, D, N, M, max;
  ll ans;
  cin >> N >> M;
  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  if (N == 1) {
    rep(i, M) {
      if (A[0] <= 1) {
        Out("0");
        return 0;
      }
      A[0] /= 2;
    }
    Out(A[0]);
    return 0;
  }
  for (int j = 0; 0 < M; j++) {

    sort(RALL(A));

    max = A[0];
    for (int i = 0; i < N, 0 < M; i++) {
      if (A[i] < (max / 2))
        break;
      A[i] /= 2;

      M--;
    }
  }
  ans = 0;
  rep(i, N) { ans += A[i]; }
  Out(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(a) int((a).size())
#define pb push_back
#define eb emplace_back
#define FOR(var, from, to) for (int var = (from); var <= (to); ++var)
#define rep(var, n) for (int var = 0; var < (n); ++var)
#define rep1(var, n) for (int var = 1; var <= (n); ++var)
#define repC2(vari, varj, n)                                                   \
  for (int vari = 0; vari < (n)-1; ++vari)                                     \
    for (int varj = vari + 1; varj < (n); ++varj)
#define repC3(vari, varj, vark, n)                                             \
  for (int vari = 0; vari < (n)-2; ++vari)                                     \
    for (int varj = vari + 1; varj < (n)-1; ++varj)                            \
      for (int vark = varj + 1; vark < (n); ++vark)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define tr(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define mset(arr, val) memset(arr, val, sizeof(arr))
#define mid(x, y) ((x) + ((y) - (x)) / 2)
#define IN(x, a, b) ((a) <= (x) && (x) <= (b))
using namespace std;
template <typename T> void Out(T x) { cout << x << endl; }
template <typename T1, typename T2> void Ans(bool f, T1 y, T2 n) {
  if (f)
    Out(y);
  else
    Out(n);
}

int main() {
  int B, C, D, N, M, max;
  ll ans;
  cin >> N >> M;
  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  if (N == 1) {
    rep(i, M) {
      if (A[0] <= 1) {
        Out("0");
        return 0;
      }
      A[0] /= 2;
    }
    Out(A[0]);
    return 0;
  }
  for (int j = 0; 0 < M; j++) {

    sort(RALL(A));

    max = A[0];
    for (int i = 0; (i < N && 0 < M); i++) {
      if (A[i] < (max / 2))
        break;
      A[i] /= 2;

      M--;
    }
  }
  ans = 0;
  rep(i, N) { ans += A[i]; }
  Out(ans);
  return 0;
}