https://www.acmicpc.net/problem/2910
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int n, c;
map<int, int> firstindex;
map<int,int > countarr;
bool cmp(int x,int y){
if (x == y) return false;
if (countarr[x] > countarr[y]) return true;
else if (countarr[x] == countarr[y]) {
if (firstindex[x] >= firstindex[y]) return false;
else return true;
}
else return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
vector<int> input;
cin >> n >> c;
input.resize(n);
for (int i = 0; i < n; i++) {
cin >> input[i];
countarr[input[i]]++;
if (firstindex.find(input[i]) == firstindex.end()) {
firstindex[input[i]]= i;
}
}
sort(input.begin(), input.end(), cmp);
for (int i = 0; i < n; i++) {
cout << input[i] << " ";
}
cout << "\n";
return 0;
}