Maximum Subarray

 avatar
user_3763047219
c_cpp
2 years ago
456 B
2
Indexable
Never
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
int main()
{
	int N = 0, A[101] = {0};
	scanf("%d", &N);
	for (int i = 1; i <= N; i++) {
		scanf("%d", &A[i]);
	}
	int maxp = A[1];
	int p2 = A[1];
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N-i+1; j++) {
			int p = 0;
			for (int k = 0; k < i; k++) {
				p = p + A[j+k];
			}
			if (p > p2) {
				p2 = p;
			}
		}
		if(p2>maxp)
		maxp = p2;
	}
	printf("%d", maxp);
}