题目描述
给出一段序列,选出其中连续且非空的一段使得这段和最大。
输入输出格式
输入格式:输入文件maxsum1.in的第一行是一个正整数N,表示了序列的长度。
第2行包含N个绝对值不大于10000的整数A[i],描述了这段序列。
输出格式:输入文件maxsum1.out仅包括1个整数,为最大的子段和是多少。子段的最小长度为1。
输入输出样例
说明
【样例说明】2 -4 3 -1 2 -4 3
【数据规模与约定】
对于40%的数据,有N ≤ 2000。
对于100%的数据,有N ≤ 200000。
思路:dp
#include<iostream>
using namespace std;
int main()
{
//freopen("1.txt", "r", stdin);
int n;
cin >> n;
int tms = 0; int ms = -1000000;
int x;
for (int i = 0; i < n; i++)
{
cin >> x;
if (tms>0)tms += x;
else tms = x;
if (tms > ms) ms = tms;
}
cout << ms;
return 0;
}