CF433C.Ryouko's Memory Note

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory.

Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work.

Ryouko's notebook consists of nn pages, numbered from 1 to nn . To make life (and this problem) easier, we consider that to turn from page xx to page yy , xy|x-y| pages should be turned. During analyzing, Ryouko needs mm pieces of information, the ii -th piece of information is on page aia_{i} . Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is .

Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page xx to page yy , she would copy all the information on page xx to y (1<=x,y<=n)y (1<=x,y<=n) , and consequently, all elements in sequence aa that was xx would become yy . Note that xx can be equal to yy , in which case no changes take place.

Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers.

输入格式

The first line of input contains two integers nn and m (1<=n,m<=105)m (1<=n,m<=10^{5}) .

The next line contains mm integers separated by spaces: a1,a2,...,ama_{1},a_{2},...,a_{m} (1<=ai<=n)(1<=a_{i}<=n) .

输出格式

Print a single integer — the minimum number of pages Ryouko needs to turn.

输入输出样例

  • 输入#1

    4 6
    1 2 3 4 3 2
    

    输出#1

    3
    
  • 输入#2

    10 5
    9 4 3 8 8
    

    输出#2

    6
    

说明/提示

In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence aa becomes 1,2,3,3,3,2{1,2,3,3,3,2} , so the number of pages Ryouko needs to turn is 12+23+33+33+32=3|1-2|+|2-3|+|3-3|+|3-3|+|3-2|=3 .

In the second sample, optimal solution is achieved by merging page 9 to 4.

首页