#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define ndl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using indexed_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());//uniform_int_distribution<int> (l, r)
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<int, ull> piu;
typedef vector<vector<int>> matrix;
const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 3e5 + 123;
const int M = 1e6 + 123;
const double pi = 3.14159265359;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
struct node{
ll a = 0, b = 0;
priority_queue<ll> q;
node operator+(node &a)const{
node res = *this;
res.a += a.a;
res.b += a.b;
if (sz(res.q) < sz(a.q)){
swap(res.q, a.q);
}
while (!a.q.empty()){
res.q.push(a.q.top());
a.q.pop();
}
return res;
}
}dp[N];
vector<int> g[N];
int c[N];
void dfs(int v){
if (g[v].empty()){
dp[v].a = -1;
dp[v].b = c[v];
dp[v].q.push(c[v]);
dp[v].q.push(c[v]);
return;
}
for (int to : g[v]){
dfs(to);
dp[v] = dp[v] + dp[to];
}
while (dp[v].a + sz(dp[v].q) > 1){
dp[v].q.pop();
}
ll x1 = dp[v].q.top();
dp[v].q.pop();
ll x2 = dp[v].q.top();
dp[v].q.pop();
dp[v].b += c[v];
dp[v].q.push(x1+c[v]);
dp[v].q.push(x2+c[v]);
}
void solve(){
int n, m;
cin >> n >> m;
for (int i = 2, p; i <= n+m; i++){
cin >> p >> c[i];
g[p].pb(i);
}
dfs(1);
vector<ll> v;
while (!dp[1].q.empty()) {
v.push_back(dp[1].q.top());
dp[1].q.pop();
}
v.pb(0);
for (int i = sz(v)-2; dp[1].a < 0; i--, dp[1].a++){
dp[1].b += (v[i] - v[i+1]) * dp[1].a;
}
cout << dp[1].b;
}
int main(){
#ifdef me
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++){
//cout << "Case #" << i << ": ";
solve();
}
#ifdef KAZAKH
// cout << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl;
#endif
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21472 KB |
Output is correct |
2 |
Correct |
11 ms |
21468 KB |
Output is correct |
3 |
Correct |
12 ms |
21416 KB |
Output is correct |
4 |
Correct |
12 ms |
21516 KB |
Output is correct |
5 |
Correct |
12 ms |
21484 KB |
Output is correct |
6 |
Correct |
12 ms |
21464 KB |
Output is correct |
7 |
Correct |
12 ms |
21456 KB |
Output is correct |
8 |
Correct |
12 ms |
21452 KB |
Output is correct |
9 |
Correct |
12 ms |
21452 KB |
Output is correct |
10 |
Correct |
12 ms |
21464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
21372 KB |
Output is correct |
2 |
Correct |
12 ms |
21436 KB |
Output is correct |
3 |
Correct |
12 ms |
21452 KB |
Output is correct |
4 |
Correct |
12 ms |
21452 KB |
Output is correct |
5 |
Correct |
14 ms |
21580 KB |
Output is correct |
6 |
Correct |
12 ms |
21452 KB |
Output is correct |
7 |
Correct |
11 ms |
21420 KB |
Output is correct |
8 |
Correct |
12 ms |
21452 KB |
Output is correct |
9 |
Correct |
12 ms |
21500 KB |
Output is correct |
10 |
Correct |
12 ms |
21380 KB |
Output is correct |
11 |
Correct |
12 ms |
21380 KB |
Output is correct |
12 |
Correct |
12 ms |
21392 KB |
Output is correct |
13 |
Correct |
12 ms |
21452 KB |
Output is correct |
14 |
Correct |
12 ms |
21456 KB |
Output is correct |
15 |
Correct |
12 ms |
21396 KB |
Output is correct |
16 |
Correct |
12 ms |
21452 KB |
Output is correct |
17 |
Correct |
12 ms |
21412 KB |
Output is correct |
18 |
Correct |
12 ms |
21452 KB |
Output is correct |
19 |
Correct |
12 ms |
21464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21472 KB |
Output is correct |
2 |
Correct |
11 ms |
21468 KB |
Output is correct |
3 |
Correct |
12 ms |
21416 KB |
Output is correct |
4 |
Correct |
12 ms |
21516 KB |
Output is correct |
5 |
Correct |
12 ms |
21484 KB |
Output is correct |
6 |
Correct |
12 ms |
21464 KB |
Output is correct |
7 |
Correct |
12 ms |
21456 KB |
Output is correct |
8 |
Correct |
12 ms |
21452 KB |
Output is correct |
9 |
Correct |
12 ms |
21452 KB |
Output is correct |
10 |
Correct |
12 ms |
21464 KB |
Output is correct |
11 |
Correct |
11 ms |
21372 KB |
Output is correct |
12 |
Correct |
12 ms |
21436 KB |
Output is correct |
13 |
Correct |
12 ms |
21452 KB |
Output is correct |
14 |
Correct |
12 ms |
21452 KB |
Output is correct |
15 |
Correct |
14 ms |
21580 KB |
Output is correct |
16 |
Correct |
12 ms |
21452 KB |
Output is correct |
17 |
Correct |
11 ms |
21420 KB |
Output is correct |
18 |
Correct |
12 ms |
21452 KB |
Output is correct |
19 |
Correct |
12 ms |
21500 KB |
Output is correct |
20 |
Correct |
12 ms |
21380 KB |
Output is correct |
21 |
Correct |
12 ms |
21380 KB |
Output is correct |
22 |
Correct |
12 ms |
21392 KB |
Output is correct |
23 |
Correct |
12 ms |
21452 KB |
Output is correct |
24 |
Correct |
12 ms |
21456 KB |
Output is correct |
25 |
Correct |
12 ms |
21396 KB |
Output is correct |
26 |
Correct |
12 ms |
21452 KB |
Output is correct |
27 |
Correct |
12 ms |
21412 KB |
Output is correct |
28 |
Correct |
12 ms |
21452 KB |
Output is correct |
29 |
Correct |
12 ms |
21464 KB |
Output is correct |
30 |
Correct |
14 ms |
21528 KB |
Output is correct |
31 |
Correct |
12 ms |
21580 KB |
Output is correct |
32 |
Correct |
13 ms |
21628 KB |
Output is correct |
33 |
Correct |
13 ms |
21708 KB |
Output is correct |
34 |
Correct |
14 ms |
21732 KB |
Output is correct |
35 |
Correct |
13 ms |
21700 KB |
Output is correct |
36 |
Correct |
14 ms |
21864 KB |
Output is correct |
37 |
Correct |
15 ms |
21876 KB |
Output is correct |
38 |
Correct |
16 ms |
21852 KB |
Output is correct |
39 |
Correct |
15 ms |
21864 KB |
Output is correct |
40 |
Correct |
14 ms |
22732 KB |
Output is correct |
41 |
Correct |
16 ms |
22732 KB |
Output is correct |
42 |
Correct |
14 ms |
21680 KB |
Output is correct |
43 |
Correct |
17 ms |
22344 KB |
Output is correct |
44 |
Correct |
18 ms |
22220 KB |
Output is correct |
45 |
Correct |
17 ms |
22220 KB |
Output is correct |
46 |
Correct |
17 ms |
22092 KB |
Output is correct |
47 |
Correct |
17 ms |
22052 KB |
Output is correct |
48 |
Correct |
21 ms |
22056 KB |
Output is correct |
49 |
Correct |
22 ms |
22152 KB |
Output is correct |
50 |
Correct |
25 ms |
21968 KB |
Output is correct |
51 |
Correct |
26 ms |
21944 KB |
Output is correct |
52 |
Correct |
23 ms |
22076 KB |
Output is correct |
53 |
Correct |
20 ms |
21952 KB |
Output is correct |
54 |
Correct |
39 ms |
22188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
21472 KB |
Output is correct |
2 |
Correct |
11 ms |
21468 KB |
Output is correct |
3 |
Correct |
12 ms |
21416 KB |
Output is correct |
4 |
Correct |
12 ms |
21516 KB |
Output is correct |
5 |
Correct |
12 ms |
21484 KB |
Output is correct |
6 |
Correct |
12 ms |
21464 KB |
Output is correct |
7 |
Correct |
12 ms |
21456 KB |
Output is correct |
8 |
Correct |
12 ms |
21452 KB |
Output is correct |
9 |
Correct |
12 ms |
21452 KB |
Output is correct |
10 |
Correct |
12 ms |
21464 KB |
Output is correct |
11 |
Correct |
11 ms |
21372 KB |
Output is correct |
12 |
Correct |
12 ms |
21436 KB |
Output is correct |
13 |
Correct |
12 ms |
21452 KB |
Output is correct |
14 |
Correct |
12 ms |
21452 KB |
Output is correct |
15 |
Correct |
14 ms |
21580 KB |
Output is correct |
16 |
Correct |
12 ms |
21452 KB |
Output is correct |
17 |
Correct |
11 ms |
21420 KB |
Output is correct |
18 |
Correct |
12 ms |
21452 KB |
Output is correct |
19 |
Correct |
12 ms |
21500 KB |
Output is correct |
20 |
Correct |
12 ms |
21380 KB |
Output is correct |
21 |
Correct |
12 ms |
21380 KB |
Output is correct |
22 |
Correct |
12 ms |
21392 KB |
Output is correct |
23 |
Correct |
12 ms |
21452 KB |
Output is correct |
24 |
Correct |
12 ms |
21456 KB |
Output is correct |
25 |
Correct |
12 ms |
21396 KB |
Output is correct |
26 |
Correct |
12 ms |
21452 KB |
Output is correct |
27 |
Correct |
12 ms |
21412 KB |
Output is correct |
28 |
Correct |
12 ms |
21452 KB |
Output is correct |
29 |
Correct |
12 ms |
21464 KB |
Output is correct |
30 |
Correct |
14 ms |
21528 KB |
Output is correct |
31 |
Correct |
12 ms |
21580 KB |
Output is correct |
32 |
Correct |
13 ms |
21628 KB |
Output is correct |
33 |
Correct |
13 ms |
21708 KB |
Output is correct |
34 |
Correct |
14 ms |
21732 KB |
Output is correct |
35 |
Correct |
13 ms |
21700 KB |
Output is correct |
36 |
Correct |
14 ms |
21864 KB |
Output is correct |
37 |
Correct |
15 ms |
21876 KB |
Output is correct |
38 |
Correct |
16 ms |
21852 KB |
Output is correct |
39 |
Correct |
15 ms |
21864 KB |
Output is correct |
40 |
Correct |
14 ms |
22732 KB |
Output is correct |
41 |
Correct |
16 ms |
22732 KB |
Output is correct |
42 |
Correct |
14 ms |
21680 KB |
Output is correct |
43 |
Correct |
17 ms |
22344 KB |
Output is correct |
44 |
Correct |
18 ms |
22220 KB |
Output is correct |
45 |
Correct |
17 ms |
22220 KB |
Output is correct |
46 |
Correct |
17 ms |
22092 KB |
Output is correct |
47 |
Correct |
17 ms |
22052 KB |
Output is correct |
48 |
Correct |
21 ms |
22056 KB |
Output is correct |
49 |
Correct |
22 ms |
22152 KB |
Output is correct |
50 |
Correct |
25 ms |
21968 KB |
Output is correct |
51 |
Correct |
26 ms |
21944 KB |
Output is correct |
52 |
Correct |
23 ms |
22076 KB |
Output is correct |
53 |
Correct |
20 ms |
21952 KB |
Output is correct |
54 |
Correct |
39 ms |
22188 KB |
Output is correct |
55 |
Correct |
21 ms |
22492 KB |
Output is correct |
56 |
Correct |
59 ms |
25652 KB |
Output is correct |
57 |
Correct |
99 ms |
28448 KB |
Output is correct |
58 |
Correct |
139 ms |
30840 KB |
Output is correct |
59 |
Correct |
197 ms |
33448 KB |
Output is correct |
60 |
Correct |
236 ms |
36060 KB |
Output is correct |
61 |
Correct |
268 ms |
38188 KB |
Output is correct |
62 |
Correct |
313 ms |
40172 KB |
Output is correct |
63 |
Correct |
321 ms |
43240 KB |
Output is correct |
64 |
Correct |
418 ms |
44852 KB |
Output is correct |
65 |
Correct |
151 ms |
97860 KB |
Output is correct |
66 |
Correct |
150 ms |
97880 KB |
Output is correct |
67 |
Correct |
144 ms |
37088 KB |
Output is correct |
68 |
Execution timed out |
2091 ms |
67860 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |