結果

問題 No.258 回転寿司(2)
ユーザー nCk_cvnCk_cv
提出日時 2015-08-01 16:12:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 219 ms / 2,000 ms
コード長 1,316 bytes
コンパイル時間 2,562 ms
コンパイル使用メモリ 81,168 KB
実行使用メモリ 43,512 KB
最終ジャッジ日時 2024-04-24 11:07:44
合計ジャッジ時間 17,022 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 212 ms
43,132 KB
testcase_01 AC 195 ms
43,208 KB
testcase_02 AC 184 ms
42,488 KB
testcase_03 AC 135 ms
41,568 KB
testcase_04 AC 179 ms
42,332 KB
testcase_05 AC 185 ms
42,412 KB
testcase_06 AC 205 ms
43,064 KB
testcase_07 AC 159 ms
41,916 KB
testcase_08 AC 180 ms
42,308 KB
testcase_09 AC 153 ms
42,280 KB
testcase_10 AC 140 ms
41,928 KB
testcase_11 AC 162 ms
42,104 KB
testcase_12 AC 170 ms
42,300 KB
testcase_13 AC 146 ms
41,628 KB
testcase_14 AC 197 ms
43,004 KB
testcase_15 AC 154 ms
41,712 KB
testcase_16 AC 159 ms
41,836 KB
testcase_17 AC 142 ms
41,472 KB
testcase_18 AC 206 ms
43,316 KB
testcase_19 AC 135 ms
41,588 KB
testcase_20 AC 192 ms
42,772 KB
testcase_21 AC 191 ms
43,228 KB
testcase_22 AC 117 ms
40,796 KB
testcase_23 AC 126 ms
41,720 KB
testcase_24 AC 126 ms
41,560 KB
testcase_25 AC 111 ms
41,400 KB
testcase_26 AC 180 ms
42,432 KB
testcase_27 AC 145 ms
41,332 KB
testcase_28 AC 202 ms
43,356 KB
testcase_29 AC 150 ms
42,096 KB
testcase_30 AC 152 ms
41,516 KB
testcase_31 AC 141 ms
41,800 KB
testcase_32 AC 163 ms
42,008 KB
testcase_33 AC 200 ms
42,692 KB
testcase_34 AC 183 ms
42,696 KB
testcase_35 AC 180 ms
42,528 KB
testcase_36 AC 139 ms
41,680 KB
testcase_37 AC 137 ms
41,908 KB
testcase_38 AC 151 ms
42,124 KB
testcase_39 AC 148 ms
41,948 KB
testcase_40 AC 181 ms
43,080 KB
testcase_41 AC 191 ms
42,588 KB
testcase_42 AC 155 ms
42,096 KB
testcase_43 AC 147 ms
41,956 KB
testcase_44 AC 199 ms
43,140 KB
testcase_45 AC 190 ms
42,476 KB
testcase_46 AC 118 ms
40,420 KB
testcase_47 AC 198 ms
42,828 KB
testcase_48 AC 161 ms
41,824 KB
testcase_49 AC 156 ms
42,096 KB
testcase_50 AC 131 ms
41,716 KB
testcase_51 AC 188 ms
42,680 KB
testcase_52 AC 193 ms
42,712 KB
testcase_53 AC 148 ms
41,808 KB
testcase_54 AC 194 ms
42,776 KB
testcase_55 AC 219 ms
43,512 KB
testcase_56 AC 193 ms
42,800 KB
testcase_57 AC 148 ms
41,812 KB
testcase_58 AC 183 ms
42,692 KB
testcase_59 AC 207 ms
43,024 KB
testcase_60 AC 137 ms
41,596 KB
testcase_61 AC 204 ms
42,896 KB
testcase_62 AC 160 ms
42,012 KB
testcase_63 AC 190 ms
42,788 KB
testcase_64 AC 198 ms
43,100 KB
testcase_65 AC 185 ms
42,644 KB
testcase_66 AC 143 ms
41,608 KB
testcase_67 AC 202 ms
42,852 KB
testcase_68 AC 193 ms
43,032 KB
testcase_69 AC 197 ms
42,972 KB
testcase_70 AC 155 ms
42,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] list = new int[n];
		for(int i = 0; i < n; i++) {
			list[i] = sc.nextInt();
		}
		if(n == 1) {
			System.out.println(list[0]);
			System.out.println(1);
			return;
		}
		int[] dp = new int[n];
		
		int[] pre = new int[n];
		dp[0] = list[0];
		pre[0] = -1;
		dp[1] = list[1];
		pre[1] = -1;
		
		
		for(int i = 2; i < n; i++) {
			if(i >= 3) {
				int a = dp[i-2] + list[i];
				int b = dp[i-3] + list[i];
				if(a < b) {
					dp[i] = b;
					pre[i] = i-3;
				}
				if(a >= b) {
					dp[i] = a;
					pre[i] = i-2;
				}
			}
			else {
				dp[i] = dp[i-2] + list[i];
				pre[i] = i-2;
			}
		}
		ArrayList<Integer> plist = new ArrayList<Integer>();
		if(dp[n-1] >= dp[n-2]) {
			int prenumber = n-1;
			while(prenumber != -1) {
				plist.add(prenumber);
				prenumber = pre[prenumber];
				
			}
		}
		else {
			int prenumber = n-2;
			while(prenumber != -1) {
				plist.add(prenumber);
				prenumber = pre[prenumber];
				
			}
		}
		System.out.println(Math.max(dp[n-1], dp[n-2]));
		System.out.print((plist.get(plist.size()-1) + 1));
		for(int i = plist.size()-2; i >= 0; i--) {
			System.out.print(" " + (plist.get(i) + 1));
		}
		System.out.println();
		
		
		
	}
}
0