Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は本棚に本を並べようとしています。本棚には第 1 段、第 2 段、…、第 N 段の N 個の段があり、各段の横幅はすべて W センチメートルです。はじめ、すべての段は空です。
本棚に並べる本は N 冊あり、i 番目 (1 \leq i \leq N) の本の厚さは L_i センチメートルです。各本の厚さは W 以下であることが保証されているため、どの本も単体では必ず 1 つの段に収まります。
ある段に k 冊 (k \geq 1) の本(厚さ a_1, a_2, \ldots, a_k)が左から順に並んでいるとき、その段で使用されている幅を (a_1 + a_2 + \cdots + a_k) + (k - 1) センチメートルと定めます。これは、隣り合う本どうしの間に必ず 1 センチメートルの隙間を設けることに対応します。例えば、1 冊だけ置かれている段の使用幅はその本の厚さに等しく、1 冊も本が置かれていない段の使用幅は 0 センチメートルです。
高橋君は 1 番目の本から N 番目の本まで、番号の小さい順に 1 冊ずつ以下のルールに従って配置していきます。「現在の段」は最初は第 1 段です。i 番目の本を配置するとき:
- 現在の段に 1 冊も本が置かれていない場合、現在の段の左端から i 番目の本を配置します。現在の段は変わりません。
- 現在の段にすでに本があり、使用されている幅が S センチメートルであるとします。
- S + 1 + L_i \leq W ならば、既に置かれている本の右側に 1 センチメートルの隙間を空けて、i 番目の本を現在の段に続けて配置します。現在の段は変わりません。
- S + 1 + L_i > W ならば、i 番目の本は現在の段に収まりません。現在の段の次の段(番号が 1 大きい段)を新たな「現在の段」とします。番号の小さい段から順に埋めていくため、この新たな段は必ず空です。その段の左端から i 番目の本を配置します。
すべての N 冊の本を並べ終えたとき、本が 1 冊以上置かれている段の数は全部で何段になるかを求めてください。
制約
- 1 \leq N \leq 10^6
- 1 \leq W \leq 10^9
- 1 \leq L_i \leq W (1 \leq i \leq N)
- 入力はすべて整数である。
入力
N W L_1 L_2 \ldots L_N
- 1 行目には、本の冊数を表す整数 N と、本棚の各段の横幅を表す整数 W が、スペース区切りで与えられる。
- 2 行目には、各本の厚さを表す整数 L_1, L_2, \ldots, L_N が、スペース区切りで与えられる。
出力
本が 1 冊以上置かれている段の数を 1 行で出力してください。
入力例 1
5 10 3 4 2 5 3
出力例 1
3
入力例 2
8 15 5 5 5 5 5 5 5 5
出力例 2
4
入力例 3
10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
出力例 3
10
Score : 266 pts
Problem Statement
Takahashi is arranging books on a bookshelf. The bookshelf has N shelves: shelf 1, shelf 2, …, shelf N, and each shelf has a width of W centimeters. Initially, all shelves are empty.
There are N books to arrange on the bookshelf, and the i-th book (1 \leq i \leq N) has a thickness of L_i centimeters. It is guaranteed that the thickness of each book is at most W, so every book can individually fit on a single shelf.
When k (k \geq 1) books (with thicknesses a_1, a_2, \ldots, a_k) are lined up from left to right on a shelf, the used width of that shelf is defined as (a_1 + a_2 + \cdots + a_k) + (k - 1) centimeters. This corresponds to always leaving a 1-centimeter gap between adjacent books. For example, the used width of a shelf with only one book equals the thickness of that book, and the used width of a shelf with no books is 0 centimeters.
Takahashi places the books one by one in order from the 1-st book to the N-th book, following the rules below. The "current shelf" is initially shelf 1. When placing the i-th book:
- If no books are currently placed on the current shelf, place the i-th book starting from the left end of the current shelf. The current shelf does not change.
- Suppose books are already on the current shelf, and the used width is S centimeters.
- If S + 1 + L_i \leq W, place the i-th book on the current shelf to the right of the already placed books, leaving a 1-centimeter gap. The current shelf does not change.
- If S + 1 + L_i > W, the i-th book does not fit on the current shelf. The shelf after the current shelf (the shelf with the number one greater) becomes the new "current shelf." Since shelves are filled in order from the smallest number, this new shelf is always empty. Place the i-th book starting from the left end of that shelf.
After all N books have been arranged, determine the total number of shelves that have at least one book placed on them.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq W \leq 10^9
- 1 \leq L_i \leq W (1 \leq i \leq N)
- All input values are integers.
Input
N W L_1 L_2 \ldots L_N
- The first line contains an integer N representing the number of books and an integer W representing the width of each shelf, separated by a space.
- The second line contains integers L_1, L_2, \ldots, L_N representing the thickness of each book, separated by spaces.
Output
Print the number of shelves that have at least one book placed on them, in a single line.
Sample Input 1
5 10 3 4 2 5 3
Sample Output 1
3
Sample Input 2
8 15 5 5 5 5 5 5 5 5
Sample Output 2
4
Sample Input 3
10 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
10
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君はあるチェーン店の売上データを分析するアルバイトをしています。
このチェーン店には N 日分の売上記録があり、i 日目 (1 \leq i \leq N) の売上は T_i 円でした。
高橋君は連続する K 日間の売上の平均値を計算し、その最大値を求めることで、最も売上が好調だった期間の成績を把握しようとしています。
具体的には、各整数 l (1 \leq l \leq N - K + 1) に対して、l 日目から l + K - 1 日目までの連続する K 日間の売上の平均値を
M_l = \frac{T_l + T_{l+1} + \cdots + T_{l+K-1}}{K}
と定めます。すべての l (1 \leq l \leq N - K + 1) に対する M_l の最大値を M とします。
\lfloor M \times 1000 \rfloor を求めてください。ここで \lfloor x \rfloor は x 以下の最大の整数(床関数)を表します。これは、M の小数第 4 位以下を切り捨てたものを 1000 倍した値に相当します。
ヒント: 連続する K 日間の売上の合計の最大値を S とすると、T_i はすべて整数であるため S も整数であり、\lfloor M \times 1000 \rfloor = \left\lfloor \frac{1000 \times S}{K} \right\rfloor が成り立ちます。したがって、答えは整数演算のみで求めることができます。
制約
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq T_i \leq 10^6
- 入力はすべて整数
入力
N K T_1 T_2 \cdots T_N
- 1 行目には、売上記録の日数を表す整数 N と、期間の長さを表す整数 K が、スペース区切りで与えられる。
- 2 行目には、各日の売上を表す整数 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。
出力
連続する K 日間の売上の平均値の最大値を M としたとき、\lfloor M \times 1000 \rfloor の値を 1 行で出力せよ。
入力例 1
5 3 10 20 30 25 15
出力例 1
25000
入力例 2
7 2 100 150 80 200 190 50 120
出力例 2
195000
入力例 3
10 4 1234 5678 9012 3456 7890 2345 6789 1357 2468 8024
出力例 3
6509000
Score : 300 pts
Problem Statement
Takahashi is working a part-time job analyzing sales data for a chain store.
This chain store has sales records for N days, and the sales on day i (1 \leq i \leq N) were T_i yen.
Takahashi wants to calculate the average sales over every consecutive K-day period and find the maximum value, in order to understand the performance of the period with the best sales.
Specifically, for each integer l (1 \leq l \leq N - K + 1), the average sales over the consecutive K days from day l to day l + K - 1 is defined as
M_l = \frac{T_l + T_{l+1} + \cdots + T_{l+K-1}}{K}
Let M be the maximum value of M_l over all l (1 \leq l \leq N - K + 1).
Find \lfloor M \times 1000 \rfloor, where \lfloor x \rfloor denotes the largest integer not exceeding x (the floor function). This corresponds to truncating M below the 4th decimal place and multiplying by 1000.
Hint: Let S be the maximum sum of sales over any consecutive K days. Since all T_i are integers, S is also an integer, and \lfloor M \times 1000 \rfloor = \left\lfloor \frac{1000 \times S}{K} \right\rfloor holds. Therefore, the answer can be computed using only integer arithmetic.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq T_i \leq 10^6
- All inputs are integers
Input
N K T_1 T_2 \cdots T_N
- The first line contains the integer N representing the number of days of sales records and the integer K representing the length of the period, separated by a space.
- The second line contains the integers T_1, T_2, \ldots, T_N representing the sales on each day, separated by spaces.
Output
Let M be the maximum average sales over any consecutive K-day period. Output the value of \lfloor M \times 1000 \rfloor on a single line.
Sample Input 1
5 3 10 20 30 25 15
Sample Output 1
25000
Sample Input 2
7 2 100 150 80 200 190 50 120
Sample Output 2
195000
Sample Input 3
10 4 1234 5678 9012 3456 7890 2345 6789 1357 2468 8024
Sample Output 3
6509000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は N 個のドミノを一列に並べている。左から順に 1, 2, \ldots, N と番号が付けられており、各ドミノ i には「高さ」を表す正整数 A_i が定められている。
この世界のドミノ倒しでは、倒れたドミノはすぐ右隣のまだ立っているドミノに衝突し、相手の高さが自分より真に小さければ、そのドミノも倒してしまう。具体的には、連鎖は以下のルールに従って進む。
- ドミノ i が倒れたとき、まだ倒れていないドミノのうち、i より右にあり番号が最も小さいもの(すなわち、ドミノ i のすぐ右隣にあるまだ倒れていないドミノ)をドミノ j とする。
- そのようなドミノ j が存在し、かつ A_j < A_i(ドミノ j の高さがドミノ i の高さより真に小さい)ならば、ドミノ i がドミノ j を倒す。続いて、新たに倒れたドミノ j を起点として同じルールを適用する。すなわち、次はドミノ j の高さ A_j を基準にして、さらに右隣のまだ倒れていないドミノを倒せるか判定する。
- ドミノ i より右にまだ倒れていないドミノが存在しない場合、またはドミノ j の高さがドミノ i の高さ以上(A_j \geq A_i)である場合、連鎖はそこで止まる。
高橋君はドミノ 1, 2, \ldots, N の順に、まだ倒れていなければ指で倒す操作を行う。既に倒れている場合は何もせず次のドミノに進む。指で倒した場合、そのドミノを起点として上記の連鎖ルールが適用される。
すべてのドミノが倒れた後、各ドミノ i(1 \leq i \leq N)について、ドミノ i を倒した直接の原因を求めてください。ドミノ i が別のドミノ k の連鎖によって倒された場合(上記ルールにおいてドミノ k が倒れた直後にドミノ i が倒された場合)は、そのドミノの番号 k を出力してください。高橋君が指で直接倒した場合は 0 を出力してください。
制約
- 1 \leq N \leq 5 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力はすべて整数である。
入力
N A_1 A_2 \ldots A_N
- 1 行目には、ドミノの個数を表す整数 N が与えられる。
- 2 行目には、各ドミノの高さを表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。
出力
N 個の整数をスペース区切りで 1 行に出力せよ。i 番目の整数は、ドミノ i を倒した直接の原因となったドミノの番号(高橋君が直接倒した場合は 0)である。
入力例 1
5 5 3 4 2 1
出力例 1
0 1 0 3 4
入力例 2
3 2 2 1
出力例 2
0 0 2
入力例 3
12 10 7 6 8 5 4 9 3 3 2 11 1
出力例 3
0 1 2 0 4 5 0 7 0 9 0 11
入力例 4
30 100 90 80 85 70 60 50 75 74 73 20 21 19 18 200 150 160 140 130 120 110 111 109 108 50 49 48 47 46 45
出力例 4
0 1 2 0 4 5 6 0 8 9 10 0 12 13 0 15 0 17 18 19 20 0 22 23 24 25 26 27 28 29
入力例 5
1 1000000000
出力例 5
0
Score : 366 pts
Problem Statement
Takahashi has lined up N dominoes in a row. They are numbered 1, 2, \ldots, N from left to right, and each domino i has a positive integer A_i representing its "height."
In this world's domino toppling, a fallen domino collides with the nearest still-standing domino immediately to its right, and if the other domino's height is strictly less than its own, it knocks that domino down as well. Specifically, the chain proceeds according to the following rules:
- When domino i falls, let domino j be the domino that is to the right of i with the smallest number among those that have not yet fallen (i.e., the nearest still-standing domino immediately to the right of domino i).
- If such a domino j exists and A_j < A_i (the height of domino j is strictly less than the height of domino i), then domino i knocks down domino j. Subsequently, the same rule is applied starting from the newly fallen domino j. That is, using domino j's height A_j as the reference, it is determined whether the next still-standing domino to the right can be knocked down.
- If there is no still-standing domino to the right of domino i, or if the height of domino j is greater than or equal to the height of domino i (A_j \geq A_i), the chain stops there.
Takahashi performs the operation of pushing down dominoes with his finger in the order 1, 2, \ldots, N, pushing each one only if it has not yet fallen. If it has already fallen, he does nothing and moves on to the next domino. When he pushes a domino with his finger, the chain rule described above is applied starting from that domino.
After all dominoes have fallen, for each domino i (1 \leq i \leq N), determine the direct cause of domino i falling. If domino i was knocked down by the chain from another domino k (i.e., domino i fell immediately after domino k fell according to the above rules), output that domino's number k. If Takahashi directly pushed it down with his finger, output 0.
Constraints
- 1 \leq N \leq 5 \times 10^5
- 1 \leq A_i \leq 10^9
- All inputs are integers.
Input
N A_1 A_2 \ldots A_N
- The first line contains an integer N representing the number of dominoes.
- The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the height of each domino.
Output
Output N integers separated by spaces on a single line. The i-th integer is the number of the domino that directly caused domino i to fall (or 0 if Takahashi directly pushed it down).
Sample Input 1
5 5 3 4 2 1
Sample Output 1
0 1 0 3 4
Sample Input 2
3 2 2 1
Sample Output 2
0 0 2
Sample Input 3
12 10 7 6 8 5 4 9 3 3 2 11 1
Sample Output 3
0 1 2 0 4 5 0 7 0 9 0 11
Sample Input 4
30 100 90 80 85 70 60 50 75 74 73 20 21 19 18 200 150 160 140 130 120 110 111 109 108 50 49 48 47 46 45
Sample Output 4
0 1 2 0 4 5 6 0 8 9 10 0 12 13 0 15 0 17 18 19 20 0 22 23 24 25 26 27 28 29
Sample Input 5
1 1000000000
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は N 行 M 列のグリッドを持っています。グリッドの i 行 j 列目のマスには整数 A_{i,j} が書かれています。
高橋君は、このグリッドの中から長方形領域を 1 つ選び、その領域内に含まれる全てのマスの値の合計を求めます。ここで「長方形領域」とは、整数 r_1, r_2 ( 1 \le r_1 \le r_2 \le N )と c_1, c_2 ( 1 \le c_1 \le c_2 \le M )を選んだとき、行が r_1 以上 r_2 以下かつ列が c_1 以上 c_2 以下である全てのマスからなる領域のことを指します。
高橋君は、この合計が最大となるように長方形領域を選びたいと考えています。長方形領域は必ず 1 つ以上のマスを含むため、長方形領域を選ばずに合計を 0 とすることはできないことに注意してください。特に、全てのマスの値が負である場合でも、いずれかの長方形領域を選ばなければなりません。
高橋君が選べる長方形領域内の値の合計の最大値を求めてください。
制約
- 1 \leq N \leq 500
- 1 \leq M \leq 500
- -10^4 \leq A_{i,j} \leq 10^4
- 入力はすべて整数である
入力
N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
- 1 行目には、グリッドの行数を表す整数 N と列数を表す整数 M が、スペース区切りで与えられる。
- 続く N 行のうち i 行目( 1 \le i \le N )には、グリッドの i 行目の各マスの値 A_{i,1}, A_{i,2}, \ldots, A_{i,M} がスペース区切りで与えられる。
出力
高橋君が選べる長方形領域内の値の合計の最大値を整数として 1 行で出力せよ。
入力例 1
3 3 1 -2 3 -4 5 6 7 -8 9
出力例 1
18
入力例 2
2 2 -5 -3 -1 -7
出力例 2
-1
入力例 3
5 4 2 1 -3 4 -1 3 2 -1 4 -2 1 3 -3 1 -4 2 1 2 3 -1
出力例 3
14
入力例 4
8 10 3 -1 2 -4 5 1 -3 2 4 -2 -2 4 1 3 -1 2 -5 3 1 0 1 -3 5 2 4 -1 3 -2 1 2 -4 2 -1 6 3 5 -2 4 -3 1 0 1 3 -2 4 2 1 -1 5 3 -1 -2 4 1 -3 6 2 3 -4 2 3 1 -5 2 1 -2 4 1 3 -1 -2 3 2 -1 3 1 -3 2 -1 4
出力例 4
77
入力例 5
1 1 -7
出力例 5
-7
Score : 400 pts
Problem Statement
Takahashi has a grid with N rows and M columns. The cell at row i and column j of the grid contains an integer A_{i,j}.
Takahashi will select one rectangular region from this grid and compute the sum of the values of all cells contained in that region. Here, a "rectangular region" refers to the region consisting of all cells whose row is between r_1 and r_2 (inclusive) and whose column is between c_1 and c_2 (inclusive), where r_1, r_2 (1 \le r_1 \le r_2 \le N) and c_1, c_2 (1 \le c_1 \le c_2 \le M) are chosen integers.
Takahashi wants to choose the rectangular region so that this sum is maximized. Note that a rectangular region must contain at least one cell, so it is not possible to choose no rectangular region and have a sum of 0. In particular, even if all cell values are negative, he must still choose some rectangular region.
Find the maximum possible sum of values within a rectangular region that Takahashi can choose.
Constraints
- 1 \leq N \leq 500
- 1 \leq M \leq 500
- -10^4 \leq A_{i,j} \leq 10^4
- All input values are integers
Input
N M
A_{1,1} A_{1,2} \ldots A_{1,M}
A_{2,1} A_{2,2} \ldots A_{2,M}
\vdots
A_{N,1} A_{N,2} \ldots A_{N,M}
- The first line contains an integer N representing the number of rows and an integer M representing the number of columns of the grid, separated by a space.
- The following N lines each describe a row of the grid. The i-th of these lines (1 \le i \le N) contains the values A_{i,1}, A_{i,2}, \ldots, A_{i,M} of the cells in row i of the grid, separated by spaces.
Output
Print the maximum possible sum of values within a rectangular region that Takahashi can choose, as an integer on a single line.
Sample Input 1
3 3 1 -2 3 -4 5 6 7 -8 9
Sample Output 1
18
Sample Input 2
2 2 -5 -3 -1 -7
Sample Output 2
-1
Sample Input 3
5 4 2 1 -3 4 -1 3 2 -1 4 -2 1 3 -3 1 -4 2 1 2 3 -1
Sample Output 3
14
Sample Input 4
8 10 3 -1 2 -4 5 1 -3 2 4 -2 -2 4 1 3 -1 2 -5 3 1 0 1 -3 5 2 4 -1 3 -2 1 2 -4 2 -1 6 3 5 -2 4 -3 1 0 1 3 -2 4 2 1 -1 5 3 -1 -2 4 1 -3 6 2 3 -4 2 3 1 -5 2 1 -2 4 1 3 -1 -2 3 2 -1 3 1 -3 2 -1 4
Sample Output 4
77
Sample Input 5
1 1 -7
Sample Output 5
-7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は山岳地帯のハイキングコースを歩く計画を立てています。
このコースは N 個の地点が一直線に並んでおり、地点 i(1 \leq i \leq N)の標高は H_i です。高橋君は地点 1 から出発し、地点 1, 2, \ldots, N の順に進んで地点 N まで歩きます。
高橋君は高所恐怖症であり、地点 i から地点 i+1 への移動(1 \leq i \leq N-1)において、標高が K 以上下がる場合(すなわち H_i - H_{i+1} \geq K となる場合)に恐怖を感じます。それ以外の移動では恐怖を感じません。
コースの管理者は、高橋君が恐怖を感じずに歩けるよう、地点 2, 3, \ldots, N-1 の中から任意の個数(0 個でもよい)の地点を選び、それぞれの標高を好きな非負整数値に変更できます。地点ごとに異なる値に変更してもよく、変更後の値に上限はありません。ただし、地点 1 と地点 N の標高は変更できません。
高橋君が一度も恐怖を感じることなく地点 1 から地点 N まで歩けるようにするために、標高を変更する地点数の最小値を求めてください。ただし、どのように標高を変更しても恐怖を回避できない場合は -1 を出力してください。
注意: N \geq 3 であっても -1 になる場合があります。変更できない地点 1 と地点 N の標高差が大きすぎると、中間地点の標高をどのように変更しても恐怖を回避できません。
制約
- 2 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^9
- 0 \leq H_i \leq 10^9(1 \leq i \leq N)
- 入力はすべて整数である
入力
N K H_1 H_2 \ldots H_N
1 行目には、地点の数 N と恐怖を感じる基準となる標高の下降量 K が、スペース区切りで与えられる。2 行目には、各地点の標高 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。
出力
高橋君が一度も恐怖を感じずに地点 1 から地点 N まで歩けるようにするために、標高を変更する地点数の最小値を 1 行で出力せよ。ただし、恐怖を回避することが不可能な場合は -1 を出力せよ。
入力例 1
5 3 10 8 4 5 3
出力例 1
1
入力例 2
3 5 20 0 5
出力例 2
-1
入力例 3
12 4 15 13 9 12 8 6 10 5 4 7 3 2
出力例 3
4
入力例 4
30 7 100 95 90 82 85 78 70 72 65 60 54 58 50 49 42 35 38 31 25 20 23 18 12 10 5 8 3 2 1 0
出力例 4
8
入力例 5
2 1 0 0
出力例 5
0
Score : 433 pts
Problem Statement
Takahashi is planning to walk a hiking course in a mountainous area.
This course consists of N points arranged in a straight line, and the elevation of point i (1 \leq i \leq N) is H_i. Takahashi starts at point 1 and walks to point N by proceeding through points 1, 2, \ldots, N in order.
Takahashi has a fear of heights, and when moving from point i to point i+1 (1 \leq i \leq N-1), he feels fear if the elevation decreases by K or more (that is, if H_i - H_{i+1} \geq K). He does not feel fear during any other movements.
The course manager can select any number (possibly 0) of points from points 2, 3, \ldots, N-1 and change each of their elevations to any non-negative integer value, so that Takahashi can walk without feeling fear. Different points may be changed to different values, and there is no upper limit on the changed values. However, the elevations of points 1 and N cannot be changed.
Find the minimum number of points whose elevations need to be changed so that Takahashi can walk from point 1 to point N without ever feeling fear. If it is impossible to avoid fear no matter how the elevations are changed, output -1.
Note: Even when N \geq 3, the answer may be -1. If the elevation difference between the unchangeable points 1 and N is too large, fear cannot be avoided no matter how the elevations of intermediate points are changed.
Constraints
- 2 \leq N \leq 3 \times 10^5
- 1 \leq K \leq 10^9
- 0 \leq H_i \leq 10^9 (1 \leq i \leq N)
- All inputs are integers
Input
N K H_1 H_2 \ldots H_N
The first line contains the number of points N and the elevation descent threshold K that triggers fear, separated by a space. The second line contains the elevations of each point H_1, H_2, \ldots, H_N, separated by spaces.
Output
Print in one line the minimum number of points whose elevations need to be changed so that Takahashi can walk from point 1 to point N without ever feeling fear. If it is impossible to avoid fear, output -1.
Sample Input 1
5 3 10 8 4 5 3
Sample Output 1
1
Sample Input 2
3 5 20 0 5
Sample Output 2
-1
Sample Input 3
12 4 15 13 9 12 8 6 10 5 4 7 3 2
Sample Output 3
4
Sample Input 4
30 7 100 95 90 82 85 78 70 72 65 60 54 58 50 49 42 35 38 31 25 20 23 18 12 10 5 8 3 2 1 0
Sample Output 4
8
Sample Input 5
2 1 0 0
Sample Output 5
0