1000B Codeforces - Light it Up





Solution in c++

#include <iostream>
#include <vector>

using namespace std;

typedef long long int lli;

int n,en,trn=1,pos;

vector<lli> all(100009),penal;

int main()
{
    cin>>n>>en;

    if(n%2==0)trn*=-1;
    penal.resize(n+1);
    for(int i=1;i<=n;i++)
    {
        lli t;cin>>t;
        all[i]=t;
    }
    all[n+1]=en;
    int y=0;

    for(int i=n+1;i>=1;i--)
    {
        penal[y]=trn*(all[i]-all[i-1]);y++;trn*=-1;
        if(trn==1)pos+=-penal[y-1];
    }

    int mx=0,acc=0;
    for(auto x:penal)
    {
        acc+=x;
        mx=max(mx,acc);
    }
    cout<<max(pos,pos-1+mx);
    return 0;
}

Share on Google Plus

About Mohamed Sobhi

My name is Mo.Sobhy ,I study at School excelling high school in Ain Shams ,Cairo
My hobbies is programming ,and web development ,playing chess and writing horror stories.
    Blogger Comment
    Facebook Comment

0 التعليقات:

إرسال تعليق