B. Two Cakes

B. Two Cakes

time limit per test  1 second

memory limit per test   256 megabytes


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 2⋅n2⋅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 aiai (1≤ai≤n1≤ai≤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.

Input

The first line of the input contains an integer number nn — the number of tiers in each cake (1≤n≤1051≤n≤105).

The second line contains 2⋅n2⋅n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤n1≤ai≤n), where aiai 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.

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

Examples

input

3
1 1 2 2 3 3

output

9

input

2
2 1 1 2

output

5

input

4
4 1 3 2 2 3 1 4

output

17


Note

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

  • Sasha buys a tier of size 11 near the 11-st house (a1=1a1=1);
  • Dima goes to the house 22;
  • Dima buys a tier of size 11 near the 22-nd house (a2=1a2=1);
  • Sasha goes to the house 44;
  • Sasha buys a tier of size 22 near the 44-th house (a4=2a4=2);
  • Sasha goes to the house 55;
  • Sasha buys a tier of size 33 near the 55-th house (a5=3a5=3);
  • Dima goes to the house 33;
  • Dima buys a tier of size 22 near the 33-rd house (a3=2a3=2);
  • Dima goes to the house 66;
  • Dima buys a tier of size 33 near the 66-th house (a6=3a6=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.

贪心思想:小位置到小位置,大位置到大位置。

#include <bits/stdc++.h>
using namespace std;
long long  a[200005],c[100005],b[100005];
long long n;
int main(){
	cin>>n;
	for(int i=1;i<=2*n;i++) {
		cin>>a[i];
		if(c[a[i]]==0) c[a[i]] = i;
		else b[a[i]] = i;
	}

	long long ans=0;
	int i=1;
	int c0=1,b0=1;
	while(i<=n){
		if(c0>b0) { c0=c0^b0; b0=c0^b0; c0=c0^b0; } // 交换两个数的值
		if(c[i]>b[i]){ c[i]=c[i]^b[i]; b[i]=c[i]^b[i]; c[i]=c[i]^b[i]; }
		ans += fabs(c[i]-c0);c0=c[i];
		ans += fabs(b[i]-b0);b0=b[i];
		i++;
	}
	cout<<ans<<endl;
	return 0;
}
发布了46 篇原创文章 · 获赞 81 · 访问量 5564

猜你喜欢

转载自blog.csdn.net/zfq17796515982/article/details/87991171
two