#include <bits/stdc++.h>
#define DIM 100010
using namespace std;
int v[DIM],cost[DIM],d[DIM],a[DIM];
long long dp[DIM][30];
vector <int> L[DIM];
int n,m,k,i,j,x,y;
void dfs (int nod, int tata){
int ok = 0;
for (auto vecin : L[nod])
if (vecin != tata){
ok = 1;
dfs (vecin,nod);
}
if (!ok){
if (v[nod]){
for (int i=v[nod];i<=k;i++)
dp[nod][i] = cost[nod];
}
} else {
for (auto vecin : L[nod]){
if (vecin == tata)
continue;
for (int i=1;i<=k;i++){
dp[nod][i] += dp[vecin][i];
}
}
if (v[nod]){
long long val = cost[nod];
for (auto vecin : L[nod])
if (vecin != tata)
val += dp[vecin][v[nod]];
dp[nod][v[nod]] = max (dp[nod][v[nod]],val);
}
for (int i=1;i<=k;i++)
dp[nod][i] = max (dp[nod][i],dp[nod][i-1]);
}
}
int main (){
//ifstream cin ("date.in");
//ofstream cout ("date.out");
cin>>n>>m>>k;
int ok2 = 1; /// cazul cu lant
for (i=2;i<=n;i++){
cin>>x;
L[x].push_back(i);
L[i].push_back(x);
if (x != i-1)
ok2 = 0;
}
int ok = 1; long long sum = 0;
for (i=1;i<=m;i++){
cin>>x;
cin>>v[x]>>cost[x];
if (L[x].size() > 1)
ok = 0;
sum += cost[x];
if (cost[x] != 1)
ok2 = 0;
}
if (ok){
cout<<sum;
return 0;
}
if (ok2){
int el = 0;
for (i=1;i<=n;i++)
if (v[i])
a[++el] = v[i];
reverse (a+1,a+el+1);
int sz = 0;
for (i=1;i<=el;i++){
int st = 1, dr = sz;
while (st <= dr){
int mid = (st+dr)>>1;
if (a[d[mid]] <= a[i])
st = mid+1;
else dr = mid-1;
}
if (st == sz+1)
sz++;
d[st] = i;
}
cout<<sz;
return 0;
}
dfs (1,0);
long long sol = 0;
for (i=1;i<=k;i++)
sol = max (sol,dp[1][i]);
cout<<sol;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
6592 KB |
Output is correct |
2 |
Correct |
69 ms |
5924 KB |
Output is correct |
3 |
Correct |
142 ms |
7052 KB |
Output is correct |
4 |
Correct |
128 ms |
6956 KB |
Output is correct |
5 |
Correct |
142 ms |
6860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
125 ms |
6884 KB |
Output is correct |
5 |
Correct |
126 ms |
7208 KB |
Output is correct |
6 |
Correct |
138 ms |
6840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
30080 KB |
Output is correct |
2 |
Correct |
204 ms |
30084 KB |
Output is correct |
3 |
Correct |
161 ms |
32544 KB |
Output is correct |
4 |
Correct |
131 ms |
30396 KB |
Output is correct |
5 |
Correct |
179 ms |
36232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
151 ms |
30020 KB |
Output is correct |
11 |
Correct |
148 ms |
30128 KB |
Output is correct |
12 |
Correct |
146 ms |
32600 KB |
Output is correct |
13 |
Correct |
110 ms |
30376 KB |
Output is correct |
14 |
Correct |
111 ms |
6840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2090 ms |
8596 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
3 ms |
2636 KB |
Output is correct |
11 |
Correct |
3 ms |
2636 KB |
Output is correct |
12 |
Correct |
3 ms |
2636 KB |
Output is correct |
13 |
Correct |
125 ms |
6884 KB |
Output is correct |
14 |
Correct |
126 ms |
7208 KB |
Output is correct |
15 |
Correct |
138 ms |
6840 KB |
Output is correct |
16 |
Correct |
151 ms |
30020 KB |
Output is correct |
17 |
Correct |
148 ms |
30128 KB |
Output is correct |
18 |
Correct |
146 ms |
32600 KB |
Output is correct |
19 |
Correct |
110 ms |
30376 KB |
Output is correct |
20 |
Correct |
111 ms |
6840 KB |
Output is correct |
21 |
Incorrect |
138 ms |
8124 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2636 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
2 ms |
2636 KB |
Output is correct |
5 |
Correct |
2 ms |
2636 KB |
Output is correct |
6 |
Correct |
2 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
2 ms |
2636 KB |
Output is correct |
9 |
Correct |
2 ms |
2636 KB |
Output is correct |
10 |
Correct |
80 ms |
6592 KB |
Output is correct |
11 |
Correct |
69 ms |
5924 KB |
Output is correct |
12 |
Correct |
142 ms |
7052 KB |
Output is correct |
13 |
Correct |
128 ms |
6956 KB |
Output is correct |
14 |
Correct |
142 ms |
6860 KB |
Output is correct |
15 |
Correct |
3 ms |
2636 KB |
Output is correct |
16 |
Correct |
3 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
125 ms |
6884 KB |
Output is correct |
19 |
Correct |
126 ms |
7208 KB |
Output is correct |
20 |
Correct |
138 ms |
6840 KB |
Output is correct |
21 |
Correct |
167 ms |
30080 KB |
Output is correct |
22 |
Correct |
204 ms |
30084 KB |
Output is correct |
23 |
Correct |
161 ms |
32544 KB |
Output is correct |
24 |
Correct |
131 ms |
30396 KB |
Output is correct |
25 |
Correct |
179 ms |
36232 KB |
Output is correct |
26 |
Correct |
151 ms |
30020 KB |
Output is correct |
27 |
Correct |
148 ms |
30128 KB |
Output is correct |
28 |
Correct |
146 ms |
32600 KB |
Output is correct |
29 |
Correct |
110 ms |
30376 KB |
Output is correct |
30 |
Correct |
111 ms |
6840 KB |
Output is correct |
31 |
Execution timed out |
2090 ms |
8596 KB |
Time limit exceeded |
32 |
Halted |
0 ms |
0 KB |
- |