How to write a program in C++ to find the highest sum of subsequence elements of a given sequence? At the input, the user gives the length of the sequence and then the individual elements of this sequence. The elements of the sequence are integers. For example -10 23 4 91 3 18 -67 9 12 5. In this case the highest sum is 139. I would be grateful for any advice or tips.
#include <iostream>
#include <vector>
usingnamespace std;
int bestSubsequenceSum( const vector<int> &A )
{
int sum = 0, lowsum = 0, best = 0;
for ( int e : A )
{
sum += e;
if ( sum < lowsum ) lowsum = sum;
if ( sum - lowsum > best ) best = sum - lowsum;
}
return best;
}
int main()
{
vector<int> A = { -10, 23, 4, 91, 3, 18, -67, 9, 12, 5 };
cout << bestSubsequenceSum( A );
}