AtCoder Regular Contest 194 (Div. 2)
B
Minimum Cost Sort
AtCoder
⌀ 1048
原题
← AC →
翻译中…

Problem Statement

翻译中…

You are given a permutation $P = (P_1, P_2, \ldots, P_N)$ of $(1, 2, \ldots, N)$. Takahashi can repeatedly perform the following operation on $P$ (possibly zero times):

翻译中…
  • Choose an integer $i$ satisfying $1 \leq i \leq N-1$. Pay a cost of $i$, and swap $P_i$ and $P_{i+1}$.
翻译中…

Find the minimum total cost required to sort $P$ in ascending order.

翻译中…

Constraints

翻译中…
  • $2 \leq N \leq 2 \times 10^5$
  • $(P_1, P_2, \ldots, P_N)$ is a permutation of $(1, 2, \ldots, N)$.
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N
P_1 P_2 \ldots P_N

翻译中…

Output

翻译中…

Print the minimum total cost required to sort $P$ in ascending order.

翻译中…

样例

Input #1
3
3 2 1
Output #1
4
Takahashi can sort $P$ in ascending order as follows: - Pay a cost of $1$ and swap $P_1 = 3$ and $P_2 = 2$. Now, $P = (2, 3, 1)$. - Pay a cost of $2$ and swap $P_2 = 3$ and $P_3 = 1$. Now, $P = (2, 1, 3)$. - Pay a cost of $1$ and swap $P_1 = 2$ and $P_2 = 1$. Now, $P = (1, 2, 3)$. The total cost for these operations is $4$, which is the minimum possible.
Input #2
5
2 4 1 3 5
Output #2
6
Input #3
2
1 2
Output #3
0
题目信息
题号
arc194_b
时间限制
2 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。