結果

問題 No.45 回転寿司
ユーザー mits58
提出日時 2015-12-11 22:46:51
言語 Java
(openjdk 23)
結果
AC  
実行時間 195 ms / 5,000 ms
コード長 634 bytes
コンパイル時間 4,516 ms
コンパイル使用メモリ 76,880 KB
実行使用メモリ 42,160 KB
最終ジャッジ日時 2024-12-27 12:32:12
合計ジャッジ時間 9,064 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

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