Help me in solving CHFHEIST problem

My issue

include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t–){
int D,d,p,q;
cin>>D>>d>>p>>q;
long long a[D]={0};
for(int i=0;i<D;i++){
if(i%d==0&&i!=0) {
a[i]=p+q;
p=p+q;}
else a[i]=p;

    }
    long long k=accumulate(a,a+D,0);
    cout<<k<<endl;
    
}
return 0;

}
what is the issue in this code?

My code

#include <bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin>>t;
	while(t--){
	    int D,d,p,q;
	    cin>>D>>d>>p>>q;
	    long long a[D]={0};
	    for(int i=0;i<D;i++){
	        if(i%d==0&&i!=0) {
	            a[i]=p+q;
	            p=p+q;}
	        else a[i]=p;
	        
	        
	    }
	    long long k=accumulate(a,a+D,0);
	    cout<<k<<endl;
	    
	}
	return 0;
}

Problem Link: Bella ciao Practice Coding Problem - CodeChef

@rotorop
its basically a maths problems
here refer my c++ code for better understanding

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    long long int a,b,c,d;
        cin>>a>>b>>c>>d;
        long long int  ans=a*c;
        long long int n=(a-b)/b;
        long long int r=(a-b)%b;
        ans=ans+((n*(n+1))/2)*d*b+(n+1)*r*d;
        cout<<ans<<endl;
	}
	return 0;
}