---
{"category_name":"easy","problem_code":"BEAUSUB","problem_name":"Beautiful Subsequence","problemComponents":{"constraints":"- $1 \\leq T \\leq 100$\n- $1 \\leq N \\leq 10^3$\n- $1 \\leq K \\leq 10^3$\n- $1 \\leq A_i \\leq 10^3$","constraintsState":true,"subtasks":"","subtasksState":false,"inputFormat":"The first line contains an integer $T$ - the number of test cases. Then $T$ test cases follow.\n\nThe first line of each test case contains two integers $N$, $K$ - the length of array $A$ and the parameter for a beautiful sequence.\n\nThe second line of each testcase contains $N$ integers $A_1,\\ldots, A_N$.","inputFormatState":true,"outputFormat":"For each test case, output the maximum possible length of a $K$-beautiful subsequence of $A$.\n","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"4\n4 1\n1 1 2 3\n9 2\n1 2 3 2 4 5 6 7 5\n5 5\n1 1 1 1 1\n10 1\n1 2 1 2 1 2 1 2 1 2\n","output":"3\n5\n5\n6","explanation":"- **Test Case 1** : You can either choose $[1, 1, 2]$ or $[1, 1, 3]$ as a subsequence. Both of the subsequences are $1$-beautiful. So the maximum length of a $1$-beautiful subsequence is $3$.\n\n- **Test Case 2** : It is optimal to choose the subsequence $[1, 2, 2, 5, 5]$. So the maximum length of a $2$-beautiful subsequence is $5$.\n\n- **Test Case 3** : You can choose the entire array as a subsequence because the given array is $K$-beautiful for any $K\\ge 0$, which includes $K=5$.\n","isDeleted":false}}},"video_editorial_url":"","languages_supported":{"0":"CPP14","1":"C","2":"JAVA","3":"PYTH 3.6","4":"CPP17","5":"PYTH","6":"PYP3","7":"CS2","8":"ADA","9":"PYPY","10":"TEXT","11":"PAS fpc","12":"NODEJS","13":"RUBY","14":"PHP","15":"GO","16":"HASK","17":"TCL","18":"PERL","19":"SCALA","20":"LUA","21":"kotlin","22":"BASH","23":"JS","24":"LISP sbcl","25":"rust","26":"PAS gpc","27":"BF","28":"CLOJ","29":"R","30":"D","31":"CAML","32":"FORT","33":"ASM","34":"swift","35":"FS","36":"WSPC","37":"LISP clisp","38":"SQL","39":"SCM guile","40":"PERL6","41":"ERL","42":"CLPS","43":"ICK","44":"NICE","45":"PRLG","46":"ICON","47":"COB","48":"SCM chicken","49":"PIKE","50":"SCM qobi","51":"ST","52":"SQLQ","53":"NEM"},"max_timelimit":1,"source_sizelimit":50000,"problem_author":"soumyadeep_21","problem_tester":"","date_added":"14-07-2021","tags":{"0":"cook131","1":"dynamic","2":"easy","3":"soumyadeep_21"},"problem_difficulty_level":"Easy-Medium","best_tag":"Dynamic Programming","editorial_url":"https://discuss.codechef.com/problems/BEAUSUB","time":{"view_start_date":1627492504,"submit_start_date":1627492504,"visible_start_date":1627492504,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=BEAUSUB","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problems statements in [Mandarin Chinese](https://www.codechef.com/download/translated/COOK131/mandarin/BEAUSUB.pdf), [Russian](https://www.codechef.com/download/translated/COOK131/russian/BEAUSUB.pdf), [Vietnamese](https://www.codechef.com/download/translated/COOK131/vietnamese/BEAUSUB.pdf) and [Bengali](https://www.codechef.com/download/translated/COOK131/bengali/BEAUSUB.pdf) as well.
An array $A$ consisting of $N$ integers is called *$K$-beautiful* if it holds the following property:
The number of adjacent indices with different values is at most $K$. More formally, there are at most $K$ indices $i$ ($1\le i\lt N$) such that $A_i\ne A_{i+1}$. Note that according to this definition, if an array $A$ is $K$-beautiful, then $A$ is also $(K+1)$-beautiful.
You are given an array $A$ consisting of $N$ integers and an integer $K$. Find the length of the longest $K$-beautiful subsequence of $A$, where a subsequence is defined below.
For example, consider the array $A = [1 , 1, 2, 3, 2 , 4]$. The subsequences $[1, 1]$ and $[1, 1, 2]$ are $1$-beautiful, and the subsequences $[1, 2, 3]$ and $[1, 2, 2, 4]$ are $2$-beautiful.
**Note:**
- A sequence $a$ is a *subsequence* of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero) elements. For example, $[3,1]$ is a subsequence of $[3,2,1]$ and $[4,3,1]$, but not a subsequence of $[1,3,3,7]$ and $[3,10,4]$.
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>