Why this code is not working?

#include
#include<bits/stdc++.h>
using namespace std;

int solve(vector arr,int n,int k){

int dp[n]={0};

int max1=INT_MIN;

int max2=INT_MIN;

int ind1=-1;

int ind2=-1;

for(int i=0;i<k;i++){

	if(arr[i]>max1){
		max1=arr[i];
		ind1=i;
	}
	dp[i]=arr[i];
}

for(int i=0;i<k;i++){
	if(arr[i]>max2 && arr[i]<max1){
		max2=arr[i];
		ind2=i;
	}
}

int currblock=1;
int blocks=ceil(n/k);
bool flag=true;
for(int i=0;i<blocks-1;i++){
	int z=currblock*k;
	for(int j=z;j<z+k;j++){
		if(j==n){
			flag=false;
			break;
		}
		if(j-ind1!=k) dp[j]=arr[j]+max1;
		else dp[j]=arr[j]+max2;
	}
	
	if(flag==false) break;
	for(int j=z;j<z+k;j++){
		if(dp[j]>max1){
			max1=dp[j];
			ind1=j;
		}
	}

	for(int j=z;j<z+k;j++){
		if(dp[j]>max2 && dp[j]<max1){
			max2=dp[j];
			ind2=j;
		}
	}
	currblock++;
}

int ans=0;
for(int i=0;i<n;i++){
	ans=max(ans,dp[i]);
}
return ans;

}

int main () {

int n,k;

cin >> n >> k;

vector<int> arr(n);

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

	cin >> arr[i];

}

cout << solve(arr,n,k);
return 0;

}