One Girl One Sequence
Practice
5 (3 votes)
Advanced data structures
Data structures
Fenwick tree
Medium
Problem
78% Success 959 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code
You are given a sequence of\(\) distinct integers \(a_1, a_2, \ldots, a_n\) and an integer \(x\) and you have to perform the following operations:
- Remove the current minimum from \(a\). This operation costs \(x\).
- 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 \(a_i\) 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\) (\(1\leq n \leq 10^6\) , \(1\leq x \leq 10^9\)) denoting the size of the sequence and the cost of the first type of operation
- Second line: A sequence \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\))
- It is guaranteed that all elements of \(a\) are distinct.
Output format
Print an integer denoting the minimum cost to erase the whole sequence.
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
Medium
Points:50
36 votes
Tags:
Bit ManipulationGraphsMediumReadySegment Trees
3.Gifts
Points:50
Tags:
ApprovedBinary treeFenwick treeMathMediumOpenTrees
Editorial