int A[] = { 2, -3, 1, 2, -2, 4, -2, -3, -6, 4, -5, 3, -2, 3, -1, -1, 1, 3, -6, 2, -4, 2, -1, 2 }; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ----- If I already have a sequence that ends at position 23, what it the maximum possible benefit (increase to the sum) I could get by continuing further with it? 0 because that's the end of the array, it doesn't go any further. ----- If I already have a sequence that ends at position 22, what it the maximum possible benefit (increase to the sum) I could get by continuing further with it? 2 at the next position [23] there is a positive number, 2, so of course I would benefit by adding that, and I've already worked out there's no point in going further than that. ----- If I already have a sequence that ends at position 21, what it the maximum possible benefit (increase to the sum) I could get by continuing further with it? 1 at the next position [22] there is a negative number, -1, but I already know that after that I can get a benefit of +2, so there is an overall benefit of +1 from going further. ----- If I already have a sequence that ends at position 20, what it the maximum possible benefit (increase to the sum) I could get by continuing further with it? 3 at the next position [21] there is a positive number, 2, so that is always worth taking, and even though the next number is negative, I already know that there is still an aditional benefit of +1 by going beyond. ----- If I already have a sequence that ends at position 19, what it the maximum possible benefit (increase to the sum) I could get by continuing further with it? -1 at the next position [20] there is a negative number, -4, and I already know that I can only get an additional benefit of +3 going further than that, so it will never be worth it. and so on. N = 24 int benefit[24]; benefit[i] will be the maximum benefit I can get by continuing an existing sequence that ends at [i-1] through [i] and maybe further. But if going further makes things worse, I'll just say the possible benefit is zero. void solve_mscs(int A[], int N) { benefit[N] = 0; for (int i=N-1; i>=0; i-=1) { int best_increase = A[i] + benefit[i+1]; if (best_increase > 0) benefit[i] = best_increase; else benefit[i] = 0; } Now find the best starting place: int start = 0; for (int i=0; i benefit[start]) start = i; Now find where to stop int end = start; for (int i=start; i=0; i-=1) { int best_increase = A[i] + benefit[i+1]; if (best_increase > 0) benefit[i] = best_increase; else benefit[i] = 0; if (benefit[i] == 0) last_zero = i; if (benefit[i] >= benefit[start]) { start = i; end = last_zero-1; } } ...