結果

問題 No.1247 ブロック登り
ユーザー milanis48663220milanis48663220
提出日時 2021-05-20 22:47:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,140 ms / 3,000 ms
コード長 2,517 bytes
コンパイル時間 1,360 ms
コンパイル使用メモリ 120,704 KB
実行使用メモリ 318,692 KB
最終ジャッジ日時 2024-04-18 14:09:57
合計ジャッジ時間 15,498 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,840 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 3 ms
13,692 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 6 ms
24,092 KB
testcase_05 AC 5 ms
20,108 KB
testcase_06 AC 3 ms
7,660 KB
testcase_07 AC 22 ms
84,000 KB
testcase_08 AC 21 ms
85,568 KB
testcase_09 AC 12 ms
50,652 KB
testcase_10 AC 15 ms
65,340 KB
testcase_11 AC 1,140 ms
276,616 KB
testcase_12 AC 1,071 ms
317,568 KB
testcase_13 AC 1,107 ms
318,692 KB
testcase_14 AC 1,083 ms
318,584 KB
testcase_15 AC 1,087 ms
318,384 KB
testcase_16 AC 1,069 ms
318,008 KB
testcase_17 AC 1,066 ms
318,432 KB
testcase_18 AC 982 ms
315,340 KB
testcase_19 AC 232 ms
317,268 KB
testcase_20 AC 139 ms
314,700 KB
testcase_21 AC 86 ms
313,488 KB
testcase_22 AC 21 ms
59,132 KB
testcase_23 AC 16 ms
50,788 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 1,106 ms
318,092 KB
testcase_26 AC 1,027 ms
317,744 KB
testcase_27 AC 1,037 ms
318,652 KB
testcase_28 AC 1,031 ms
318,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

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; }

using namespace std;
typedef long long ll;
const ll INF = 1e18;

ll dp[305][305][305][2];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, k; cin >> n >> k;
    vector<ll> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int j = k; j >= 0; j--){
        for(int l = 0; l < n; l++){
            for(int r = l+1; r <= n; r++){
                dp[l][r][j][0] = -INF;
                dp[l][r][j][1] = -INF;
            }
        }
    }

    for(int l = 0; l < n; l++){
        dp[l][l+1][k][0] = a[l]*k;
        dp[l][l+1][k][1] = a[l]*k;
    }

    for(int j = k; j > 0; j--){
        // cout << j << endl;
        for(int l = 0; l < n; l++){
            for(int r = l+1; r <= n; r++){
                for(int p = 0; p < 2; p++){
                    // cout << "dp[" << l << "][" << r << "][" << j << "][" << p << "] = " << dp[l][r][j][p] << endl;
                    int cur = (p == 0 ? l : r-1);
                    // とどまる
                    if(j >= 2 && r-l >= 2) chmax(dp[l][r][j-2][p], dp[l][r][j][p]);
                    // l-1に行く
                    int dist = cur-(l-1);
                    if(l > 0 && dist <= j) chmax(dp[l-1][r][j-dist][0], dp[l][r][j][p]+a[l-1]*(j-dist));
                    // rに行く
                    dist = r-cur;
                    if(r < n && dist <= j) chmax(dp[l][r+1][j-dist][1], dp[l][r][j][p]+a[r]*(j-dist));
                }
            }
        }
    }

    for(int i = 0; i < n; i++){
        ll ans = -INF;
        for(int l = 0; l <= i; l++){
            for(int r = i+1; r <= n; r++){
                if(r-l < 2) continue;
                // lから来る
                if(i-l <= k) chmax(ans, dp[l][r][i-l][0]);
                // r-1から来る
                if((r-1)-i <= k) chmax(ans, dp[l][r][(r-1)-i][1]);
            }
        }
        cout << ans << endl;
    }
}
0