All problems

Problem 737

Shortest Job First Scheduling

Easy · 10 pts · Greedy, Sorting

Problem

N jobs all arrive at time 0 and must run one at a time on a single CPU without interruption. Job i takes BURST[i] time units. The waiting time of a job is the time it spends before it starts running. Using shortest job first (always run the shortest remaining job next), which minimises the average waiting time, print that average rounded down to an integer. Sort the burst times, and each job waits for the sum of the bursts before it.

Input

The first line contains N. The second line contains the N burst times.

Output

Print the average waiting time, rounded down.

Constraints

1 <= N <= 100000; 1 <= BURST[i] <= 10^5

Example input

5
4 3 7 1 2

Example output

4

Explanation

For bursts 4 3 7 1 2, the order is 1 2 3 4 7 and the waiting times are 0, 1, 3, 6 and 10, totalling 20. The average is 20 / 5 = 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Fractional Knapsack Next: Page Faults in LRU