732 Codeforces - Cormen --- The best friend Of a Man


732B Codeforces - Cormen --- The best friend of a man






Solution in C++


#include <bits/stdc++.h>

using namespace std;

#define pii pair<int,int>
typedef long long int lli;

lli n,m,t,sol;
map<lli,lli> all,occ;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    int nn[n];
    for(int i=0;i<n;i++)cin>>nn[i];
    for(int i=1;i<n;i++)
    {
        int t=nn[i];
        if(nn[i]+nn[i-1]<m)
            nn[i]=m-abs(nn[i]-nn[i-1]-nn[i]),sol+=abs(nn[i]-t);
    }
    cout<<sol<<"\n";
    for(int i=0;i<n;i++)cout<<nn[i]<<" ";
    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 التعليقات:

إرسال تعليق