結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー 沙耶花沙耶花
提出日時 2022-10-14 21:40:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 1,232 bytes
コンパイル時間 4,497 ms
コンパイル使用メモリ 263,440 KB
実行使用メモリ 17,360 KB
最終ジャッジ日時 2023-09-08 20:32:44
合計ジャッジ時間 11,646 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 126 ms
12,936 KB
testcase_05 AC 101 ms
10,324 KB
testcase_06 AC 20 ms
5,212 KB
testcase_07 AC 25 ms
5,732 KB
testcase_08 AC 132 ms
15,320 KB
testcase_09 AC 98 ms
9,224 KB
testcase_10 AC 52 ms
5,636 KB
testcase_11 AC 97 ms
11,024 KB
testcase_12 AC 69 ms
7,628 KB
testcase_13 AC 82 ms
12,404 KB
testcase_14 AC 60 ms
6,236 KB
testcase_15 AC 87 ms
11,848 KB
testcase_16 AC 65 ms
6,604 KB
testcase_17 AC 58 ms
10,852 KB
testcase_18 AC 85 ms
12,284 KB
testcase_19 AC 148 ms
15,444 KB
testcase_20 AC 34 ms
6,040 KB
testcase_21 AC 68 ms
8,040 KB
testcase_22 AC 64 ms
6,512 KB
testcase_23 AC 118 ms
13,364 KB
testcase_24 AC 165 ms
17,000 KB
testcase_25 AC 170 ms
17,180 KB
testcase_26 AC 164 ms
17,144 KB
testcase_27 AC 165 ms
17,112 KB
testcase_28 AC 167 ms
17,248 KB
testcase_29 AC 165 ms
17,152 KB
testcase_30 AC 164 ms
17,164 KB
testcase_31 AC 162 ms
17,136 KB
testcase_32 AC 165 ms
17,084 KB
testcase_33 AC 163 ms
17,124 KB
testcase_34 AC 87 ms
7,524 KB
testcase_35 AC 79 ms
6,728 KB
testcase_36 AC 78 ms
6,692 KB
testcase_37 AC 72 ms
6,596 KB
testcase_38 AC 84 ms
7,044 KB
testcase_39 AC 64 ms
6,776 KB
testcase_40 AC 64 ms
6,464 KB
testcase_41 AC 65 ms
6,452 KB
testcase_42 AC 65 ms
6,552 KB
testcase_43 AC 65 ms
6,432 KB
testcase_44 AC 116 ms
17,112 KB
testcase_45 AC 117 ms
17,164 KB
testcase_46 AC 115 ms
17,360 KB
testcase_47 AC 113 ms
17,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 2000000000000000001

void solve(vector<long long> h,vector<int> x,vector<int> y){
	int n = h.size();
	vector<vector<int>> E(n);
	rep(i,x.size()){
		E[min(x[i],y[i])].push_back(max(x[i],y[i]));
	}
	
	vector dp(n,vector<long long>(2,-Inf64));
	dp[0][0] = 0;
	dp[0][1] = 0;
	rep(i,n-1){
		rep(j,2){
			if(dp[i][j]==-Inf64)continue;
			rep(k,E[i].size()){
				int to = E[i][k];
				int F;
				if(h[i] < h[to])F = 1;
				else F = 0;
				if(F==j&&j==1)continue;
				long long C = 0;
				if(F==1)C = h[to] - h[i];
				dp[to][F] = max(dp[to][F],dp[i][j]+C);
			}
		}
	}
	/*
	rep(i,n){
		cout<<dp[i][0]<<','<<dp[i][1]<<endl;
	}*/
	long long ans = max(dp[n-1][0],dp[n-1][1]);
	if(ans==-Inf64)ans = -1;
	cout<<ans<<endl;
}

int main(){
	
	int n,m;
	cin>>n>>m;
	
	vector<long long> h(n);
	rep(i,n){
		cin>>h[i];
	}
	
	vector<int> x(m),y(m);
	rep(i,m){
		cin>>x[i]>>y[i];
		x[i]--,y[i]--;
	}
	solve(h,x,y);
	rep(i,m){
		x[i] = n-1-x[i];
		y[i] = n-1-y[i];
	}
	reverse(h.begin(),h.end());
	solve(h,x,y);
	return 0;
}
0