結果

問題 No.3289 Make More Happy Connection
ユーザー elphe
提出日時 2025-10-04 18:14:03
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 1,375 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 278,576 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-10-04 18:14:10
合計ジャッジ時間 5,419 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

[[nodiscard]] static inline constexpr uint_fast64_t prepare([[maybe_unused]] const uint_fast32_t N, const std::vector<std::pair<uint_least32_t, uint_least32_t>>& operations) noexcept
{
	uint_fast64_t sum = 0;
	for (const auto& [x, y] : operations)
		if (x == y) sum += x;

	return sum;
}

[[nodiscard]] static inline constexpr uint_fast64_t solve(const uint_fast32_t N, const std::vector<std::pair<uint_least32_t, uint_least32_t>>& operations) noexcept
{
	std::array<std::array<uint_fast64_t, 2>, 2> dp = { std::array<uint_fast64_t, 2>{ 0, 0 }, std::array<uint_fast64_t, 2>{ 0, 0 } };
	for (uint_fast32_t i = 1; i < N; ++i)
	{
		const auto& [x, y] = operations[i];
		const auto& [prev_x, prev_y] = operations[i - 1];
		dp[(i & 1) ^ 1][0] = std::max<uint_fast64_t>(dp[i & 1][0] + (x == prev_y ? x : 0), dp[i & 1][1] + (x == prev_x ? x : 0));
		dp[(i & 1) ^ 1][1] = std::max<uint_fast64_t>(dp[i & 1][0] + (y == prev_y ? y : 0), dp[i & 1][1] + (y == prev_x ? y : 0));
	}

	return std::max<uint_fast64_t>(dp[N & 1][0], dp[N & 1][1]);
}

int main()
{
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);
	
	uint_fast32_t N;
	std::cin >> N;
	std::vector<std::pair<uint_least32_t, uint_least32_t>> operations(N);
	for (auto& [x, y] : operations)
		std::cin >> x >> y;

	std::cout << prepare(N, operations) + solve(N, operations) << '\n';
	return 0;
}
0