Codeforces Round 542 [Alex Lopashev Thanks-Round] (Div. 2)
B
Two Cakes
Codeforces
1200
原题
← AC →
翻译中…

Sasha and Dima want to buy two $n$-tier cakes. Each cake should consist of $n$ different tiers: from the size of $1$ to the size of $n$. Tiers should go in order from the smallest to the biggest (from top to bottom).

翻译中…

They live on the same street, there are $2 \cdot n$ houses in a row from left to right. Each house has a pastry shop where you can buy a cake tier. Unfortunately, in each pastry shop you can buy only one tier of only one specific size: in the $i$-th house you can buy a tier of the size $a_i$ ($1 \le a_i \le n$).

翻译中…

Since the guys carry already purchased tiers, and it is impossible to insert a new tier in the middle of the cake, they agreed to buy tiers from the smallest to the biggest. That is, each of them buys tiers in order: $1$, then $2$, then $3$ and so on up to $n$.

翻译中…

Initially, Sasha and Dima are located near the first (leftmost) house. Output the minimum distance that they will have to walk in total to buy both cakes. The distance between any two neighboring houses is exactly $1$.

翻译中…

Input

翻译中…

The first line of the input contains an integer number $n$ — the number of tiers in each cake ($1 \le n \le 10^5$).

翻译中…

The second line contains $2 \cdot n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le n$), where $a_i$ is equal to the size of the tier, which can be bought in the $i$-th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from $1$ to $n$ occurs in $a$ exactly two times.

翻译中…

Output

翻译中…

Print one number  — the minimum distance that the guys have to walk in total to buy both cakes. Guys can be near same house at the same time. They begin near the first (leftmost) house. Each of the guys should buy $n$ tiers in ascending order of their sizes.

翻译中…

Note

翻译中…

In the first example, the possible optimal sequence of actions is:

翻译中…
  • Sasha buys a tier of size $1$ near the $1$-st house ($a_1=1$);
  • Dima goes to the house $2$;
  • Dima buys a tier of size $1$ near the $2$-nd house ($a_2=1$);
  • Sasha goes to the house $4$;
  • Sasha buys a tier of size $2$ near the $4$-th house ($a_4=2$);
  • Sasha goes to the house $5$;
  • Sasha buys a tier of size $3$ near the $5$-th house ($a_5=3$);
  • Dima goes to the house $3$;
  • Dima buys a tier of size $2$ near the $3$-rd house ($a_3=2$);
  • Dima goes to the house $6$;
  • Dima buys a tier of size $3$ near the $6$-th house ($a_6=3$).
翻译中…

So, Sasha goes the distance $3+1=4$, and Dima goes the distance $1+1+3=5$. In total, they cover a distance of $4+5=9$. You can make sure that with any other sequence of actions they will walk no less distance.

翻译中…

样例

Input #1
3
1 1 2 2 3 3
Output #1
9
InputCopy OutputCopy
Input #2
2
2 1 1 2
Output #2
5
InputCopy OutputCopy
Input #3
4
4 1 3 2 2 3 1 4
Output #3
17
InputCopy OutputCopy
题目信息
题号
1130B
时间限制
1 s
内存限制
256 MB
标签
贪心

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