Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 266 点
問題文
高橋君のクラスでは、クリスマス会でプレゼント渡しを行います。クラスには N 人の生徒がおり、それぞれ 1 から N までの出席番号が付けられています。
プレゼント渡しのルールは次の通りです。各生徒はちょうど 1 つのプレゼントを用意し、自分以外のクラスメイト 1 人を選んで渡します。このとき、複数の生徒が同じ相手を選ぶこともあります。
プレゼント渡しが終わった後、高橋君はプレゼントを 1 つも受け取れなかった生徒がいることに気づきました。
各生徒 i ( 1 \leq i \leq N )がプレゼントを渡す相手の出席番号 T_i が与えられます。プレゼントを 1 つも受け取れなかった生徒の人数を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq T_i \leq N
- T_i \neq i (各生徒は自分自身にはプレゼントを渡さない)
- 入力はすべて整数である
入力
N T_1 T_2 \ldots T_N
- 1 行目には、クラスの生徒数を表す整数 N が与えられる。
- 2 行目には、生徒 i ( 1 \leq i \leq N )がプレゼントを渡す相手の出席番号 T_i が、スペース区切りで N 個与えられる。
出力
プレゼントを 1 つも受け取れなかった生徒の人数を 1 行で出力せよ。
入力例 1
5 2 1 1 2 3
出力例 1
2
入力例 2
8 2 3 4 5 6 7 8 1
出力例 2
0
入力例 3
10 3 1 1 1 3 7 8 7 10 9
出力例 3
4
Score : 266 pts
Problem Statement
Takahashi's class is holding a present exchange at their Christmas party. There are N students in the class, each assigned a student number from 1 to N.
The rules for the present exchange are as follows. Each student prepares exactly 1 present and chooses one classmate other than themselves to give it to. Multiple students may choose the same recipient.
After the present exchange, Takahashi noticed that some students didn't receive any presents at all.
Given the student number T_i of the recipient that each student i (1 \leq i \leq N) gives their present to, find the number of students who didn't receive any presents.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq T_i \leq N
- T_i \neq i (no student gives a present to themselves)
- All input values are integers
Input
N T_1 T_2 \ldots T_N
- The first line contains an integer N, representing the number of students in the class.
- The second line contains N space-separated integers, where T_i is the student number of the recipient that student i (1 \leq i \leq N) gives their present to.
Output
Print the number of students who didn't receive any presents, on a single line.
Sample Input 1
5 2 1 1 2 3
Sample Output 1
2
Sample Input 2
8 2 3 4 5 6 7 8 1
Sample Output 2
0
Sample Input 3
10 3 1 1 1 3 7 8 7 10 9
Sample Output 3
4
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋君は N 日間の旅行を計画しています。
旅行の 1 日目が始まる前の時点で、高橋君は X 円の所持金を持っています。
各 i 日目 (1 \leq i \leq N) には、次の順に処理が行われます。
- A_i 円の収入を得る(日払いのアルバイト代や仕送りなど)。
- B_i 円の支出がある(宿泊費や食費など)。
i 日目の処理が始まる直前の所持金を S とすると、i 日目の処理がすべて終わった直後の所持金は S + A_i - B_i 円になります。
1 日目から N 日目まで順に処理を行うとき、すべての i (1 \leq i \leq N) について、i 日目の処理がすべて終わった直後の所持金が 0 以上であるような、0 以上の整数 X の最小値を求めてください。
なお、各日の処理の途中の所持金については条件を問いません。条件として確認するのは、各日の処理がすべて終わった直後の所持金のみです。
制約
- 1 \leq N \leq 10^5
- 0 \leq A_i \leq 10^9
- 0 \leq B_i \leq 10^9
- 入力はすべて整数である
入力
N A_1 B_1 A_2 B_2 \vdots A_N B_N
1 行目には、旅行の日数を表す整数 N が与えられる。続く N 行のうち i 行目には、i 日目の収入 A_i と支出 B_i がスペース区切りで与えられる。
出力
条件を満たす初期所持金 X の最小値を整数で 1 行に出力してください。
入力例 1
3 1 3 4 3 2 4
出力例 1
3
入力例 2
4 5 2 0 1 3 3 2 1
出力例 2
0
入力例 3
8 3 7 10 2 1 6 0 3 8 1 2 5 4 4 1 2
出力例 3
4
入力例 4
15 100 300 50 20 0 100 500 10 30 80 1000 1200 200 50 0 500 700 100 60 70 90 400 1000000000 999999900 1 100 500 0 100 600
出力例 4
380
入力例 5
1 0 1000000000
出力例 5
1000000000
Score : 300 pts
Problem Statement
Takahashi is planning a trip lasting N days.
Before the start of day 1 of the trip, Takahashi has X yen in hand.
On each day i (1 \leq i \leq N), the following operations are performed in order:
- He receives an income of A_i yen (such as daily wages from a part-time job or an allowance from family).
- He has an expenditure of B_i yen (such as accommodation or food costs).
If his amount of money just before the start of day i's operations is S, then his amount of money just after all of day i's operations are completed is S + A_i - B_i yen.
When processing days 1 through N in order, find the minimum value of a non-negative integer X such that for all i (1 \leq i \leq N), the amount of money just after all of day i's operations are completed is at least 0.
Note that there is no condition on the amount of money during the middle of each day's operations. The condition only checks the amount of money just after all of each day's operations are completed.
Constraints
- 1 \leq N \leq 10^5
- 0 \leq A_i \leq 10^9
- 0 \leq B_i \leq 10^9
- All input values are integers
Input
N A_1 B_1 A_2 B_2 \vdots A_N B_N
The first line contains an integer N representing the number of days of the trip. Of the following N lines, the i-th line contains the income A_i and expenditure B_i for day i, separated by a space.
Output
Print the minimum value of the initial amount of money X that satisfies the condition, as an integer on a single line.
Sample Input 1
3 1 3 4 3 2 4
Sample Output 1
3
Sample Input 2
4 5 2 0 1 3 3 2 1
Sample Output 2
0
Sample Input 3
8 3 7 10 2 1 6 0 3 8 1 2 5 4 4 1 2
Sample Output 3
4
Sample Input 4
15 100 300 50 20 0 100 500 10 30 80 1000 1200 200 50 0 500 700 100 60 70 90 400 1000000000 999999900 1 100 500 0 100 600
Sample Output 4
380
Sample Input 5
1 0 1000000000
Sample Output 5
1000000000
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 366 点
問題文
高橋君は、長さ M のビリヤード台の上でボールを転がします。ビリヤード台は一直線の区間 [0, M] で表されます。
ボールの初期位置は座標 S です。高橋君は N 回のショットを順番に行います。i 番目のショットは方向 C_i と距離 A_i で表されます。
- C_i =
Lのとき、ボールを左向き(座標が減る方向)に距離 A_i だけ転がします。 - C_i =
Rのとき、ボールを右向き(座標が増える方向)に距離 A_i だけ転がします。
ボールは区間 [0, M] の外には出ません。転がっている途中でボールが端点 0 または M に到達し、まだ移動距離が残っている場合、進行方向をただちに反転させて転がり続けます。この反転を 1 回の 反射 と呼びます。1 回のショットの中で反射は複数回発生することがあります。
また、あるショットの開始時点でボールが端点 0 にあり C_i = L である場合、または端点 M にあり C_i = R である場合は、移動距離を消費せずにただちに進行方向を反転させ、これも 1 回の反射と数えます。
ただし、A_i = 0 のショットでは、ボールの位置や方向にかかわらず、移動も反射も一切発生しません。
N 回のショットをすべて処理したときの、ボールの最終位置と、発生した反射の総回数を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^9
- 0 \leq S \leq M
- C_i は
LまたはR - 0 \leq A_i \leq 10^9
- N, M, S, A_i はすべて整数である
入力
N M S C_1 A_1 C_2 A_2 \vdots C_N A_N
- 1 行目には、ショットの回数を表す整数 N、台の長さを表す整数 M、ボールの初期位置を表す整数 S が、スペース区切りで与えられる。
- 続く N 行のうち i 行目には、i 番目のショットの方向を表す文字 C_i と、転がす距離を表す整数 A_i が、スペース区切りで与えられる。
出力
ボールの最終位置 P と、発生した反射の総回数 K を、この順にスペース区切りで 1 行に出力せよ。
入力例 1
3 10 4 R 3 L 5 R 8
出力例 1
10 0
入力例 2
4 5 0 L 2 R 3 R 1 L 0
出力例 2
4 2
入力例 3
10 17 8 R 12 L 4 L 20 R 0 R 35 L 1 R 17 L 18 R 5 L 33
出力例 3
17 7
入力例 4
20 1000000000 123456789 R 987654321 L 1000000000 R 1 R 999999999 L 500000000 L 750000000 R 250000000 L 0 R 1000000000 L 999999998 R 333333333 L 666666666 R 123456789 L 987654321 R 400000000 R 600000000 L 100000000 R 900000000 L 800000000 R 700000000
出力例 4
713580243 10
入力例 5
1 1 0 L 0
出力例 5
0 0
Score : 366 pts
Problem Statement
Takahashi rolls a ball on a billiard table of length M. The billiard table is represented as a one-dimensional interval [0, M].
The initial position of the ball is at coordinate S. Takahashi performs N shots in order. The i-th shot is represented by a direction C_i and a distance A_i.
- If C_i =
L, the ball is rolled to the left (in the direction of decreasing coordinate) by a distance of A_i. - If C_i =
R, the ball is rolled to the right (in the direction of increasing coordinate) by a distance of A_i.
The ball never leaves the interval [0, M]. If, while rolling, the ball reaches an endpoint 0 or M and there is still remaining travel distance, the direction of travel is immediately reversed and the ball continues rolling. This reversal is called one reflection. Multiple reflections may occur within a single shot.
Additionally, if at the start of a shot the ball is at endpoint 0 and C_i = L, or at endpoint M and C_i = R, the direction of travel is immediately reversed without consuming any travel distance, and this also counts as one reflection.
However, for a shot with A_i = 0, regardless of the ball's position or direction, no movement or reflection occurs at all.
Find the final position of the ball and the total number of reflections that occurred after processing all N shots.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 10^9
- 0 \leq S \leq M
- C_i is
LorR - 0 \leq A_i \leq 10^9
- N, M, S, A_i are all integers
Input
N M S C_1 A_1 C_2 A_2 \vdots C_N A_N
- The first line contains an integer N representing the number of shots, an integer M representing the length of the table, and an integer S representing the initial position of the ball, separated by spaces.
- The i-th of the following N lines contains a character C_i representing the direction of the i-th shot and an integer A_i representing the rolling distance, separated by a space.
Output
Output the final position P of the ball and the total number of reflections K, in this order, separated by a space on a single line.
Sample Input 1
3 10 4 R 3 L 5 R 8
Sample Output 1
10 0
Sample Input 2
4 5 0 L 2 R 3 R 1 L 0
Sample Output 2
4 2
Sample Input 3
10 17 8 R 12 L 4 L 20 R 0 R 35 L 1 R 17 L 18 R 5 L 33
Sample Output 3
17 7
Sample Input 4
20 1000000000 123456789 R 987654321 L 1000000000 R 1 R 999999999 L 500000000 L 750000000 R 250000000 L 0 R 1000000000 L 999999998 R 333333333 L 666666666 R 123456789 L 987654321 R 400000000 R 600000000 L 100000000 R 900000000 L 800000000 R 700000000
Sample Output 4
713580243 10
Sample Input 5
1 1 0 L 0
Sample Output 5
0 0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
高橋君は、N 台のサーバーを管理しています。それぞれのサーバーには 1 から N までの番号が付けられており、サーバー i の通信負荷は A_i です。
高橋君は、これらのサーバーをネットワークケーブルで接続し、すべてのサーバーが直接または他のサーバーを経由して互いに通信できる連結なネットワークを構築しようとしています。N = 1 のときは、ケーブルを使用せずとも連結であるとみなします。
高橋君は M 本のネットワークケーブルを持っており、ケーブル j の帯域幅は L_j です。高橋君は、各ケーブルについて、異なる 2 台のサーバーを選んでそのケーブルで接続することができます。ただし、サーバー u とサーバー v(u \neq v)をケーブル j で接続するには、ケーブルの帯域幅が両方のサーバーの通信負荷の和以上であること、すなわち L_j \geq A_u + A_v を満たす必要があります。
ケーブルの使用には以下の制約があります。
- 各ケーブルは高々 1 組のサーバーペアの接続にしか使用できません。使用しないケーブルがあってもかまいません。
- 同じ 2 台のサーバーの間に複数のケーブルを接続することはできません。ただし、ネットワーク全体として閉路を含むことは許されます。
すべてのサーバーを 1 つの連結なネットワークにまとめることができるかどうか判定し、できる場合は Yes を、できない場合は No を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j \leq 10^9 (1 \leq j \leq M)
- 入力はすべて整数
入力
N M A_1 A_2 \ldots A_N L_1 L_2 \ldots L_M
- 1 行目には、サーバーの台数を表す整数 N と、ケーブルの本数を表す整数 M が、スペース区切りで与えられる。
- 2 行目には、各サーバーの通信負荷を表す整数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
- 3 行目には、各ケーブルの帯域幅を表す整数 L_1, L_2, \ldots, L_M が、スペース区切りで与えられる。
出力
すべてのサーバーを 1 つの連結なネットワークにまとめることができる場合は Yes を、できない場合は No を 1 行で出力してください。
入力例 1
3 2 1 2 3 4 5
出力例 1
Yes
入力例 2
4 5 3 1 4 2 5 6 4 7 3
出力例 2
Yes
入力例 3
5 3 10 20 30 40 50 25 35 45
出力例 3
No
Score : 400 pts
Problem Statement
Takahashi manages N servers. Each server is numbered from 1 to N, and the communication load of server i is A_i.
Takahashi is trying to connect these servers with network cables to build a connected network where all servers can communicate with each other, either directly or through other servers. When N = 1, the network is considered connected without using any cables.
Takahashi has M network cables, and the bandwidth of cable j is L_j. For each cable, Takahashi can choose two distinct servers and connect them with that cable. However, to connect server u and server v (u \neq v) with cable j, the bandwidth of the cable must be at least the sum of the communication loads of both servers, i.e., L_j \geq A_u + A_v must be satisfied.
The following constraints apply to the use of cables:
- Each cable can be used to connect at most one pair of servers. It is acceptable to leave some cables unused.
- Multiple cables cannot be connected between the same two servers. However, the network as a whole is allowed to contain cycles.
Determine whether it is possible to connect all servers into a single connected network. If it is possible, output Yes; otherwise, output No.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq M \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
- 1 \leq L_j \leq 10^9 (1 \leq j \leq M)
- All input values are integers
Input
N M A_1 A_2 \ldots A_N L_1 L_2 \ldots L_M
- The first line contains two integers separated by a space: N, the number of servers, and M, the number of cables.
- The second line contains N integers separated by spaces: A_1, A_2, \ldots, A_N, representing the communication load of each server.
- The third line contains M integers separated by spaces: L_1, L_2, \ldots, L_M, representing the bandwidth of each cable.
Output
If it is possible to connect all servers into a single connected network, output Yes; otherwise, output No, in a single line.
Sample Input 1
3 2 1 2 3 4 5
Sample Output 1
Yes
Sample Input 2
4 5 3 1 4 2 5 6 4 7 3
Sample Output 2
Yes
Sample Input 3
5 3 10 20 30 40 50 25 35 45
Sample Output 3
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 466 点
問題文
高橋君は N 台のサーバーからなる大規模ネットワークを管理しています。サーバーには 1 から N までの番号が付けられています。
ネットワークには M 本の一方通行の通信回線があり、i 番目の回線(1 \leq i \leq M)はサーバー U_i からサーバー V_i へデータを送信できます。同じ始点・終点の組を持つ回線が複数存在することもありますが、始点と終点が同じサーバーである回線(自己ループ)は存在しません。各サーバー j(1 \leq j \leq N)には現在のセキュリティレベルが整数値 W_j として定められています。
ここで、サーバー x からサーバー y に「到達可能」であるとは、サーバー x から回線の向きに沿って 1 本以上の回線を辿ることでサーバー y に到達できることを意味します。x = y の場合も例外ではなく、サーバー x から x 自身に到達可能であるとは、x から出発して 1 本以上の回線を辿って再び x に戻れること、すなわち x を含む有向閉路が存在することを意味します。
高橋君はネットワーク全体のセキュリティを向上させるため、以下の操作を何回でも(0 回でもよい)行えます。毎回の操作で選ぶサーバーは 1 から N のいずれでもよく、同じサーバーを複数回選ぶこともできます。
操作: サーバー v を 1 つ選ぶ。v から到達可能かつ v へも到達可能であるようなサーバー u 全体の集合を S(v) とする(v 自身も、v から v へ到達可能である場合に限り S(v) に含まれる)。S(v) が空でない場合、S(v) に含まれるすべてのサーバーのセキュリティレベルを一律に 1 増加させる。S(v) が空の場合、何も起こらない。
すなわち、v を含む有向閉路が存在するとき、S(v) は v と互いに到達可能なサーバー全体の集合(v を含む強連結成分)と一致し、これらのサーバーすべてのセキュリティレベルが 1 増加します。v を含む有向閉路が存在しないとき、S(v) は空集合となり、操作を行っても何も変化しません。
各サーバー j(1 \leq j \leq N)には「要求水準」として整数値 T_j が定められています。すべてのサーバー j についてセキュリティレベルが T_j 以上であるとき、ネットワーク全体が基準達成であるといいます。
高橋君の目標は、操作を繰り返すことでネットワーク全体を基準達成の状態にすることです。必要な操作の合計回数の最小値を求めてください。
ただし、操作をどのように行っても基準達成にできない場合は -1 を出力してください。
制約
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 0 \leq W_j \leq 10^9 (1 \leq j \leq N)
- 0 \leq T_j \leq 10^9 (1 \leq j \leq N)
- 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
- U_i \neq V_i (1 \leq i \leq M)
- 入力はすべて整数である
入力
N M W_1 W_2 \ldots W_N T_1 T_2 \ldots T_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- 1 行目には、サーバーの台数 N と回線の本数 M がスペース区切りで与えられる。
- 2 行目には、各サーバーの初期セキュリティレベル W_1, W_2, \ldots, W_N がスペース区切りで与えられる。
- 3 行目には、各サーバーの要求水準 T_1, T_2, \ldots, T_N がスペース区切りで与えられる。
- 4 行目から M 行にわたり、各回線の始点 U_i と終点 V_i がスペース区切りで与えられる。
出力
ネットワーク全体を基準達成にするために必要な操作の最小回数を 1 行で出力せよ。達成不可能な場合は -1 を出力せよ。
なお、答えは非常に大きくなることがあるため、64 bit 整数型を使用することを推奨する。
入力例 1
4 4 1 3 5 2 4 4 5 1 1 2 2 1 2 3 3 4
出力例 1
3
入力例 2
3 2 0 0 1 2 1 5 1 2 2 1
出力例 2
-1
入力例 3
10 14 10 5 7 1 2 100 0 4 10 3 12 9 7 3 6 50 5 8 12 3 1 2 2 3 3 1 3 4 4 5 5 4 5 6 6 7 7 8 8 9 9 7 2 4 4 5 8 10
出力例 3
13
入力例 4
20 31 100 95 98 90 0 10 5 50 1 2 3 4 5 1000 999 7 0 0 0 123 101 100 99 97 3 12 8 50 10 5 8 4 20 1000 1005 6 1 2 3 123 1 2 2 3 3 4 4 1 1 3 2 4 5 6 6 7 7 5 5 7 9 10 10 11 11 12 12 13 13 9 9 11 10 12 14 15 15 14 17 18 18 19 19 17 17 19 4 5 3 8 7 9 8 9 13 14 15 16 12 17 19 20
出力例 4
34
入力例 5
1 0 0 0
出力例 5
0
Score : 466 pts
Problem Statement
Takahashi manages a large-scale network consisting of N servers. The servers are numbered from 1 to N.
The network has M one-way communication links. The i-th link (1 \leq i \leq M) can transmit data from server U_i to server V_i. There may be multiple links with the same source-destination pair, but there are no links where the source and destination are the same server (no self-loops). Each server j (1 \leq j \leq N) has a current security level defined as an integer value W_j.
Here, server x is said to be "reachable" to server y if it is possible to reach server y from server x by following one or more links in their directed order. The case x = y is no exception: server x being reachable to itself means that starting from x, one can follow one or more links and return to x, i.e., there exists a directed cycle containing x.
To improve the security of the entire network, Takahashi can perform the following operation any number of times (including 0 times). In each operation, any server from 1 to N may be chosen, and the same server may be chosen multiple times.
Operation: Choose one server v. Let S(v) be the set of all servers u such that u is reachable from v and v is reachable from u (v itself is included in S(v) only if v is reachable from v). If S(v) is non-empty, increase the security level of all servers in S(v) by 1. If S(v) is empty, nothing happens.
In other words, when a directed cycle containing v exists, S(v) coincides with the set of all servers mutually reachable with v (the strongly connected component containing v), and the security levels of all these servers are increased by 1. When no directed cycle containing v exists, S(v) is the empty set, and performing the operation causes no change.
For each server j (1 \leq j \leq N), a "required level" is defined as an integer value T_j. The entire network is said to have met the standard when the security level of every server j is at least T_j.
Takahashi's goal is to bring the entire network to a state where the standard is met by repeating operations. Find the minimum total number of operations required.
If it is impossible to meet the standard regardless of how operations are performed, output -1.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 0 \leq M \leq 2 \times 10^5
- 0 \leq W_j \leq 10^9 (1 \leq j \leq N)
- 0 \leq T_j \leq 10^9 (1 \leq j \leq N)
- 1 \leq U_i, V_i \leq N (1 \leq i \leq M)
- U_i \neq V_i (1 \leq i \leq M)
- All input values are integers
Input
N M W_1 W_2 \ldots W_N T_1 T_2 \ldots T_N U_1 V_1 U_2 V_2 \vdots U_M V_M
- The first line contains the number of servers N and the number of links M, separated by a space.
- The second line contains the initial security levels W_1, W_2, \ldots, W_N of each server, separated by spaces.
- The third line contains the required levels T_1, T_2, \ldots, T_N of each server, separated by spaces.
- From the fourth line onward, M lines each contain the source U_i and destination V_i of each link, separated by a space.
Output
Output in one line the minimum number of operations required to bring the entire network to a state where the standard is met. If it is impossible, output -1.
Note that the answer may be very large, so it is recommended to use a 64-bit integer type.
Sample Input 1
4 4 1 3 5 2 4 4 5 1 1 2 2 1 2 3 3 4
Sample Output 1
3
Sample Input 2
3 2 0 0 1 2 1 5 1 2 2 1
Sample Output 2
-1
Sample Input 3
10 14 10 5 7 1 2 100 0 4 10 3 12 9 7 3 6 50 5 8 12 3 1 2 2 3 3 1 3 4 4 5 5 4 5 6 6 7 7 8 8 9 9 7 2 4 4 5 8 10
Sample Output 3
13
Sample Input 4
20 31 100 95 98 90 0 10 5 50 1 2 3 4 5 1000 999 7 0 0 0 123 101 100 99 97 3 12 8 50 10 5 8 4 20 1000 1005 6 1 2 3 123 1 2 2 3 3 4 4 1 1 3 2 4 5 6 6 7 7 5 5 7 9 10 10 11 11 12 12 13 13 9 9 11 10 12 14 15 15 14 17 18 18 19 19 17 17 19 4 5 3 8 7 9 8 9 13 14 15 16 12 17 19 20
Sample Output 4
34
Sample Input 5
1 0 0 0
Sample Output 5
0