It is giving coorect output except one testcase , whats wrong in this code then

public static void main(String[] args) {
	
	Scanner sc=new Scanner(System.in);
	int n=sc.nextInt();
	int cap=sc.nextInt();
	
	ArrayList<Integer> wt = new ArrayList<>();
	ArrayList<Integer> val = new ArrayList<>();

	for(int i=0; i<n ; i++)
	{
		int temp=sc.nextInt();
		wt.add(temp);
	}
	for(int i=0 ; i<n ; i++)
	{
		int temp=sc.nextInt();
		val.add(temp);
	}
	
	
	int ans=0;
	while(cap>0)
	{
		int index=0 ;
		double max=0;
		for(int i=0 ; i<val.size() ; i++)
		{
			double temp=(double)val.get(i)/wt.get(i);
			
			if(temp>max && wt.get(i)<=cap)
			{
				max=temp;
				index=i;
			}
		}
		
		int numberoftimes = cap/wt.get(index);
		
		if(numberoftimes==0) break;
		
		ans = ans + numberoftimes * val.get(index);
		
		cap = cap - numberoftimes*wt.get(index);
		
		wt.remove(index);
		val.remove(index);
	}
	
	System.out.println(ans);
}

Hey @Himanshu-Jhawar-2273952536067590
try for this input :
4 12
5 7 9 8
17 22 30 26
correct output : 39