305263: CF1000B. Light It Up

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Light It Up

题意翻译

**题目大意:** 有一台灯,这个灯在时间为$0$时打开,$m$时关闭,在$0$到$m$这段时间内有$n$个时间点灯的状态会改变(即开变关,关变开),现在可以在**剩余**的时间点选一个让灯的状态改变一次,求这个灯最大亮着的时间 **输入格式:** 第一行两个整数,$n,m$,$n<=10^5,m<=10^9$ 以下一行有$n$个整数,即改变时间点 **输出格式:** 一个整数,即最大亮着的时间 感谢@守望 提供翻译

题目描述

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 $ 0 $ and turn power off at moment $ M $ . 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 $ a $ , where $ 0 < a_1 < a_2 < \dots < a_{|a|} < M $ . All $ a_i $ must be integers. Of course, preinstalled program is a good program. The lamp follows program $ a $ in next manner: at moment $ 0 $ turns power and light on. Then at moment $ a_i $ 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 $ 1 $ and then do nothing, the total time when the lamp is lit will be $ 1 $ . Finally, at moment $ 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 $ , so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of $ a $ , or even at the begining or at the end of $ 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 $ x $ till moment $ y $ , then its lit for $ y - x $ units of time. Segments of time when the lamp is lit are summed up.

输入输出格式

输入格式


First line contains two space separated integers $ n $ and $ M $ ( $ 1 \le n \le 10^5 $ , $ 2 \le M \le 10^9 $ ) — the length of program $ a $ and the moment when power turns off. Second line contains $ n $ space separated integers $ a_1, a_2, \dots, a_n $ ( $ 0 < a_1 < a_2 < \dots < a_n < M $ ) — initially installed program $ a $ .

输出格式


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

输入输出样例

输入样例 #1

3 10
4 6 7

输出样例 #1

8

输入样例 #2

2 12
1 10

输出样例 #2

9

输入样例 #3

2 7
3 4

输出样例 #3

6

说明

In the first example, one of possible optimal solutions is to insert value $ x = 3 $ before $ a_1 $ , so program will be $ [3, 4, 6, 7] $ and time of lamp being lit equals $ (3 - 0) + (6 - 4) + (10 - 7) = 8 $ . Other possible solution is to insert $ x = 5 $ in appropriate place. In the second example, there is only one optimal solution: to insert $ x = 2 $ between $ a_1 $ and $ a_2 $ . Program will become $ [1, 2, 10] $ , and answer will be $ (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 $ .

Input

题意翻译

**题目大意:** 有一台灯,这个灯在时间为$0$时打开,$m$时关闭,在$0$到$m$这段时间内有$n$个时间点灯的状态会改变(即开变关,关变开),现在可以在**剩余**的时间点选一个让灯的状态改变一次,求这个灯最大亮着的时间 **输入格式:** 第一行两个整数,$n,m$,$n<=10^5,m<=10^9$ 以下一行有$n$个整数,即改变时间点 **输出格式:** 一个整数,即最大亮着的时间 感谢@守望 提供翻译

加入题单

上一题 下一题 算法标签: