結果

問題 No.1247 ブロック登り
ユーザー 👑 jupirojupiro
提出日時 2020-10-19 19:54:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,147 ms / 3,000 ms
コード長 2,800 bytes
コンパイル時間 1,473 ms
コンパイル使用メモリ 141,688 KB
実行使用メモリ 331,604 KB
最終ジャッジ日時 2023-09-28 13:24:06
合計ジャッジ時間 15,518 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 5 ms
5,340 KB
testcase_08 AC 6 ms
5,728 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 992 ms
331,360 KB
testcase_12 AC 965 ms
316,344 KB
testcase_13 AC 1,009 ms
331,604 KB
testcase_14 AC 1,061 ms
331,380 KB
testcase_15 AC 1,053 ms
331,372 KB
testcase_16 AC 983 ms
331,604 KB
testcase_17 AC 1,125 ms
331,520 KB
testcase_18 AC 1,147 ms
301,000 KB
testcase_19 AC 92 ms
53,464 KB
testcase_20 AC 33 ms
23,064 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 15 ms
9,876 KB
testcase_23 AC 12 ms
8,020 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 988 ms
331,520 KB
testcase_26 AC 1,090 ms
331,384 KB
testcase_27 AC 1,093 ms
331,428 KB
testcase_28 AC 981 ms
331,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
//typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

//constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr int MAX = 1100000;

using ll = int;
const ll INF = 1 << 30;
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	ll n, K; cin >> n >> K;
	vector<ll> a(n); for (int i = 0; i < n; i++) cin >> a[i];

	if (K == 1) {
		for (int i = 0; i < n; i++) {
			ll ans = -INF;
			if (i + 1 < n) chmax(ans, a[i + 1]);
			if (i > 0) chmax(ans, a[i - 1]);
			cout << ans << "\n";
		}
		return 0;
	}
	vector dp(2, vector(K + 1, vector(n, vector<ll>(n, -INF))));
	for (int i = 0; i < n; i++) {
		dp[0][K - 1][i][i] = dp[1][K - 1][i][i] = a[i] * K;
	}
	vector<ll> res(n, -INF);
	for (int i = K - 1; i > 0; i--) {
		for (int l = 0; l < n; l++) {
			for (int r = l; r < n; r++) {

				if (l > 0) {
					chmax(dp[0][i - 1][l - 1][r], dp[0][i][l][r] + a[l - 1] * i);
					if (i == 1) {
						chmax(res[l - 1], dp[0][i][l][r] + a[l - 1] * i);
					}
				}
				if (r + 1 < n) {
					chmax(dp[1][i - 1][l][r + 1], dp[1][i][l][r] + a[r + 1] * i);
					if (i == 1) {
						chmax(res[r + 1], dp[1][i][l][r] + a[r + 1] * i);
					}
				}
				if (l != r) {
					{
						int nxt = max(0, i - 2);
						for (int j = 0; j < 2; j++) chmax(dp[j][nxt][l][r], dp[j][i][l][r]);
						if (i == 2) {
							chmax(res[l], dp[0][i][l][r]);
							chmax(res[r], dp[1][i][l][r]);
						}
					}
					{
						int nxt = max(0, i - (r - l));
						for (int j = 0; j < 2; j++) chmax(dp[j ^ 1][nxt][l][r], dp[j][i][l][r]);
						if (r - l >= i) {
							chmax(res[l + i], dp[0][i][l][r]);
							chmax(res[r - i], dp[1][i][l][r]);
						}
					}
				}
			}
		}
	}
	for (int i = 0; i < n; i++) {
		ll ans = -INF;
		if (i + 1 < n) chmax(ans, res[i + 1]);
		if (i > 0) chmax(ans, res[i - 1]);
		cout << ans << "\n";
	}
}
0