/
Main.java
78 lines (63 loc) · 2.21 KB
/
Main.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
75
76
77
78
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) throws Exception{
Solve solve = new Solve();
solve.run();
}
}
class Solve{
class Dungeon{
private int needExp, getExp;
public Dungeon(int needExp, int getExp){
this.needExp = needExp;
this.getExp = getExp;
}
}
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private String[] read;
private int N, T;
private Dungeon[] dungeon;
private int[][] dp;
private int[][] move;
public void run() throws Exception{
input();
System.out.println(getMaximumExp(1));
}
private void input() throws Exception{
read = br.readLine().split(" ");
N = Integer.parseInt(read[0]);
T = Integer.parseInt(read[1]);
dp = new int[N+5][T+5];
move = new int[N+5][N+5];
dungeon = new Dungeon[N+5];
for(int i = 0; i <= N; i++)
for(int j = 0; j <= T; j++) dp[i][j] = -1;
for(int i = 0; i < N; i++){
read = br.readLine().split(" ");
dungeon[i] = new Dungeon(Integer.parseInt(read[0]), Integer.parseInt(read[1]));
if(dungeon[i].needExp == 0) dp[i][0] = 0;
}
for(int i = 0; i < N; i++){
read = br.readLine().split(" ");
for(int j = 0; j < N; j++) move[i][j] = Integer.parseInt(read[j]);
}
}
private int getMaximumExp(int time){
if(time > T){
int ret = 0;
for(int i = 0; i < N; i++) ret = Math.max(dp[i][T], ret);
return ret;
}
for(int i = 0; i < N; i++){
if(dp[i][time-1] != -1) dp[i][time] = dp[i][time-1] + dungeon[i].getExp;
for(int j = 0; j < N; j++){ // 다른경로에서 i로 오는경우
if(i == j) continue;
int bTime = time - move[j][i];
if(bTime <= 0 || dp[j][bTime] < dungeon[i].needExp) continue;
dp[i][time] = Math.max(dp[i][time], dp[j][bTime]);
}
}
return getMaximumExp(time+1);
}
}