-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy path12.2.cpp
43 lines (37 loc) · 919 Bytes
/
12.2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
* 题目名称:最大序列和
* 题目来源:清华大学复试上机题
* 题目链接:http://t.cn/AiYSlQMU
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <climits>
using namespace std;
const int maxn = 1e6 + 10;
const int INF = INT_MAX;
long long arr[maxn];
long long dp[maxn];
long long MaxSubsequence(int n) {
long long maximum = -INF;
for (int i = 0; i < n; ++i) {
if (i == 0) {
dp[i] = arr[i];
} else {
dp[i] = max(arr[i], dp[i - 1] + arr[i]);
}
maximum = max(maximum, dp[i]);
}
return maximum;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) {
scanf("%lld", &arr[i]);
}
long long answer = MaxSubsequence(n);
printf("%lld\n", answer);
}
return 0;
}