-
Notifications
You must be signed in to change notification settings - Fork 103
/
boi7seq.cpp
39 lines (39 loc) · 906 Bytes
/
boi7seq.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
// 2016-04-28
#include <cstdio>
#include <vector>
using namespace std;
int input() {
int res = 0; char c;
do {
c = getchar_unlocked();
} while (c <= 32);
do {
res = res*10 + c - '0';
c = getchar_unlocked();
} while (c > 32);
return res;
}
int main() {
int n = input();
long long total = 0;
vector<int> stk;
stk.push_back(input());
for (int i = 1; i < n; i++) {
int x = input();
if (x >= stk.back()) {
while (stk.size() >= 1 && x >= stk.back()) {
stk.pop_back();
if (stk.empty() || x < stk.back()) {
total += x;
} else {
total += stk.back();
}
}
}
stk.push_back(x);
}
for (int i = 0; i+1 < stk.size(); i++) {
total += stk[i];
}
printf("%lld\n", total);
}