結果

問題 No.258 回転寿司(2)
ユーザー tenten
提出日時 2021-11-10 21:21:46
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,706 bytes
コンパイル時間 2,577 ms
コンパイル使用メモリ 78,648 KB
実行使用メモリ 48,764 KB
最終ジャッジ日時 2024-11-21 13:21:54
合計ジャッジ時間 10,368 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21 WA * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        if (n == 1) {
            System.out.println(values[0]);
            System.out.println(1);
            return;
        } else if (n == 2) {
            if (values[0] < values[1]) {
                System.out.println(values[1]);
                System.out.println(2);
            } else {
                System.out.println(values[0]);
                System.out.println(1);
            }
            return;
        }
        int[] dp = new int[n];
        int[] prev = new int[n];
        dp[0] = values[0];
        prev[0] = -1;
        dp[1] = values[1];
        prev[1] = -1;
        dp[2] = values[0] + values[1];
        prev[2] = 0;
        for (int i = 3; i < n; i++) {
            if (dp[i - 2] < dp[i - 3]) {
                dp[i] = dp[i - 3] + values[i];
                prev[i] = i - 3;
            } else {
                dp[i] = dp[i - 2] + values[i];
                prev[i] = i - 2;
            }
        }
        int idx;
        int ans;
        if (dp[n - 1] < dp[n - 2]) {
            idx = n - 2;
            ans = dp[n - 2];
        } else {
            idx = n - 1;
            ans = dp[n - 1];
        }
        ArrayDeque<Integer> deq = new ArrayDeque<>();
        while (idx >= 0) {
            deq.push(idx + 1);
            idx = prev[idx];
        }
        StringBuilder sb = new StringBuilder();
        boolean isFirst = true;
        while (deq.size() > 0) {
            if (!isFirst) {
                sb.append(" ");
            }
            isFirst = false;
            sb.append(deq.poll());
        }
        System.out.println(ans);
        System.out.println(sb);
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0