最大連續子序列和

#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1000;
int a[maxn], dp[maxn];
int main()
{
	int n;  
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	//邊界
	dp[0] = a[0];
	for (int i = 1; i < n; i++)
		dp[i] = max(a[i], dp[i - 1] + a[i]);
	//dp數組中最大的就是答案
	int res = dp[0];
	for (int i = 1; i < n; i++)
		res = max(res, dp[i]);
	cout << res << endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章