-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaekjoon14501.java
74 lines (57 loc) · 2.11 KB
/
Baekjoon14501.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/14501
* 백준 14501번 퇴사
*/
public class Baekjoon14501 {
private static int[][] works;
private static long maxProfit = 0;
private static boolean[] available;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
works = new int[n + 1][2];
available = new boolean[n + 1];
Arrays.fill(available, true);
for (int i = 1; i < n + 1; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
works[i][0] = Integer.parseInt(st.nextToken());
works[i][1] = Integer.parseInt(st.nextToken());
}
for (int i = 1; i < n + 1; i++) {
if (i + works[i][0] <= available.length) {
for (int j = i; j < i + works[i][0]; j++) {
if (j >= available.length) break;
available[j] = false;
}
dfs(i, works[i][1]);
for (int j = i; j < i + works[i][0]; j++) {
if (j >= available.length) break;
available[j] = true;
}
}
}
System.out.println(maxProfit);
}
private static void dfs(int today, long profit) {
maxProfit = Math.max(profit, maxProfit);
for (int i = today + 1; i < available.length; i++) {
if (available[i] && i + works[i][0] <= available.length) {
for (int j = i; j < i + works[i][0]; j++) {
if (j >= available.length) break;
available[j] = false;
}
dfs(i, profit + works[i][1]);
for (int j = i; j < i + works[i][0]; j++) {
if (j >= available.length) break;
available[j] = true;
}
}
}
}
}