-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathB.cpp
57 lines (45 loc) · 1.28 KB
/
B.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll ara[100009], cum[100009], cum2[100009];
int main()
{
ll n, m;
cin >> n >> m;
for(ll i = 1; i <= n; i++)
scanf("%lld", &ara[i]);
ara[n + 1] = m;
bool state = 1;
for(ll i = 1; i <= n + 1; i++) {
if(state) {
cum[i] = cum[i - 1] + ara[i] - ara[i - 1];
cum2[i] = cum2[i - 1];
}
else {
cum2[i] = cum2[i - 1] + ara[i] - ara[i - 1];
cum[i] = cum[i - 1];
}
state ^= 1;
}
ll ans = cum[n + 1];
//cout << cum2[n + 1] << endl;
ll ans2 = 0;
for(ll i = 1; i <= n + 1; i++) {
if(ara[i] - ara[i - 1] == 1)
continue;
if(i % 2 == 1) {
ll tmp = ara[i] - 1;
ans2 = cum[i - 1] + (tmp - ara[i - 1]) + cum2[n + 1] - cum2[i];
//cout << ans << endl;
ans = max(ans, ans2);
}
else {
ll tmp = ara[i - 1] + 1;
ans2 = cum[i - 1] + (ara[i] - tmp) + cum2[n + 1] - cum2[i];
//cout << ans2 << " " << i << " " << cum[i - 1] << " " << ara[i] - tmp << " " << cum2[n + 1] << " " << cum2[i - 1] << endl;
ans = max(ans, ans2);
}
}
cout << ans << endl;
return 0;
}