This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef string str;
typedef double db;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
const ll MOD = 1e9 + 7; //998244353
const ll INF = 1e18;
const ll MX = 1e9 + 10;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up
#ifndef LOCAL
#define cerr if(false) cerr
#endif
#define debug(x) cerr << #x << " : " << x << endl;
#define debugs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define debugv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;
void IO() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void distance_tree(vector<pair<ll, ll>> adj[], ll u, ll e, vector<ll> &dist)
{
for(auto v: adj[u])
{
if(v.fi!=e)
{
dist[v.fi] = dist[u] + v.se;
distance_tree(adj, v.fi, u, dist);
}
}
}
int main()
{
boost;
IO();
ll n, m;
cin >> n >> m;
vector<pair<ll, ll>> adj[n+m+1];
ll p, c;
FOR(i, 2, n+m+1)
{
cin >> p >> c;
adj[p].pb(mp(i, c));
adj[i].pb(mp(p, c));
}
vector<ll> dist(n+m+1, 0);
distance_tree(adj, 1, -1, dist);
vector<ll> v;
FOR(i, n+1, n+m+1)
{
v.pb(dist[i]);
}
sort(all(v));
ll mediane = 0;
ll len = v.size();
if(len%2==1)
mediane = v[len/2];
else
mediane = (v[len/2]+v[(len-2)/2])/2;
ll res = 0;
FOR(i, 0, len)
res = res + abs(mediane - v[i]);
cout << res << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |