Problem p03495 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #define int long long
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];

  int limit = 2000000;
  int cnt[limit + 1];
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
  }
  int type = 0;
  int Max = -(1 << 30);
  int maxi = -1;
  vector<int> mini;
  int Min = (1 << 30);
  vector<int> v;
  for (int i = 0; i <= limit; i++) {
    if (cnt[i] != 0) {
      type++;
      v.push_back(i);
    }
    if (Max < cnt[i] && cnt[i] != 0) {
      Max = cnt[i];
      maxi = i;
    }
    if (Min > cnt[i] && cnt[i] != 0 && maxi != i) {
      mini.clear();
      Min = cnt[i];
    }
    if (Min == cnt[i])
      mini.push_back(i);
  }

  int ans = 0;
  vector<int> er;
  while (true) {
    if (type <= k)
      break;
    if (mini.size() != 0) {
      if (Max == 1) {
        ans += v.size() - k;

      } else {
        int tmp = mini[0];
        mini.erase(mini.begin());
        ans += cnt[tmp];
        cnt[maxi] += cnt[tmp];
        cnt[tmp] = 0;
        type--;
      }
    } else {
      Min = (1 << 30);
      for (int i = 0; i < v.size(); i++) {
        if (cnt[v[i]] == 0)
          er.push_back(i);
        if (Min > cnt[v[i]] && cnt[v[i]] != 0 && maxi != v[i]) {
          mini.clear();
          Min = cnt[v[i]];
        }
        if (Min == cnt[v[i]])
          mini.push_back(v[i]);
      }
      for (int i = er.size() - 1; i >= 0; i--) {
        v.erase(v.begin() + er[i]);
      }
      er.clear();
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #define int long long
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int a[n];

  int limit = 2000000;
  int cnt[limit + 1];
  memset(cnt, 0, sizeof(cnt));
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
  }
  int type = 0;
  int Max = -(1 << 30);
  int maxi = -1;
  vector<int> mini;
  int Min = (1 << 30);
  vector<int> v;
  for (int i = 0; i <= limit; i++) {
    if (cnt[i] != 0) {
      type++;
      v.push_back(i);
    }
    if (Max < cnt[i] && cnt[i] != 0) {
      Max = cnt[i];
      maxi = i;
    }
    if (Min > cnt[i] && cnt[i] != 0 && maxi != i) {
      mini.clear();
      Min = cnt[i];
    }
    if (Min == cnt[i])
      mini.push_back(i);
  }

  int ans = 0;
  vector<int> er;
  while (true) {
    if (type <= k)
      break;
    if (mini.size() != 0) {
      if (Max == 1) {
        ans += v.size() - k;
        break;
      } else {
        int tmp = mini[0];
        mini.erase(mini.begin());
        ans += cnt[tmp];
        cnt[maxi] += cnt[tmp];
        cnt[tmp] = 0;
        type--;
      }
    } else {
      Min = (1 << 30);
      for (int i = 0; i < v.size(); i++) {
        if (cnt[v[i]] == 0)
          er.push_back(i);
        if (Min > cnt[v[i]] && cnt[v[i]] != 0 && maxi != v[i]) {
          mini.clear();
          Min = cnt[v[i]];
        }
        if (Min == cnt[v[i]])
          mini.push_back(v[i]);
      }
      for (int i = er.size() - 1; i >= 0; i--) {
        v.erase(v.begin() + er[i]);
      }
      er.clear();
    }
  }
  cout << ans << endl;

  return 0;
}