Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment 

The lamp allows only good programs. Good program can be represented as a non-empty array ai must be integers. Of course, preinstalled program is a good program.

The lamp follows program M the lamp is turning its power off regardless of its state.

Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program a.

Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from y−x units of time. Segments of time when the lamp is lit are summed up.

Input

First line contains two space separated integers aand the moment when power turns off.

Second line contains a.

Output

Print the only integer — maximum possible total time when the lamp is lit.

Examples
input
Copy
3 10
4 6 7
output
Copy
8
input
Copy
2 12
1 10
output
Copy
9
input
Copy
2 7
3 4
output
Copy
6
Note

In the first example, one of possible optimal solutions is to insert value x=5 in appropriate place.

In the second example, there is only one optimal solution: to insert (1−0)+(10−2)=9.

In the third example, optimal answer is to leave program untouched, so answer will be (3−0)+(7−4)=6.

 

 

 

这题主要运用了一个思想 就是加入一个点就相当于把一个点拆成了两个 ,

根据题目的要求贪心 ,将a[i] 拆成两个点的时候 就是 拆成a[i]-1 和 a[i];这个我们就可以获得最优解 ,

ans[i]记录的题目要求值得前缀和

因为改变了a[i] 所以就需要a[i]后的关灯的值 

  m - a[i] - (ans[n + 1] - ans[i]) 这个为a[i]后关灯的值

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 4e5 + 10;
 4 typedef long long LL;
 5 int a[maxn], n, m, ans[maxn];
 6 int main() {
 7     cin >> n >> m;
 8     a[0] = 0, a[n + 1] = m;
 9     for (int i = 1 ; i <= n ; i++) scanf("%d", &a[i]);
10     int flag = 1;
11     ans[0] = 0;
12     for (int i = 1 ; i <= n + 1 ; i++) {
13         ans[i] = ans[i - 1] + flag * (a[i] - a[i - 1]);
14         flag ^= 1;
15     }
16     int cnt = ans[n + 1];
17     for (int i = 1 ; i <= n + 1 ; i++) {
18         cnt = max(cnt, ans[i] - 1 + m - a[i] - (ans[n + 1] - ans[i]));
19     }
20     printf("%d\n", cnt);
21     return 0;
22 }

 

相关文章:

  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
  • 2021-08-05
  • 2021-06-05
  • 2021-10-22
猜你喜欢
  • 2022-12-23
  • 2018-06-28
  • 2021-10-16
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2021-12-11
相关资源
相似解决方案