-
Notifications
You must be signed in to change notification settings - Fork 0
/
P2422.cpp
100 lines (94 loc) · 1.67 KB
/
P2422.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>
#define INF 999999999
using namespace std;
inline long long read()
{
long long x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
void write(const long long &x)
{
if (!x)
{
putchar('0');
return;
}
char f[100];
long long tmp = x;
if (tmp < 0)
{
tmp = -tmp;
putchar('-');
}
long long s = 0;
while (tmp > 0)
{
f[s++] = tmp % 10 + '0';
tmp /= 10;
}
while (s > 0)
{
putchar(f[--s]);
}
}
#ifdef THODEBUG
const long long maxN = 90;
#else
const long long maxN = 100090;
#endif
long long totN;
long long nums[maxN];
struct Node
{
long long ID;
long long num;
};
stack<Node> S;
long long sum[maxN];
long long DP[maxN];
long long totANS;
int main()
{
totN = read();
for (int i = 1; i <= totN; ++i)
{
nums[i] = read();
sum[i] = nums[i] + sum[i - 1];
}
for (int i = 1; i <= totN; ++i)
{
while (!S.empty() and S.top().num > nums[i])
{
DP[S.top().ID] += (sum[i - 1] - sum[S.top().ID]);
S.pop();
}
if (!S.empty())
{
DP[i] = sum[i] - sum[S.top().ID];
}
else
{
DP[i] = sum[i];
}
S.push({i, nums[i]});
}
for (int i = 1; i <= totN; ++i)
{
totANS = max(totANS, DP[i] * nums[i]);
}
write(totANS);
return 0;
} //Thomitics Code