You design a robot that explores places people cannot reach, and you want it to travel as far as possible. You have n energy sources. Source i accelerates the robot at ai m/s2 and keeps doing so for si seconds. The robot starts at rest, so its initial velocity is 0.
Once you pick a source you use it for all of its si seconds, then switch straight to a source you have not used yet. The switch takes no time, and each source can be used only once. You choose the order of the sources so that the distance traveled is as large as possible.
Given the acceleration and the duration of every source, write a program that computes the distance traveled under the best order minus the distance traveled under the order given in the input.
Physics background: if the velocity is v right before you start a source whose acceleration is a, then after t seconds the robot has traveled vt+21at2 more meters and its velocity is v′=v+at.