This page is still under construction.

Parts of this page are still being built. What you see may change.

Out of Sorts

Time limit2sMemory limit512 MB

Summary
Given an array, simulate a hybrid of quicksort and bubble sort that repeatedly bubbles until partition points appear, then splits, and report the total work counter.
Level

Hard8 of 10

Topics
Sorting, Simulation, Implementation, Divide and conquer
Solved
No attempts yet

Problem

Bessie the cow is thinking about a career beyond the farm, so she has started learning algorithms from online coding sites. Her two favorite algorithms are bubble sort and quicksort. She keeps confusing the two, and what she ends up implementing is an odd hybrid of both.

Call the gap between positions ii and i+1i+1 of an array A a partition point when the maximum of A[0..i] is no larger than the minimum of A[i+1..N-1]. Quicksort rearranges an array so that it has a partition point and then sorts the two sides A[0..i] and A[i+1..N-1] recursively. Bessie remembers that much, and she also knows, correctly, that every partition point of an array can be found in linear time. What she has forgotten is how quicksort rearranges the array to create a partition point quickly, so she uses one pass of bubble sort for that job.

One pass of bubble sort is written like this:

bubble_sort_pass (A) {
   for i = 0 to length(A)-2
      if A[i] > A[i+1], swap A[i] and A[i+1]
}

Her recursive sort is then structured like this:

quickish_sort (A) {
   if length(A) = 1, return
   do { // main loop
      work_counter = work_counter + length(A)
      bubble_sort_pass(A)
   } while (no partition points exist in A)
   divide A at all partition points; recursively quickish_sort each piece
}

Bessie wants to know how fast her code runs. She treats one iteration of the main loop as work proportional to the length of the array it runs on, so she adds that length to the global variable work_counter inside the loop. Given the initial array, predict the final value of work_counter after quickish_sort runs on it.

Input

The first line contains NN (1≤N≤1000001 \le N \le 100000). Each of the next NN lines contains one element of the array, A[0] through A[N-1], and every element is an integer between 00 and 10910^9. The elements are not guaranteed to be distinct.

Output

Print the final value of work_counter.

Hint

Start with the array 20 2 3 4 9 8 7. One pass of bubble sort adds 7 to the counter and leaves 2 | 3 | 4 | 9 8 7 | 20, where | marks a partition point. The pieces 2, 3, 4 and 20 have length 1, so they cost nothing. The piece 9 8 7 takes one iteration of the main loop (3 units of work) and becomes 8 7 | 9, and one last iteration over 8 7 (2 units of work) finishes the sort. The counter ends at 12.

Examples4

  1. Example 1

    Input
    7
    20
    2
    3
    4
    9
    8
    7
    
    Expected output
    12
    
  2. Example 2

    Input
    1
    42
    
    Expected output
    0
    
  3. Example 3

    Input
    5
    1
    2
    3
    4
    5
    
    Expected output
    5
    
  4. Example 4

    Input
    5
    5
    4
    3
    2
    1
    
    Expected output
    14