#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,k;
vector<int> adj[100010];
int ripe[100010];
int weight[100010];
int memo[100010][1010];
int dp(int u,int j){
if (memo[u][j] != -1) return memo[u][j];
int s1 = (ripe[u] <= j)?weight[u]:0,s2 = 0;
for (int i = 0; i < adj[u].size(); i++){
int v = adj[u][i];
s1 += dp(v,ripe[u]),s2 += dp(v,j);
}
return memo[u][j] = max(s1,s2);
}
main(){
cin >> n >> m >> k;
for (int i = 2; i <= n; i++){
int p;
cin >> p;
adj[p].push_back(i);
}
vector<int> temp;
temp.push_back(0);
for (int i = 0; i < m; i++){
int v,d,w;
cin >> v >> d >> w;
ripe[v] = d;
weight[v] = w;
temp.push_back(d);
}
sort(temp.begin(),temp.end());
temp.erase(unique(temp.begin(),temp.end()),temp.end());
for (int i = 1; i <= n; i++) ripe[i] = lower_bound(temp.begin(),temp.end(),ripe[i])-temp.begin();
//for (int i = 1; i <= n; i++) cout << ripe[i] << "\n";
for (int i = 1; i <= n; i++) for (int j = 1; j <= temp.size(); j++) memo[i][j] = -1;
cout << dp(1,temp.size());
}
Compilation message
magictree.cpp: In function 'long long int dp(long long int, long long int)':
magictree.cpp:12:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
magictree.cpp: At global scope:
magictree.cpp:18:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
magictree.cpp: In function 'int main()':
magictree.cpp:38:52: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i <= n; i++) for (int j = 1; j <= temp.size(); j++) memo[i][j] = -1;
~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Incorrect |
6 ms |
2816 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2129 ms |
762228 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
9856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
374 ms |
412204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Incorrect |
6 ms |
2816 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
161656 KB |
Output is correct |
2 |
Correct |
472 ms |
797408 KB |
Output is correct |
3 |
Correct |
456 ms |
797304 KB |
Output is correct |
4 |
Correct |
473 ms |
797432 KB |
Output is correct |
5 |
Correct |
426 ms |
796008 KB |
Output is correct |
6 |
Incorrect |
1189 ms |
798840 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Incorrect |
6 ms |
2816 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2816 KB |
Output is correct |
3 |
Correct |
7 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Incorrect |
6 ms |
2816 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |