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
|
void solve() {
int n;
ll h;
cin >> n >> h;
vl pre(n);
vl suf(n);
vl a(n);
rep(i, 0, n - 1) cin >> a[i];
rep(i, 0, n - 1) a[i] = h - a[i];
ll maxx = LLONG_MIN;
rep(i, 0, n - 1) {
ll tot = 0;
ll tem = LLONG_MAX;
frep(j, i, 0) {
tem = min(tem, a[j]);
tot += tem;
}
pre[i] = tot;
}
frep(i, n - 1, 0) {
ll tot = 0;
ll tem = LLONG_MAX;
rep(j, i, n - 1) {
tem = min(tem, a[j]);
tot += tem;
}
suf[i] = tot;
}
vl cnt(n);
rep(i, 0, n - 1) {
cnt[i] = pre[i] + suf[i] - a[i];
maxx = max(maxx, cnt[i]);
}
rep(i, 0, n - 1) {
int idx = i;
rep(j, i + 1, n - 1) {
if (a[j] < a[idx]) idx = j;
maxx = max(maxx, cnt[i] + cnt[j] - cnt[idx]);
}
}
cout << maxx << endl;
return;
}
|