prithivMLmods/Coder-Stat huggingface dataset:
Problem p03495 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  map<int, int> m;
  int N, K;
  cin >> N >> K;
  REP(i, N) {
    int x;
    cin >> x;
    if (m.count(x)) {
      m.at(x)++;
    } else {
      m.emplace(x, 1);
    }
  }
  vector<int> v(m.size());
  int y = 0;
  for (auto i = m.begin(); i != m.end(); i++) {
    v.at(y) = (*i).second;
    y++;
  }
  int ans = 0;
  int sized = v.size();
  REP(i, sized - K) {
    ans += *min_element(v.begin(), v.end());
    v.erase(min_element(v.begin(), v.end()));
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  map<int, int> m;
  int N, K;
  cin >> N >> K;
  REP(i, N) {
    int x;
    cin >> x;
    if (m.count(x)) {
      m.at(x)++;
    } else {
      m.emplace(x, 1);
    }
  }
  vector<int> v(m.size());
  int y = 0;
  for (auto i = m.begin(); i != m.end(); i++) {
    v.at(y) = (*i).second;
    y++;
  }
  int ans = 0;
  int sized = v.size();
  sort(v.begin(), v.end());
  REP(i, sized - K) { ans += v.at(i); }
  cout << ans << "\n";
}
Problem p03495 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
#define print(x) cout << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a) for (int i = 0; i < a; i++)
#define printall(n, array)                                                     \
  {                                                                            \
    for (int i = 0; i < n; i++) {                                              \
      cout << array[i] << " ";                                                 \
    }                                                                          \
    cout << endl;                                                              \
  }
#define U() cout << endl;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 10000000007; // 10^9+7

int k, n;
VE a;
int cnt[200002];

int main() {
  cin >> n >> k;
  int temp;
  int max_ = 0;
  REP(i, n) {
    cin >> temp;
    a.push_back(temp);
    max_ = max(max_, temp);
  }
  REP(i, max_ + 1) { cnt[i] = count(a.begin(), a.end(), i); }
  // REP(i,max_+1)print(cnt[i]);///
  sort(cnt, cnt + max_ + 1, std::greater<int>());
  int K = 0;
  int ans;
  REP(i, max_ + 1) {
    if (cnt[i] != 0) {
      if (K >= k) {
        // print("i "<<i<<" "<<cnt[i]);///
        ans += cnt[i];
      } else {
        K++;
      }
    }
  }
  print(ans);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define print(x) cout << x << endl;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a) for (int i = 0; i < a; i++)
#define printall(n, array)                                                     \
  {                                                                            \
    for (int i = 0; i < n; i++) {                                              \
      cout << array[i] << " ";                                                 \
    }                                                                          \
    cout << endl;                                                              \
  }
#define U() cout << endl;
typedef long long ll;
typedef pair<int, int> PI;
typedef pair<int, PI> V;
typedef vector<int> VE;
const ll mod = 10000000007; // 10^9+7

int k, n;
VE a;
int cnt[200002];

int main() {
  cin >> n >> k;
  int temp;
  int max_ = 0;
  REP(i, n) {
    cin >> temp;
    a.push_back(temp);
    max_ = max(max_, temp);
  }
  REP(i, a.size()) { cnt[a[i]]++; }
  // REP(i,max_+1)print(cnt[i]);///
  sort(cnt, cnt + max_ + 1, std::greater<int>());
  int K = 0;
  int ans;
  REP(i, max_ + 1) {
    if (cnt[i] != 0) {
      if (K >= k) {
        // print("i "<<i<<" "<<cnt[i]);///
        ans += cnt[i];
      } else {
        K++;
      }
    }
  }
  print(ans);
}
Problem p03495 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
typedef unsigned long long ull;
typedef long long ll;
#define MAX 200005
using namespace std;
int A[MAX] = {};
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A[a]++;
  }
  vector<int> S;
  for (int i = 1; i <= MAX; i++) {
    if (A[i] != 0) {
      S.push_back(A[i]);
    }
  }
  sort(S.begin(), S.end());

  int count = S.size(), j = 0, max = 0;
  while (count > k) {
    count -= 1;
    max += S[j++];
  }
  cout << max << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
typedef unsigned long long ull;
typedef long long ll;
#define MAX 200005
using namespace std;
int A[MAX] = {};
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    A[a]++;
  }
  vector<int> S;
  for (int i = 1; i <= 200000; i++) {
    if (A[i] != 0) {
      S.push_back(A[i]);
    }
  }
  sort(S.begin(), S.end());

  int count = S.size(), j = 0, max = 0;
  while (count > k) {
    count -= 1;
    max += S[j++];
  }
  cout << max << endl;

  return 0;
}
Problem p03495 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main(void) {
  int N, K;
  int A[200010];
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // 整数の個数リストを作って，その上からK個を取って，その個数の和を求める。
  // N個からその個数の和を引けば良い。
  int integer_num[200010] = {0};
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= N; j++) {
      if (A[i] == j) {
        integer_num[j]++;
        break;
      }
    }
  }
  sort(integer_num, integer_num + N + 1, greater<int>());
  int sumK = 0;
  for (int i = 0; i < K; i++) {
    sumK += integer_num[i];
  }
  cout << N - sumK << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main(void) {
  int N, K;
  int A[200010];
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  // 整数の個数リストを作って，その上からK個を取って，その個数の和を求める。
  // N個からその個数の和を引けば良い。
  int integer_num[200010] = {0};
  for (int i = 0; i < N; i++) {
    integer_num[A[i]]++;
  }
  sort(integer_num, integer_num + N + 1, greater<int>());
  int sumK = 0;
  for (int i = 0; i < K; i++) {
    sumK += integer_num[i];
  }
  cout << N - sumK << endl;
  return 0;
}
Problem p03495 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  multiset<int> A;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    A.insert(t);
  }
  set<pair<int, int>> C;
  for (auto it = A.begin(); it != A.end(); ++it) {
    C.insert(make_pair(A.count(*it), *it));
  }
  int c = 0;
  while (C.size() > K) {
    c += (*C.begin()).first;
    C.erase(*C.begin());
  }
  cout << c << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <list>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  multiset<int> A;
  for (int i = 0; i < N; i++) {
    int t;
    cin >> t;
    A.insert(t);
  }
  set<pair<int, int>> C;
  while (A.size() > 0) {
    C.insert(make_pair(A.count(*A.begin()), *A.begin()));
    A.erase(*A.begin());
  }
  int c = 0;
  while (C.size() > K) {
    c += (*C.begin()).first;
    C.erase(*C.begin());
  }
  cout << c << endl;
}
Problem p03495 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> count(N);
  for (int i = 0; i < N; i++) {
    int sum = 0;
    for (int j = 0; j < N; j++) {
      if (i == A.at(j) - 1) {
        sum++;
      }
    }
    count.at(i) = sum;
  }
  sort(count.begin(), count.end());
  reverse(count.begin(), count.end());
  int ans = 0;
  for (int i = K; i < N; i++) {
    ans += count.at(i);
  }
  cout << ans << endl;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> count(N);
  for (int i = 0; i < N; i++) {
    count.at(A.at(i) - 1)++;
  }
  sort(count.begin(), count.end());
  reverse(count.begin(), count.end());
  int ans = 0;
  for (int i = K; i < N; i++) {
    ans += count.at(i);
  }
  cout << ans << endl;
}
Problem p03495 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int ma[100000];
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ma[x]++;
  }
  sort(ma, ma + n + 1);
  int ans = 0;
  for (int i = 0; i <= n - k; i++) {
    ans += ma[i];
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int ma[325000];
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ma[x]++;
  }
  sort(ma, ma + n + 1);
  int ans = 0;
  for (int i = 0; i <= n - k; i++) {
    ans += ma[i];
  }
  cout << ans;
  return 0;
}
Problem p03496 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int n;
int a[60];
int Max = 0;

void setMax() {
  for (int i = 1; i < n; i++) {
    if (abs(a[i]) > abs(a[Max])) {
      Max = i;
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  setMax();

  // 全要素に足す
  int ans[2][60];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    ans[0][cnt] = Max + 1, ans[1][cnt] = i + 1;
    cnt++;
    a[i] += a[Max];
  }

  // 累積和
  if (a[Max] >= 0) {
    for (int i = 1; i < n; i++) {
      a[i] += a[i] + a[i - 1];
      ans[0][cnt] = i, ans[1][cnt] = i + 1;
      cnt++;
    }
  } else {
    for (int i = n - 2; i >= 0; i--) {
      a[i] += a[i] + a[i + 1];
      ans[0][cnt] = i + 2, ans[1][cnt] = i + 1;
      cnt++;
    }
  }

  cout << cnt << endl;
  for (int i = 0; i < cnt; i++) {
    cout << ans[0][i] << " " << ans[1][i] << endl;
  }

  // for (int i = 0; i < n; i++) {
  //    cout << a[i] << " ";
  // }
  // cout << endl;

  return 0;
}

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

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int n;
int a[60];
int Max = 0;

void setMax() {
  for (int i = 1; i < n; i++) {
    if (abs(a[i]) > abs(a[Max])) {
      Max = i;
    }
  }
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  setMax();

  // 全要素に足す
  int ans[2][109];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    ans[0][cnt] = Max + 1, ans[1][cnt] = i + 1;
    cnt++;
    a[i] += a[Max];
  }

  // 累積和
  if (a[Max] >= 0) {
    for (int i = 1; i < n; i++) {
      a[i] += a[i] + a[i - 1];
      ans[0][cnt] = i, ans[1][cnt] = i + 1;
      cnt++;
    }
  } else {
    for (int i = n - 2; i >= 0; i--) {
      a[i] += a[i] + a[i + 1];
      ans[0][cnt] = i + 2, ans[1][cnt] = i + 1;
      cnt++;
    }
  }

  cout << cnt << endl;
  for (int i = 0; i < cnt; i++) {
    cout << ans[0][i] << " " << ans[1][i] << endl;
  }

  // for (int i = 0; i < n; i++) {
  //    cout << a[i] << " ";
  // }
  // cout << endl;

  return 0;
}
Problem p03496 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;

int a[55];

int N;

int main() {
  cin >> N;

  bool plus = true;
  bool minus = true;
  a[54] = 1000000;
  a[0] = -10000000;
  int mai = 0;
  int mii = 54;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    if (a[i] > 0) {
      minus = false;
    }
    if (a[i] < 0) {
      plus = false;
    }

    if (a[mai] < a[i])
      mai = i;
    if (a[mii] > a[i])
      mii = i;
  }

  stringstream res;
  int cou = 0;
  if (plus == false && minus == false) {
    if (abs(a[mai]) >= abs(a[mii])) {
      for (int i = 1; i <= N; i++) {
        res << mai << " " << i << endl;
        cou++;
      }
      plus = true;
    } else {

      for (int i = 1; i <= N; i++) {
        res << mii << " " << i << endl;
        cou++;
      }
      minus = true;
    }
  }

  if (minus) {
    for (int i = N - 1; i >= 1; i++) {
      cou++;
      res << i + 1 << " " << i << endl;
    }

    cout << cou << endl;
    cout << res.str();
    return 0;
  }

  if (plus) {
    for (int i = 1; i < N; i++) {
      cou++;
      res << i << " " << i + 1 << endl;
    }
    cout << cou << endl;
    cout << res.str();
    return 0;
  }
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;

int a[55];

int N;

int main() {
  cin >> N;

  bool plus = true;
  bool minus = true;
  a[54] = 1000000;
  a[0] = -10000000;
  int mai = 0;
  int mii = 54;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    if (a[i] > 0) {
      minus = false;
    }
    if (a[i] < 0) {
      plus = false;
    }

    if (a[mai] < a[i])
      mai = i;
    if (a[mii] > a[i])
      mii = i;
  }

  stringstream res;
  int cou = 0;
  if (plus == false && minus == false) {
    if (abs(a[mai]) >= abs(a[mii])) {
      for (int i = 1; i <= N; i++) {
        res << mai << " " << i << endl;
        cou++;
      }
      plus = true;
    } else {

      for (int i = 1; i <= N; i++) {
        res << mii << " " << i << endl;
        cou++;
      }
      minus = true;
    }
  }

  if (minus) {
    for (int i = N - 1; i >= 1; i--) {
      cou++;
      res << i + 1 << " " << i << endl;
    }

    cout << cou << endl;
    cout << res.str();
    return 0;
  }

  if (plus) {
    for (int i = 1; i < N; i++) {
      cou++;
      res << i << " " << i + 1 << endl;
    }
    cout << cou << endl;
    cout << res.str();
    return 0;
  }
}
Problem p03496 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int n;
long long a[100];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int mi = 0;
  int ma = 0;
  for (int i = 0; i < n; i++) {
    if (abs(a[i]) > ma) {
      mi = i;
      ma = abs(a[i]);
    }
  }
  cout << (2 * n - 2) << endl;
  for (int i = 0; i < n; i++)
    if (mi != i) {
      cout << (mi + 1) << " " << (i + 1) << endl;
    }
  if (a[mi] > 0) {
    for (int i = 0; i < n - 1; i++) {
      cout << (i + 1) << " " << (i + 2) << endl;
    }
  } else {
    for (int i = n - 1; i > 0; i++) {
      cout << (i + 1) << " " << (i) << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

int n;
long long a[100];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int mi = 0;
  int ma = 0;
  for (int i = 0; i < n; i++) {
    if (abs(a[i]) > ma) {
      mi = i;
      ma = abs(a[i]);
    }
  }
  cout << (2 * n - 2) << endl;
  for (int i = 0; i < n; i++)
    if (mi != i) {
      cout << (mi + 1) << " " << (i + 1) << endl;
    }
  if (a[mi] > 0) {
    for (int i = 0; i < n - 1; i++) {
      cout << (i + 1) << " " << (i + 2) << endl;
    }
  } else {
    for (int i = n - 1; i > 0; i--) {
      cout << (i + 1) << " " << (i) << endl;
    }
  }

  return 0;
}
Problem p03496 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];

  ll val = 0;
  int pos = 0;
  rep(i, n) {
    if (abs(val) < abs(v[i])) {
      val = v[i];
      pos = i;
    }
  }

  using P = pair<int, int>;
  vector<P> ans;

  rep(i, n) {
    if (val > 0 && v[i] < 0) {
      v[i] += val;
      ans.push_back({pos, i});
    }
    if (val < 0 && v[i] > 0) {
      v[i] += val;
      ans.push_back({pos, i});
    }
  }

  if (val >= 0) {
    rep(i, n - 1) ans.push_back({i, i + 1});
  } else {
    for (int i = 1; i < n; i--)
      ans.push_back({i, i - 1});
  }

  cout << ans.size() << endl;
  rep(i, ans.size()) cout << ans[i].first + 1 << " " << ans[i].second + 1
                          << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const ll INF = 1e18;
const ll MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];

  ll val = 0;
  int pos = 0;
  rep(i, n) {
    if (abs(val) < abs(v[i])) {
      val = v[i];
      pos = i;
    }
  }

  using P = pair<int, int>;
  vector<P> ans;

  rep(i, n) {
    if (val > 0 && v[i] < 0) {
      v[i] += val;
      ans.push_back({pos, i});
    }
    if (val < 0 && v[i] > 0) {
      v[i] += val;
      ans.push_back({pos, i});
    }
  }

  if (val >= 0) {
    rep(i, n - 1) ans.push_back({i, i + 1});
  } else {
    for (int i = n - 1; i >= 1; i--)
      ans.push_back({i, i - 1});
  }

  cout << ans.size() << endl;
  rep(i, ans.size()) cout << ans[i].first + 1 << " " << ans[i].second + 1
                          << endl;
}
Problem p03496 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define int long long
using namespace std;

typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

signed main() {

  int N;
  cin >> N;
  vector<int> A(N);
  readvec(A);

  int absmax = -1, sign = 0, ind = -1;

  REP(i, N) {
    if (llabs(A[i]) > absmax) {
      absmax = llabs(A[i]);
      sign = absmax / A[i];
      ind = i;
    }
  }

  cout << 2 * N - 1 << endl;
  // Aの符号を全てsignにする
  REP(i, N) { cout << ind + 1 << " " << i + 1 << endl; }
  if (sign == 1) {
    REP(i, N - 1) {
      cout << i + 1 << " " << i + 2;
      if (i != N - 2)
        cout << endl;
    }
  } else {
    REP(i, N - 1) {
      cout << N - i << " " << N - i - 1;
      if (i != N - 2)
        cout << endl;
    }
  }

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, begin, end) for (int i = (begin); i < (end); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(a) sort(a.begin(), a.end())
#define int long long
using namespace std;

typedef pair<int, int> Pii;

template <typename T> void readvec(vector<T> &a);
void readindex(vector<int> &a);

signed main() {

  int N;
  cin >> N;
  vector<int> A(N);
  readvec(A);

  int absmax = 0, sign = 1, ind = 0;

  REP(i, N) {
    if (llabs(A[i]) > absmax) {
      absmax = llabs(A[i]);
      sign = absmax / A[i];
      ind = i;
    }
  }

  cout << 2 * N - 1 << endl;
  // Aの符号を全てsignにする
  REP(i, N) { cout << ind + 1 << " " << i + 1 << endl; }
  if (sign == 1) {
    REP(i, N - 1) {
      cout << i + 1 << " " << i + 2;
      if (i != N - 2)
        cout << endl;
    }
  } else {
    REP(i, N - 1) {
      cout << N - i << " " << N - i - 1;
      if (i != N - 2)
        cout << endl;
    }
  }

  return 0;
}

template <typename T> void readvec(vector<T> &a) {
  REP(i, a.size()) { cin >> a[i]; }
}
void readindex(vector<int> &a) {
  REP(i, a.size()) {
    cin >> a[i];
    a[i]--;
  }
}
Problem p03496 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int id = -1;
  ll mx = 0;
  bool p = true;
  rep(i, n) {
    if (abs(a[i]) > mx) {
      mx = abs(a[i]);
      id = i;
      if (a[i] > 0)
        p = true;
      else
        p = false;
    }
  }
  if (id == -1) {
    cout << 0 << endl;
    return 0;
  }
  vector<P> ans;
  if (p) {
    rep(i, n - 1) {
      if (a[i] <= a[i + 1])
        continue;
      else {
        while (a[i] > a[i + 1]) {
          a[i + 1] += mx;
          ans.push_back(P(id, i + 1));
        }
        if (a[i + 1] > mx) {
          mx = a[i + 1];
          id = i + 1;
        }
      }
    }
  } else {
    for (int i = n - 2; i >= 0; i--) {
      if (a[i] <= a[i + 1])
        continue;
      else {
        while (a[i] > a[i + 1]) {
          a[i] -= mx;
          ans.push_back(P(id, i));
        }
        if (a[i] < mx) {
          mx = a[i];
          id = i;
        }
      }
    }
  }
  cout << (int)(ans.size()) << endl;
  rep(i, ans.size()) cout << (ans[i].first + 1) << " " << (ans[i].second + 1)
                          << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  int id = -1;
  ll mx = 0;
  bool p = true;
  rep(i, n) {
    if (abs(a[i]) > mx) {
      mx = abs(a[i]);
      id = i;
      if (a[i] > 0)
        p = true;
      else
        p = false;
    }
  }
  if (id == -1) {
    cout << 0 << endl;
    return 0;
  }
  vector<P> ans;
  if (p) {
    rep(i, n - 1) {
      if (a[i] <= a[i + 1])
        continue;
      else {
        while (a[i] > a[i + 1]) {
          a[i + 1] += mx;
          ans.push_back(P(id, i + 1));
        }
        if (a[i + 1] > mx) {
          mx = a[i + 1];
          id = i + 1;
        }
      }
    }
  } else {
    for (int i = n - 2; i >= 0; i--) {
      if (a[i] <= a[i + 1])
        continue;
      else {
        while (a[i] > a[i + 1]) {
          a[i] -= mx;
          ans.push_back(P(id, i));
        }
        if (a[i] < 0 && -a[i] > mx) {
          mx = -a[i];
          id = i;
        }
      }
    }
  }
  cout << (int)(ans.size()) << endl;
  rep(i, ans.size()) cout << (ans[i].first + 1) << " " << (ans[i].second + 1)
                          << endl;
  return 0;
}
Problem p03496 (C++)

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

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define mem(x, val) memset((x), (val), sizeof(x))
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define PI acos(-1.0)
#define MAXN 52

const int INF = 1 << 29;
typedef long long ll;
typedef pair<int, int> pii;

int a[MAXN];

int main() {
  int n, hi = -100000000, lo = 10000000, id1, id2;
  scanf("%d", &n);
  vector<pii> ans;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > hi) {
      hi = a[i];
      id1 = i + 1;
    }
    if (a[i] < lo) {
      lo = a[i];
      id2 = i + 1;
    }
  }

  if (abs(hi) >= abs(lo)) {
    for (int i = 1; i <= n; i++)
      ans.pb(mp(id1, i));
    for (int i = 1; i <= n - 1; i++)
      ans.pb(mp(i, i + 1));

  } else {
    for (int i = 1; i <= n; i++)
      ans.pb(mp(id2, i));
    for (int i = n - 1; i >= 1; i++)
      ans.pb(mp(i + 1, i));
  }

  id1 = 2 * n - 1;
  printf("%d\n", id1);
  for (int i = 0; i < id1; i++)
    printf("%d %d\n", ans[i].first, ans[i].second);

  return 0;
}

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

#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define mem(x, val) memset((x), (val), sizeof(x))
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define PI acos(-1.0)
#define MAXN 52

const int INF = 1 << 29;
typedef long long ll;
typedef pair<int, int> pii;

int a[MAXN];

int main() {
  int n, hi = -100000000, lo = 10000000, id1, id2;
  scanf("%d", &n);
  vector<pii> ans;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > hi) {
      hi = a[i];
      id1 = i + 1;
    }
    if (a[i] < lo) {
      lo = a[i];
      id2 = i + 1;
    }
  }

  if (abs(hi) >= abs(lo)) {
    for (int i = 1; i <= n; i++)
      ans.pb(mp(id1, i));
    for (int i = 1; i <= n - 1; i++)
      ans.pb(mp(i, i + 1));

  } else {
    for (int i = 1; i <= n; i++)
      ans.pb(mp(id2, i));
    for (int i = n - 1; i >= 1; i--)
      ans.pb(mp(i + 1, i));
  }

  id1 = 2 * n - 1;
  printf("%d\n", id1);
  for (int i = 0; i < id1; i++)
    printf("%d %d\n", ans[i].first, ans[i].second);

  return 0;
}
Problem p03496 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max_index = 0;
  for (int i = 1; i < n; i++) {
    if (abs(a[i]) > abs(a[max_index])) {
      max_index = i;
    }
  }
  cout << 2 * n - 1 << endl;
  for (int i = 0; i < n; i++) {
    cout << max_index + 1 << " " << i + 1 << endl;
  }
  if (a[max_index] >= 0) {
    for (int i = 0; i < n - 1; i++) {
      cout << i + 1 << " " << i + 2 << endl;
    }
  } else {
    for (int i = n; i > 1; i++) {
      cout << i << " " << i - 1 << endl;
    }
  }
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int max_index = 0;
  for (int i = 1; i < n; i++) {
    if (abs(a[i]) > abs(a[max_index])) {
      max_index = i;
    }
  }
  cout << 2 * n - 1 << endl;
  for (int i = 0; i < n; i++) {
    cout << max_index + 1 << " " << i + 1 << endl;
  }
  if (a[max_index] >= 0) {
    for (int i = 0; i < n - 1; i++) {
      cout << i + 1 << " " << i + 2 << endl;
    }
  } else {
    for (int i = n; i > 1; i--) {
      cout << i << " " << i - 1 << endl;
    }
  }
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <vector>
using namespace std;
#define f(i, n) for (int i = 0; i < (int)n; i++)
#define MOD 1000000009

int main(void) {
  int a[200000];
  f(i, 200000) a[i] = 0;
  vector<int> b;
  int k;
  int n;
  int x;
  int ans = 0;

  scanf("%d %d", &n, &k);
  f(i, n) {
    scanf("%d", &x);
    a[x - 1]++;
  }

  f(i, n) {
    if (a[i] > 0)
      b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i < b.size() - k; i++) {
    ans += b[i];
  }

  printf("%d\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <vector>
using namespace std;
#define f(i, n) for (int i = 0; i < (int)n; i++)
#define MOD 1000000009

int main(void) {
  int a[200000];
  f(i, 200000) a[i] = 0;
  vector<int> b;
  int k;
  int n;
  int x;
  int ans = 0;

  scanf("%d %d", &n, &k);
  f(i, n) {
    scanf("%d", &x);
    a[x - 1]++;
  }

  f(i, n) {
    if (a[i] > 0)
      b.push_back(a[i]);
  }
  sort(b.begin(), b.end());
  for (int i = 0; i + k < b.size(); i++) {

    ans += b[i];
  }

  printf("%d\n", ans);

  return 0;
}
Problem p03497 (C++)

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

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  // val,count
  map<int, int> mp;
  int N, K;
  cin >> N >> K;
  REP(i, N) {
    int A;
    cin >> A;
    if (mp.count(A) == 0)
      mp[A] = 0;
    mp[A]++;
  }

  // count
  vector<int> v;
  for (const auto &itr : mp) {
    v.push_back(itr.second);
  }

  sort(ALL(v));

  int ans = 0;
  while (v.size() > K) {
    ans += v.front();
    v.erase(v.begin());
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  // val,count
  map<int, int> mp;
  int N, K;
  cin >> N >> K;
  REP(i, N) {
    int A;
    cin >> A;
    if (mp.count(A) == 0)
      mp[A] = 0;
    mp[A]++;
  }

  // count
  vector<int> v;
  for (const auto &itr : mp) {
    v.push_back(itr.second);
  }

  sort(ALL(v));

  int ans = 0;
  int i = 0;
  while (v.size() - i > K) {
    ans += v[i];
    i++;
  }

  cout << ans << endl;

  return 0;
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
// C.

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef pair<int, int> II;

int main(int argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  map<int, int> m;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    m[a] += 1;
  }
  vector<II> v;
  for (auto kv : m) {
    v.push_back(II(kv.second, kv.first));
  }
  sort(v.begin(), v.end());
  int ans = 0;
  for (int i = 0; i < (v.size() - k); ++i) {
    ans += v[i].first;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// C.

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef pair<int, int> II;

int main(int argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  map<int, int> m;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    m[a] += 1;
  }
  vector<II> v;
  for (auto kv : m) {
    v.push_back(II(kv.second, kv.first));
  }
  sort(v.begin(), v.end());
  int ans = 0;
  for (int i = 0; i < ((int)v.size() - k); ++i) {
    ans += v[i].first;
  }
  cout << ans << endl;
  return 0;
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define p(s) cout << (s) << endl
#define REP(i, n, N) for (int i = n; i < N; i++)
#define RREP(i, n, N) for (int i = N - 1; i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf = 1e9 + 7;

int a, b, c, N, K, M;
string s;
vector<int> vt;
map<int, int> m;
int sum;
int main() {
  cin >> N >> K;
  REP(i, 0, N) {
    int A;
    cin >> A;
    m[A]++;
  }
  for (auto d : m) {
    vt.push_back(d.second);
  }
  sort(vt.begin(), vt.end());
  reverse(vt.begin(), vt.end());
  REP(i, 0, K) { sum += vt[i]; }
  p(N - sum);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define p(s) cout << (s) << endl
#define REP(i, n, N) for (int i = n; i < N; i++)
#define RREP(i, n, N) for (int i = N - 1; i >= n; i--)
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf = 1e9 + 7;

int a, b, c, N, K, M;
string s;
vector<int> vt;
map<int, int> m;
int sum;
int main() {
  cin >> N >> K;
  REP(i, 0, N) {
    int A;
    cin >> A;
    m[A]++;
  }
  for (auto d : m) {
    vt.push_back(d.second);
  }
  sort(vt.begin(), vt.end());
  reverse(vt.begin(), vt.end());
  if (vt.size() <= K)
    p(0);
  else {
    REP(i, 0, K) { sum += vt[i]; }
    p(N - sum);
  }
  return 0;
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli A, B, C, D, E, H, W, M, K, L, R, N, num = 0, sum = 0, flag = 0;
  string S, T;
  cin >> N >> K;
  Vec(P, lli, N, 0);
  rep(i, 0, N) {
    cin >> A;
    P[A - 1]++;
  }
  Sort(P);
  Rev(P);
  rep(i, K, N) sum += P[i];
  Out(sum);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i, m, n) for (lli i = m; i < n; i++)
#define rrep(i, m, n) for (lli i = m - 1; i >= n; i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(), N.end()), N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(), n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K, L, N, S) vector<L> K(N, S)
#define DV(K, L, N, M, S) vector<vector<L>> K(N, vector<L>(M, S))
#define TV(K, L, N, M, R, S)                                                   \
  vector<vector<vector<L>>> K(N, vector<vector<L>>(M, vector<L>(R, S)))
#define pint pair<lli, lli>
#define paf(L, R) pair<L, R>
#define mod 1000000007
#define MAX 10000000
#define ALL(a) a.begin(), a.end()
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = (((a) > (b)) ? (b) : (a))
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli A, B, C, D, E, H, W, M, K, L, R, N, num = 0, sum = 0, flag = 0;
  string S, T;
  cin >> N >> K;
  Vec(P, lli, N, 0);
  rep(i, 0, N) {
    cin >> A;
    P[A - 1]++;
  }
  Sort(P);
  Rev(P);
  rep(i, K, N) sum += P[i];
  Out(sum);
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
// V solo
#include <bits/stdc++.h>

#define forn(i, a, b) for (int i = a; i <= b; ++i)
#define forr(i, a, b) for (int i = a; i >= b; --i)

using namespace std;

const int N = (int)1e5 + 1;
const int INF = 1e9 + 7;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n, k, a[N];
set<int> st;
map<int, int> cnt;

bool cmp(int x, int y) { return cnt[x] < cnt[y]; }

int main() {
  scanf("%d %d", &n, &k);
  forn(i, 1, n) scanf("%d", a + i), st.insert(a[i]), cnt[a[i]]++;
  int ans = 0;
  if ((int)st.size() > k) {
    vector<int> v;
    for (auto i : st) {
      v.push_back(i);
    }
    sort(v.begin(), v.end(), cmp);
    int sz = st.size();
    forn(i, 0, (int)v.size() - 1) {
      ans += cnt[v[i]];
      sz--;
      if (sz == k)
        break;
    }
  }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
// V solo
#include <bits/stdc++.h>

#define forn(i, a, b) for (int i = a; i <= b; ++i)
#define forr(i, a, b) for (int i = a; i >= b; --i)

using namespace std;

const int N = (int)2e5 + 7;
const int INF = 1e9 + 7;

typedef long long ll;
typedef long double ld;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int n, k, a[N];
set<int> st;
map<int, int> cnt;

bool cmp(int x, int y) { return cnt[x] < cnt[y]; }

int main() {
  scanf("%d %d", &n, &k);
  forn(i, 1, n) scanf("%d", a + i), st.insert(a[i]), cnt[a[i]]++;
  int ans = 0;
  if ((int)st.size() > k) {
    vector<int> v;
    for (auto i : st) {
      v.push_back(i);
    }
    sort(v.begin(), v.end(), cmp);
    int sz = st.size();
    forn(i, 0, (int)v.size() - 1) {
      ans += cnt[v[i]];
      sz--;
      if (sz == k)
        break;
    }
  }
  printf("%d\n", ans);
  return 0;
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define PP(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1e18

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<char> vc;
typedef pair<int, int> pi;
// int dy[8]={1,1,1,0,-1,-1,-1,0};
// int dx[8]={-1,0,1,1,1,0,-1,-1};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int N, K;
map<int, int> Map;
vi a;
void input() {
  cin >> N >> K;
  a.resize(N + 1);
  rep(i, N) {
    cin >> a[i];
    Map[a[i]]++;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  input();
  vi tmp;
  for (auto it = Map.begin(); it != Map.end(); it++) {
    tmp.pb(it->second);
  }
  sort(ALL(tmp));
  int ans = 0;

  rep(i, tmp.size() - K) { ans += tmp[i]; }
  P(ans)
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define ALL(g) (g).begin(), (g).end()
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define P(p) cout << (p) << endl;
#define PP(p) cout << (p) << " ";
#define pb push_back
#define mp make_pair
#define INF 1e18

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef vector<char> vc;
typedef pair<int, int> pi;
// int dy[8]={1,1,1,0,-1,-1,-1,0};
// int dx[8]={-1,0,1,1,1,0,-1,-1};
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

int N, K;
map<int, int> Map;
vi a;
void input() {
  cin >> N >> K;
  a.resize(N + 1);
  rep(i, N) {
    cin >> a[i];
    Map[a[i]]++;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  input();
  vi tmp;
  for (auto it = Map.begin(); it != Map.end(); it++) {
    tmp.pb(it->second);
  }
  sort(ALL(tmp));
  int ans = 0;
  int len = tmp.size();
  rep(i, max(0, len - K)) { ans += tmp[i]; }
  P(ans)
  return 0;
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#define maxn 100005
using namespace std;
int n, k, tot;
int a[maxn], b[maxn], cnt[maxn];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + 1 + n);
  int sz = unique(b + 1, b + 1 + n) - (b + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(b + 1, b + 1 + sz, a[i]) - b;
    if (!cnt[a[i]])
      tot++;
    cnt[a[i]]++;
    ;
  }
  sort(cnt + 1, cnt + 1 + sz);
  if (tot <= k)
    return printf("%d\n", 0), 0;
  int ans = 0;
  for (int i = 1; i <= tot - k; i++)
    ans += cnt[i];
  printf("%d\n", ans);
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
#define maxn 200005
using namespace std;
int n, k, tot;
int a[maxn], b[maxn], cnt[maxn];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]), b[i] = a[i];
  sort(b + 1, b + 1 + n);
  int sz = unique(b + 1, b + 1 + n) - (b + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = lower_bound(b + 1, b + 1 + sz, a[i]) - b;
    if (!cnt[a[i]])
      tot++;
    cnt[a[i]]++;
    ;
  }
  sort(cnt + 1, cnt + 1 + sz);
  if (tot <= k)
    return printf("%d\n", 0), 0;
  int ans = 0;
  for (int i = 1; i <= tot - k; i++)
    ans += cnt[i];
  printf("%d\n", ans);
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

bool sortByVal(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

int main() {
  int N, K;
  cin >> N >> K;

  map<int, int> cnt;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;

    auto found = cnt.find(A);
    if (found == cnt.end()) {
      cnt[A] = 1;
    } else {
      cnt[A]++;
    }
  }

  vector<pair<int, int>> vec;
  for (auto const &x : cnt) {
    vec.push_back(pair<int, int>(x.first, x.second));
  }
  sort(vec.begin(), vec.end(), sortByVal);

  int num = 0;
  for (int i = 0; i < vec.size() - K; i++) {
    num += vec[i].second;
  }

  cout << num << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <iostream>

using namespace std;

typedef long long ll;

bool sortByVal(const pair<int, int> &a, const pair<int, int> &b) {
  return (a.second < b.second);
}

int main() {
  int N, K;
  cin >> N >> K;

  map<int, int> cnt;
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;

    auto found = cnt.find(A);
    if (found == cnt.end()) {
      cnt[A] = 1;
    } else {
      cnt[A]++;
    }
  }

  vector<pair<int, int>> vec;
  for (auto const &x : cnt) {
    vec.push_back(pair<int, int>(x.first, x.second));
  }
  sort(vec.begin(), vec.end(), sortByVal);

  int num = 0;
  int lim = max((unsigned long)0, vec.size() - K);
  for (int i = 0; i < lim; i++) {
    num += vec[i].second;
  }

  cout << num << endl;
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> t3;

int main(void) {
  ll n, k;
  cin >> n >> k;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    m[a]++;
  }
  vector<int> ps;
  for (auto e : m) {
    ps.push_back(e.second);
  }
  sort(ps.begin(), ps.end(), std::greater<int>());
  ll sum = 0;
  for (int i = 0; i < k; i++) {
    sum += ps[i];
  }
  cout << n - sum << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> t3;

int main(void) {
  ll n, k;
  cin >> n >> k;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    m[a]++;
  }
  vector<int> ps;
  for (auto e : m) {
    ps.push_back(e.second);
  }
  sort(ps.begin(), ps.end(), std::greater<int>());
  ll sum = 0;
  for (int i = 0; i < min<ll>(k, (ll)ps.size()); i++) {
    sum += ps[i];
  }
  cout << n - sum << endl;
  return 0;
}
Problem p03497 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repeat(i, s, n) for (int(i) = s; (i) < (n); (i)++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(std::numeric_limits<float>::max_digits10);
  map<int, int> mp;
  int n, k;
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    mp[a]++;
  }
  vector<int> As;
  for (auto &x : mp) {
    As.push_back(x.second);
  }
  sort(As.begin(), As.end());
  int ans = 0;
  rep(i, As.size() - k) { ans += As[i]; }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ll long long
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repeat(i, s, n) for (int(i) = s; (i) < (n); (i)++)

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(std::numeric_limits<float>::max_digits10);
  map<int, int> mp;
  int n, k;
  cin >> n >> k;
  rep(i, n) {
    int a;
    cin >> a;
    mp[a]++;
  }
  vector<int> As;
  for (auto &x : mp) {
    As.push_back(x.second);
  }
  sort(As.begin(), As.end());
  int ans = 0;
  rep(i, (int)As.size() - k) { ans += As[i]; }
  cout << ans << endl;
  return 0;
}