📅  最后修改于: 2022-03-11 14:44:51.947000             🧑  作者: Mango
#include
using namespace std;
int kadane(int arr[], int n)
{
int currentSum = 0;
int maxSum = INT_MIN;
for (int i = 0; i < n; i++)
{
currentSum += arr[i];
if (currentSum < 0)
{
currentSum = 0;
}
maxSum = max(maxSum, currentSum);
}
return maxSum;
}
int main()
{
//Input Array
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int wrapsum, totalsum = 0;
int nonwrapsum;
nonwrapsum = kadane(arr, n);
for (int i = 0; i < n; i++)
{
totalsum += arr[i];
arr[i] = -arr[i];
}
wrapsum = totalsum + kadane(arr, n);
cout << max(wrapsum, nonwrapsum) << endl;
return 0;
}