There is a crossroad where N roads meet. Due to heavy traffic, managing traffic signals is quite difficult. On all N roads, there are vehicles waiting for the green light. You are given a two-dimensional array A where the 4 denotes a list of 4 elements containing road number, the number of two- wheelers on road, the number of three-wheelers on road, and the number of four-wheelers on road waiting for the traffic signal to turn green. In one second, 3 two-wheelers or 1 two- wheeler and 1 four-wheeler or two-wheeler and 1 three- wheeler or less than that can cross a road. All roads are of the same kind.
You have to manage the traffic signals and decide when to give the green signal on the roads. The rules for traffic signals are:
Note: None of the roads are empty.
Find out in which sequence the green signal should be given and when it should be given to the roads in order to minimize the sum of waiting time over all roads. If there are several sequences having the same sum of waiting times, determine the lexicographically minimum.