content: class Solution {
      public long totalCost(int[] costs, int k, int candidates) {
          PriorityQueue<Integer> headWorkers = new PriorityQueue<>();
          PriorityQueue<Integer> tailWorkers = new PriorityQueue<>();
          
          // headWorkers stores the first k workers.
          // tailWorkers stores at most last k workers without any workers from the first k workers.
          for (int i = 0; i < candidates; i++) {
              headWorkers.add(costs[i]);
          }
          for (int i = Math.max(candidates, costs.length - candidates); i < costs.length; i++) {
              tailWorkers.add(costs[i]);
          }
  
          long answer = 0;
          int nextHead = candidates;
          int nextTail = costs.length - 1 - candidates;
  
          for (int i = 0; i < k; i++) {
              if (tailWorkers.isEmpty() || !headWorkers.isEmpty() && headWorkers.peek() <= tailWorkers.peek()) {
                  answer += headWorkers.poll();
                  
                  // Only refill the queue if there are workers outside the two queues.
                  if (nextHead <= nextTail) {
                      headWorkers.add(costs[nextHead]);
                      nextHead++;
                  }
              } 
              
              else {
                  answer += tailWorkers.poll();
  
                  // Only refill the queue if there are workers outside the two queues.
                  if (nextHead <= nextTail) {
                      tailWorkers.add(costs[nextTail]);
                      nextTail--;
                  }
              }
          }
  
          return answer;
      }
  }