Garland
InterviewTime limit1sMemory limit128 MB
Given the leftmost height A and the sag rule H_i = (H_{i-1}+H_{i+1})/2 - 1, find the smallest rightmost height B so all heights stay nonnegative.
- Level
Medium6 of 10
- Topics
- Math, Binary search, Implementation
- Solved
- No attempts yet
Problem

A New Year garland consists of lamps attached to a single wire whose two ends hang down to the points where the outermost lamps are fixed. The wire sags under the weight of the lamps, so each lamp hangs exactly lower than the average height of its two immediate neighbours.
The leftmost lamp hangs at a height of above the ground. Determine the lowest possible height of the rightmost lamp such that no lamp goes below the ground, although some lamps may touch it.
Ignore the size of each lamp. Number the lamps from to from left to right and let denote the height of the -th lamp in millimetres. Then the following relations hold:
- for every with
- for every with
The figure above shows an example garland with 8 lamps.
Input
A single line contains two numbers and separated by a space. () is an integer, the number of lamps in the garland. () is a real number, the height of the leftmost lamp above the ground in millimetres.
Output
Print a single real number , the lowest possible height of the rightmost lamp, accurate to two digits after the decimal point.