시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 512 MB55121132.353%

문제

You own several hats, some of which you wear more than others. Because wearing all n at once would be impractical, you store the spares on a rack with n − 1 hooks. The first hook is a half metre from the door, the second one metre, the third a metre-and-a-half, and so on. This means that walking from and to the door to the ith hook involves i metres of walking.

Tonight you will need to make a series of public appearances, each time wearing headgear appropriate to the role. When you come back from one engagement, you will take the hat you need from its hook and exchange it with the one you had been wearing before. This means that hats can move around as the night goes on.

Given the plan for tonight, and assuming you are already wearing the first one, what is the best way to arrange the hats on the rack before setting off so as to minimise the number of metres walked?

입력

  • The first line of input contains two integers: c and n (1 ≤ c, n ≤ 105), the number of hats in total and the length of the itinerary respectively.
  • The second line of input contains n integers: v1 . . . vn (1 ≤ v ≤ c; v[i] ≠ v[i − 1]), the order of hats you need to put on. You are already wearing the first one, and you never need to wear the same hat twice consecutively.

출력

Output the minimum number of metres you need to walk, once you have optimised your hat rack.

Next, output c − 1 integers: an initial ordering of the hat rack that gives minimum walking distance, starting from place 1. This should include exactly once every hat except the first.

If there are multiple correct answers, you may output any one of them.

예제 입력 1

3 5
1 3 2 3 2

예제 출력 1

5
2 3

예제 입력 2

5 9
1 4 2 3 5 2 5 2 5

예제 출력 2

14
3 5 4 2

출처

ICPC > Regionals > Europe > Northwestern European Regional Contest > The UK & Ireland Programming Contest > UKIEPC 2019 H번

  • 문제를 만든 사람: Robin Lee