CF BUDDY
← Problems·

1710E · Two Arrays

2400 · binary search, games, graph matchings

Problem: You are given two arrays of integers a1,a2,,ana_1,a_2,\dots,a_n and b1,b2,,bmb_1,b_2,\dots,b_m.

Alice and Bob are going to play a game. Alice moves first and they take turns making a move.

They play on a grid of size n×mn \times m (a grid with nn rows and mm columns). Initially, there is a rook positioned on the first row and first column of the grid.

During her/his move, a player can do one of the following two operations:

  1. Move the rook to a different cell on the same row or the same column of the current cell. A player cannot move the rook to a cell that has been visited 10001000 times before (i.e., the rook can stay in a certain cell at most 10001000 times during the entire game). Note that the starting cell is considered to be visited once at the beginning of the game.
  2. End the game immediately with a score of ar+bca_r+b_c, where (r,c)(r, c) is the current cell (i.e., the rook is on the rr-th row and cc-th column).

Bob wants to maximize the score while Alice wants to minimize it. If they both play this game optimally, what is the final score of the game?

Input Format: The first line contains two integers nn and mm (1n,m21051 \leq n,m \leq 2 \cdot 10^5) — the length of the arrays aa and bb (which coincide with the number of rows and columns of the grid).

The second line contains the nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai51081 \leq a_i \leq 5 \cdot 10^8).

The third line contains the mm integers b1,b2,,bnb_1, b_2,\dots, b_n (1bi51081 \leq b_i \leq 5 \cdot 10^8).

Output Format: Print a single line containing the final score of the game.

Note: In the first test case, Alice moves the rook to (2,1)(2, 1) and Bob moves the rook to (1,1)(1, 1). This process will repeat for 999999 times until finally, after Alice moves the rook, Bob cannot move it back to (1,1)(1, 1) because it has been visited 10001000 times before. So the final score of the game is a2+b1=4a_2+b_1=4.

In the second test case, the final score of the game is a3+b5a_3+b_5.

Sample Cases

Case 1

Input

2 1
3 2
2

Output

4

Case 2

Input

4 5
235499701 451218171 355604420 132973458
365049318 264083156 491406845 62875547 175951751

Output

531556171

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback