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 ;
}

B. Light It Up 思维题的更多相关文章

  1. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  2. cf A. Inna and Pink Pony(思维题)

    题目:http://codeforces.com/contest/374/problem/A 题意:求到达边界的最小步数.. 刚开始以为是 bfs,不过数据10^6太大了,肯定不是... 一个思维题, ...

  3. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  4. 洛谷P4643 [国家集训队]阿狸和桃子的游戏(思维题+贪心)

    思维题,好题 把每条边的边权平分到这条边的两个顶点上,之后就是个sb贪心了 正确性证明: 如果一条边的两个顶点被一个人选了,一整条边的贡献就凑齐了 如果分别被两个人选了,一作差就抵消了,相当于谁都没有 ...

  5. C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

    C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. PJ考试可能会用到的数学思维题选讲-自学教程-自学笔记

    PJ考试可能会用到的数学思维题选讲 by Pleiades_Antares 是学弟学妹的讲义--然后一部分题目是我弄的一部分来源于洛谷用户@ 普及组的一些数学思维题,所以可能有点菜咯别怪我 OI中的数 ...

  7. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

  8. HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)

    HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...

  9. cf796c 树形,思维题

    一开始以为是个树形dp,特地去学了..结果是个思维题 /* 树结构,设最大点权值为Max,则答案必在在区间[Max,Max+2] 证明ans <= Max+2 任取一个点作为根节点,那么去掉这个 ...

随机推荐

  1. Java学习笔记二:Java开发工具Eclipse的安装与使用

    Java开发工具Eclipse的安装与使用 正如office一样我们在开发java语言过程中同样需要依款不错的开发工具,目前市场上的IDE很多,这里只演示Eclipse的安装: 一:下载软件: 1.下 ...

  2. Leecode刷题之旅-C语言/python-136只出现一次的数字

    /* * @lc app=leetcode.cn id=136 lang=c * * [136] 只出现一次的数字 * * https://leetcode-cn.com/problems/singl ...

  3. 渗透测试实验(i春秋 真的很简单)

    首先利用给的提示: 所以用户名是 ichunqiu 密码是adab29e084ff095ce3eb 可以确定一般密码都是md5的,但是这个20位 应该去掉ada b29e084ff095ce3e才是正 ...

  4. QOS-基本拥塞管理机制(PQ CQ WFQ RTPQ)

    QOS-基本拥塞管理机制(PQ CQ WFQ RTPQ) 2018年7月7日    20:29 拥塞:是指当前供给资源相对于正常转发处理需要资源的不足,从而导致服务质量下降的一种现象 拥塞管理概述: ...

  5. android 界面切换教程

    Android的界面切换其实并没有什么难度,就和java的界面切换一样简单,但是java的使用的是Swing框架进行的切换,而Android使用的是Intent 来切换框架的,这是一个全新的使用方法, ...

  6. 39-Role以及Claims授权

    asp.net core多鼓励使用claims授权 1-使用role授权 在类或方法上贴上Roles,这样就知道有user的角色才可以访问 [Authorize(Roles="user&qu ...

  7. java字符流实现文件间的内容复制

    package com.io.demo1; import java.io.FileReader; import java.io.FileWriter; public class TestFileSTr ...

  8. Bash中使用MySQL导入导出CSV格式数据[转]

    转自: http://codingstandards.iteye.com/blog/604541 MySQL中导出CSV格式数据的SQL语句样本如下:   select * from test_inf ...

  9. javascript的优美与鸡肋

    --总结来自:<javascript语言精粹> 任何语言都有其优美的地方和其鸡肋的地方.避归一些语言的糟粕,能相应的降低bug出现的几率. 优美处: 函数是头等对象 基于原型继承的动态对象 ...

  10. ABP框架设置默认语言

    在Global.asax文件中设置 private readonly IAbpWebLocalizationConfiguration _webLocalizationConfiguration; p ...