CF1130B.Two Cakes

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

They live on the same street, there are 2n2 \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 ii -th house you can buy a tier of the size aia_i ( 1ain1 \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: 11 , then 22 , then 33 and so on up to nn .

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 11 .

输入格式

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

The second line contains 2n2 \cdot n integers a1,a2,,a2na_1, a_2, \dots, a_{2n} ( 1ain1 \le a_i \le n ), where aia_i is equal to the size of the tier, which can be bought in the ii -th house. Remember that in each house you can buy only one tier. It is guaranteed that every number from 11 to nn occurs in aa exactly two times.

输出格式

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 nn tiers in ascending order of their sizes.

输入输出样例

  • 输入#1

    3
    1 1 2 2 3 3
    

    输出#1

    9
    
  • 输入#2

    2
    2 1 1 2
    

    输出#2

    5
    
  • 输入#3

    4
    4 1 3 2 2 3 1 4
    

    输出#3

    17
    

说明/提示

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

  • Sasha buys a tier of size 11 near the 11 -st house ( a1=1a_1=1 );
  • Dima goes to the house 22 ;
  • Dima buys a tier of size 11 near the 22 -nd house ( a2=1a_2=1 );
  • Sasha goes to the house 44 ;
  • Sasha buys a tier of size 22 near the 44 -th house ( a4=2a_4=2 );
  • Sasha goes to the house 55 ;
  • Sasha buys a tier of size 33 near the 55 -th house ( a5=3a_5=3 );
  • Dima goes to the house 33 ;
  • Dima buys a tier of size 22 near the 33 -rd house ( a3=2a_3=2 );
  • Dima goes to the house 66 ;
  • Dima buys a tier of size 33 near the 66 -th house ( a6=3a_6=3 ).

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

首页