#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = 1; i <= n; ++i)
#define ff first
#define ss second
using namespace std;
typedef long long ll;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ", ";
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? ", " : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifdef LOCAL
#define dbg(x...) \
cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \
_print(x); \
cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
const int MAXN = 300100;
int par[MAXN];
vector<int> child[MAXN];
ll cost[MAXN];
vector<ll> fuck[MAXN];
ll dp[MAXN];
void dfs(int v){
if(child[v].empty()){
dp[v] = 0; fuck[v].pb(cost[v]); fuck[v].pb(cost[v]); return;
}
vector<ll> shit;
ll tmp = 0;
for(auto ne : child[v]){
dfs(ne);
dp[v] += dp[ne];
for(auto x : fuck[ne]) shit.pb(x);
tmp -= abs(fuck[ne][0]-fuck[ne][1]);
}
sort(all(shit));
ll mid = shit[shit.size()/2];
for(auto x : shit) tmp += abs(x-mid);
dbg(tmp)
dp[v] += tmp/2;
fuck[v].pb(mid);
fuck[v].pb(shit[shit.size()/2-1]);
dbg(v,shit,fuck[v],dp[v])
}
void solve(){
int n,m; cin >> n >> m;
n += m;
for(int i=2; i<=n+m; i++){
ll x; cin >> par[i] >> x;
cost[i] = cost[par[i]] + x;
child[par[i]].pb(i);
}
dfs(1);
cout << dp[1];
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
9 ms |
14428 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
7 ms |
14424 KB |
Output is correct |
5 |
Correct |
8 ms |
14424 KB |
Output is correct |
6 |
Correct |
10 ms |
14428 KB |
Output is correct |
7 |
Correct |
10 ms |
14532 KB |
Output is correct |
8 |
Correct |
8 ms |
14424 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
8 ms |
14428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
14428 KB |
Output is correct |
2 |
Correct |
9 ms |
14428 KB |
Output is correct |
3 |
Incorrect |
8 ms |
14568 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
9 ms |
14428 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
7 ms |
14424 KB |
Output is correct |
5 |
Correct |
8 ms |
14424 KB |
Output is correct |
6 |
Correct |
10 ms |
14428 KB |
Output is correct |
7 |
Correct |
10 ms |
14532 KB |
Output is correct |
8 |
Correct |
8 ms |
14424 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
8 ms |
14428 KB |
Output is correct |
11 |
Correct |
11 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14428 KB |
Output is correct |
13 |
Incorrect |
8 ms |
14568 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14428 KB |
Output is correct |
2 |
Correct |
9 ms |
14428 KB |
Output is correct |
3 |
Correct |
8 ms |
14424 KB |
Output is correct |
4 |
Correct |
7 ms |
14424 KB |
Output is correct |
5 |
Correct |
8 ms |
14424 KB |
Output is correct |
6 |
Correct |
10 ms |
14428 KB |
Output is correct |
7 |
Correct |
10 ms |
14532 KB |
Output is correct |
8 |
Correct |
8 ms |
14424 KB |
Output is correct |
9 |
Correct |
7 ms |
14428 KB |
Output is correct |
10 |
Correct |
8 ms |
14428 KB |
Output is correct |
11 |
Correct |
11 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14428 KB |
Output is correct |
13 |
Incorrect |
8 ms |
14568 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |