View Code of Problem 4066

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int arr[10000005];
int dp[10000005];
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
		dp[i] = arr[i];
	}
	int res = -1e9;
	for (int i = 0; i < n; i++) {
		if (i == 0) {
			dp[i] = arr[i];
		}
		else {
			dp[i] = max(dp[i - 1] + arr[i], dp[i]);
		}
		res = max(res, dp[i]);
	}
	cout << res << endl;
	
	return 0;
}

Double click to view unformatted code.


Back to problem 4066