結果

問題 No.258 回転寿司(2)
ユーザー scachescache
提出日時 2015-11-17 15:20:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 249 ms / 2,000 ms
コード長 1,585 bytes
コンパイル時間 3,269 ms
コンパイル使用メモリ 83,428 KB
実行使用メモリ 48,308 KB
最終ジャッジ日時 2024-04-24 11:14:00
合計ジャッジ時間 18,681 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 228 ms
48,064 KB
testcase_01 AC 249 ms
47,996 KB
testcase_02 AC 219 ms
44,152 KB
testcase_03 AC 107 ms
40,496 KB
testcase_04 AC 177 ms
42,640 KB
testcase_05 AC 189 ms
42,952 KB
testcase_06 AC 233 ms
48,308 KB
testcase_07 AC 164 ms
41,984 KB
testcase_08 AC 178 ms
43,056 KB
testcase_09 AC 148 ms
42,132 KB
testcase_10 AC 145 ms
41,472 KB
testcase_11 AC 157 ms
41,880 KB
testcase_12 AC 178 ms
42,708 KB
testcase_13 AC 142 ms
42,160 KB
testcase_14 AC 226 ms
48,164 KB
testcase_15 AC 148 ms
41,868 KB
testcase_16 AC 145 ms
41,672 KB
testcase_17 AC 146 ms
42,084 KB
testcase_18 AC 232 ms
48,240 KB
testcase_19 AC 122 ms
41,200 KB
testcase_20 AC 219 ms
44,448 KB
testcase_21 AC 234 ms
48,192 KB
testcase_22 AC 117 ms
41,120 KB
testcase_23 AC 106 ms
40,224 KB
testcase_24 AC 107 ms
40,352 KB
testcase_25 AC 101 ms
39,924 KB
testcase_26 AC 218 ms
44,444 KB
testcase_27 AC 150 ms
42,240 KB
testcase_28 AC 237 ms
48,240 KB
testcase_29 AC 153 ms
42,320 KB
testcase_30 AC 129 ms
41,500 KB
testcase_31 AC 147 ms
41,792 KB
testcase_32 AC 157 ms
41,836 KB
testcase_33 AC 204 ms
44,656 KB
testcase_34 AC 190 ms
43,656 KB
testcase_35 AC 191 ms
42,964 KB
testcase_36 AC 146 ms
41,616 KB
testcase_37 AC 131 ms
40,832 KB
testcase_38 AC 159 ms
42,180 KB
testcase_39 AC 152 ms
42,252 KB
testcase_40 AC 204 ms
43,836 KB
testcase_41 AC 210 ms
45,696 KB
testcase_42 AC 146 ms
42,100 KB
testcase_43 AC 140 ms
41,340 KB
testcase_44 AC 232 ms
48,208 KB
testcase_45 AC 192 ms
44,360 KB
testcase_46 AC 121 ms
41,412 KB
testcase_47 AC 203 ms
44,280 KB
testcase_48 AC 135 ms
41,376 KB
testcase_49 AC 151 ms
41,956 KB
testcase_50 AC 122 ms
41,136 KB
testcase_51 AC 195 ms
43,644 KB
testcase_52 AC 186 ms
43,080 KB
testcase_53 AC 138 ms
41,680 KB
testcase_54 AC 197 ms
44,588 KB
testcase_55 AC 235 ms
47,880 KB
testcase_56 AC 203 ms
44,264 KB
testcase_57 AC 137 ms
41,448 KB
testcase_58 AC 197 ms
43,180 KB
testcase_59 AC 229 ms
48,144 KB
testcase_60 AC 125 ms
41,644 KB
testcase_61 AC 208 ms
44,748 KB
testcase_62 AC 156 ms
42,364 KB
testcase_63 AC 191 ms
44,096 KB
testcase_64 AC 231 ms
47,812 KB
testcase_65 AC 202 ms
44,240 KB
testcase_66 AC 106 ms
40,160 KB
testcase_67 AC 214 ms
48,056 KB
testcase_68 AC 220 ms
48,064 KB
testcase_69 AC 205 ms
44,640 KB
testcase_70 AC 145 ms
42,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main258 {
    public static void main(String[] args){
        new Main258();
    }

    public Main258(){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] v = new int[n];
        for(int i=0;i<n;i++)
            v[i] = sc.nextInt();

        solve(v);
    }

    private void solve(int[] v){
        int n = v.length;
        int[][] dp = new int[n+1][n];

        for(int i=0;i<dp[1].length;i++)
            dp[1][i] = v[0];

        for(int i=2;i<=n;i++){
            for(int j=0;j<n;j++){
                dp[i][j] = Math.max(dp[i-1][j], dp[i-2][j]+v[i-1]);
            }
        }

        int mindex = 0;
        for(int i=0;i<n;i++){
            if(dp[n][mindex]<dp[n][i]){
                mindex = i;
            }
        }
        System.out.println(dp[n][mindex]);

        ArrayList<Integer> l = new ArrayList<>();
        int cur = n;
        while(cur>1){
            while(dp[cur][mindex]==dp[cur-1][mindex]){
                cur--;
            }
            if(cur<=1)
                break;

            for(int i=0;i<n;i++) {
                if(dp[cur][mindex]==dp[cur-2][i]+v[cur-1]){
                    l.add(cur);
                    mindex = i;
                    cur-=2;
                    break;
                }
            }
        }

        if(cur==1)
            l.add(1);

        for(int i=l.size()-1;0<i;i--){
            System.out.print(l.get(i)+" ");
        }
        System.out.println(l.get(0));
    }


}
0