• 문제 링크
17216번: 가장 큰 감소 부분 수열
수열 A가 주어졌을 때, 그 수열의 감소 부분 수열 중에서 합이 가장 큰 것을 구하는 프로그램을 작성하시오. 예를 들어, 수열 A = {1, 100, 2, 50, 60, 8, 7, 3, 6, 5} 인 경우에 합이 가장 큰 감소 부분 수열
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
int[] arr = new int[n], dp = new int[n];
int max = 0;
for (int i = 0; i < n; i++) {
arr[i] = dp[i] = read();
for (int j = 0; j < i; j++) if (arr[i] < arr[j]) dp[i] = Math.max(dp[i], dp[j] + arr[i]);
max = Math.max(max, dp[i]);
}
bw.write(String.valueOf(max));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 4172 sqrt log sin - Dynamic Programming / Java (0) | 2023.09.27 |
---|---|
[백준] 22971 증가하는 부분 수열의 개수 - Dynamic Programming / Java (0) | 2023.09.26 |
[백준] 28464 Potato - Greedy / Java (0) | 2023.09.24 |
[백준] 28353 고양이 카페 - Greedy / Java (0) | 2023.09.23 |
[백준] 25707 팔찌 만들기 - Greedy / Java (0) | 2023.09.22 |
댓글