#FT0000198. Chi phí nhỏ nhất để sắp xếp (Minimum Cost Sort)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Chi phí nhỏ nhất để sắp xếp (Minimum Cost Sort)
Minimum Cost Sort
Source: AtCoder
Version: Phuoc Hung OJ Extended
Problem Statement
You are given a permutation containing each integer from to exactly once.
You may perform the following operation any number of times, including zero: choose an index with , pay a cost of , and swap the adjacent elements and . The cost depends on the swap position, not on the values being swapped.
Find the minimum total cost required to obtain the increasing permutation .
Input
- The first line contains an integer , the length of the permutation.
- The second line contains exactly integers , in their original order.
Output
Print one integer: the minimum total cost of adjacent swaps needed to sort the permutation. If it is already sorted, print 0.
Subtasks
- Subtask 1 (20 points): ; is a permutation of .
- Subtask 2 (30 points): ; is a permutation of .
- Subtask 3 (50 points): ; is a permutation of .
Examples
Input
3
3 2 1
Output
4
Explanation
Initially . One way to sort it is:
- Swap at for cost : .
- Swap at for cost : .
- Swap at for cost : .
The total cost is . The initial permutation has three inversions, so at least three adjacent swaps are necessary; this sequence achieves the minimum possible total cost for the example. Thus the answer is 4.