using System.Collections.Generic; using System.Linq; public class PriorityQueue { readonly SortedList> priorities = new (); public int Count { get; set; } public void Enqueue(TElement element, TPriority priority) { if (!priorities.ContainsKey(priority)) { priorities[priority] = new Queue(); } priorities[priority].Enqueue(element); Count++; } public TElement Dequeue() { if (priorities.Count == 0) throw new System.InvalidOperationException("The queue is empty"); var firstPair = priorities.First(); var element = firstPair.Value.Dequeue(); if (firstPair.Value.Count == 0) priorities.Remove(firstPair.Key); Count--; return element; } }