Minimum Cost Sort

AtCoder
IDarc194_b
Time2000ms
Memory256MB
Difficulty
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$ [samples]
Samples
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
API Response (JSON)
{
  "problem": {
    "name": "Minimum Cost Sort",
    "description": {
      "content": "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$ sati",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc194_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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):\n\n*   Choose an integer $i$ sati...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments