Skip to content

Commit 55cd1df

Browse files
authored
[20250507] BOJ / P1 / 닌자배치 / 권혁준
1 parent 3ce5b3a commit 55cd1df

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
7+
// IO field
8+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
static StringTokenizer st = new StringTokenizer("");
11+
12+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
13+
static String nextToken() throws Exception {
14+
while(!st.hasMoreTokens()) nextLine();
15+
return st.nextToken();
16+
}
17+
static int nextInt() throws Exception { return Integer.parseInt(nextToken()); }
18+
static long nextLong() throws Exception { return Long.parseLong(nextToken()); }
19+
static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); }
20+
static void bwEnd() throws Exception {bw.flush();bw.close();}
21+
22+
// Additional field
23+
24+
static int N, M;
25+
static long[] b, c, l, s;
26+
static long ans = 0;
27+
static PriorityQueue<Long>[] Q;
28+
29+
public static void main(String[] args) throws Exception {
30+
31+
ready();
32+
solve();
33+
34+
bwEnd();
35+
36+
}
37+
38+
static void ready() throws Exception{
39+
40+
N = nextInt();
41+
M = nextInt();
42+
b = new long[N+1];
43+
c = new long[N+1];
44+
l = new long[N+1];
45+
s = new long[N+1];
46+
Q = new PriorityQueue[N+1];
47+
for(int i=1;i<=N;i++) {
48+
b[i] = nextInt();
49+
c[i] = nextInt();
50+
l[i] = nextInt();
51+
s[i] = c[i];
52+
Q[i] = new PriorityQueue<>((a,b) -> Long.compare(b, a));
53+
Q[i].add(c[i]);
54+
ans = Math.max(ans, l[i]);
55+
}
56+
57+
58+
}
59+
60+
static void solve() throws Exception {
61+
62+
for(int i=N;i>1;i--) {
63+
int p = (int)b[i];
64+
if(Q[i].size() > Q[p].size()) {
65+
Q[i].addAll(Q[p]);
66+
s[i] += s[p];
67+
Q[p] = Q[i];
68+
s[p] = s[i];
69+
}
70+
else {
71+
Q[p].addAll(Q[i]);
72+
s[p] += s[i];
73+
}
74+
while(s[p] > M) s[p] -= Q[p].poll();
75+
ans = Math.max(ans, l[p] * Q[p].size());
76+
}
77+
78+
bw.write(ans + "\n");
79+
80+
}
81+
82+
}
83+
```

0 commit comments

Comments
 (0)