Travel
Practice
2.8 (10 votes)
Basic programming
Dynamic programming
Algorithms
Introduction to dynamic programming 1
Implementation
Basics of implementation
Problem
92% Success 1746 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Alice lives in a country. In this country, there are only \(N\) cities located in a row, each city has a magic number such as the \(i^{th}\) city contains \(a_i\) number. She wants to visit the \(K\) cities of this country. Alice starts with a city where the magic number of that city is \(1\). Then if you are in city \(X\), then the next city can be city \(Y\), if \(a_y = a_x + 1\). Alice wants to choose the order of the cities she will visit so that the distance traveled was the maximum. The distance between neighboring cities is \(1\).

Input format

  • The first line is the number \(T\) denoting the number of tests.
  • The first line of each test contains two integers \(N\) and \(K\) denoting the number of cities in the country and the number of cities Alice wants to visit.
  • The second line contains \(N\) integers \(a_i\) denoting magic numbers for each city.

Output format

In a single line, print one integer denoting the maximum distance that Alice will have to cover.

Constraints

\(1 ≤  T ≤  10\)
\(1 ≤  K ≤  N ≤  10^5\)
\(1≤  a_i ≤  K\)

For each $$j$$, there is at least a city with the magic number \(j (1≤ j ≤  K) \).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
AlgorithmsDynamic ProgrammingDynamic programmingEasy
Points:30
60 votes
Tags:
Dynamic ProgrammingAlgorithmsIntroduction to Dynamic Programming 1
Points:30
5 votes
Tags:
Dynamic ProgrammingAlgorithmsC++Introduction to Dynamic Programming 1