-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChefAug2.cpp
More file actions
53 lines (53 loc) · 825 Bytes
/
ChefAug2.cpp
File metadata and controls
53 lines (53 loc) · 825 Bytes
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
#include<bits/stdc++.h>
#define test int t;scanf("%d",&t);while(t--)
#define loop(i,a,b) for(long long int i=a;i<b;i++)
#define pii pair<int,long int>
#define pli pair<long int,int>
#define ll long long int
using namespace std;
int main()
{
test
{
ll n,d,x,sum=0,factor,sumx=0;
scanf("%lld%lld",&n,&d);
vector<ll> a;
loop(i,0,n)
{
scanf("%lld",&x);
sum=sum+x;
a.push_back(x);
}
if(sum%n!=0)
{
printf("-1\n");
continue;
}
factor=sum/n;
x=0;
loop(i,0,d)
{
ll j=i,cnt=0,score=0;
while(j<n)
{
sumx+=a[j];
a[j]+=score;
score=a[j]-factor;
x+=abs(score);
j+=d;
cnt++;
}
if(sumx/cnt!=factor)
{
printf("-1\n");
x=-11;
break;
}
sumx=0;
}
if(x==-11)
continue;
printf("%lld\n",x);
}
return 0;
}