fork download
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long ll;

struct frequency{
	int count = 1;
	int initial;
};

bool cmp(pair<int, frequency> f1, pair<int, frequency> f2){
	if(f1.second.count > f2.second.count) return true;
	else if(f1.second.count==f2.second.count) {
		if(f1.second.initial < f2.second.initial) return true;
		else return false;
	}else{
		return false;
	}
}

unordered_map<ll, frequency> us;
vector<pair<int, frequency>> v;

int main() {
	// your code goes here
	int N, C;
	cin>>N>>C;
	
	for(int i=0;i<N;i++){
		ll value;
		cin>>value;

		if(us.count(value)==0){
			frequency f;
			f.initial = i;
			us.insert(make_pair(value, f));
		}else{
			us[value].count++;
		}
	}
	
	v.assign(us.begin(), us.end());
	sort(v.begin(), v.end(), cmp);
	
	for(int i=0;i<v.size();i++){
		while(v[i].second.count--){
			cout<<v[i].first<<" ";
		}
	}
	
	return 0;
}
Success #stdin #stdout 0s 5292KB
stdin
9 3
1 3 3 3 2 2 2 1 1
stdout
1 1 1 3 3 3 2 2 2