#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
struct fenwick{
vector<int> f;
fenwick(int n){
f.resize(n + 2);
};
void add(int i, int x){
i++;
while (i < (int)f.size()){
f[i] = max(f[i], x);
i += (-i) & i;
}
}
int get(int i){
int res = 0;
while (i > 0){
res = max(res, f[i]);
i -= (-i) & i;
}
return res;
}
};
vector<int> g[N];
vector<vector<ll>> dp;
pair<int, int> fruit[N];
void dfs(int v){
dp[v][fruit[v].first] = fruit[v].second;
for (int u : g[v]){
dfs(u);
}
for (int t = 1; t < dp[v].size(); t++){
for (int u : g[v]){
dp[v][t] += dp[u][t];
}
}
for (int t = 1; t < dp[v].size(); t++) dp[v][t] = max(dp[v][t], dp[v][t - 1]);
}
int LIS(const vector<int> &a){
int n = (int) a.size();
fenwick f(n);
int res = 0;
for (int i = 0; i < n; i++){
int _dp = f.get(a[i]) + 1;
f.add(a[i], _dp);
res = max(res, _dp);
}
return res;
}
void solve(){
int n, m, k;
cin >> n >> m >> k;
ll sum = 0;
bool path = true;
for (int i = 1; i < n; i++){
int p;
cin >> p;
--p;
if (p != i - 1) path = false;
g[p].push_back(i);
}
map<int, int> mp;
vector<int> a(n);
for (int i = 0; i < m; i++){
int v, d, w;
cin >> v >> d >> w;
--v;
mp[d];
fruit[v] = MP(d, w);
sum += w;
a[v] = d;
}
if (path){
cout << LIS(a) << endl;
return;
}
mp[0];
k = 0;
for (auto &now : mp){
now.second = ++k;
}
for (int i = 0; i < n; i++){
fruit[i].first = mp[fruit[i].first];
}
if (k > 1000){
cout << sum << endl;
return;
}
dp.assign(n, vector<ll>(k + 1, 0));
dfs(0);
cout << dp[0].back() << endl;
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
Compilation message
magictree.cpp: In function 'void dfs(int)':
magictree.cpp:44:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int t = 1; t < dp[v].size(); t++){
| ~~^~~~~~~~~~~~~~
magictree.cpp:50:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int t = 1; t < dp[v].size(); t++) dp[v][t] = max(dp[v][t], dp[v][t - 1]);
| ~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
6308 KB |
Output is correct |
2 |
Correct |
29 ms |
7164 KB |
Output is correct |
3 |
Correct |
82 ms |
7240 KB |
Output is correct |
4 |
Correct |
82 ms |
7032 KB |
Output is correct |
5 |
Correct |
77 ms |
7244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
12512 KB |
Output is correct |
2 |
Correct |
59 ms |
10956 KB |
Output is correct |
3 |
Correct |
53 ms |
15204 KB |
Output is correct |
4 |
Correct |
37 ms |
11212 KB |
Output is correct |
5 |
Incorrect |
39 ms |
7372 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
147 ms |
128140 KB |
Output is correct |
2 |
Correct |
665 ms |
607480 KB |
Output is correct |
3 |
Correct |
677 ms |
610600 KB |
Output is correct |
4 |
Correct |
859 ms |
789084 KB |
Output is correct |
5 |
Correct |
1655 ms |
787856 KB |
Output is correct |
6 |
Correct |
854 ms |
789760 KB |
Output is correct |
7 |
Correct |
838 ms |
791908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |