結果

問題 No.2100 [Cherry Alpha C] Two-way Steps
ユーザー NAMIDAIRO
提出日時 2022-10-15 18:13:28
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 1,183 bytes
コンパイル時間 1,020 ms
コンパイル使用メモリ 111,700 KB
実行使用メモリ 18,216 KB
最終ジャッジ日時 2024-06-26 20:10:30
合計ジャッジ時間 7,626 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

void chmax(ll & x, ll y) { x = max(x, y); }

ll solve(int n, vi<ll> &h, vii<int> &to) {
	const ll inf = 1e18;
	vii<ll> dp(2, vi<ll>(n, -inf));
	dp[0][0] = 0;
	rep(v, n) {
		for (int nv : to[v]) {
			if (h[v] < h[nv]) chmax(dp[1][nv], dp[0][v] + h[nv] - h[v]);
			else {
				chmax(dp[0][nv], dp[1][v]);
				chmax(dp[0][nv], dp[0][v]);
			}
		}
	}
	return max(dp[0][n - 1], dp[1][n - 1]);
}

int main()
{
	int n, m;
	cin >> n >> m;
	
	vi<ll> hc(n), hz(n);
	rep(i, n) cin >> hc[i];
	rep(i, n) hz[i] = hc[n - 1 - i];

	vii<int> toc(n), toz(n);
	rep(i, m) {
		int x, y;
		cin >> x >> y;
		x--, y--;
		toc[x].push_back(y);
		toz[n - 1 - y].push_back(n - 1 - x);
	}
	ll resc = solve(n, hc, toc);
	ll resz = solve(n, hz, toz);
	cout << (resc < 0 ? -1 : resc) << endl;
	cout << (resz < 0 ? -1 : resz) << endl;

	return 0;
}
0