Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep_i(i, n) for (int i = 0; i < (n); ++i)
#define rep_ll(i, n) for (long long i = 0; i < (n); ++i)
#define r_rep_i(i, start, end) for (int i = (start); i < (end); ++i)
#define r_rep_ll(i, start, end) for (long long i = (start); i < (end); ++i)
#define debug_vi(v)                                                            \
  copy((v).begin(), (v).end(), ostream_iterator<int>(cout, " "));
#define debug_vll(v)                                                           \
  copy((v).begin(), (v).end(), ostream_iterator<long long>(cout, " "));
#define debug_vd(v)                                                            \
  copy((v).begin(), (v).end(), ostream_iterator<double>(cout, " "));
#define sort_v(v) sort((v).begin(), (v).end());                       // 昇順
#define d_sort_vi(v) sort((v).begin(), (v).end(), greater<int>());    // 降
#define d_sort_vd(v) sort((v).begin(), (v).end(), greater<double>()); // 昇順
#define say(t) cout << (t) << endl;
#define sum_vi(v) accumulate((v).begin(), (v).end(), 0);
#define sum_vll(v) accumulate((v).begin(), (v).end(), 0LL);

int main() {
  int n, k;
  // int nums[10];
  vector<int> nums(200000);
  cin >> n >> k;
  int tmp;
  // rep_i(i, 10){
  //   cout << nums[i] << endl;
  // }
  // cout << endl;
  rep_i(i, n) {
    cin >> tmp;
    nums[tmp - 1] += 1;
  }
  auto result =
      remove_if(nums.begin(), nums.end(), [](int x) { return x == 0; });
  nums.erase(result, nums.end());
  // rep_i(i, 10){
  //   cout << nums[i] << endl;
  // }
  int count = 0;
  sort_v(nums);
  // debug_vi(nums);
  // cout << endl;
  r_rep_i(i, 0, nums.size() - k) { count += nums[i]; }
  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep_i(i, n) for (int i = 0; i < (n); ++i)
#define rep_ll(i, n) for (long long i = 0; i < (n); ++i)
#define r_rep_i(i, start, end) for (int i = (start); i < (end); ++i)
#define r_rep_ll(i, start, end) for (long long i = (start); i < (end); ++i)
#define debug_vi(v)                                                            \
  copy((v).begin(), (v).end(), ostream_iterator<int>(cout, " "));
#define debug_vll(v)                                                           \
  copy((v).begin(), (v).end(), ostream_iterator<long long>(cout, " "));
#define debug_vd(v)                                                            \
  copy((v).begin(), (v).end(), ostream_iterator<double>(cout, " "));
#define sort_v(v) sort((v).begin(), (v).end());                       // 昇順
#define d_sort_vi(v) sort((v).begin(), (v).end(), greater<int>());    // 降
#define d_sort_vd(v) sort((v).begin(), (v).end(), greater<double>()); // 昇順
#define say(t) cout << (t) << endl;
#define sum_vi(v) accumulate((v).begin(), (v).end(), 0);
#define sum_vll(v) accumulate((v).begin(), (v).end(), 0LL);

int main() {
  int n, k;
  // int nums[10];
  vector<int> nums(200000);
  cin >> n >> k;
  int tmp;
  // rep_i(i, 10){
  //   cout << nums[i] << endl;
  // }
  // cout << endl;
  rep_i(i, n) {
    cin >> tmp;
    nums[tmp - 1] += 1;
  }
  auto result =
      remove_if(nums.begin(), nums.end(), [](int x) { return x == 0; });
  nums.erase(result, nums.end());
  // rep_i(i, 10){
  //   cout << nums[i] << endl;
  // }
  int count = 0;
  sort_v(nums);
  // debug_vi(nums);
  // cout << endl;
  // cout << endl;
  // cout << nums.size() << endl;
  // cout << endl;
  // cout << (int)nums.size() - k << endl;

  if ((int)nums.size() - k <= 0) {
    cout << count << endl;
    return 0;
  }
  r_rep_i(i, 0, (int)nums.size() - k) { count += nums[i]; }
  cout << count << endl;
}