☕Opening Rush!
5 orders at once! Only one drink at a time. Who first?
Loading...
5 orders at once! Only one drink at a time. Who first?
At the neighborhood cafe 'CodeBean', N customers rush in the moment it opens and all order drinks at once. The barista can make only one drink at a time, and each order's cook time (minutes) is given. A customer waits until their drink is ready; one customer's 'waiting time' is the total time from the start until their order is finished. When the barista may freely choose the processing order, decide the order that minimizes the sum of every customer's waiting time, and find that minimum total waiting time.
5 3 1 4 1 5
31
Sorting cook times ascending gives [1, 1, 3, 4, 5]. Processed in this order, each customer's finish time (= waiting time) is 1, 2, 5, 9, 14 minutes. The sum 1+2+5+9+14 = 31 minutes is the minimum. (e.g., keeping the input order gives 38, which is larger.)
3 2 2 2
12
When all are equal, order doesn't matter. Finish times 2, 4, 6 → sum 12.