Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君は音楽サークルの部長です。高橋君を除くサークルのメンバーは N 人います。
今度のサークル活動で演奏する曲を決めるため、高橋君は 1 番から M 番までの番号が付けられた M 曲の候補リストを用意しました。N 人のメンバーはそれぞれ、このリストの中から自分が演奏したい曲を 1 曲以上選び(同じ曲を複数回選ぶことはありません)、高橋君に報告しました。なお、高橋君自身は曲を選びません。
i 番目のメンバー (1 \leq i \leq N) は K_i 曲を選び、選んだ曲の番号は C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} でした。
N 人のメンバー全員が共通して選んだ曲の個数を求めてください。すなわち、1 番から M 番までの曲のうち、N 人全員が選んでいる曲がいくつあるかを出力してください。
制約
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq K_i \leq M
- 1 \leq C_{i,1} < C_{i,2} < \cdots < C_{i,K_i} \leq M(各メンバーが選んだ曲の番号は相異なり、狭義昇順で与えられる)
- \sum_{i=1}^{N} K_i \leq 2 \times 10^5
- 入力はすべて整数
入力
N M
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_N C_{N,1} C_{N,2} \ldots C_{N,K_N}
- 1 行目には、メンバーの人数 N と、候補リストの曲数 M が、スペース区切りで与えられる。
- 第 (i+1) 行目 (1 \leq i \leq N) には、i 番目のメンバーが選んだ曲の数 K_i と、選んだ曲の番号 C_{i,1}, C_{i,2}, \ldots, C_{i,K_i} が、スペース区切りで与えられる。曲の番号は狭義昇順に並んでいる。
出力
N 人のメンバー全員が共通して選んだ曲の個数を 1 行で出力せよ。
入力例 1
3 5 3 1 2 4 2 2 4 4 1 2 4 5
出力例 1
2
入力例 2
4 10 5 1 3 5 7 9 3 2 4 6 4 1 3 5 7 6 1 2 3 5 7 9
出力例 2
0
入力例 3
5 100 10 5 10 20 30 40 50 60 70 80 90 8 5 10 20 30 50 70 80 90 12 1 5 10 15 20 25 30 50 70 80 90 100 6 5 10 30 50 70 90 9 5 10 20 30 50 60 70 80 90
出力例 3
6
Score : 266 pts
Problem Statement
Takahashi is the leader of a music club. There are N members in the club, excluding Takahashi.
To decide which songs to perform at the next club activity, Takahashi prepared a candidate list of M songs numbered from 1 to M. Each of the N members selected one or more songs they want to perform from this list (without selecting the same song more than once) and reported their choices to Takahashi. Note that Takahashi himself does not select any songs.
The i-th member (1 \leq i \leq N) selected K_i songs, and the numbers of the selected songs were C_{i,1}, C_{i,2}, \ldots, C_{i,K_i}.
Find the number of songs that were selected by all N members in common. In other words, among the songs numbered from 1 to M, output how many songs were selected by all N members.
Constraints
- 1 \leq N \leq 10^5
- 1 \leq M \leq 10^5
- 1 \leq K_i \leq M
- 1 \leq C_{i,1} < C_{i,2} < \cdots < C_{i,K_i} \leq M (The song numbers selected by each member are distinct and given in strictly ascending order)
- \sum_{i=1}^{N} K_i \leq 2 \times 10^5
- All input values are integers
Input
N M
K_1 C_{1,1} C_{1,2} \ldots C_{1,K_1}
K_2 C_{2,1} C_{2,2} \ldots C_{2,K_2}
\vdots
K_N C_{N,1} C_{N,2} \ldots C_{N,K_N}
- The first line contains the number of members N and the number of songs in the candidate list M, separated by a space.
- The (i+1)-th line (1 \leq i \leq N) contains the number of songs selected by the i-th member K_i, followed by the numbers of the selected songs C_{i,1}, C_{i,2}, \ldots, C_{i,K_i}, separated by spaces. The song numbers are listed in strictly ascending order.
Output
Output in one line the number of songs that were selected by all N members in common.
Sample Input 1
3 5 3 1 2 4 2 2 4 4 1 2 4 5
Sample Output 1
2
Sample Input 2
4 10 5 1 3 5 7 9 3 2 4 6 4 1 3 5 7 6 1 2 3 5 7 9
Sample Output 2
0
Sample Input 3
5 100 10 5 10 20 30 40 50 60 70 80 90 8 5 10 20 30 50 70 80 90 12 1 5 10 15 20 25 30 50 70 80 90 100 6 5 10 30 50 70 90 9 5 10 20 30 50 60 70 80 90
Sample Output 3
6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は山道のハイキングコースに挑戦します。
このハイキングコースには N 個のチェックポイントがあり、チェックポイント 1 から N まで番号が付けられています。コースは一本道であり、高橋君はチェックポイント 1 からスタートし、チェックポイント 1, 2, 3, \ldots, N の順に進みます。高橋君は好きなチェックポイントでハイキングを終了できます。つまり、途中のチェックポイントでそれ以上先に進まずに終了してもよいですし、チェックポイント N まで進んでもよいです。
各チェックポイント i(1 \leq i \leq N)には「景観スコア」 S_i が設定されています。高橋君がチェックポイント i に到達すると、景観スコア S_i を得ます。一方、チェックポイント i からチェックポイント i+1 へ移動するには体力コスト C_i(1 \leq i \leq N-1)がかかります。
高橋君は、いずれかのチェックポイント k(1 \leq k \leq N)でハイキングを終了します。このとき、高橋君の「ハイキングの満足度」は次の式で計算されます:
\left(\sum_{i=1}^{k} S_i\right) - \left(\sum_{i=1}^{k-1} C_i\right)
すなわち、チェックポイント 1 から k までで得られる景観スコアの合計から、チェックポイント 1 から k に到達するまでにかかる体力コストの合計を引いた値です。k = 1 の場合、移動は発生しないため体力コストの合計は 0 となり、満足度は S_1 です。
高橋君がハイキングを終了するチェックポイントを最適に選んだとき、「ハイキングの満足度」の最大値を求めてください。
制約
- 1 \leq N \leq 10^6
- 1 \leq S_i \leq 10^9(1 \leq i \leq N)
- 1 \leq C_i \leq 10^9(1 \leq i \leq N-1)
- 入力はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
S_1 S_2 \ldots S_N
C_1 C_2 \ldots C_{N-1}
- 1 行目には、チェックポイントの数を表す整数 N が与えられる。
- 2 行目には、各チェックポイントの景観スコアを表す N 個の整数 S_1, S_2, \ldots, S_N がスペース区切りで与えられる。
- 3 行目には、隣接するチェックポイント間の体力コストを表す N-1 個の整数 C_1, C_2, \ldots, C_{N-1} がスペース区切りで与えられる。ここで C_i はチェックポイント i からチェックポイント i+1 への移動にかかる体力コストである。ただし、N = 1 の場合、3 行目は空行になる。
出力
「ハイキングの満足度」の最大値を 1 行で出力してください。
入力例 1
5 5 3 8 2 6 4 10 1 3
出力例 1
6
入力例 2
4 10 1 1 1 100 100 100
出力例 2
10
入力例 3
12 7 15 3 20 6 8 25 4 10 12 5 18 5 12 4 30 2 7 20 1 15 3 9
出力例 3
25
入力例 4
20 13 8 21 5 34 2 18 27 6 11 40 3 16 9 25 7 30 4 14 22 6 20 4 10 30 1 15 35 2 8 12 25 5 18 3 28 7 9 11
出力例 4
66
入力例 5
1 1000000000
出力例 5
1000000000
Score : 300 pts
Problem Statement
Takahashi is going to attempt a mountain trail hiking course.
This hiking course has N checkpoints, numbered from checkpoint 1 to N. The course is a single path, and Takahashi starts at checkpoint 1 and proceeds in the order of checkpoints 1, 2, 3, \ldots, N. Takahashi can end the hike at any checkpoint he likes. That is, he may stop at an intermediate checkpoint without proceeding further, or he may go all the way to checkpoint N.
Each checkpoint i (1 \leq i \leq N) has a "scenery score" S_i assigned to it. When Takahashi reaches checkpoint i, he gains the scenery score S_i. On the other hand, moving from checkpoint i to checkpoint i+1 costs a stamina cost of C_i (1 \leq i \leq N-1).
Takahashi ends the hike at some checkpoint k (1 \leq k \leq N). At that point, Takahashi's "hiking satisfaction" is calculated by the following formula:
\left(\sum_{i=1}^{k} S_i\right) - \left(\sum_{i=1}^{k-1} C_i\right)
In other words, it is the total scenery scores gained from checkpoints 1 through k, minus the total stamina cost required to travel from checkpoint 1 to checkpoint k. When k = 1, no movement occurs, so the total stamina cost is 0, and the satisfaction is S_1.
Find the maximum value of "hiking satisfaction" when Takahashi optimally chooses the checkpoint at which to end the hike.
Constraints
- 1 \leq N \leq 10^6
- 1 \leq S_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq C_i \leq 10^9 (1 \leq i \leq N-1)
- All inputs are integers.
Input
The input is given from standard input in the following format.
N
S_1 S_2 \ldots S_N
C_1 C_2 \ldots C_{N-1}
- The first line contains an integer N representing the number of checkpoints.
- The second line contains N integers S_1, S_2, \ldots, S_N separated by spaces, representing the scenery scores of each checkpoint.
- The third line contains N-1 integers C_1, C_2, \ldots, C_{N-1} separated by spaces, representing the stamina costs between adjacent checkpoints. Here, C_i is the stamina cost for moving from checkpoint i to checkpoint i+1. However, when N = 1, the third line is an empty line.
Output
Output the maximum value of "hiking satisfaction" in a single line.
Sample Input 1
5 5 3 8 2 6 4 10 1 3
Sample Output 1
6
Sample Input 2
4 10 1 1 1 100 100 100
Sample Output 2
10
Sample Input 3
12 7 15 3 20 6 8 25 4 10 12 5 18 5 12 4 30 2 7 20 1 15 3 9
Sample Output 3
25
Sample Input 4
20 13 8 21 5 34 2 18 27 6 11 40 3 16 9 25 7 30 4 14 22 6 20 4 10 30 1 15 35 2 8 12 25 5 18 3 28 7 9 11
Sample Output 4
66
Sample Input 5
1 1000000000
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、N 本の果物の木が一列に並んでいる果樹園を訪れました。木には端から順に 1, 2, \ldots, N と番号が付けられており、i 番目の木には A_i 個の果物が実っています。
高橋君は 1 番目の木から N 番目の木まで、番号の小さい順に 1 本ずつ木の前を通っていきます。各木の前を通るのはちょうど 1 回であり、引き返すことはできません。それぞれの木の前を通る際、その木の果物をすべて収穫するか、まったく収穫しないかのどちらかを選びます。一部だけを収穫することはできません。どの木からも収穫しないという選択も許されます。
ただし、ある木で果物を収穫すると、収穫の疲れにより、その木の直後の K 本の木では収穫ができなくなります。すなわち、i 番目の木で収穫した場合、i+1 番目から i+K 番目までの木では収穫できず、次に収穫できるのは i+K+1 番目以降の木です。言い換えると、収穫する木を番号の小さい順に並べたとき、隣り合う任意の 2 つの番号 i, j(i < j)について j \geq i + K + 1 を満たす必要があります。なお、K = 0 の場合はこの制限により連続する木で収穫することも可能です。
高橋君が収穫できる果物の合計個数の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq N - 1
- 1 \leq A_i \leq 10^9
- 入力はすべて整数
入力
N K A_1 A_2 \ldots A_N
- 1 行目には、果物の木の本数 N と、収穫後に収穫できなくなる木の本数 K が、スペース区切りで与えられる。
- 2 行目には、各木に実っている果物の個数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
出力
高橋君が収穫できる果物の合計個数の最大値を 1 行で出力してください。
入力例 1
5 2 3 7 2 5 8
出力例 1
15
入力例 2
8 1 10 20 30 40 50 60 70 80
出力例 2
200
入力例 3
15 3 100 50 30 20 200 10 5 150 80 40 300 25 15 60 250
出力例 3
850
Score : 366 pts
Problem Statement
Takahashi visited an orchard where N fruit trees are lined up in a row. The trees are numbered 1, 2, \ldots, N from one end, and the i-th tree bears A_i fruits.
Takahashi walks past the trees one by one in order from tree 1 to tree N. He passes in front of each tree exactly once and cannot turn back. When passing in front of each tree, he chooses either to harvest all the fruits from that tree or to harvest none at all. He cannot harvest only a portion of the fruits. It is also allowed to harvest from no trees at all.
However, when he harvests fruits from a tree, the fatigue from harvesting prevents him from harvesting at the next K trees immediately after it. That is, if he harvests from the i-th tree, he cannot harvest from trees i+1 through i+K, and the earliest he can next harvest is from tree i+K+1 or later. In other words, if the trees he harvests from are listed in increasing order of their numbers, any two adjacent numbers i, j (i < j) in the list must satisfy j \geq i + K + 1. Note that when K = 0, this restriction allows harvesting from consecutive trees.
Find the maximum total number of fruits that Takahashi can harvest.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq K \leq N - 1
- 1 \leq A_i \leq 10^9
- All inputs are integers.
Input
N K A_1 A_2 \ldots A_N
- The first line contains the number of fruit trees N and the number of trees that become unavailable for harvesting after a harvest K, separated by a space.
- The second line contains the number of fruits on each tree A_1, A_2, \ldots, A_N, separated by spaces.
Output
Print the maximum total number of fruits that Takahashi can harvest, on a single line.
Sample Input 1
5 2 3 7 2 5 8
Sample Output 1
15
Sample Input 2
8 1 10 20 30 40 50 60 70 80
Sample Output 2
200
Sample Input 3
15 3 100 50 30 20 200 10 5 150 80 40 300 25 15 60 250
Sample Output 3
850
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、N 個のバス停がある街に住んでいる。バス停には 1 から N の番号がついている。
この街には M 本のバス路線が運行されており、バス路線には 1 から M の番号がついている。
バス路線 i は K_i 個のバス停 A_{i,1}, A_{i,2}, \dots, A_{i,K_i} を通る。
この路線に乗ると、これらのバス停のうち任意の 1 つから乗車し、任意の別の 1 つで下車することができる。
高橋君は最初バス停 S にいて、バス停 T に行きたい。
高橋君は次の操作を 0 回以上好きなだけ繰り返せる。
- 現在いるバス停を通るバス路線を 1 つ選んで乗車する。その路線が通るバス停のうち、現在いるバス停とは異なる好きなバス停で下車する。
同じバス路線を複数回利用してもよい。
バス停 T に到達するために必要な最小の操作回数(すなわち乗車回数)を求めよ。到達できない場合は -1 を出力せよ。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^5
- 1 \leq S \leq N
- 1 \leq T \leq N
- 1 \leq K_i \leq N (1 \leq i \leq M)
- 1 \leq A_{i,j} \leq N (1 \leq i \leq M,\ 1 \leq j \leq K_i)
- 各 i について、A_{i,1}, A_{i,2}, \dots, A_{i,K_i} はすべて相異なる。
- \displaystyle \sum_{i=1}^{M} K_i \leq 5 \times 10^5
- 入力はすべて整数である。
入力
N M S T
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \dots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \dots A_{M,K_M}
- 1 行目には、バス停の個数 N、バス路線の本数 M、出発地のバス停番号 S、目的地のバス停番号 T が、スペース区切りで与えられる。
- 続く M 行のうち i 番目の行には、バス路線 i の情報が与えられる。
- 先頭の K_i は、バス路線 i が通るバス停の個数を表す。
- 続く K_i 個の整数 A_{i,1}, A_{i,2}, \dots, A_{i,K_i} は、バス路線 i が通るバス停の番号を表す。
出力
バス停 T に到達するために必要な最小の乗車回数を 1 行で出力せよ。到達できない場合は -1 を出力せよ。
入力例 1
5 3 1 5 3 1 2 3 2 3 4 2 2 5
出力例 1
2
入力例 2
6 3 1 6 2 1 2 2 2 3 2 5 6
出力例 2
-1
入力例 3
12 7 1 12 4 1 2 3 4 3 4 5 6 3 6 7 8 3 2 9 10 2 10 12 3 8 11 12 3 3 7 9
出力例 3
3
入力例 4
20 10 1 20 5 1 2 3 4 5 4 5 6 7 8 4 8 9 10 11 4 11 12 13 14 4 14 15 16 20 3 3 9 15 4 2 6 12 18 3 18 19 20 2 7 17 3 10 17 20
出力例 4
3
入力例 5
1 1 1 1 1 1
出力例 5
0
Score : 400 pts
Problem Statement
Takahashi lives in a city with N bus stops. The bus stops are numbered from 1 to N.
There are M bus routes operating in this city, numbered from 1 to M.
Bus route i passes through K_i bus stops A_{i,1}, A_{i,2}, \dots, A_{i,K_i}.
By taking this route, one can board at any one of these bus stops and get off at any other one of these bus stops.
Takahashi is initially at bus stop S and wants to go to bus stop T.
Takahashi can repeat the following operation any number of times (including zero):
- Choose a bus route that passes through the bus stop he is currently at and board it. Then get off at any bus stop (different from the current one) that the route passes through.
The same bus route may be used multiple times.
Find the minimum number of operations (i.e., the minimum number of rides) required to reach bus stop T. If it is impossible to reach it, output -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^5
- 1 \leq S \leq N
- 1 \leq T \leq N
- 1 \leq K_i \leq N (1 \leq i \leq M)
- 1 \leq A_{i,j} \leq N (1 \leq i \leq M,\ 1 \leq j \leq K_i)
- For each i, A_{i,1}, A_{i,2}, \dots, A_{i,K_i} are all distinct.
- \displaystyle \sum_{i=1}^{M} K_i \leq 5 \times 10^5
- All input values are integers.
Input
N M S T
K_1 A_{1,1} A_{1,2} \dots A_{1,K_1}
K_2 A_{2,1} A_{2,2} \dots A_{2,K_2}
\vdots
K_M A_{M,1} A_{M,2} \dots A_{M,K_M}
- The first line contains the number of bus stops N, the number of bus routes M, the starting bus stop number S, and the destination bus stop number T, separated by spaces.
- The i-th of the following M lines contains the information for bus route i.
- The leading value K_i represents the number of bus stops that bus route i passes through.
- The following K_i integers A_{i,1}, A_{i,2}, \dots, A_{i,K_i} represent the bus stop numbers that bus route i passes through.
Output
Output in one line the minimum number of rides required to reach bus stop T. If it is impossible to reach it, output -1.
Sample Input 1
5 3 1 5 3 1 2 3 2 3 4 2 2 5
Sample Output 1
2
Sample Input 2
6 3 1 6 2 1 2 2 2 3 2 5 6
Sample Output 2
-1
Sample Input 3
12 7 1 12 4 1 2 3 4 3 4 5 6 3 6 7 8 3 2 9 10 2 10 12 3 8 11 12 3 3 7 9
Sample Output 3
3
Sample Input 4
20 10 1 20 5 1 2 3 4 5 4 5 6 7 8 4 8 9 10 11 4 11 12 13 14 4 14 15 16 20 3 3 9 15 4 2 6 12 18 3 18 19 20 2 7 17 3 10 17 20
Sample Output 4
3
Sample Input 5
1 1 1 1 1 1
Sample Output 5
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 433 点
問題文
高橋君は美術館の学芸員です。美術館には N 個の作品が展示候補としてあり、それぞれ 1 から N までの番号が付けられています。i 番目の作品の評価スコアは H_i です。
高橋君はこれらの作品の中から 1 個以上を選んで展示することにしました。同じ作品を複数回選ぶことはできません。選んだ作品は番号の小さい順に一列に並べて展示します(並び順を自由に変えることはできません)。
展示の統一感を保つために、並べた作品の列は以下の条件を満たす必要があります。
- 選んだ作品の個数を k(k \geq 1)とし、それらを番号の小さい方から順に a_1, a_2, \ldots, a_k(a_1 < a_2 < \cdots < a_k)とする。このとき、すべての 1 \leq j \leq k-1 について、|H_{a_j} - H_{a_{j+1}}| \leq D が成り立つ。
すなわち、番号順に並べた展示作品の列において、隣り合う 2 つの作品の評価スコアの差の絶対値がすべて D 以下でなければなりません。作品を 1 つだけ選ぶ場合、隣り合う組が存在しないため、この条件は自動的に満たされます。
高橋君は、できるだけ多くの作品を展示したいと考えています。上記の条件を満たしながら選べる作品の個数の最大値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq D \leq 10^9
- 1 \leq H_i \leq 10^9
- 入力はすべて整数である。
入力
N D H_1 H_2 \ldots H_N
- 1 行目には、作品の個数を表す整数 N と、隣り合う作品間で許容される評価スコアの差の絶対値の上限(以下)を表す整数 D が、スペース区切りで与えられる。
- 2 行目には、各作品の評価スコアを表す整数 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
出力
条件を満たしながら選べる作品の個数の最大値を 1 行で出力せよ。
入力例 1
5 3 4 1 5 8 3
出力例 1
3
入力例 2
5 0 3 1 3 3 2
出力例 2
3
入力例 3
10 5 10 3 8 12 7 15 9 6 11 2
出力例 3
7
入力例 4
20 10 50 45 55 40 35 30 25 20 28 33 38 43 48 53 58 63 68 60 55 50
出力例 4
19
入力例 5
1 0 1000000000
出力例 5
1
Score : 433 pts
Problem Statement
Takahashi is a curator at an art museum. The museum has N works as exhibition candidates, numbered from 1 to N. The evaluation score of the i-th work is H_i.
Takahashi has decided to select 1 or more works from these to exhibit. The same work cannot be selected more than once. The selected works are displayed in a row in ascending order of their numbers (the arrangement order cannot be freely changed).
To maintain a sense of unity in the exhibition, the sequence of arranged works must satisfy the following condition:
- Let k (k \geq 1) be the number of selected works, and let them be a_1, a_2, \ldots, a_k (a_1 < a_2 < \cdots < a_k) in ascending order of their numbers. Then, for all 1 \leq j \leq k-1, |H_{a_j} - H_{a_{j+1}}| \leq D must hold.
In other words, in the sequence of exhibited works arranged in order of their numbers, the absolute difference of evaluation scores between every two adjacent works must be at most D. When selecting only one work, there are no adjacent pairs, so this condition is automatically satisfied.
Takahashi wants to exhibit as many works as possible. Find the maximum number of works that can be selected while satisfying the above condition.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq D \leq 10^9
- 1 \leq H_i \leq 10^9
- All input values are integers.
Input
N D H_1 H_2 \ldots H_N
- The first line contains an integer N representing the number of works and an integer D representing the upper limit (inclusive) of the absolute difference of evaluation scores allowed between adjacent works, separated by a space.
- The second line contains integers H_1, H_2, \ldots, H_N representing the evaluation scores of each work, separated by spaces.
Output
Output the maximum number of works that can be selected while satisfying the condition, in a single line.
Sample Input 1
5 3 4 1 5 8 3
Sample Output 1
3
Sample Input 2
5 0 3 1 3 3 2
Sample Output 2
3
Sample Input 3
10 5 10 3 8 12 7 15 9 6 11 2
Sample Output 3
7
Sample Input 4
20 10 50 45 55 40 35 30 25 20 28 33 38 43 48 53 58 63 68 60 55 50
Sample Output 4
19
Sample Input 5
1 0 1000000000
Sample Output 5
1