-
Notifications
You must be signed in to change notification settings - Fork 0
/
aug_1.cpp
56 lines (56 loc) · 1.06 KB
/
aug_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*author:Avinash
college:Nit Trichy
*/
#include<iostream>
#include<math.h>
#include<stdlib.h>
#define ll long long int
using namespace std;
int main()
{
ll no,n,d,i,c,s1,k,j,s,p,def;
ll a[100000];
cin>>no;
while(no--)
{
cin>>n>>d;
s1=0,c=0;
for(i=0;i<n;i++){
cin>>a[i];
s1=s1+a[i];
}
if(s1%n!=0)
cout<<"-1"<<endl;
else
{
k=s1/n;
bool f=0;
for(i=0;i<d;i++)
{
s=0,p=0;
for(j=i;j<n;j=j+d)
s=s+a[j],p++;
if(s%p!=0||(s%p==0&&s/p!=k))
{
f=1;break;
}
def=0;
for(j=i;j<n;j=j+d)
{
if(a[j]+def==k)
def=0;
else
{
def=def+(a[j]-k);
c=c+abs(def);
}
}
}
if(f)
cout<<"-1"<<endl;
else
cout<<c<<endl;
}
}
return 0;
}