結果

問題 No.595 登山
ユーザー vjudge1
提出日時 2025-08-23 12:49:48
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 546 bytes
コンパイル時間 5,206 ms
コンパイル使用メモリ 210,032 KB
実行使用メモリ 8,140 KB
最終ジャッジ日時 2025-08-23 12:49:56
合計ジャッジ時間 7,368 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 4 WA * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std; 
const int MAXN=200010;
long long h[MAXN],r[MAXN],l[MAXN]; 
int main(){
	int n;long long p;
	cin>>n>>p;
	for(int i=0;i<n;++i)cin>>h[i];
	for(int i=1;i<n;++i)r[i]=r[i-1]+max(0LL,h[i]-h[i-1]);
	for(int i=n-2;i>=0;--i)l[i]=l[i+1]+max(0LL,h[i]-h[i+1]);
	long long c1=r[n-1]; 
	long long c2=(n-1)*p; 
	long long sr_end=r[n-1],mv=LLONG_MAX;
	for(int x=0;x<n-1;++x){
		long long cost=min(l[x+1], sr_end - r[x+1]);
		mv=min(mv, r[x] + cost);
	}
	long long c3=mv+p;
	cout<<min({c1,c2,c3})<<endl;
	return 0;
}
0