#include <iostream> using namespace std; int max(int x, int y) { return x > y ? x : y; } int MaxSum(int* a, int n) { int i, res; int* start = new int[n]; int* all = new int[n]; start[n - 1] = a[n - 1]; all[n - 1] = a[n - 1]; for(i = n - 2; i >= 0; i--) { start[i] = max(a[i], a[i] + start[i + 1]); all[i] = max(start[i], all[i + 1]); } res = all[0]; delete[] start; delete[] all; return res; } int MyMaxSum(int* a, int n) { int i; int max, sum; sum = 0; max = 1 << 31; for(i = 0; i < n; i++) { if(sum > 0) sum += a[i]; else sum = a[i]; if(sum > max) max = sum; } return max; } int main() { int arr[] = {-2, 2, 4, 3, 2, 1, -3, -2, -1, 1, 2, 3, 0, -2}; cout << MaxSum(arr, sizeof(arr) / sizeof(int)) << endl; cout << MyMaxSum(arr, sizeof(arr) / sizeof(int)) << endl; return 0; }