B. Light It Up
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 a and 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.

题解:贪心,枚举每个最好位置即可

AC代码为:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+7;
LL n,m,A[N],sum[N],suf[N];
inline int read()
{
    int now=0;register char c=getchar();
    for(;!isdigit(c);c=getchar());
    for(;isdigit(c);now=now*10+c-'0',c=getchar());
    return now;
}


int main()
{
    n=read(),m=read();
    for(int i=1; i<=n; ++i) A[i]=read();
    for(int i=1; i<=n; i+=2) sum[i+1]=sum[i]=sum[i-1]+A[i]-A[i-1];
    if(n&1)
    {
        for(int i=n-1;i>0;i-=2) suf[i-1]=suf[i]=suf[i+1]+A[i+1]-A[i];
    }
    else
    {
        A[n+1]=m;
        for(int i=n;i>0;i-=2) suf[i-1]=suf[i]=suf[i+1]+A[i+1]-A[i];
    }


    LL ans=sum[n];
    if(!(n&1)) ans+=m-A[n];
    if(n&1) A[++n]=m;
    for(LL i=1;i<=n;++i)
    {
        if(A[i]-A[i-1]>1) ans=max(ans,sum[i-1]+A[i]-A[i-1]-1+m-A[i]-suf[i]);    
    }
    printf("%lld",ans);


    return 0;
}



相关文章: