#C7766. Minimum Days to Complete Tasks
Minimum Days to Complete Tasks
Minimum Days to Complete Tasks
You are given a list of integers representing the duration of each task and an integer k representing the number of tasks that must be completed. Each task takes exactly one day to complete regardless of its duration. However, the worker must take a mandatory rest day after completing a task, except after the final task. That means, if the worker completes k tasks, they need k - 1 rest days interleaved between them. The worker may choose to rest on any day, and tasks cannot be split across days.
The goal is to determine the minimum number of days required to complete at least k tasks. If k is greater than the number of available tasks, output -1
.
The relationship used to compute the days is given by the formula:
\( \text{days} = \begin{cases} -1, & \text{if } k > n \\ 2k - 1, & \text{otherwise} \end{cases} \)
where \(n\) is the total number of tasks.
inputFormat
The input is given via standard input in the following format:
n x1 x2 ... xn k
Here, n
represents the number of tasks, followed by a line with n
space-separated integers representing the durations of each task (these values are not used in the calculation, but they are provided as part of the input). The final line contains the integer k
, representing the number of tasks to complete.
outputFormat
Output a single integer representing the minimum number of days required to complete k
tasks, or -1
if it is impossible.
5
2 3 1 4 2
3
5