This is a solution for Max sum K-partitions problem, when I use array declarations it shows Runtime error, but works fine with vector declarations. Can't figure out why? How they are different?
Max Sum K-partitions Problem Statement: Siraj has an array of N integers and an integer K. He wants to create a subsequence of this array with some conditions applied. He builds ceil(N/K) blocks of this array with size of each block as [i∗K+1,min((i+1)∗k,N)] for 0≤i≤N/K. For two consecutive integers in this subsequence, he wants the integers to be of two different blocks. (It is not a compulsion to have one integer from each block). Also, lets say the indexes of any two integers of this subsequence be i and j, then he wants |i−j| != K. Siraj takes the sum of integers in the subsequence. He wonders what can be the maximum sum obtained? Help siraj in this problem.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll maxsum(ll arr[], ll n, ll k){
ll dp[n]={0}; // vector<ll> dp(n,0);
ll max1=INT_MIN;
ll max2=INT_MIN;
ll maxind=-1;
ll i = 0;
for(i = 0 ; i < k ;i++){
dp[i] = arr[i];
if(arr[i] > max1){
max2 = max1;
max1 = arr[i];
maxind = i;
continue;
}
max2 = max(max2 , arr[i]);
}
while(count--){
pos = i;
for( ; i < min(n, pos+k) ; i++){
dp[i] = arr[i];
if(arr[i] >= 0 ){
if(abs(i-maxind) != k){
dp[i] += max1;
}else
dp[i] += max2;
}else
dp[i] = max1;
}
for(i = pos ; i < min(n,pos+ k) ; i++){
if(dp[i] > max1){
max2 = max1;
max1 = dp[i];
maxind = i;
continue;
}
max2 = max(max2, dp[i]);
}
}
return max1;
}
int main(){
ll n,k;
cin>>n>>k;
ll a[n]; //vector<ll> a(n);
for(int i=0;i<n;i++)
cin>>a[i];
cout<<maxsum(a,n,k)<<"\n";
return 0;
}