A - Deduplicated Log

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233

問題文

高橋君はサーバーの監視システムを管理しています。サーバーからは N 件のイベントログが時系列順に送られてきます。i 番目に送られてくるログのイベントコードは A_i です。

高橋君は、これらのログを A_1, A_2, \ldots, A_N の順に確認し、記録リストに追加していきます。ただし、記録リストが空でないとき、記録リストの末尾の要素と同じイベントコードのログが来た場合は重複とみなし、記録リストには追加せず読み飛ばします。記録リストが空の場合、または記録リストの末尾の要素と異なるイベントコードの場合は、そのイベントコードを記録リストの末尾に追加します。

N 件のイベントコードが時系列順に与えられるので、全てのログを確認し終えた後の記録リストの内容を出力してください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^91 \leq i \leq N
  • 入力はすべて整数である。

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、イベントログの件数 N が与えられる。
  • 2 行目には、イベントコード A_1, A_2, \ldots, A_N が空白区切りで与えられる。

出力

全てのログを確認し終えた後の記録リストの各要素を、先頭から順に空白区切りで出力せよ。


入力例 1

7
1 1 2 2 2 3 2

出力例 1

1
2
3
2

入力例 2

6
1 2 1 2 1 2

出力例 2

1
2
1
2
1
2

入力例 3

15
8 8 8 1 1 2 3 3 2 2 2 5 5 5 1

出力例 3

8
1
2
3
2
5
1

入力例 4

30
10 10 9 9 8 7 7 7 8 8 8 1000000000 1000000000 1 2 2 3 3 3 2 2 4 4 4 4 5 1 1 6 6

出力例 4

10
9
8
7
8
1000000000
1
2
3
2
4
5
1
6

入力例 5

1
1000000000

出力例 5

1000000000

Score : 233 pts

Problem Statement

Takahashi manages a server monitoring system. The server sends N event logs in chronological order. The event code of the i-th log sent is A_i.

Takahashi checks these logs in the order A_1, A_2, \ldots, A_N and appends them to a record list. However, if the record list is not empty and a log has the same event code as the last element of the record list, it is considered a duplicate and skipped without being added to the list. If the record list is empty, or if the log's event code is different from the last element of the record list, he appends that event code to the end of the record list.

Given the N event codes in chronological order, print the contents of the record list after all logs have been checked.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N
A_1 A_2 \ldots A_N
  • The first line contains the number of event logs, N.
  • The second line contains the event codes A_1, A_2, \ldots, A_N separated by spaces.

Output

Print the elements of the record list after all logs have been checked, in order from first to last, separated by spaces.


Sample Input 1

7
1 1 2 2 2 3 2

Sample Output 1

1
2
3
2

Sample Input 2

6
1 2 1 2 1 2

Sample Output 2

1
2
1
2
1
2

Sample Input 3

15
8 8 8 1 1 2 3 3 2 2 2 5 5 5 1

Sample Output 3

8
1
2
3
2
5
1

Sample Input 4

30
10 10 9 9 8 7 7 7 8 8 8 1000000000 1000000000 1 2 2 3 3 3 2 2 4 4 4 4 5 1 1 6 6

Sample Output 4

10
9
8
7
8
1000000000
1
2
3
2
4
5
1
6

Sample Input 5

1
1000000000

Sample Output 5

1000000000
B - Factory Order Processing

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は工場の生産管理を担当しています。この工場では、ある製品を生産しています。

製品 1 個を作るには、M 種類の部品がそれぞれ 1 個ずつ必要です。j 番目の部品の在庫は最初 B_j 個あります。

今日、N 件の注文を i = 1, 2, \ldots, N の順に処理します。i 番目の注文では、製品をちょうど A_i 個納品する必要があります。

i 番目の注文を処理するとき、その時点での在庫がすべての種類の部品について A_i 個以上であれば、製品を A_i 個生産して納品します。このとき、すべての種類の部品の在庫がそれぞれ A_i 個減ります。いずれか 1 種類でも在庫が A_i 個未満であれば、その注文はキャンセルとなり、製品を 1 個も生産せず、部品の在庫も変化しません。

すべての注文を処理した後、実際に納品できた注文の件数を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq M \leq 5 \times 10^5
  • N + M \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_j \leq 10^9
  • 入力はすべて整数である。

入力

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M
  • 1 行目には、注文の件数 N と部品の種類数 M が、スペース区切りで与えられる。
  • 2 行目には、各注文で納品すべき製品数 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。
  • 3 行目には、各部品の初期在庫数 B_1, B_2, \ldots, B_M が、スペース区切りで与えられる。

出力

実際に納品できた注文の件数を 1 行で出力せよ。


入力例 1

4 3
2 1 3 1
5 4 6

出力例 1

3

入力例 2

5 2
4 2 3 1 2
3 5

出力例 2

2

入力例 3

8 6
5 7 4 6 3 2 8 1
20 17 23 19 18 21

出力例 3

4

入力例 4

20 15
8 7 10 6 5 9 4 3 12 2 11 1 13 5 4 7 6 2 8 3
60 55 58 62 57 59 61 56 63 54 64 65 66 53 67

出力例 4

9

入力例 5

1 1
1000000000
1000000000

出力例 5

1

Score : 300 pts

Problem Statement

Takahashi is in charge of production management at a factory. This factory produces a certain product.

To make 1 unit of the product, exactly 1 of each of M types of parts is required. The initial stock of the j-th part is B_j units.

Today, N orders are processed in the order i = 1, 2, \ldots, N. The i-th order requires delivering exactly A_i units of the product.

When processing the i-th order, if the current stock of every type of part is at least A_i, then A_i units of the product are produced and delivered. In this case, the stock of every type of part decreases by A_i. If even one type of part has stock less than A_i, the order is cancelled — no products are produced and the part stocks do not change.

After all orders have been processed, determine the number of orders that were actually delivered.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq M \leq 5 \times 10^5
  • N + M \leq 5 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_j \leq 10^9
  • All input values are integers.

Input

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M
  • The first line contains the number of orders N and the number of part types M, separated by a space.
  • The second line contains the number of products to deliver for each order A_1, A_2, \ldots, A_N, separated by spaces.
  • The third line contains the initial stock of each part B_1, B_2, \ldots, B_M, separated by spaces.

Output

Print the number of orders that were actually delivered, on a single line.


Sample Input 1

4 3
2 1 3 1
5 4 6

Sample Output 1

3

Sample Input 2

5 2
4 2 3 1 2
3 5

Sample Output 2

2

Sample Input 3

8 6
5 7 4 6 3 2 8 1
20 17 23 19 18 21

Sample Output 3

4

Sample Input 4

20 15
8 7 10 6 5 9 4 3 12 2 11 1 13 5 4 7 6 2 8 3
60 55 58 62 57 59 61 56 63 54 64 65 66 53 67

Sample Output 4

9

Sample Input 5

1 1
1000000000
1000000000

Sample Output 5

1
C - Skyline View

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366

問題文

東西に伸びる一本の通りに沿って、N 棟のビルが一列に並んでいます。西から順にビル 1、ビル 2、…、ビル N と番号がついており、ビル i の高さは H_i です。

高橋君は、ビル 1 よりもさらに西側の地点に立って、東の方向を眺めています。ビルが見えるかどうかは、高さのみによって次のように決まります:

  • ビル i より西側にあるすべてのビル(ビル 1 からビル i-1 まで)の高さがいずれも H_i 未満である場合、ビル i は高橋君から見えます
  • そうでない場合、すなわち、ビル 1 からビル i-1 までの中に高さが H_i 以上のビルが1つでも存在する場合、ビル i は手前のビルに遮られて見えません。高さがちょうど同じビルが手前にある場合も遮られることに注意してください。

特に、ビル 1 は西側にビルが存在しないため、常に見えます。

高橋君は、N 棟のビルの中からちょうど1棟を選んで取り壊します。取り壊すビルは、見えているビル・見えていないビルのいずれでも構いませんが、この操作は必ず1回行わなければならず、省略することはできません。取り壊されたビルは完全に消滅し、残ったビルの並び順はそのままで、取り壊されたビルが存在しないものとして改めて眺望を判定します。すなわち、取り壊されたビルは他のビルを遮ることもなくなり、見えるビルの数にもカウントしません。

高橋君は、取り壊すビルを最適に選ぶことで、取り壊し後に見えるビルの数を最大化したいと考えています。

最適にビルを1棟選んで取り壊したとき、高橋君から見えるビルの数の最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq H_i \leq 10^9
  • 入力はすべて整数である。

入力

N
H_1 H_2 \cdots H_N
  • 1 行目には、ビルの棟数を表す整数 N が与えられる。
  • 2 行目には、各ビルの高さを表す整数 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。

出力

最適なビルを1棟取り壊したときに、高橋君から見えるビルの数の最大値を 1 行で出力せよ。


入力例 1

5
2 1 3 2 4

出力例 1

3

入力例 2

6
5 3 5 6 6 7

出力例 2

4

入力例 3

12
4 2 6 3 5 7 1 7 8 2 9 6

出力例 3

5

入力例 4

30
100 20 50 120 110 130 10 125 140 140 60 150 149 151 5 152 80 153 153 154 1 90 155 154 156 2 157 50 158 159

出力例 4

15

入力例 5

1
1000000000

出力例 5

0

Score : 366 pts

Problem Statement

Along a straight road running east-west, N buildings stand in a row. They are numbered Building 1, Building 2, …, Building N from west to east, and the height of Building i is H_i.

Takahashi is standing at a point further west than Building 1, looking toward the east. Whether a building is visible or not is determined solely by height as follows:

  • If the heights of all buildings to the west of Building i (from Building 1 to Building i-1) are strictly less than H_i, then Building i is visible to Takahashi.
  • Otherwise, that is, if there exists at least one building among Building 1 to Building i-1 whose height is greater than or equal to H_i, then Building i is blocked by a building in front and is not visible. Note that a building with exactly the same height in front also blocks it.

In particular, Building 1 is always visible since there are no buildings to its west.

Takahashi will choose exactly one building from the N buildings and demolish it. The building to demolish may be either a visible or non-visible building, but this operation must be performed exactly once and cannot be skipped. The demolished building completely disappears, and the view is re-evaluated with the remaining buildings in their original order, treating the demolished building as if it never existed. That is, the demolished building no longer blocks any other buildings, and it is not counted in the number of visible buildings.

Takahashi wants to maximize the number of visible buildings after the demolition by optimally choosing which building to demolish.

Find the maximum number of buildings visible to Takahashi when one building is optimally chosen and demolished.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq H_i \leq 10^9
  • All inputs are integers.

Input

N
H_1 H_2 \cdots H_N
  • The first line contains an integer N representing the number of buildings.
  • The second line contains integers H_1, H_2, \ldots, H_N separated by spaces, representing the height of each building.

Output

Print in one line the maximum number of buildings visible to Takahashi when one building is optimally chosen and demolished.


Sample Input 1

5
2 1 3 2 4

Sample Output 1

3

Sample Input 2

6
5 3 5 6 6 7

Sample Output 2

4

Sample Input 3

12
4 2 6 3 5 7 1 7 8 2 9 6

Sample Output 3

5

Sample Input 4

30
100 20 50 120 110 130 10 125 140 140 60 150 149 151 5 152 80 153 153 154 1 90 155 154 156 2 157 50 158 159

Sample Output 4

15

Sample Input 5

1
1000000000

Sample Output 5

0
D - Optimizing Work Groups

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋君は、N 人の社員を管理するプロジェクトリーダーです。

N 人の社員には社員 1, 2, \ldots, N と番号がついています。各社員 i1 \leq i \leq N)には「能力値」 P_i が設定されています。能力値は負の値をとることもあります。

高橋君はこの N 人の社員を、1 つ以上 N 個以下のグループに分割します。各グループは番号が連続する社員からなり、すべての社員がちょうど 1 つのグループに属するようにします。すなわち、あるグループ数 k1 \leq k \leq N)と 0 = c_0 < c_1 < \cdots < c_k = N を満たす整数列 c_0, c_1, \ldots, c_k が存在して、第 j グループ(1 \leq j \leq k)が社員 c_{j-1}+1, c_{j-1}+2, \ldots, c_j からなるような分割を考えます。N 人全員を 1 つのグループにすることも、各社員をそれぞれ単独のグループにすることも許されます。

チームワークの効果により、グループの人数が多いほど相乗効果が生まれます。具体的には、社員 l, l+1, \ldots, r1 \leq l \leq r \leq N)からなるグループの生産性は次の式で計算されます。

\text{生産性} = (r - l + 1) \times \sum_{i=l}^{r} P_i

ここで (r - l + 1) はグループの人数、\displaystyle\sum_{i=l}^{r} P_i はグループ内の能力値の合計です。グループ内の能力値の合計が負の場合、そのグループの生産性も負になることに注意してください。

高橋君は、すべてのグループの生産性の合計を最大化するように分割を決めたいです。生産性の合計の最大値を求めてください。

制約

  • 1 \leq N \leq 5000
  • -10^6 \leq P_i \leq 10^61 \leq i \leq N
  • 入力はすべて整数である。
  • 生産性の合計の最大値は 64 ビット符号付き整数型に収まる。

入力

N
P_1 P_2 \ldots P_N
  • 1 行目には、社員の人数を表す整数 N が与えられる。
  • 2 行目には、各社員の能力値を表す N 個の整数 P_1, P_2, \ldots, P_N がスペース区切りで与えられる。

出力

生産性の合計の最大値を整数として 1 行で出力せよ。


入力例 1

3
1 2 3

出力例 1

18

入力例 2

4
3 -5 -5 3

出力例 2

-4

入力例 3

8
2 -1 3 -2 4 1 -3 5

出力例 3

72

入力例 4

15
10 -3 5 8 -12 7 2 -1 6 -4 3 9 -8 11 4

出力例 4

555

入力例 5

1
-1000000

出力例 5

-1000000

Score : 400 pts

Problem Statement

Takahashi is a project leader managing N employees.

The N employees are numbered employee 1, 2, \ldots, N. Each employee i (1 \leq i \leq N) has an "ability value" P_i. Ability values can be negative.

Takahashi will divide these N employees into at least 1 and at most N groups. Each group consists of employees with consecutive numbers, and every employee belongs to exactly one group. Specifically, we consider a partition where there exists some number of groups k (1 \leq k \leq N) and an integer sequence c_0, c_1, \ldots, c_k satisfying 0 = c_0 < c_1 < \cdots < c_k = N, such that the j-th group (1 \leq j \leq k) consists of employees c_{j-1}+1, c_{j-1}+2, \ldots, c_j. It is allowed to place all N employees into a single group, or to place each employee into their own individual group.

Due to the effect of teamwork, larger groups produce greater synergy. Specifically, the productivity of a group consisting of employees l, l+1, \ldots, r (1 \leq l \leq r \leq N) is calculated by the following formula:

\text{productivity} = (r - l + 1) \times \sum_{i=l}^{r} P_i

Here, (r - l + 1) is the number of people in the group, and \displaystyle\sum_{i=l}^{r} P_i is the sum of ability values within the group. Note that if the sum of ability values within a group is negative, the productivity of that group is also negative.

Takahashi wants to determine the partition that maximizes the total productivity across all groups. Find the maximum value of the total productivity.

Constraints

  • 1 \leq N \leq 5000
  • -10^6 \leq P_i \leq 10^6 (1 \leq i \leq N)
  • All inputs are integers.
  • The maximum value of the total productivity fits in a 64-bit signed integer type.

Input

N
P_1 P_2 \ldots P_N
  • The first line contains an integer N representing the number of employees.
  • The second line contains N integers P_1, P_2, \ldots, P_N representing the ability values of each employee, separated by spaces.

Output

Output the maximum value of the total productivity as an integer on a single line.


Sample Input 1

3
1 2 3

Sample Output 1

18

Sample Input 2

4
3 -5 -5 3

Sample Output 2

-4

Sample Input 3

8
2 -1 3 -2 4 1 -3 5

Sample Output 3

72

Sample Input 4

15
10 -3 5 8 -12 7 2 -1 6 -4 3 9 -8 11 4

Sample Output 4

555

Sample Input 5

1
-1000000

Sample Output 5

-1000000
E - Print Factory Schedule

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 466

問題文

高橋君は印刷工場の工場長です。工場では様々なサイズのポスターを印刷しています。

高橋君のもとに N 件の注文が届きました。i 番目の注文(1 \leq i \leq N)では、横幅 W_i、縦幅 H_i の同一サイズのポスターを C_i 枚印刷する必要があります。

工場には M 台の印刷機があります。j 番目の印刷機(1 \leq j \leq M)は、横幅が L_j 以上 R_j 以下であるポスターのみを印刷することができます。ポスターの縦幅による制限はありません。すなわち、H_i の値にかかわらず、L_j \leq W_i \leq R_j を満たしていれば、j 番目の印刷機で i 番目の注文のポスターを印刷できます。(なお、H_i は注文の情報として入力に含まれますが、印刷機の割り当てには影響しません。)

各印刷機は 1 日あたり高々 1 枚のポスターしか印刷できません。すなわち、ある 1 日において、1 台の印刷機に割り当てられるポスターは最大 1 枚であり、同じ日に同じ印刷機で複数の注文のポスターを印刷することはできません。

1 つの注文の C_i 枚のポスターを、複数の印刷機に分担させて印刷することが可能です。同じ日に複数の印刷機で同じ注文のポスターを並行して印刷しても構いませんし、異なる日にまたがって印刷しても構いません。また、ある印刷機が日によって異なる注文のポスターを印刷することも可能です。

すべての注文を完了するために必要な最小の日数を求めてください。ここで、すべての注文が完了するとは、各注文 i1 \leq i \leq N)について、C_i 枚のポスターがすべて印刷し終わることを意味します。

ある注文 i について、L_j \leq W_i \leq R_j を満たす印刷機 j が 1 台も存在しない場合、その注文を印刷する手段がないため、すべての注文を完了することは不可能です。その場合は -1 を出力してください。

制約

  • 1 \leq N
  • 1 \leq M
  • N + M \leq 10^5
  • 1 \leq W_i \leq 10^9
  • 1 \leq H_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • \displaystyle \sum_{i=1}^{N} C_i \leq 10^9
  • 1 \leq L_j \leq R_j \leq 10^9
  • 入力はすべて整数である

入力

N M
W_1 H_1 C_1
W_2 H_2 C_2
\vdots
W_N H_N C_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • 1 行目には、注文の数を表す整数 N と、印刷機の台数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目から N + 1 行目では、各注文の情報が与えられる。
  • 1 + i 行目では、i 番目の注文のポスターの横幅 W_i、縦幅 H_i、枚数 C_i がスペース区切りで与えられる。
  • N + 2 行目から N + M + 1 行目では、各印刷機の情報が与えられる。
  • N + 1 + j 行目では、j 番目の印刷機が印刷できるポスターの横幅の下限 L_j と上限 R_j がスペース区切りで与えられる。

出力

すべての注文を完了するために必要な最小の日数を 1 行で出力せよ。すべての注文を完了できない場合は -1 を出力せよ。


入力例 1

2 2
10 5 3
20 8 2
1 15
10 30

出力例 1

3

入力例 2

2 3
10 100 4
50 200 3
1 20
21 30
60 80

出力例 2

-1

入力例 3

5 6
5 10 10
12 20 8
20 30 15
30 40 7
40 50 12
1 10
1 25
10 35
15 45
25 50
35 45

出力例 3

9

入力例 4

12 15
3 100 5
8 200 12
15 150 20
22 300 18
27 120 25
35 80 14
42 90 30
50 110 22
60 130 16
75 160 28
90 170 10
100 180 24
1 20
1 30
5 25
10 40
20 50
25 60
30 70
40 80
45 100
55 95
70 110
80 120
1 100
35 45
90 100

出力例 4

15

入力例 5

1 1
1000000000 1000000000 1000000000
1 1000000000

出力例 5

1000000000

Score : 466 pts

Problem Statement

Takahashi is the manager of a printing factory. The factory prints posters of various sizes.

Takahashi has received N orders. The i-th order (1 \leq i \leq N) requires printing C_i posters of the same size, with a width of W_i and a height of H_i.

The factory has M printing machines. The j-th printing machine (1 \leq j \leq M) can only print posters whose width is at least L_j and at most R_j. There is no restriction regarding the height of the poster. That is, regardless of the value of H_i, the j-th printing machine can print posters for the i-th order as long as L_j \leq W_i \leq R_j holds. (Note that although H_i is included in the input as part of the order information, it does not affect the assignment of printing machines.)

Each printing machine can print at most one poster per day. That is, on any single day, at most one poster can be assigned to each printing machine, and a machine cannot print posters for multiple orders on the same day.

It is possible to distribute the C_i posters of a single order among multiple printing machines. Multiple printing machines can print posters for the same order in parallel on the same day, or the printing can span across multiple days. Also, a single printing machine can print posters for different orders on different days.

Find the minimum number of days required to complete all orders. Here, completing all orders means that for each order i (1 \leq i \leq N), all C_i posters have been printed.

If there is an order i for which no printing machine j satisfies L_j \leq W_i \leq R_j, there is no way to print that order, making it impossible to complete all orders. In this case, output -1.

Constraints

  • 1 \leq N
  • 1 \leq M
  • N + M \leq 10^5
  • 1 \leq W_i \leq 10^9
  • 1 \leq H_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • \displaystyle \sum_{i=1}^{N} C_i \leq 10^9
  • 1 \leq L_j \leq R_j \leq 10^9
  • All input values are integers.

Input

N M
W_1 H_1 C_1
W_2 H_2 C_2
\vdots
W_N H_N C_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • The first line contains the integer N, representing the number of orders, and the integer M, representing the number of printing machines, separated by a space.
  • The next N lines (from the 2nd line to the N + 1-th line) provide the information for each order.
  • The 1 + i-th line contains the width W_i, height H_i, and the quantity C_i of the posters for the i-th order, separated by spaces.
  • The subsequent M lines (from the N + 2-th line to the N + M + 1-th line) provide the information for each printing machine.
  • The N + 1 + j-th line contains the lower bound L_j and upper bound R_j of the poster width that the j-th printing machine can print, separated by a space.

Output

Print the minimum number of days required to complete all orders in a single line. If it is impossible to complete all orders, print -1.


Sample Input 1

2 2
10 5 3
20 8 2
1 15
10 30

Sample Output 1

3

Sample Input 2

2 3
10 100 4
50 200 3
1 20
21 30
60 80

Sample Output 2

-1

Sample Input 3

5 6
5 10 10
12 20 8
20 30 15
30 40 7
40 50 12
1 10
1 25
10 35
15 45
25 50
35 45

Sample Output 3

9

Sample Input 4

12 15
3 100 5
8 200 12
15 150 20
22 300 18
27 120 25
35 80 14
42 90 30
50 110 22
60 130 16
75 160 28
90 170 10
100 180 24
1 20
1 30
5 25
10 40
20 50
25 60
30 70
40 80
45 100
55 95
70 110
80 120
1 100
35 45
90 100

Sample Output 4

15

Sample Input 5

1 1
1000000000 1000000000 1000000000
1 1000000000

Sample Output 5

1000000000