Problem p02912 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (int i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (int i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (int i = x.size() - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define fst first
#define scd second
#define nextline putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;

int n, m;
map<ll, int> data;

int main() {
  cin >> n >> m;
  REP(i, n) {
    int a;
    cin >> a;
    data[a]++;
  }
  while (m) {
    auto mdata = MAX_ELEMENT(data);
    if (mdata->first == 0) {
      break;
    }
    if (m > mdata->scd) {
      data[mdata->fst / 2] += mdata->scd;
      m -= mdata->scd;
      data.erase(mdata->fst);
    } else {
      data[mdata->fst / 2] += m;
      data[mdata->fst] -= m;
      break;
    }
  }
  ll ans = 0ll;
  for (auto itr = data.begin(); itr != data.end(); itr++) {
    ans += itr->fst * itr->scd;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (int i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (int i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (int i = x.size() - 1; i >= 0; i--)
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define fst first
#define scd second
#define nextline putchar('\n')
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;

int n, m;
map<ll, int> data;

int main() {
  cin >> n >> m;
  REP(i, n) {
    int a;
    cin >> a;
    data[a]++;
  }
  while (m) {
    auto mdata = data.end();
    mdata--;
    if (mdata->fst == 0) {
      break;
    }
    if (m > mdata->scd) {
      data[mdata->fst / 2] += mdata->scd;
      m -= mdata->scd;
      data.erase(mdata->fst);
    } else {
      data[mdata->fst / 2] += m;
      data[mdata->fst] -= m;
      break;
    }
  }
  ll ans = 0ll;
  for (auto itr = data.begin(); itr != data.end(); itr++) {
    ans += itr->fst * itr->scd;
  }
  cout << ans << endl;

  return 0;
}