#K7356. Minimal Energy Task Scheduling
Minimal Energy Task Scheduling
Minimal Energy Task Scheduling
You are given a list of tasks where each task requires a certain amount of energy to complete. Among these tasks, one task is designated as the high priority task and must be executed as specified. The remaining tasks can be executed in an order that minimizes the total energy consumed. In this problem, the total energy is defined as the sum of the energy required for each task when completed in sequence. To achieve the minimal total energy, you must execute the high priority task at its given position, and then carry out the rest of the tasks in ascending order of their energy requirements.
Formally, you are given an integer n representing the number of tasks, followed by a list of n integers representing the energy required for each task. Finally, you are given a target index p (0-indexed) that identifies the high priority task. The optimal strategy is to extract the high priority task and order the remaining tasks in ascending order, then sum the energy of all tasks. Note that since summing is commutative this strategy always gives the sum of all tasks, but the process simulates an ordering decision.
Your task is to implement a function that computes the minimal total energy required.
inputFormat
The input is read from standard input and consists of three lines:
- The first line contains an integer n, representing the number of tasks.
- The second line contains n space-separated integers, where each integer represents the energy required for a task.
- The third line contains a single integer p, the index (0-indexed) of the high priority task.
outputFormat
Output a single integer on a new line representing the minimal total energy required to complete all the tasks.
## sample5
50 30 20 10 40
2
150
</p>