Walking Route
InterviewTime limit1sMemory limit128 MB
Given N vectors forming a walking route, output the total displacement and the minimum distance from the start after removing exactly one segment.
- Level
Easy3 of 10
- Topics
- Array, Math, Brute force
- Solved
- No attempts yet
Problem
A navigation device suggests one walking route. The route consists of several line segments, and each segment is represented by two integers (X, Y). This pair is a vector that describes the direction and distance of that segment.
For instance, the route in the left figure below consists of four segments. The first segment is (-1, 1), the second is (1, 1), the third is (1, 0), and the fourth is (0, -2). After following the whole route, the final position is 1 meter east of the starting position.

To avoid ending the walk too far from the start, exactly one segment will be removed from the route. After removing one segment, the remaining segments are still followed in their original order, and the distance between the starting position and the final position should be as small as possible.
In the route shown in the figure, removing the third segment makes the final position exactly match the starting position, as in the right figure.
Given the suggested walking route, compute the final position when the whole route is used as given, and the minimum possible distance between the starting position and the final position after removing exactly one segment.
Input
The first line contains the number of segments N. (1 < N <= 30)
Each of the next N lines contains two integers X and Y describing one segment. (-1000 <= X, Y <= 1000)
Output
On the first line, print the final position after following the whole walking route. The starting position is (0, 0), and the final position must be printed in the form x y.
On the second line, print the minimum possible distance between the starting position and the final position after removing exactly one segment, rounded to two digits after the decimal point.