You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
658 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
#define maxn 1000
using namespace std;
int dp[maxn];
int main() {
int n;
scanf("%d", &n);
int a[maxn];
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
dp[0] = a[0];//边界 (结果总是确定的,动态规划总是从这些边界出发)
for (int i = 0; i < n; i++) {
dp[i] = max(a[i], dp[i - 1] + a[i]);//递推式
}
//dp[i]存放以a[i]结尾的连续序列的最大和需要遍历i得到最大的才是结果
int k = 0;
for (int i = 0; i < n; i++) {
if (dp[i] > dp[k]) {
k = i;
}
}
printf("%d\n", dp[k]);
return 0;
}