結果

問題 No.258 回転寿司(2)
ユーザー tentententen
提出日時 2021-11-10 21:21:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,706 bytes
コンパイル時間 2,751 ms
コンパイル使用メモリ 78,772 KB
実行使用メモリ 37,400 KB
最終ジャッジ日時 2024-05-01 09:53:36
合計ジャッジ時間 8,989 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 58 ms
37,248 KB
testcase_05 AC 58 ms
36,808 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 49 ms
37,100 KB
testcase_09 WA -
testcase_10 AC 49 ms
37,192 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 49 ms
36,944 KB
testcase_14 WA -
testcase_15 AC 48 ms
37,260 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 50 ms
37,284 KB
testcase_21 WA -
testcase_22 AC 46 ms
36,952 KB
testcase_23 AC 49 ms
36,972 KB
testcase_24 AC 48 ms
37,244 KB
testcase_25 AC 50 ms
37,020 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 AC 49 ms
36,984 KB
testcase_31 AC 50 ms
36,808 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 49 ms
37,080 KB
testcase_37 AC 49 ms
37,244 KB
testcase_38 WA -
testcase_39 AC 54 ms
37,248 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 52 ms
37,308 KB
testcase_44 WA -
testcase_45 WA -
testcase_46 AC 53 ms
37,244 KB
testcase_47 WA -
testcase_48 AC 54 ms
36,948 KB
testcase_49 AC 50 ms
37,112 KB
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 AC 49 ms
36,768 KB
testcase_54 AC 52 ms
36,820 KB
testcase_55 WA -
testcase_56 WA -
testcase_57 AC 51 ms
37,204 KB
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
testcase_64 AC 53 ms
37,340 KB
testcase_65 WA -
testcase_66 AC 47 ms
36,980 KB
testcase_67 WA -
testcase_68 WA -
testcase_69 WA -
testcase_70 WA -
権限があれば一括ダウンロードができます

ソースコード

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