View Code of Problem 4066

#include <iostream>
#include <string>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;
//int dp[10000000];甚至不用dp。。可以节省很大的空间
//int a[10000000];再用vector试试
int main()
{
	long long n;
	cin >> n;
	vector<int>a(n);
	for (long long i = 0;i < n;i++)
	{
		cin >> a[i];
	}
	int pre;
	if (a[0] > 0)
		pre = a[0];
	else
		pre = 0;
	int max = -1;
	int num;
	for (long long i = 1;i < n;i++)
	{
		//cin >> a[i];
		if (a[i] +pre > 0)
			num = a[i] + pre;
		else if (a[i] + pre < 0)
			num = 0;
		if (num > max)
			max = num;
		pre = num;
	}
	cout << max;
}

Double click to view unformatted code.


Back to problem 4066