#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 5e5 + 10;
const int M = 1e5 + 10;
int mod = 1e9+7;
const int infi = INT_MAX;
const ll infl = 2e18;
const int P = 505319;
const ld eps = 1e-9;
int mult(int a, int b){
return a * 1LL * b % mod;
}
int sum(int a, int b){
a %= mod;
b %= mod;
if(a + b >= mod)
return a + b - mod;
if(a + b < 0)
return a + b + mod;
return a + b;
}
int binpow(int a, int n){
if (n == 0)
return 1;
if (n % 2 == 1){
return mult(binpow(a, n - 1), a);
}
else{
auto b = binpow(a, n / 2);
return mult(b, b);
}
}
int n, m, h[N], l[N], r[N];
vector<pii> g[N];
void dfs(int v, int w){
if(v > n){
h[v] = 0;
l[v] = w;
r[v] = w;
return;
}
for(auto ee : g[v]){
int u = ee.F, w = ee.S;
dfs(u, w);
h[v] += h[u];
if(l[v] == 0){
l[v] = l[u];
r[v] = r[u];
}else{
if(max(l[v], l[u]) <= min(r[v], r[u])){
l[v] = max(l[v], l[u]);
r[v] = min(r[v], r[u]);
}else if(r[v] < l[u]){
h[v] += (l[u] - r[v]);
l[v] = r[v];
r[v] = l[u];
}else if(r[u] < l[v]){
h[v] += (l[v] - r[u]);
r[v] = l[v];
l[v] = r[u];
}
}
}
l[v] += w;
r[v] += w;
}
void solve(){
cin >> n >> m;
for(int i = 2; i <= n + m; i++){
int p, w;
cin >> p >> w;
g[p].pb({i, w});
}
dfs(1, 0);
cout << h[1] << '\n';
}
signed main(){
mispertion;
int t = 1;
//cin >> t;
while (t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Incorrect |
5 ms |
12124 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
12380 KB |
Output is correct |
2 |
Incorrect |
6 ms |
12124 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Incorrect |
5 ms |
12124 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
12124 KB |
Output is correct |
2 |
Incorrect |
5 ms |
12124 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |