結果

問題 No.258 回転寿司(2)
ユーザー 37zigen37zigen
提出日時 2020-03-27 20:07:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 3,245 bytes
コンパイル時間 4,211 ms
コンパイル使用メモリ 87,380 KB
実行使用メモリ 57,508 KB
最終ジャッジ日時 2023-08-30 16:46:17
合計ジャッジ時間 20,286 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 195 ms
57,076 KB
testcase_01 AC 203 ms
56,356 KB
testcase_02 AC 186 ms
56,524 KB
testcase_03 AC 123 ms
55,992 KB
testcase_04 AC 182 ms
56,276 KB
testcase_05 AC 147 ms
56,712 KB
testcase_06 AC 197 ms
57,312 KB
testcase_07 AC 141 ms
56,076 KB
testcase_08 AC 156 ms
55,648 KB
testcase_09 AC 138 ms
56,156 KB
testcase_10 AC 127 ms
55,676 KB
testcase_11 AC 136 ms
55,780 KB
testcase_12 AC 150 ms
56,688 KB
testcase_13 AC 138 ms
56,232 KB
testcase_14 AC 185 ms
57,376 KB
testcase_15 AC 147 ms
56,120 KB
testcase_16 AC 142 ms
56,012 KB
testcase_17 AC 145 ms
55,728 KB
testcase_18 AC 191 ms
56,732 KB
testcase_19 AC 119 ms
55,916 KB
testcase_20 AC 181 ms
57,300 KB
testcase_21 AC 186 ms
57,052 KB
testcase_22 AC 114 ms
55,660 KB
testcase_23 AC 114 ms
55,428 KB
testcase_24 AC 117 ms
56,012 KB
testcase_25 AC 112 ms
55,680 KB
testcase_26 AC 178 ms
56,604 KB
testcase_27 AC 142 ms
55,860 KB
testcase_28 AC 197 ms
57,184 KB
testcase_29 AC 152 ms
56,120 KB
testcase_30 AC 127 ms
56,024 KB
testcase_31 AC 128 ms
55,644 KB
testcase_32 AC 144 ms
55,864 KB
testcase_33 AC 184 ms
56,700 KB
testcase_34 AC 186 ms
56,128 KB
testcase_35 AC 177 ms
56,740 KB
testcase_36 AC 131 ms
55,748 KB
testcase_37 AC 130 ms
55,764 KB
testcase_38 AC 143 ms
56,120 KB
testcase_39 AC 142 ms
56,232 KB
testcase_40 AC 182 ms
56,804 KB
testcase_41 AC 180 ms
56,376 KB
testcase_42 AC 153 ms
55,956 KB
testcase_43 AC 134 ms
56,080 KB
testcase_44 AC 191 ms
57,088 KB
testcase_45 AC 184 ms
54,648 KB
testcase_46 AC 120 ms
55,688 KB
testcase_47 AC 185 ms
56,460 KB
testcase_48 AC 130 ms
55,996 KB
testcase_49 AC 151 ms
55,920 KB
testcase_50 AC 120 ms
55,704 KB
testcase_51 AC 180 ms
56,528 KB
testcase_52 AC 184 ms
56,396 KB
testcase_53 AC 136 ms
56,152 KB
testcase_54 AC 184 ms
57,508 KB
testcase_55 AC 198 ms
56,760 KB
testcase_56 AC 183 ms
57,284 KB
testcase_57 AC 135 ms
55,892 KB
testcase_58 AC 183 ms
56,612 KB
testcase_59 AC 192 ms
57,000 KB
testcase_60 AC 126 ms
54,512 KB
testcase_61 AC 185 ms
56,720 KB
testcase_62 AC 149 ms
56,060 KB
testcase_63 AC 183 ms
56,748 KB
testcase_64 AC 195 ms
56,448 KB
testcase_65 AC 189 ms
56,836 KB
testcase_66 AC 119 ms
55,692 KB
testcase_67 AC 199 ms
56,836 KB
testcase_68 AC 199 ms
57,292 KB
testcase_69 AC 190 ms
56,624 KB
testcase_70 AC 152 ms
55,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	final long MOD=(long)1e9+7;
	
	void run() {
		Scanner sc = new Scanner(System.in);
		int N=sc.nextInt();
		int[] V=new int[N];
		int[][] max=new int[N][2];
		for(int i=0;i<N;++i)V[i]=sc.nextInt();
		for(int i=0;i<N;++i) {
			max[i][0]=Math.max(V[i]+(i>=2?max[i-2][0]:0), (i>0?max[i-1][0]:0));
			if(max[i][0]==V[i]+(i>=2?max[i-2][0]:0)){
				max[i][1]=i;
			}else {
				max[i][1]=max[i-1][1];
			}
		}
		ArrayDeque<Integer> list=new ArrayDeque<>();
		long ans=0;
		int p=N-1,res=max[p][0];
		while(p>=0) {
			if(max[p][1]!=p||res!=max[p][0]) {
				--p;
				continue;
			}
			list.addFirst(p+1);
			ans+=V[p];
			res-=V[p];
			p-=2;
		}
		System.out.println(ans);
		while(!list.isEmpty()) {
			System.out.print(list.pollFirst()+(list.isEmpty()?"\n":" "));
		}
	}
	
	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

}
	
	
class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}
    public boolean hasNext() { skipUnprintable(); return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    
    public int nextInt() {
    	return (int)nextLong();
    }
}
0