結果

問題 No.1247 ブロック登り
ユーザー jupiro
提出日時 2020-10-19 19:54:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,012 ms / 3,000 ms
コード長 2,800 bytes
コンパイル時間 1,662 ms
コンパイル使用メモリ 137,136 KB
最終ジャッジ日時 2025-01-15 11:49:07
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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