public class TaskScheduling {


   
public static int getMinTime(List<Integer> task_memory, List<Integer> task_type, int max_memory) {
       
int minTime = 0;
       
Map<Integer, Integer> typeToMemory = new HashMap<>();

       
for (int i = 0; i < task_memory.size(); i++) {
           
int memory = task_memory.get(i);
           
int type = task_type.get(i);

           
if (typeToMemory.containsKey(type)) {
               
int totalMemory = typeToMemory.get(type) + memory;
               
if (totalMemory <= max_memory) {
                   
typeToMemory.put(type, totalMemory);
                }
else {
                   
minTime++; // Process the task separately
                   
typeToMemory.put(type, memory);
                }
            }
else {
               
typeToMemory.put(type, memory);
               
minTime++; // Process the first task separately
           
}
        }

       
return minTime;
    }


   
public static void main(String[] args) {
       
List<Integer> task_memory = List.of(1, 2, 3, 4, 2);
       
List<Integer> task_type = List.of(1, 2, 1, 2, 3);
       
int max_memory = 4;
       
System.out.println(getMinTime(task_memory, task_type, max_memory)); // Output: 4

       
task_memory = List.of(20, 17, 18, 13, 11, 13, 19, 15, 13, 10, 13, 12, 11, 15, 19, 16, 10, 11, 14, 18, 19);
       
task_type = List.of(20, 4, 3, 4, 1, 1, 3, 3, 4, 2, 2, 4, 3, 5, 1, 3, 4, 3, 2, 3, 1);
       
max_memory = 213;
       
System.out.println(getMinTime(task_memory, task_type, max_memory)); // Output: 12
   
}
}