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 00 and turn power off at moment MM. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp.

The lamp allows only good programs. Good program can be represented as a non-empty array aa, where 0<a1<a2<⋯<a|a|<M0<a1<a2<⋯<a|a|<M. All aiai must be integers. Of course, preinstalled program is a good program.

The lamp follows program aa in next manner: at moment 00 turns power and light on. Then at moment aiai the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment 11 and then do nothing, the total time when the lamp is lit will be 11. Finally, at moment MM 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 aa, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of aa, or even at the begining or at the end of aa.

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 xx till moment yy, then its lit for y−xy−x units of time. Segments of time when the lamp is lit are summed up.

Input

First line contains two space separated integers nn and MM (1≤n≤1051≤n≤105, 2≤M≤1092≤M≤109) — the length of program aaand the moment when power turns off.

Second line contains nn space separated integers a1,a2,…,ana1,a2,…,an (0<a1<a2<⋯<an<M0<a1<a2<⋯<an<M) — initially installed program aa.

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=3x=3 before a1a1, so program will be [3,4,6,7][3,4,6,7]and time of lamp being lit equals (3−0)+(6−4)+(10−7)=8(3−0)+(6−4)+(10−7)=8. Other possible solution is to insert x=5x=5 in appropriate place.

In the second example, there is only one optimal solution: to insert x=2x=2 between a1a1 and a2a2. Program will become [1,2,10][1,2,10], and answer will be (1−0)+(10−2)=9(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(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]后关灯的值

 #include <bits/stdc++.h>
using namespace std;
const int maxn = 4e5 + ;
typedef long long LL;
int a[maxn], n, m, ans[maxn];
int main() {
cin >> n >> m;
a[] = , a[n + ] = m;
for (int i = ; i <= n ; i++) scanf("%d", &a[i]);
int flag = ;
ans[] = ;
for (int i = ; i <= n + ; i++) {
ans[i] = ans[i - ] + flag * (a[i] - a[i - ]);
flag ^= ;
}
int cnt = ans[n + ];
for (int i = ; i <= n + ; i++) {
cnt = max(cnt, ans[i] - + m - a[i] - (ans[n + ] - ans[i]));
}
printf("%d\n", cnt);
return ;
}

最新文章

  1. SYN攻击
  2. 可能是一场很 IN 的技术分享
  3. 【Alpha版本】冲刺阶段——Day 8
  4. 1029. Median (25)
  5. linux下查找某个目录下包含某个字符串的文件
  6. IOCTL函数用法
  7. Android--将Bitmip转化成字符串
  8. bzoj 2751 快速幂
  9. Java-note-字符串连接
  10. android开发之-查看、编辑手机sqlite数据库文件-实测
  11. SmartSql漫谈
  12. 洗礼灵魂,修炼python(7)--元组,集合,不可变集合
  13. Android 开发笔记___FrameLayout
  14. BZOJ1008: [HNOI2008]越狱-快速幂+取模
  15. 解决centos7命令行中文乱码
  16. js实现截取或查找字符串中的子字符串
  17. PHP之fopen wrappers模块
  18. Got fatal error 1236原因和解决方法
  19. 动态规划-最大的正方形面积 Maximal Square
  20. AdaBoost, LogitBoost and GradientBoosting

热门文章

  1. Scrapy框架的初步使用
  2. Python3 函数return
  3. SIMD数据并行(三)——图形处理单元(GPU)
  4. WRITE
  5. python2.7入门---Number(数字)
  6. ORB-SLAM 代码笔记(四)tracking代码结构
  7. 白话HMM系列1——从一个缩略语还原的例子说起
  8. cf#513 B. Maximum Sum of Digits
  9. Python 3基础教程22-单个列表操作
  10. [POJ3585]Accumulation Degree