import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int N = Integer.parseInt(st.nextToken());
		
		long dp[][] = new long[N][2];
		
		st = new StringTokenizer(br.readLine());
		for (int i = 0; i < N; i++) {
			dp[i][0] =Integer.parseInt(st.nextToken());
		}
		long max = -1;
		if(dp[0][0]> 0) { 
			dp[0][1] = dp[0][0];
		}
		max = dp[0][0];
		for (int i = 1; i < N; i++) {
			dp[i][1] = dp[i-1][1]+dp[i][0] > dp[i][0] ? dp[i-1][1]+dp[i][0] :dp[i][0];
			max = dp[i][1] > max ? dp[i][1] : max ;
		}
		
		System.out.println(max);

	}

}

'알고리즘' 카테고리의 다른 글

JAVA 백준 1463번 1로 만들기  (0) 2020.02.06
JAVA 백준 1000번 A+B  (0) 2020.02.06

 

 

 

DP로 풀었다. 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

        
        int N = Integer.parseInt(st.nextToken());
        

        long dp[] = new long[N+1];
        
        dp[0] = Long.parseLong("0");
        dp[1] = Long.parseLong("0");
        
        for(int i=2; i<=N; i++) {
//        	System.out.println("N=="+N+",i=="+i);
        	if (i%3 == 0) {
//        		System.out.println(i+"::mod3");
        		if (dp[i]==0 || (dp[i] >1+dp[i/3])) { 	
        			dp[i] = 1+dp[i/3];
        		}
        	}
        	if (i%2 == 0) {
//        		System.out.println(i+"::mod2");
        		if (dp[i]==0 || (dp[i] >1+dp[i/2])) {
//        			System.out.println(i/2+">"+dp[i/2]);
        			dp[i] = 1+dp[i/2]; 
        		}
        	}
        	
        	if((i%3 != 0 && i%2 != 0) || dp[i] > dp[i-1]+1) {
//        		System.out.println(i+"::1+"+dp[i-1]);
        		dp[i] = dp[i-1]+1;
        	}
        	
//        	System.out.println(i+"="+dp[i]);
        }
        
        
        System.out.println(dp[N]);
		
	}

}

'알고리즘' 카테고리의 다른 글

JAVA 백준 1912 연속합  (0) 2020.02.11
JAVA 백준 1000번 A+B  (0) 2020.02.06

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.StringTokenizer; 

public class Main { 
public static void main(String args[]) throws IOException { 
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
StringTokenizer st = new StringTokenizer(br.readLine()); 
int a = Integer.parseInt(st.nextToken()); 
int b = Integer.parseInt(st.nextToken()); 
System.out.println(a+b); 
} 
} 

 

'알고리즘' 카테고리의 다른 글

JAVA 백준 1912 연속합  (0) 2020.02.11
JAVA 백준 1463번 1로 만들기  (0) 2020.02.06