intMaxSubSum(int A[], int left, int right){ int MaxLeftSum, MaxRightSum; int MaxLeftBorderSum, MaxRightBorderSum; int LeftBorderSum, RightBoarderSum; int Center, i;
if (left == right) { if (A[left] > 0) { return A[left]; } else { return0; } } Center = (left + right) / 2; MaxLeftSum = MaxSubSum(A, left, Center); MaxRightSum = MaxSubSum(A, Center + 1, right);
MaxLeftBorderSum = LeftBorderSum = 0; for (i = Center; i >= left; i--) { LeftBorderSum += A[i]; if (LeftBorderSum > MaxLeftBorderSum) { MaxLeftBorderSum = LeftBorderSum; } } MaxRightBorderSum = RightBoarderSum = 0; for (i = Center + 1; i <= right; i++) { RightBoarderSum += A[i]; if (RightBoarderSum > MaxRightBorderSum) { MaxRightBorderSum = RightBoarderSum; } } return Max3(MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum); }