January 08, 2022
백준 문제 링크 : https://www.acmicpc.net/problem/11055
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B_11055 {
static int N;
static int[] arr;
static int[] dp;
public static int sol(int n){
if(dp[n] != 0) return dp[n];
dp[n] = arr[n];
for(int i = n-1; i>=0; i--){
if(arr[i] < arr[n]){
dp[n] = Math.max(dp[n], arr[n] + sol(i));
}
}
return dp[n];
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N];
dp = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
int i = 0;
while(st.hasMoreTokens()){
arr[i++] = Integer.parseInt(st.nextToken());
}
int solution = 0;
for(i=0; i<N; i++){
solution = Math.max(solution, sol(i));
}
System.out.println(solution);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B_11055 {
static int N;
static int[] arr;
static int[] dp;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N];
dp = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
int i = 0;
while(st.hasMoreTokens()){
arr[i++] = Integer.parseInt(st.nextToken());
}
for(i=0; i<N; i++){
dp[i] = arr[i];
for(int j=0; j<i; j++){
if(arr[j] < arr[i] && dp[i] < dp[j]+arr[i]){
dp[i] = dp[j] + arr[i];
}
}
}
int solution = 0;
for(int el : dp){
solution = Math.max(solution, el);
}
System.out.println(solution);
}
}
바로 이전에 11053문제를 풀어서 11055문제는 수월하게 해결할 수 있었다.