One Girl One Sequence

5

3 votes
Advanced Data Structures, Data Structures, Fenwick tree, Medium
Problem

You are given a sequence of distinct integers  a1,a2,,an and an integer x and you have to perform the following operations:

  1. Remove the current minimum from a. This operation costs x.
  2. Remove the current maximum from a. This operation costs the number of elements located on the right to the maximum. Formally, if the maximum is ai and the current size of the sequence is k,  then the cost will be k - i 

What is the minimum cost to erase the whole sequence?

Input format

  • First line: Two integers n  and x  (1n106 , 1x109) denoting the size of the sequence and the cost of the first type of operation
  • Second line: A sequence a1,a2,,an (1ai109)
  • It is guaranteed that all elements of a are distinct.

Output format

Print an integer denoting the minimum cost to erase the whole sequence.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

One way to remove all the elements with the cost of 2 is to perform the first operation once and pay 1. Then perform the second operation and pay 1 and then do the second operation twice and pay 0.

Editor Image

?