Help me in solving CHEFMOVR problem

My issue

i am not getting right answer , i dont know what have i done wrong

My code

#include <iostream>
#include<bits/stdc++.h>
#define int long long 
using namespace std;

int32_t main() {
	int T;
	cin >> T;
	while(T--){
	    int n,d;
	    cin >> n >> d;
	    int arr[n];
	    int ans = 0,temp1=0,temp2=0;
	    vector<vector<int>> v(d);
	    for(int i=0;i<n;i++){
	        cin >> arr[i];
	    }
	    for(int i=0;i<n;i++){
	        v[i%d].push_back(arr[i]);
	    }
	    
	   
	    int sum = 0,z=0;
	    for(auto i:v){
	        sum = 0;
	        for(auto j:i){
	         
	            sum += j;
	            
	        }
	        if(sum%(i.size()) != 0) {
	            z=1;
	            break;
	        }
	        else{
	         
	   
	        
	            for(auto j:i){
	                if(j> sum/(i.size())){
	                   ans += j- sum/(i.size()); 
	                 
	                } 
	            }
	           
	           
	        }
	    }
	    //cout << endl;
	    if(z) cout << -1 << endl;
	    else cout << ans << endl;
	}
}

Problem Link: CHEFMOVR Problem - CodeChef