UVa 10037 - Bridge

10037 - Brigde

 * http://acm.uva.es/p/v100/10037.html

Summary

 * there is a number of people n on the one side of a brigde
 * they want to get to the other side
 * bridge can hold at most 2 people at the same time
 * people need light to get throught the bridge
 * they have only one flashlight
 * one can travel through the bridge with a given speed defined as a time required to get to the other side
 * speed of a group of people is defined by the speed of its slowest member
 * find the optimal time for all people crossing the bridge

Explanation
Do you get the idea now? a and b can be transfered to the other side in 2 different efficient ways. But which one is more efficient? actually that is all about the problem. We just check which one is more efficient and give it a go. Ok, and how do we check it? Simply constructing an inequality: if (2*A + a + b < 2*B + A + a) the first is better; else second is better; for simplicity one can observe that 2*A + a + b < 2*B + A + a is the same as b + A < 2*B
 * this one is interesting problem based on quite simple logic
 * let 'A' be the fastest member and 'B' second fastest
 * let 'a' be the slowest member and 'b' second slowest
 * main problem is to get the slowest members (a and b) of the group to the other side, how can this be done?
 * 1. the fastest member moves throught the bridge with the slowest and gets back time needed: a + A ('a' time when a and A go to the other side and 'A' time when a goes back) and if we want to take 2 slowest members than the time is: 2*A + a + b
 * 2.
 * a) two fastest members go to the other side; time: B
 * b) then A goes back and gives flashlight to most slowest 'a' and 'b'; time: A
 * c) 'a' and 'b' are ready to go to the other side, what they actually do; time: a
 * After their mission is accomplished they give flashlight to 'B', who is on the other side at that moment; time
 * d) Unfortunately 'B' needs to get back to the group in order to give them the flashlight; time: B
 * time needed: 2*B + A + a

Input
1

4 1 2 5 10

Output
17 1 2 1 5 10 2 1 2