結果

問題 No.45 回転寿司
ユーザー mits58
提出日時 2015-12-11 22:46:51
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 153 ms / 5,000 ms
コード長 634 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,766 ms
コンパイル使用メモリ 82,028 KB
実行使用メモリ 45,772 KB
最終ジャッジ日時 2026-06-02 01:35:51
合計ジャッジ時間 7,082 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.util.Scanner;

public class Main{
	static int n;
	static int dp[],v[];
	static int res(int x){
		if(x>n-1) return 0;
		else if(dp[x]!=-1) return dp[x];
		else if(x==n-1) return v[n-1];
		else if(x==n-2) return Math.max(v[n-1], v[n-2]);
		else return dp[x]=Math.max(res(x+2)+v[x], res(x+1));
	}
    public static void main(String[] args){
    	Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
        	n=sc.nextInt();
        	v=new int[n];
        	for(int i=0;i<n;i++) v[i]=sc.nextInt();
        	dp=new int[n];
        	for(int i=0;i<n;i++) dp[i]=-1;
        	System.out.println(res(0));
        }
    }
}
0