Description

Reversort

Design a hardware module that simulates the Reversort algorithm. Given an array, repeatedly find the minimum element from current position to end, reverse the subarray from current position to that minimum, then advance. Calculate the total cost where each reverse operation costs the length of the reversed subarray.

Example: For [4,2,1,3], find min(1) at pos 2, reverse [4,2,1]→[1,2,4], cost 3. Continue until sorted, sum all costs.

Source: Google Code Jam 2021 Qualification Round - Problem A: Reversort

Input

l

Stream of 8-bit Unsigned Integer
Array to sort using Reversort.

Output

16-bit Unsigned Integer
Total cost of Reversort algorithm.