The problem statement reads like this:
Given service times for a set of queries, compute a schedule for processing the queries that minimizes the total waiting time. Return the minimum waiting time. For example, if the service times are 〈2,5, 1,3〉, if we schedule in the given order, the total waiting time is 0+(2)+(2+5)+(2+5+1) = 17. If however, we schedule queries in order of decreasing service times, the total waiting time is 0+ (5) + (5 +3) + (5 + 3 + 2) = 23. As we will see, for this example, the minimum waiting time is 10.
Why are we adding the total wait times at every stage? I’d guess in the above example the answer is 6 (0 + 1 + 2 + 3) and not 10. What am I missing?