#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

class Klasemen {
	private:
		unordered_map<string, int> clubScoreMap;
		bool changed;
		vector<string> cachedKlasemen;
		
		void sortKlasemen() {
			sort(cachedKlasemen.begin(), cachedKlasemen.end(),
				[&](const string& a, const string& b) {
					if (clubScoreMap[a] != clubScoreMap[b]) {
						return clubScoreMap[a] > clubScoreMap[b];
					}
					return a < b;
				}
			);
		}
	
	public:
			Klasemen(vector<string> clubs) {
			for (string s : clubs) {
				clubScoreMap[s] = 0;
				cachedKlasemen.push_back(s);
			}
			
			changed = true;
		}
	
		void catatPermainan(string klubKandang, string klubTandang, string skor) {
			vector<int> points;
	
			stringstream ss(skor);
			string poin;
	
			while (getline(ss, poin, ':')) {
				points.push_back(stoi(poin));
			}
	        
			if (points[0] > points[1]) {
				clubScoreMap[klubKandang] += 3;
			} else if (points[0] < points[1]) {
				clubScoreMap[klubTandang] += 3;
			} else {
				clubScoreMap[klubKandang] += 1;
	            clubScoreMap[klubTandang] += 1;
			}
	        changed = true;
	    }
	
		vector<string> cetakKlasemen() {
	    	if (changed) {
				sortKlasemen();
				changed = false;
	    	}
			return cachedKlasemen;
	    }
	
		string ambilPeringkat(int nomorPeringkat) {
	    	if (changed) {
				sortKlasemen();
				changed = false;
	    	}
	    	if(nomorPeringkat <= 0 || nomorPeringkat > cachedKlasemen.size()){
	    		return "Peringkat tidak sesuai";
	    	}
			return cachedKlasemen[nomorPeringkat - 1];
		}
};

int main() {
	Klasemen klasemen({"Liverpool", "Chelsea", "Arsenal"});
	
	klasemen.catatPermainan("Arsenal", "Liverpool", "2:1");
	klasemen.catatPermainan("Arsenal", "Chelsea", "1:1");
	klasemen.catatPermainan("Chelsea", "Arsenal", "0:3");
	klasemen.catatPermainan("Chelsea", "Liverpool", "3:2");
	klasemen.catatPermainan("Liverpool", "Arsenal", "2:2");
	klasemen.catatPermainan("Liverpool", "Chelsea", "0:0");
    
	vector<string> hasil = klasemen.cetakKlasemen();
    
	cout << "Klasemen:" << endl;
    for (const string& club : hasil) {
		cout << club << endl;
	}
	
	cout << endl;
	cout << klasemen.ambilPeringkat(2) << endl;
	
	return 0;
}