Certificates
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk decided to start using lucky numbers. As you know, to get permission to use lucky numbers, you need to collect \(k\) certificates at the ministry, starting with certificate number 1. There are \(n\) offices located in a row in the ministry, which issue certificates — the \(i\)-th office issues the certificate with number \(a_i\).
When Zenyk receives a certificate in the \(i\)-th office, he is sent to office \(j\), where he needs to get the next certificate in order, that is \(a_j = a_i + 1\). The ministry is headed by a well-known devil, so the order of offices (including the starting one) is chosen so that Zenyk walks the maximum possible distance.
Determine how many meters Zenyk will have to walk, if the distance between adjacent offices is equal to 1 meter.
Input
The first line contains two integers \(n\) and \(k\) — the number of offices and certificates, respectively.
The second line contains \(n\) integers \(a_i\) — the number of the certificate issued in the \(i\)-th office.
Output
In a single line, print one integer — the maximum distance in meters that will have to be walked.
Constraints
\(1 \le k \le n \le 10^5\),
\(1 \le a_i \le k\),
there is at least one office that issues a certificate with number \(j\) (\(1 \le j \le k\)),
in 15 tests \(n \le 5000\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 6 4 1 3 1 4 2 2 | 11 |
Notes
In the given test, Zenyk will visit the offices in the following order: \(1 \rightarrow 6 \rightarrow 2 \rightarrow 4\). The distance between the 1st and 6th offices is 5 meters, between the 6th and 2nd is 4 meters, and between the 2nd and 4th is 2 meters. Thus, the answer is \(5+4+2=11\).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|