using System; using System.Linq; using System.Collections.Generic; namespace _07.PopulationCounter { class Program { static void Main(string[] args) { // Dictionary-> Countries and Cities //Dictionary -> Countries and Total Population //Read Input //Fill Dictionary //Sort Countries //Country ->Sort Cities //Print Dictionary totalPopulation = new Dictionary(); Dictionary> countriesAndCities = new Dictionary>(); while (true) { string line = Console.ReadLine(); if (line == "report") { break; } string[] tokens = line.Split('|'); string city = tokens[0]; string country = tokens[1]; long population = long.Parse(tokens[2]); //с една проверка пълня двата ключа //ако речника няма тази страна, то да я добавя if (totalPopulation.ContainsKey(country) == false) { totalPopulation.Add(country,0); //пълня само ключа countriesAndCities.Add(country, new Dictionary()); } totalPopulation[country] += population; countriesAndCities[country].Add(city, population); } foreach (var country in totalPopulation.OrderByDescending(c => c.Value)) //сортира всички държави по население { Console.WriteLine($"{country.Key} (total population: {country.Value})"); Dictionary cities = countriesAndCities[country.Key] .OrderByDescending(c=>c.Value) .ToDictionary(x=>x.Key,x=>x.Value); foreach (var city in cities) { Console.WriteLine($"=>{city.Key}: {city.Value}"); } } } } }