For problem BerPizza https://mirror.codeforces.com/contest/1468/problem/C the most easy solution is to store the key and values in a hashmap and loop through looking for either largest value or smallest index. This approach gives TLE and when I read the editorial they recommend a treeset but a treeset cannot store duplicate values. Is there a way to iterate through a map faster than o(n) or do I need to use priorityqueues for this problem?