link to Question - CodeChef: Practical coding for everyone
I am unable to generalize my solution to K > 1. Can someone guide me on how to generalize my solution?
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long N,K;
cin>> N;
cin>> K;
vector<long long>organisms;
vector <long long>weights;
for(long long i = 0; i< N; i++)
{
long long x;
cin >> x;
organisms.push_back(x);
}
for(long long i = 0; i< N; i++)
{
long long x;
cin >> x;
weights.push_back(x);
}
long long wallo = 0;
long long weightswall = 0;
long long biomasswall = 0;
long long biomass = 1e17;
long long minn_i;
if(K == 0)
{
biomass = 0;
for(long long i = 0; i < N; i++)
{
biomass += organisms[i];
}
sort(weights.begin(), weights.end());
biomass *= weights[N - 1];
cout << biomass;
}
if(K ==1)
{
for(long long i =0; i < N - 1; i++)
{
long long wall1 = 0;
for(long long j =0; j <= i; j++)
{
wall1 += organisms[j] ;
}
long long weights1 = 0;
for(long long j =0; j <= i; j++)
{
weights1 = max(weights1, weights[j]);
}
long long wall2 = 0;
for(long long j =i + 1; j < N; j++)
{
wall2 += organisms[j];
}
long long weights2 = 0;
for(long long j =i + 1; j < N; j++)
{
weights2 = max(weights2, weights[j]);
}
if(wall1*weights1+ wall2*weights2 < biomass)
{
biomass = wall1*weights1+ wall2*weights2;
}
}
cout << biomass << std::endl;
}
}