Sticks
Time limit1sMemory limit128 MB
From sticks grouped by colour, find three sticks of pairwise different colours that form a non-degenerate triangle and maximise the perimeter.
- Level
Medium7 of 10
- Topics
- Sorting, Greedy, Two pointers
- Solved
- No attempts yet
Problem
Little Johnny received a birthday present from his grandparents: a box of sticks of various lengths and colours. Johnny wonders whether he can pick three sticks that form a triangle whose three sides all have different colours. He only cares about non-degenerate triangles, that is, triangles with positive area.
Among all triangles that can be built from three sticks of three pairwise-different colours, find the one with the largest perimeter and report that perimeter.
Input
The first line contains an integer (), the number of different stick colours. The colours are numbered from to .
Each of the next lines describes the sticks of one colour. Line describes the sticks of colour : it begins with an integer (), the number of sticks of that colour, followed on the same line by integers separated by single spaces, the lengths of those sticks. Every length is a positive integer not exceeding . The total number of sticks does not exceed .
Output
Print a single line.
If at least one triangle with three pairwise-different-coloured sides and positive area can be formed, print the maximum possible perimeter of such a triangle (the sum of its three side lengths).
Otherwise, print -1.
A triangle with sides has positive area (is non-degenerate) exactly when .