Two Arrays
Contest: Codeforces Round 810 (Div. 1)
Difficulty rating: 2400
Tags: binary search, games, graph matchings

You are given two arrays of integers $$$a_1,a_2,\dots,a_n$$$ and $$$b_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 \times m$$$ (a grid with $$$n$$$ rows and $$$m$$$ 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 $$$1000$$$ times before (i.e., the rook can stay in a certain cell at most $$$1000$$$ 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 $$$a_r+b_c$$$, where $$$(r, c)$$$ is the current cell (i.e., the rook is on the $$$r$$$-th row and $$$c$$$-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 $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 2 \cdot 10^5$$$) — the length of the arrays $$$a$$$ and $$$b$$$ (which coincide with the number of rows and columns of the grid).

The second line contains the $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \leq a_i \leq 5 \cdot 10^8$$$).

The third line contains the $$$m$$$ integers $$$b_1, b_2,\dots, b_n$$$ ($$$1 \leq b_i \leq 5 \cdot 10^8$$$).

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

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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

In the second test case, the final score of the game is $$$a_3+b_5$$$.

Example input:
2 1
3 2
2

Example output:
4

Example input:
4 5
235499701 451218171 355604420 132973458
365049318 264083156 491406845 62875547 175951751

Example output:
531556171

Editorial:
Hint1: Since they are very smart, they know the result of the game at the beginning.
Hint2: If the result is $$$x$$$, then Alice will end the game when Bob moves to a cell with score less than $$$x$$$, and something analogous holds for Bob.
Hint3: Thus, Alice can only move to a certain subset of cells, and the same holds for Bob.
—-
Knowing the above facts, it is clear that we can apply binary search on the answer $$$Z$$$, which is less than $$$a_1+b_1$$$, or Alice can end the game immediately to get $$$a_1+b_1$$$.
Let's color white all the cells with $$$a_r+b_c \leq Z$$$, and black all the cells with $$$a_r+b_c > Z$$$.
Then we shall add edges between cells in the same row or same column with different colors. These edges and cells forms a bipartite graph.
Consider the game on the bipartite graph. Initially, we are at cell $$$(1,1)$$$. Alice moves first, then they take turns to move. Each player can only move the cell to another place with an edge connecting them, or the other player will end the game immediately.
Each cell can be visited at most $$$1000$$$ times, whoever cannot move loses.
If Alice wins, then the answer is no greater than $$$Z$$$, otherwise the answer is greater than $$$Z$$$.
The version of this game where each vertex can be visited exactly once is known. If both player plays optimally, the first player wins iff the starting vertex belongs to all possible maximum matchings. I'll explain why at the end of the editorial.
It turns out that the condition is exactly the same even if each vertex can be visited at most $$$1000$$$ times. Let us show why.
First, calculate the maximum matching. Then we erase the starting vertex and calculate the maximum matching in the new graph. If two matchings have the same size, the vertex does not belong to all maximum matchings and vice versa.
Now, we know that if we copy a cell $$$1000$$$ times and copy the edges as well, this problem is exactly the same as the model mentioned above. If we consider the initial bipartite graph, it's easy to see that we only need to check whether $$$(1,1)$$$ is in all maximum matchings of the initial graph, becuase the maximum matching remains unchanged in the other $$$999$$$ parts.
So, we have shifted the problem to seeing if the initial cell belongs to all matchings.
According to [Kőnig's theorem in graph theory](https://en.wikipedia.org/wiki/K Using $$$M$$$ to refer to the maximum matching, $$$I$$$ to refer to the maximum independet set, $$$V$$$ to refer to the set of vertices, we know that $$$|M|+|I|=|V|$$$
So, if we erase one vertex and $$$|M|$$$ remains unchanged, then $$$|I|$$$ must be changed and vice versa.
Now our issue is to calculate the maximum indenpendent set in this graph, with or without the initial cell.
Let us sort $$$a_i$$$ and $$$b_i$$$ (notice that reordering rows and columns does not change the game at all). Now the white cells form a "decreasing histogram". We can still find the starting cell $$$(va,vb)$$$.
First, let's compute the maximum independent set with the initial cell.
Before that, consider following constraints:
It's obvious that one column has at most one color of its cells in the independent set(we shall call it $$$I$$$), so does a row. Let's call a column white if only white cells of it are in $$$I$$$, other wise we shall calll it black.
What is the maximum size of $$$I$$$, when we must have $$$i$$$ white columns and $$$j$$$ white rows? The answer is simple. We shall select the first $$$i$$$ columns to be white and the first $$$j$$$ rows to be white, rest of the columns and rows to be black. Then the independent set consists of black cells on black rows and black columns, and white cells on white columns and white rows. It's easy to prove this greedy strategy is correct.
Now we fix the number of white columns as $$$i$$$, and try to find a $$$j$$$ that maximize $$$|I|$$$. If we make an array $$$da[i]$$$ satisfying $$$a_i+b_{da[i]}\leq Z$$$ and $$$a_i + b_{da[i]+1} > Z$$$, and a similar array $$$db[j]$$$, we can easily calculate the change of $$$|I|$$$ when we turn row $$$j$$$ from black to white and vice versa, which is $$$n-max(i,db[j])-min(i,db[j])$$$, $$$-min(i,db[j])$$$ means remove the white part from $$$I$$$, $$$+n-max(i,db[j])$$$ meanw add the black part to $$$I$$$. For a fixed $$$i$$$, it's easy to see that the change increases with $$$j$$$.
So you can maintain all positive changes of $$$|I|$$$, just decrease the $$$j$$$ with $$$i$$$ decreasing. Now you can calculate the maximum of $$$|I|$$$ in $$$O(n+m)$$$ time.
It's easy to see that $$$da[i]$$$ and $$$db[i]$$$ are non-increasing, so they can be calculated in $$$O(n+m)$$$ time.
For the maximum independent set without the initial cell, you just need to remove it when it is in $$$I$$$. Since the cell is always black, it is quite easy.
Using binary search on the answer, you can solve the whole problem in $$$O((n+m)\log A+n\log n+m\log m)$$$, where $$$A$$$ is the maximum value of the arrays $$$a$$$ and $$$b$$$.
Let us conclude with the idea of the known game on bipartite graphs.
Lemma: The first player can win if and only if all possible maximum matchings include the initial vertex $$$H$$$.
Let's prove it when $$$H$$$ satisfies the constraint.
The first player can just choose any matching $$$M$$$ and move to the vertex $$$P$$$ matching with current vertex, then any unvisited neighbor of $$$P$$$ still matches with other unvisited vertices. If $$$P$$$ has a neighbor unmatched in a certain matching $$$M$$$, we find an augmenting path and a matching $$$M'$$$ that doesn't include $$$H$$$, which is in conflict with the constraint. So no matter how the second player chooses to move, the first player always has a vertex to go after that.
Otherwise, we add an vertex $$$P$$$ with the only edge $$$(P,H)$$$, move the initial cell to $$$P$$$, swap the two players, then it turns into the situation above.