#include <stdio.h>
#define N 105
int a[N];
int d[N];
int n;
int left[N];
int max_sum()
{
int maxi = 0;
d[0] = a[0];
left[0] = 0;
for(int i = 1; i < n; ++i)
{
left[i] = (d[i - 1] > 0) ? left[i - 1] : i;
d[i] = (d[i - 1] > 0) ? (d[i - 1] + a[i]) : a[i];
maxi = d[maxi] < d[i] ? i : maxi;
}
return maxi;
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%d", a + i);
int maxi = max_sum();
printf("%d - %d: %d\n", left[maxi] + 1, maxi + 1, d[maxi]);
}
求最大子序列和
最新推荐文章于 2022-12-09 00:10:50 发布