#C120. Minimize Fuel Consumption
Minimize Fuel Consumption
Minimize Fuel Consumption
You are given a series of thrust commands for a spacecraft. Each command is represented by a pair (direction, magnitude). The spacecraft starts with an initial velocity of zero. Your goal is to determine the optimal sequence of commands that minimizes the total fuel consumption by reordering the thrust commands.
The optimal strategy is to perform all positive thrust commands first (sorted in descending order of magnitude) and then the negative thrust commands (also sorted in descending order of magnitude). If there are no commands, then the operation is considered impossible and you should return -1 on output.
The formula used in the strategy is based on ordering by magnitude, i.e., if \( m_i \) is the magnitude of a command, then for two commands with the same sign, the command with larger \( m_i \) should be executed first to optimize fuel usage.
inputFormat
The input is read from standard input. The first line contains an integer \( N \) representing the number of thrust commands. The following \( N \) lines each contain two space-separated integers: the first integer is the direction and the second is the magnitude of the thrust command.
outputFormat
If there exists an optimal sequence, print each command in the optimal order on a separate line, where each line contains the direction and magnitude separated by a space. If the input contains no commands (i.e. \( N = 0 \)), output -1.
## sample3
1 10
-1 5
1 15
1 15
1 10
-1 5
</p>