#include<stdio.h>

int main()

{

long a[100],sum=0;

int ans=0,n=0,d=0,i=0, ratio=0, t=0, dif=0;

scanf("%d", &t);

while(t–>0){

scanf("%d %d",&n, &d);

for(i=0;i<n;i++)

{

scanf("%ld", &a[i]);

sum+=a[i];

}

```
if(sum%n==0)
{
ratio=sum/n;
for(i=0;i<n-d;i++)
{
if(a[i]>ratio)
{
dif=a[i]-ratio;
a[i]-=dif;
ans+=dif;
a[i+d]+=dif;
}
else if(a[i]<ratio)
{
dif=ratio-a[i];
a[i]+=dif;
ans+=dif;
a[i+d]-=dif;
}
}
for(i=0;i<n;i++)
{
if(a[i]!=ratio)
ans=-1;
}
}
else
ans=-1;
printf("%d",ans);
}
return 0;
```

}

There is nothing wrong with the code and I have initialized all the variables too.