Skip to content

Commit d021c90

Browse files
committed
[Silver I] Title: 포도주 시식, Time: 132 ms, Memory: 16332 KB -BaekjoonHub
1 parent d9a3f3f commit d021c90

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

백준/Silver/2156. 포도주 시식/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 16600 KB, 시간: 160 ms
7+
메모리: 16332 KB, 시간: 132 ms
88

99
### 분류
1010

1111
다이나믹 프로그래밍
1212

1313
### 제출 일자
1414

15-
2024년 8월 6일 16:08:52
15+
2025년 12월 21일 20:18:56
1616

1717
### 문제 설명
1818

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
import java.io.BufferedReader;
2-
import java.io.InputStreamReader;
3-
import java.util.Arrays;
1+
import java.io.*;
2+
import java.util.*;
43

54
public class Main {
65

76
static int n, ans;
8-
static int[] grape;
7+
static int[] wine;
98
static int[][] dp;
9+
1010
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
1111
public static void main(String[] args) throws Exception{
12-
input();
13-
System.out.println(recur(0, 0));
14-
}
12+
n = Integer.parseInt(br.readLine());
13+
wine = new int[n];
14+
dp = new int[n][3];
1515

16-
public static int recur(int cnt, int seleted){
17-
if(cnt == n){
18-
return 0 ;
16+
for(int i = 0; i < n; i++){
17+
wine[i] = Integer.parseInt(br.readLine());
18+
Arrays.fill(dp[i], -1);
1919
}
20-
if(dp[cnt][seleted] != -1) return dp[cnt][seleted];
21-
dp[cnt][seleted]= recur(cnt + 1, 0);
22-
if(seleted < 2) dp[cnt][seleted] = Math.max(dp[cnt][seleted], recur(cnt + 1, seleted + 1) + grape[cnt]);
23-
return dp[cnt][seleted];
20+
System.out.println(recur(0, 0));
2421
}
2522

26-
public static void input() throws Exception{
27-
n = Integer.parseInt(br.readLine());
28-
grape = new int[n];
29-
dp = new int[n][3];
30-
for (int i = 0; i < n; i++) {
31-
grape[i] = Integer.parseInt(br.readLine());
32-
Arrays.fill(dp[i], -1);
23+
public static int recur(int cnt, int seq){
24+
if(cnt == n)return 0 ;
25+
if(dp[cnt][seq] != -1) return dp[cnt][seq];
26+
27+
if(seq < 2){
28+
dp[cnt][seq] = Math.max(recur(cnt + 1, seq + 1) + wine[cnt], dp[cnt][seq]);
3329
}
30+
return dp[cnt][seq] = Math.max(recur(cnt + 1, 0), dp[cnt][seq]);
3431
}
35-
}
32+
}

0 commit comments

Comments
 (0)