Making a Rectangle
Time limit2sMemory limit256 MB
Given up to 16 sticks, choose four disjoint groups forming two equal-length pairs of sides to maximize the rectangle's area, or return -1 if impossible.
- Level
Medium6 of 10
- Topics
- Bit manipulation, Dynamic programming, Brute force, Combinatorics
- Solved
- No attempts yet
Problem
You are given N sticks. You want to choose some of them and connect them to make a rectangle.
Sticks may be connected end to end, but a stick cannot be cut. It is allowed to leave some sticks unused. The two opposite sides of the rectangle must have equal lengths.
Find the largest possible area of a rectangle that can be made. If no rectangle can be made, output -1.
Input
The first line contains the number of sticks N. N is a natural number with 4 <= N <= 16.
The second line contains the stick lengths separated by spaces. Each stick length is a natural number not greater than 10.
Output
Print the largest possible rectangle area on the first line. If it is impossible to make a rectangle with the given sticks, print -1.