結果

問題 No.258 回転寿司(2)
コンテスト
ユーザー yuriko32
提出日時 2026-03-15 01:18:32
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 936 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,553 ms
コンパイル使用メモリ 217,532 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2026-03-15 01:19:11
合計ジャッジ時間 12,871 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 67
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

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

    int n;
    long long C=0;
    cin >> n;

    vector<long long> a(n+1), dp(n+1);
    vector<int> pre(n+1);

    for(int i=1;i<=n;i++) cin >> a[i];

    long long ans = LLONG_MIN;
    int pos = -1;

    for(int i=1;i<=n;i++){
        dp[i] = a[i];
        pre[i] = -1;

        for(int j=1;j<=i-2;j++){
            long long cand = dp[j] + a[i] - C*(i-j);

            if(cand > dp[i]){
                dp[i] = cand;
                pre[i] = j;
            }
        }

        if(dp[i] > ans){
            ans = dp[i];
            pos = i;
        }
    }

    vector<int> path;

    while(pos != -1){
        path.push_back(pos);
        pos = pre[pos];
    }

    reverse(path.begin(), path.end());

    cout << ans << "\n";
    //cout << path.size() << "\n";
    for(int x: path) cout << x << " ";

    return 0;
}
0