import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param taskDurations int整型一维数组
     * @param capacity int整型
     * @return int整型
     */
    public int animalTaskScheduler (int[] taskDurations, int capacity) {
        // write code here
        int res = 0;
        int n = taskDurations.length;
        PriorityQueue<Integer> q = new PriorityQueue<>();
        for (int i = 0; i < n; i++) {
            if (q.size() < capacity) {
                q.offer(taskDurations[i]);
            } else {
                int p = q.poll();
                q.offer(p + taskDurations[i]);
            }
        }

        while (!q.isEmpty()) {
            res = q.poll();
        }

        return res;
    }
}