A diamond is a quadrangle with 4 sides of equal length. In this problem, every diamond must be oriented so that one of its diagonals is strictly horizontal and the other is strictly vertical. For example, the diamonds shown in figures 1a, 1b and 1c below are oriented correctly, but figure 1d is not. The horizontal diagonal is called x and the vertical diagonal is called y. Please note that either x or y can be of length 0 (see example 4 for further clarification). There are two categories of diamonds: tall and wide. A diamond is called tall if y >= x, and a diamond is called wide if x >= y. The diamond in figure 1a is tall, and the diamond in figure 1b is wide. In figure 1c, x is equal to y, so you can choose to call the diamond tall or wide.
You want to nest a set of diamonds one inside another. A diamond A is nested in diamond B if exactly two vertices of A are located strictly inside diamond B, and the other two vertices of A coincide with two different vertices of diamond B. Figures 1a and 1b show correctly nested diamonds, and figures 1c and 1d show incorrectly nested diamonds.
For the purposes of this problem, a diamond A is considered nested in diamond B only if A is immediately nested in B. In other words, there must be no other diamonds between them. For example, if a diamond C is nested in diamond A, and diamond B is nested in diamond C, then B is not nested in A. The nesting of the set of diamonds must satisfy all of the following rules:
- Each diamond must be used in the nesting exactly once.
- Exactly one of the diamonds must be nested in none of the other diamonds. This diamond is called the outside diamond.
- Each diamond, except the outside diamond, must be nested in exactly one other diamond.
- Each tall diamond must be nested in a wide diamond.
- Each wide diamond, except the outside diamond, must be nested in a tall diamond.
- The outside diamond must be wide.
The height of the nesting is equal to the length of the vertical diagonal of the outside diamond. You are given a int sides, where each element is the side length of a single diamond in the set. Return the minimum possible height of their nesting. If it is impossible to create a valid nesting, return -1 instead.