Travel

2.8

10 votes
Basic Programming, Dynamic Programming, Algorithms, Introduction to Dynamic Programming 1, Implementation, Basics of Implementation
Problem

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 ith city contains ai 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 ay=ax+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 ai 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

1T10
1KN105
1aiK

For each j, there is at least a city with the magic number j(1jK).

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the first case, from what city you would not start our way, the distance you will pass is 0.

In the second case, you will visit cities in this order -> (2,5,1,4). Thus the maximum distance is 10.

Editor Image

?