For the following question,
Nested Candy Boxes
I submitted the following code as a response:
-----------------------------Start of Code-----------------------------
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt(), m = input.nextInt();
int[] a = new int[n+1];
int[] X = new int[m];
for(int i=1;i<=n;i++) {
a[i] = input.nextInt();
}
for(int i=0;i<m;i++) {
X[i] = input.nextInt();
}
for(int i=0;i<m;i++) {
int[] boxOpened = new int[n+1];
boxOpened[0] = X[i];
for(int j=1;j<=n;j++) {
boxOpened[j] = boxOpened[j-1]%a[j]==0? boxOpened[j-1]/a[j] : boxOpened[j-1]/a[j]+1;
}
int sum = 0;
for(int j=1;j<=n;j++) {
sum += boxOpened[j];
}
System.out.println(sum);
}
}
}
------------------------------End of Code------------------------------
But I kept getting “runtime error(NZEC)”
Please provide suggestions on how to edit my code so as to prevent this error.