結果

問題 No.258 回転寿司(2)
ユーザー 🍮かんプリン
提出日時 2020-05-25 00:39:34
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,848 bytes
コンパイル時間 1,335 ms
コンパイル使用メモリ 164,188 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-10-12 14:06:13
合計ジャッジ時間 5,339 ms
ジャッジサーバーID
(参考情報)
judge / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 39 WA * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.05.25 00:36:25
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

int main() {
    int n;
    cin >> n;
    vector< int > a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    if (n == 1) {
        cout << a[0] << endl;
        cout << 1 << endl;
        return 0;
    } else if (n == 2) {
        if (a[0] > a[1]) {
            cout << a[0] << endl;
            cout << 1 << endl;
        } else {
            cout << a[1] << endl;
            cout << 2 << endl;
        }
    }
    vector< int > dp(n, 0);
    dp[0] = a[0];
    dp[1] = a[1];
    dp[2] = a[0] + a[2];
    for (int i = 3; i < n; i++) {
        dp[i] = max(a[i] + dp[i - 2], a[i] + dp[i - 3]);
    }
    vector< int > ans;
    if (dp[n - 1] > dp[n - 2]) {
        cout << dp[n - 1] << endl;
        ans.push_back(n - 1);
        int t = n - 1;
        while (t >= 2) {
            if (dp[t - 2] + a[t] == dp[t]) {
                ans.push_back(t - 2);
                t = t - 2;
            } else if (t >= 3 && dp[t - 3] + a[t] == dp[t]) {
                ans.push_back(t - 3);
                t = t - 3;
            }
        }
        for (int i = ans.size() - 1; i >= 0; i--) {
            cout << ans[i] + 1 << " ";
        }
        cout << endl;
    } else {
        cout << dp[n - 2] << endl;
        ans.push_back(n - 2);
        int t = n - 2;
        while (t >= 3) {
            if (dp[t - 2] + a[t] == dp[t]) {
                ans.push_back(t - 2);
                t = t - 2;
            } else if (dp[t - 3] + a[t] == dp[t]) {
                ans.push_back(t - 3);
                t = t - 3;
            }
        }
        for (int i = ans.size() - 1; i >= 0; i--) {
            cout << ans[i] + 1 << " ";
        }
        cout << endl;
    }
    return 0;
}
0