Why My solution is wrong for Problem Buying2 ?

link to the question: https://www.codechef.com/problems/BUYING2

my code:

#include
using namespace std;
int main()
{
int test;
cin>>test;
while(test–)
{
int a,sum=0,i,j,countt=0,no,value,p;
cin>>no>>value;
for(i=1;i<=no;i++)
{
p=0;
cin>>a;
for(j=sum+1;j<=sum+a;j++)
{
if(j%value==0)
{
countt++;
p=1;
}
}
sum=sum+a;
}
if(p==0)
{
cout<<"-1"<<"\n";
}
else if(p==1){
cout<<countt<<"\n";
}
}
return 0;
}

please help me with this…thanks in advance