#include <cmath>
#include <functional>
#include <fstream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <list>
#include <time.h>
#include <math.h>
#include <random>
#include <deque>
#include <cassert>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <stack>
#include <chrono>
#include <cstring>
#include <numeric>
using namespace std;
typedef long long ll;
const int N = 200000 + 7;
const ll INF = (ll)1e18 + 7;
int n;
int par[N];
int h[N];
int c[N];
vector<int> g[N];
int dep[N];
ll dp[N];
ll dp2[N];
ll get(int a) {
ll sol = c[a];
for (auto& b : g[a]) {
sol += dp2[b];
}
sol = min(sol, dp[a]);
return sol;
}
void compute(int a) {
dp[a] = 0;
for (auto& b : g[a]) {
dp[a] += get(b);
}
dp2[a] = get(a);
int b = par[a];
while (b && get(b) < dp2[b]) {
dp2[b] = get(b);
b = par[b];
dp[b] = 0;
for (auto& c : g[b]) {
dp[b] += get(c);
}
}
}
void build1(int a) {
for (auto& b : g[a]) {
dep[b] = 1 + dep[a];
build1(b);
}
}
signed main() {
#ifdef ONPC
FILE* stream;
freopen_s(&stream, "input.txt", "r", stdin);
#else
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#endif
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> par[i] >> h[i] >> c[i];
dp[i] = dp2[i] = INF;
}
assert(par[1] == 1);
for (int i = 2; i <= n; i++) {
assert(1 <= par[i] && par[i] <= i - 1);
g[par[i]].push_back(i);
}
build1(1);
vector<int> ord(n);
iota(ord.begin(), ord.end(), 1);
sort(ord.begin(), ord.end(), [&](int a, int b) {
if (h[a] != h[b]) {
return h[a] > h[b];
}
return dep[a] > dep[b];
});
for (auto& a : ord) {
compute(a);
}
cout << dp2[1] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |