結果

問題 No.258 回転寿司(2)
ユーザー scachescache
提出日時 2015-11-17 15:20:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 1,585 bytes
コンパイル時間 4,362 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 62,164 KB
最終ジャッジ日時 2023-08-06 15:52:32
合計ジャッジ時間 23,427 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 274 ms
61,364 KB
testcase_01 AC 258 ms
61,264 KB
testcase_02 AC 225 ms
59,200 KB
testcase_03 AC 139 ms
55,636 KB
testcase_04 AC 194 ms
58,392 KB
testcase_05 AC 224 ms
58,756 KB
testcase_06 AC 251 ms
61,652 KB
testcase_07 AC 176 ms
56,052 KB
testcase_08 AC 192 ms
58,556 KB
testcase_09 AC 174 ms
56,236 KB
testcase_10 AC 150 ms
55,732 KB
testcase_11 AC 189 ms
56,108 KB
testcase_12 AC 218 ms
58,824 KB
testcase_13 AC 168 ms
56,036 KB
testcase_14 AC 256 ms
62,164 KB
testcase_15 AC 179 ms
55,924 KB
testcase_16 AC 174 ms
55,900 KB
testcase_17 AC 179 ms
55,996 KB
testcase_18 AC 262 ms
61,448 KB
testcase_19 AC 139 ms
55,680 KB
testcase_20 AC 231 ms
59,180 KB
testcase_21 AC 253 ms
61,492 KB
testcase_22 AC 132 ms
54,012 KB
testcase_23 AC 132 ms
55,932 KB
testcase_24 AC 131 ms
55,620 KB
testcase_25 AC 127 ms
56,112 KB
testcase_26 AC 240 ms
59,060 KB
testcase_27 AC 177 ms
55,904 KB
testcase_28 AC 270 ms
61,436 KB
testcase_29 AC 191 ms
56,820 KB
testcase_30 AC 152 ms
56,068 KB
testcase_31 AC 153 ms
55,712 KB
testcase_32 AC 178 ms
56,068 KB
testcase_33 AC 248 ms
59,468 KB
testcase_34 AC 224 ms
59,060 KB
testcase_35 AC 228 ms
59,160 KB
testcase_36 AC 155 ms
56,096 KB
testcase_37 AC 148 ms
55,832 KB
testcase_38 AC 188 ms
56,200 KB
testcase_39 AC 189 ms
56,228 KB
testcase_40 AC 227 ms
59,396 KB
testcase_41 AC 248 ms
61,320 KB
testcase_42 AC 178 ms
55,836 KB
testcase_43 AC 163 ms
56,048 KB
testcase_44 AC 258 ms
61,592 KB
testcase_45 AC 243 ms
59,500 KB
testcase_46 AC 137 ms
55,912 KB
testcase_47 AC 246 ms
59,424 KB
testcase_48 AC 160 ms
55,832 KB
testcase_49 AC 176 ms
56,012 KB
testcase_50 AC 138 ms
55,664 KB
testcase_51 AC 231 ms
58,940 KB
testcase_52 AC 230 ms
59,384 KB
testcase_53 AC 158 ms
56,016 KB
testcase_54 AC 238 ms
59,120 KB
testcase_55 AC 269 ms
61,272 KB
testcase_56 AC 242 ms
59,028 KB
testcase_57 AC 159 ms
55,696 KB
testcase_58 AC 224 ms
58,804 KB
testcase_59 AC 264 ms
61,600 KB
testcase_60 AC 148 ms
55,716 KB
testcase_61 AC 244 ms
59,048 KB
testcase_62 AC 175 ms
55,936 KB
testcase_63 AC 229 ms
58,780 KB
testcase_64 AC 268 ms
61,888 KB
testcase_65 AC 240 ms
58,956 KB
testcase_66 AC 139 ms
55,752 KB
testcase_67 AC 255 ms
61,712 KB
testcase_68 AC 253 ms
61,528 KB
testcase_69 AC 241 ms
58,784 KB
testcase_70 AC 174 ms
55,904 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