#C4633. Task Scheduler: Minimum Execution Time

    ID: 48193 Type: Default 1000ms 256MiB

Task Scheduler: Minimum Execution Time

Task Scheduler: Minimum Execution Time

You are given a list of tasks represented by uppercase letters and a non-negative cooling interval n. Each task takes 1 unit of time to complete. However, the same task must be separated by at least n units of time. If there is no available task to execute during the cooling period, the CPU remains idle.

Formally, if the most frequent task appears fmax times and there are c tasks with this frequency, then the minimum time is:

[ \text{time} = \max(\text{totalTasks}, (f_{max} - 1) \times (n + 1) + c) ]

Determine the least number of units of time that the CPU will take to finish all the given tasks.

inputFormat

The input is given via standard input with three lines:

  • First line: A single integer T denoting the number of tasks.
  • Second line: T space-separated uppercase letters representing the list of tasks.
  • Third line: An integer n representing the cooling interval.

outputFormat

Output a single integer representing the minimum number of time units required by the CPU to process all tasks.

## sample
6
A A A B B B
2
8

</p>