You’re given n boxes of chocolates arranged sequentially. Each box contains ai chocolates. You have to pick the maximum number of chocolates from the boxes and you must follow the given rule: ● You cannot pick chocolates from two consecutive boxes Let me explain the rule. If you pick chocolates from the ith box, you are not allowed to pick chocolates from the i+1th box or i-1 th box. The first line of the input represents n (2<=n<=1000), the number of boxes, and the following line represents the number of chocolate of those n boxes. Print the maximum number of chocolates you can collect from the boxes with satisfying the condition.
Sample Input :
8
1 2 3 4 5 6 7 8
Sample Output: 20
Sample Input 3:
5
2 50 100 75 1
Sample Output:
125
#include <iostream>
using namespace std;
int main()
{
int N;
cout<<"Please, enter a number of boxes: ";
cin>>N;
if(N<2||N>1000)
{
cout<<"Incorrect number!";
return 0;
}
int* boxes=new int[N];
cout<<"Please, enter a number of chocolates in every box: ";
for(int i=0;i<N;i++)
{
cin>>boxes[i];
}
double summEven=0;
for(int i=0;i<N;i+=2)
{
summEven+=boxes[i];
}
double summOdd=0;
for(int i=1;i<N;i+=2)
{
summOdd+=boxes[i];
}
if(summEven>=summOdd)
cout<<"The maximum number of chocolates is "<<summEven;
else
cout<<"The maximum number of chocolates is "<<summOdd;
delete[] boxes;
}
Comments
Leave a comment