-
Notifications
You must be signed in to change notification settings - Fork 0
/
1007.cpp
47 lines (38 loc) · 1.06 KB
/
1007.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
44
45
46
47
#include <cstdio>
#include <algorithm>
const int MAXN = 10000 + 10;
int a[MAXN], f[MAXN], l[MAXN];
int main() {
int n;
scanf("%d", &n);
bool allNeg = true;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] >= 0) allNeg = false;
}
if (a[0] > 0) f[0] = a[0];
else f[0] = 0;
for (int i = 1; i < n; i++) {
if (f[i - 1] + a[i] > a[i]) {
f[i] = f[i - 1] + a[i];
l[i] = l[i - 1];
} else {
f[i] = a[i];
l[i] = i;
}
}
int ans = f[0], maxId = 0;
for (int i = 0; i < n; i++) {
if (f[i] > ans) {
ans = f[i];
maxId = i;
}
}
if (maxId == 0 && n != 1) {
if (allNeg) printf("%d %d %d\n", ans, a[0], a[n - 1]);
else printf("0 0 0");
} else printf("%d %d %d\n", ans, a[l[maxId]], a[maxId]);
// if (maxId == 0) printf("%d %d %d\n", ans, a[0], a[n - 1]);
// else printf("%d %d %d\n", ans, a[l[maxId]], a[maxId]);
return 0;
}