619. Task Scheduler

0

Medium

Given a characters array tasks, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.
However, there is a non-negative integer n that represents the cooldown period between two same tasks (the same letter in the array), that is that there must be at least n units of time between any two same tasks.
Print the least number of units of times that the CPU will take to finish all the given tasks.

Input Format

The first line of the input contains an integer representing size of tasks array.

The second line contains space separated integers denoting the tasks array.

The third line contains an integer representing 'cooling intervals'.

The second line contains space separated integers denoting the tasks array.

The third line contains an integer representing 'cooling intervals'.

Output Format

Output a single integer i.e. the least number of intervals the CPU will take to finish all the given tasks.

Example

Input

6
A A A B B B
2

Output

8

Constraints

1. The number of tasks is in the range [1, 10000].

2. The integer n is in the range [0, 100].

2. The integer n is in the range [0, 100].

Loading...

View Submissions

Console