結果

問題 No.1247 ブロック登り
ユーザー mkawa2
提出日時 2021-08-19 00:18:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 385 ms / 3,000 ms
コード長 1,506 bytes
コンパイル時間 2,214 ms
コンパイル使用メモリ 198,424 KB
最終ジャッジ日時 2025-01-23 22:58:45
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

// #include <atcoder/all>
// using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
const int inf = 1e9;
const ll lim = 1e18;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};

int dp[305][2][305][305];

void chmax(int &a, const int &b) { a = max(a, b); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  rep(i, 305) rep(j, 2) rep(k, 305) rep(l, 305) dp[i][j][k][l] = -inf;

  int n, k, d;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a[i];

  rep(i, n) dp[k][0][i][i] = a[i] * k;
  vi ans(n, -inf);

  for (int i = k; i >= 0; i--) {
    rep(j, 2) {
      rep(l, n) {
        for (int r = l; r < n; r++) {
          int pre = dp[i][j][l][r];
          if (pre == -inf) continue;

          int p = j ? r - i : l + i;
          if (l <= p && p <= r) chmax(ans[p], pre);

          if (i - 2 >= 0 && l != r) chmax(dp[i - 2][j][l][r], pre);

          d = j ? r - l + 1 : 1;
          if (l - 1 >= 0 && i - d >= 0)
            chmax(dp[i - d][0][l - 1][r], pre + a[l - 1] * (i - d));

          d = j ? 1 : r - l + 1;
          if (r + 1 < n && i - d >= 0)
            chmax(dp[i - d][1][l][r + 1], pre + a[r + 1] * (i - d));
        }
      }
    }
  }
  rep(i, n) cout << ans[i] << endl;

  return 0;
}
0