🏡 index : github.com/captn3m0/codechef.git

---
{"category_name":"medium","problem_code":"MMP","problem_name":"Min-Max Path","problemComponents":{"constraints":"- $1 \\leq T \\leq 50$\n- $2 \\leq N \\leq 500$\n- $1 \\leq V_i \\leq N$ for each valid $i$\n- the sum of $N$ over all test cases does not exceed $500$\n","constraintsState":true,"subtasks":"**Subtask #1 (15 points):** $V_i \\le 2$ for each valid $i$\n\n**Subtask #2 (35 points):** the sum of $N$ over all test cases does not exceed $100$\n\n**Subtask #3 (50 points):** original constraints","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- The first line of each test case contains a single integer $N$.\n- The second line contains $N$ space-separated integers $V_1, V_2, \\ldots, V_N$.\n","inputFormatState":true,"outputFormat":"For each test case, print a single line containing one integer --- the minimum number of jumps, or $-1$ if it is impossible to reach stone $N$.","outputFormatState":true,"sampleTestCases":{"0":{"id":1,"input":"3\n3\n1 2 1\n5\n1 3 4 1 2\n11\n1 4 5 3 2 8 3 4 3 7 1","output":"1\n-1\n3","explanation":"**Example case 1:** You can jump from stone $1$ to stone $N$ directly because $\\min(V_1) + \\max(V_N) = 2$ and the length of the jump is exactly $2$.\n\n**Example case 2:** It is impossible to reach stone $N$.\n\n**Example case 3:** One valid sequence of visited stones which uses the minimum number or jumps is $(1, 5, 9, 11)$. It is valid because the lengths of the jumps are:\n- first jump: $\\min(V_1) + \\max(V_5, V_9, V_{11}) = 4$\n- second jump: $\\min(V_1, V_5) + \\max(V_9, V_{11}) = 4$\n- third jump: $\\min(V_1, V_5, V_9) + \\max(V_{11}) = 2$\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.5,"source_sizelimit":50000,"problem_author":"dannyboy1204","problem_tester":"","date_added":"25-08-2021","tags":{"0":"dannyboy1204"},"problem_difficulty_level":"Medium","best_tag":"","editorial_url":"","time":{"view_start_date":1630170002,"submit_start_date":1630170002,"visible_start_date":1630170002,"end_date":1735669800},"is_direct_submittable":false,"problemDiscussURL":"https://discuss.codechef.com/search?q=MMP","is_proctored":false,"visitedContests":{},"layout":"problem"}
---
### Read problem statements in [Mandarin](https://www.codechef.com/download/translated/LTIME99/mandarin/MMP.pdf), [Russian](https://www.codechef.com/download/translated/LTIME99/russian/MMP.pdf), and [Vietnamese](https://www.codechef.com/download/translated/LTIME99/vietnamese/MMP.pdf) as well.

There are $N$ stones numbered $1$ through $N$. Each stone has a value written on it; for each valid $i$, the value on stone $i$ is $V_i$.

You are initially on the first stone and your goal is to jump to stone $N$ by perfoming a valid sequence of jumps. Formally, let's denote a sequence of visited stones during these jumps by $S_1, S_2, \ldots, S_M$, where $M-1$ is the number of jumps. Then, the following conditions must be satisfied:
- $1 = S_1 \lt S_2 \lt \ldots \lt S_M = N$
- for each $i$ ($1 \le i \lt M$), $\min(V_{S_1}, V_{S_2}, \ldots, V_{S_i}) + \max(V_{S_{i+1}}, \ldots, V_{S_M}) = S_{i+1} - S_i$, which is the length of the $i$-th jump

Find the minimum number of jumps needed to reach stone $N$, or report that it is impossible to reach stone $N$ using valid jumps.

<aside style='background: #f8f8f8;padding: 10px 15px;'><div>All submissions for this problem are available.</div></aside>