結果

問題 No.595 登山
ユーザー vjudge1
提出日時 2025-08-23 13:54:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,187 bytes
コンパイル時間 2,990 ms
コンパイル使用メモリ 275,480 KB
実行使用メモリ 9,916 KB
最終ジャッジ日時 2025-08-23 13:54:44
合計ジャッジ時間 5,101 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 6 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define I using
#define AK namespace
#define IOI std

I AK IOI;

#define LL long long
#define LD long double
#define PLL pair<LL,LL>
#define pb push_back

const int N=2e5+5;
const int M=1e7+5;
const LL mod=998244353;

LL T,n,m,cnt,ans,ansr,idx,root,inf=1e16;
LL A[N],B[N],C[N],D[N];
bool bl[N];
string s,t;
char cr,ch;

LL fd(LL l,LL r){
	//cout<<l<<' '<<r<<' '<<C[l-1]<<' '<<C[r-1]<<' '<<D[l]<<' '<<D[r]<<endl;
	if(l==1)
	return min(C[r-1]-C[l-1],D[l]-D[r]+m);
	else
	return min(C[r-1]-C[l-1],D[l]-D[r]);
}

LL pl(LL l,LL r){
	LL y=fd(l,r);
	LL tk=-1,tp=y;
	for(int k=l;k<r;k++){
		//cout<<l<<' '<<k<<' '<<r<<' '<<fd(l,k)<<' '<<fd(k+1,r)<<' '<<tp<<'\n';
		if(fd(l,k)+fd(k+1,r)+m<tp){
			tk=k;tp=fd(l,k)+fd(k+1,r)+m;
		}
	}
	if(tk!=-1){
		y=pl(l,tk);
    	y+=pl(tk+1,r);
    	y+=m;
	}
	//cout<<l<<' '<<r<<' '<<tk<<' '<<tp<<' '<<y<<'\n';
	return y;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
    	cin>>A[i];
	}
	for(int i=1;i<n;i++){
		B[i]=A[i+1]-A[i];
		C[i]=C[i-1]+max(0ll,B[i]);
	}
	for(int i=n-1;i>0;i--){
		D[i]=D[i+1]+max(0ll,-B[i]);
	}
	ans=0;
	ans=pl(1,n);
	cout<<ans<<endl;
	
	
    return 0;
}
0