Multicolored Markers
Contest: Codeforces Round 506 (Div. 3)
Difficulty rating: 2000
Tags: binary search, brute force, math, number theory

There is an infinite board of square tiles. Initially all tiles are white.

Vova has a red marker and a blue marker. Red marker can color $$$a$$$ tiles. Blue marker can color $$$b$$$ tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly $$$a$$$ red tiles and exactly $$$b$$$ blue tiles across the board.

Vova wants to color such a set of tiles that:

- they would form a rectangle, consisting of exactly $$$a+b$$$ colored tiles;
- all tiles of at least one color would also form a rectangle.

Here are some examples of correct colorings:

Here are some examples of incorrect colorings:

Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain?

It is guaranteed that there exists at least one correct coloring.

Input format:
A single line contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le 10^{14}$$$) — the number of tiles red marker should color and the number of tiles blue marker should color, respectively.

Output format:
Print a single integer — the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly $$$a$$$ tiles red and exactly $$$b$$$ tiles blue.

It is guaranteed that there exists at least one correct coloring.

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

Note:
The first four examples correspond to the first picture of the statement.

Note that for there exist multiple correct colorings for all of the examples.

In the first example you can also make a rectangle with sides $$$1$$$ and $$$8$$$, though its perimeter will be $$$18$$$ which is greater than $$$8$$$.

In the second example you can make the same resulting rectangle with sides $$$3$$$ and $$$4$$$, but red tiles will form the rectangle with sides $$$1$$$ and $$$3$$$ and blue tiles will form the rectangle with sides $$$3$$$ and $$$3$$$.

Example input:
4 4

Example output:
12

Example input:
3 9

Example output:
14

Editorial:
$$$a+b$$$ should be area of the outer rectangle. It means that its sides are divisors of $$$a+b$$$. The same holds for the inner rectangle. Let's assume that red color forms a rectangle, we'll try it and then swap $$$a$$$ with $$$b$$$ and solve the same problem again. Write down all the divisors of $$$a$$$ up to $$$\sqrt a$$$, these are the possible smaller sides of the inner rectangle. Divisors of $$$a+b$$$ up to $$$\sqrt{a+b}$$$ are possible smaller sides of the outer rectangle.
Let's put inner rectangle to the left bottom corner of the outer rectangle and choose smaller sides of both of them as bottom and top ones. Iterate over the divisors $$$d1$$$ of $$$a+b$$$, for each of them choose the greatest divisor $$$d2$$$ of $$$a$$$ smaller or equal to it and check that $$$\frac{a+b}{d1} \ge \frac{a}{d2}$$$. Update the answer with $$$2(d1 + \frac{a+b}{d1})$$$ if it holds.
You can use both binary search or two pointers, both get AC pretty easily.
The number of divisors of $$$n$$$ can usually be estimated as $$$n^{\frac 1 3}$$$.
Overall complexity: $$$O(\sqrt{a+b})$$$.