---
{"category_name":"easy","problem_code":"SUBLCS","problem_name":"Subsequence LCS","problemComponents":{"constraints":"- $1 \\leq T \\leq 100$\n- $1 \\leq N \\leq 2 \\cdot 10^{5}$\n- the sum of $N$ over all test cases does not exceed $2 \\cdot 10^{5}$\n- $1 \\leq A_i \\leq N$ for each valid $i$\n","constraintsState":true,"subtasks":"**Subtask #1 (20 points):** \n- $T \\leq 90$\n- $N \\leq 1000$\n- the sum of $N$ over all test cases does not exceed $5000$\n- the numbers in the sequence $A$ are pairwise distinct\n\n**Subtask #2 (20 points):** \n- $T \\leq 42$\n- $N \\leq 1000$\n- the sum of $N$ over all test cases does not exceed $5000$\n\n**Subtask #3 (60 points):** original constraints\n","subtasksState":true,"inputFormat":"- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.\n\n- The first line of each test case contains an integer $N$.\n\n- The second line contains $N$ space-separated integers $A_1, A_2, \\ldots, A_N$.","inputFormatState":true,"outputFormat":"- For each test case, print a single line containing one integer ― the maximum possible length of the longest common subsequence of any longest non-decreasing subsequence of $A$ and any longest non-increasing subsequence of $A$.\n","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"4\n8\n6 5 1 2 7 8 4 3\n6\n1 6 5 2 3 4\n7\n2 5 3 3 1 3 7\n6\n6 1 4 2 4 5","output":"0\n1\n3\n2","explanation":"**Example case 1:** No pair of subsequences, containing one of the longest non-decreasing subsequences and one of longest non-increasing subsequences have a number in common. Hence, the answer is $0$.\n\n**Example case 2:** The maximum length of the longest common subsequence is $1$ and it can be achieved by choosing the longest non-decreasing subsequence $\\{1,2,3,4\\}$ and the longest non-increasing subsequence $\\{6,5,2\\}$. The longest common subsequence of those two subsequences is $\\{2\\}$.\n\n**Example case 3:** The maximum length of the longest common subsequence is $3$ and it can be achieved by choosing the longest non-decreasing subsequence $\\{2,3,3,3,7\\}$ and the longest non-increasing subsequence $\\{5,3,3,3\\}$. The longest common subsequence of those two subsequences is $\\{3,3,3\\}$.\n\n**Example case 4:** The maximum length of the longest common subsequence is $2$ and it can be achieved by choosing the longest non-decreasing subsequence $\\{1,4,4,5\\}$ and the longest non-increasing subsequence $\\{6,4,4\\}$. The longest common subsequence of those two subsequences is $\\{4,4\\}$.","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":"losmi247","problem_tester":"","date_added":"18-09-2021","tags":{"0":"easy","1":"greedy","2":"longest","3":"losmi247","4":"ltime100"},"problem_difficulty_level":"Unavailable","best_tag":"","editorial_url":"https://discuss.codechef.com/problems/SUBLCS","time":{"view_start_date":1632585602,"submit_start_date":1632585602,"visible_start_date":1632585602,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=SUBLCS","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problem statements in [Mandarin](https://www.codechef.com/download/translated/LTIME100/mandarin/SUBLCS.pdf), [Bengali](https://www.codechef.com/download/translated/LTIME100/bengali/SUBLCS.pdf), [Russian](https://www.codechef.com/download/translated/LTIME100/russian/SUBLCS.pdf), and [Vietnamese](https://www.codechef.com/download/translated/LTIME100/vietnamese/SUBLCS.pdf) as well.
You are given a sequence $A_1, A_2, \ldots, A_N$. Consider **every** pair that consists of:
$1)$ One of the **longest** non-decreasing subsequences of $A$.
$2)$ One of the **longest** non-increasing subsequences of $A$.
Find the maximum possible length of the longest common subsequence of the two sequences in such a pair.
<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>