알고리즘/JAVA
[JAVA] 백준 알고리즘 2156 : 포도주 (DP)
초보개발자꽁쥐
2018. 10. 11. 14:49
반응형
문제 |
소스 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
long[] input = new long[T+1];
for(int i=1; i<=T; i++) {
input[i] = Integer.parseInt(br.readLine());
}
long[] dp = new long[T+1];
dp[1] = input[1];
if(T>1) {
dp[2] = input[1]+input[2];
}
if(T>2) {
for(int i=3; i<=T; i++) {
dp[i] = Math.max(dp[i-3]+input[i-1]+input[i] // OXO
, Math.max(dp[i-2]+input[i] // OOX
, dp[i-1])); // XOO
}
}
System.out.println(dp[T]);
}
}
출처 |
반응형