import java.util.*;
public class NextFit {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		
		System.out.println("Enter no. of jobs:");
		int jobn = sc.nextInt();
		System.out.println("Enter jobs:");
		
		
		int job[] = new int[jobn];
		for (int i = 0; i < jobn; i++) {
		job[i] = sc.nextInt();
		}
		
		System.out.println("Enter no. of blocks:");
		int block = sc.nextInt();
		System.out.println("Enter blocks:");
		
		
		int blocks[] = new int[block];
		int totalBlocks = 0;
		for (int i = 0; i < block; i++) 
		{
		blocks[i] = sc.nextInt();
		totalBlocks += blocks[i];
		}
		
		int sumAllocatedJobs = 0;
		int lastIndex = 0;
		
		
		for (int i = 0; i < jobn; i++)
		{
			boolean allocated = false;
			int count = 0;
			int j = lastIndex;
			
			while (count < block) {
				if (job[i] <= blocks[j]) {
				sumAllocatedJobs += job[i];
				blocks[j] -= job[i];
				lastIndex = j;
				allocated = true;
				break;
				}
				
				j = (j + 1) % block; 
				count++;
				}
				
				if (!allocated)
				{
				System.out.println(" Job " + job[i] + " cannot be allocated.");
				}
		}
				double avgNext = (double) sumAllocatedJobs / totalBlocks;
				System.out.printf("avg_next:- %.2f\\n", avgNext);
	
		
	}
}
	

image.png