#include <bits/stdc++.h>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
//tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define ll long long
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define o cout<<"BUG"<<endl;
#define IOS ios_base::sync_with_stdio(0);
#define en "\n"
#define FOR(i, j, n) for(int j = i; j < n; ++j)
#define forn(i, j, n) for(int j = i; j <= n; ++j)
#define nfor(i, j, n) for(int j = n; j >= i; --j)
#define sortv(vv) sort(vv.begin(), vv.end())
#define all(v) v.begin(), v.end()
#define ld long double
#define ull unsigned long long
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using namespace std;
const ll maxn=3e5+100,inf=1e9,LOG=23,mod=1e9+7;
ll block = 500, timer = 0;
const ld EPS = 1e-7;
#define bt(i) (1 << (i))
#define int ll
#define y1 yy
//#define double long double
#define pii pair <int, int>
int n, m, a[maxn], b[maxn], c[maxn];
multiset <int> st[maxn];
vector <int> edge[maxn];
void dfs(int v)
{
if(edge[v].size() == 0)
{
st[v].insert(c[v]);
st[v].insert(c[v]);
a[v] = 1;
b[v] = -c[v];
/*cout << "V " << v << " - ";
for(auto i : st[v])
{
cout << i << " ";
}
cout << " " << a[v] << " " << b[v] << endl;*/
return;
}
for(auto to : edge[v])
{
dfs(to);
if(st[to].size() > st[v].size()) swap(st[v], st[to]);
while(st[to].size())
{
st[v].insert(*st[to].begin());
st[to].erase(st[to].begin());
}
a[v] += a[to];
b[v] += b[to];
}
while(a[v] > 1)
{
auto it = st[v].end();
it--;
b[v] += *it;
st[v].erase(it);
a[v]--;
}
auto it = st[v].end();
it--;
int c1 = *it;
it--;
int c2 = *it;
st[v].erase(st[v].find(c1));
st[v].insert(c1 + c[v]);
st[v].erase(st[v].find(c2));
st[v].insert(c2 + c[v]);
b[v] -= c[v];
/*cout << "V " << v << " - ";
for(auto i : st[v])
{
cout << i << " ";
}
cout << " " << a[v] << " " << b[v] << endl;*/
}
main()
{
IOS
cin >> n >> m;
n += m;
forn(2, i, n)
{
int p;
cin >> p >> c[i];
edge[p].pb(i);
}
dfs(1);
while(a[1] > 0)
{
auto it = st[1].end();
it--;
a[1]--;
b[1] += *it;
st[1].erase(it);
}
cout << b[1] + a[1] * (st[1].size() ? (*st[1].rbegin()) : 0);
}
Compilation message
fireworks.cpp:9:0: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(linker, "/stack:200000000")
fireworks.cpp:99:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main()
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
16 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
19 ms |
21536 KB |
Output is correct |
3 |
Correct |
16 ms |
21504 KB |
Output is correct |
4 |
Correct |
16 ms |
21504 KB |
Output is correct |
5 |
Correct |
16 ms |
21504 KB |
Output is correct |
6 |
Correct |
16 ms |
21504 KB |
Output is correct |
7 |
Correct |
18 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
17 ms |
21504 KB |
Output is correct |
11 |
Correct |
16 ms |
21504 KB |
Output is correct |
12 |
Correct |
23 ms |
21504 KB |
Output is correct |
13 |
Correct |
16 ms |
21504 KB |
Output is correct |
14 |
Correct |
16 ms |
21504 KB |
Output is correct |
15 |
Correct |
16 ms |
21504 KB |
Output is correct |
16 |
Correct |
18 ms |
21504 KB |
Output is correct |
17 |
Correct |
16 ms |
21504 KB |
Output is correct |
18 |
Correct |
18 ms |
21504 KB |
Output is correct |
19 |
Correct |
16 ms |
21504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
16 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
11 |
Correct |
16 ms |
21504 KB |
Output is correct |
12 |
Correct |
19 ms |
21536 KB |
Output is correct |
13 |
Correct |
16 ms |
21504 KB |
Output is correct |
14 |
Correct |
16 ms |
21504 KB |
Output is correct |
15 |
Correct |
16 ms |
21504 KB |
Output is correct |
16 |
Correct |
16 ms |
21504 KB |
Output is correct |
17 |
Correct |
18 ms |
21504 KB |
Output is correct |
18 |
Correct |
16 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 ms |
21504 KB |
Output is correct |
21 |
Correct |
16 ms |
21504 KB |
Output is correct |
22 |
Correct |
23 ms |
21504 KB |
Output is correct |
23 |
Correct |
16 ms |
21504 KB |
Output is correct |
24 |
Correct |
16 ms |
21504 KB |
Output is correct |
25 |
Correct |
16 ms |
21504 KB |
Output is correct |
26 |
Correct |
18 ms |
21504 KB |
Output is correct |
27 |
Correct |
16 ms |
21504 KB |
Output is correct |
28 |
Correct |
18 ms |
21504 KB |
Output is correct |
29 |
Correct |
16 ms |
21504 KB |
Output is correct |
30 |
Correct |
16 ms |
21504 KB |
Output is correct |
31 |
Correct |
16 ms |
21632 KB |
Output is correct |
32 |
Correct |
18 ms |
21632 KB |
Output is correct |
33 |
Correct |
18 ms |
21632 KB |
Output is correct |
34 |
Correct |
18 ms |
21632 KB |
Output is correct |
35 |
Correct |
21 ms |
21760 KB |
Output is correct |
36 |
Correct |
19 ms |
21760 KB |
Output is correct |
37 |
Correct |
21 ms |
21760 KB |
Output is correct |
38 |
Correct |
20 ms |
21760 KB |
Output is correct |
39 |
Correct |
25 ms |
21928 KB |
Output is correct |
40 |
Correct |
19 ms |
22272 KB |
Output is correct |
41 |
Correct |
18 ms |
22400 KB |
Output is correct |
42 |
Correct |
21 ms |
21760 KB |
Output is correct |
43 |
Correct |
19 ms |
22272 KB |
Output is correct |
44 |
Correct |
19 ms |
22016 KB |
Output is correct |
45 |
Correct |
24 ms |
22016 KB |
Output is correct |
46 |
Correct |
22 ms |
21888 KB |
Output is correct |
47 |
Correct |
20 ms |
22016 KB |
Output is correct |
48 |
Correct |
20 ms |
21888 KB |
Output is correct |
49 |
Correct |
21 ms |
21888 KB |
Output is correct |
50 |
Correct |
19 ms |
21888 KB |
Output is correct |
51 |
Correct |
19 ms |
21888 KB |
Output is correct |
52 |
Correct |
21 ms |
21888 KB |
Output is correct |
53 |
Correct |
30 ms |
21888 KB |
Output is correct |
54 |
Correct |
20 ms |
22144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21504 KB |
Output is correct |
2 |
Correct |
17 ms |
21504 KB |
Output is correct |
3 |
Correct |
17 ms |
21504 KB |
Output is correct |
4 |
Correct |
16 ms |
21504 KB |
Output is correct |
5 |
Correct |
17 ms |
21504 KB |
Output is correct |
6 |
Correct |
17 ms |
21504 KB |
Output is correct |
7 |
Correct |
16 ms |
21504 KB |
Output is correct |
8 |
Correct |
16 ms |
21504 KB |
Output is correct |
9 |
Correct |
17 ms |
21504 KB |
Output is correct |
10 |
Correct |
16 ms |
21504 KB |
Output is correct |
11 |
Correct |
16 ms |
21504 KB |
Output is correct |
12 |
Correct |
19 ms |
21536 KB |
Output is correct |
13 |
Correct |
16 ms |
21504 KB |
Output is correct |
14 |
Correct |
16 ms |
21504 KB |
Output is correct |
15 |
Correct |
16 ms |
21504 KB |
Output is correct |
16 |
Correct |
16 ms |
21504 KB |
Output is correct |
17 |
Correct |
18 ms |
21504 KB |
Output is correct |
18 |
Correct |
16 ms |
21504 KB |
Output is correct |
19 |
Correct |
17 ms |
21504 KB |
Output is correct |
20 |
Correct |
17 ms |
21504 KB |
Output is correct |
21 |
Correct |
16 ms |
21504 KB |
Output is correct |
22 |
Correct |
23 ms |
21504 KB |
Output is correct |
23 |
Correct |
16 ms |
21504 KB |
Output is correct |
24 |
Correct |
16 ms |
21504 KB |
Output is correct |
25 |
Correct |
16 ms |
21504 KB |
Output is correct |
26 |
Correct |
18 ms |
21504 KB |
Output is correct |
27 |
Correct |
16 ms |
21504 KB |
Output is correct |
28 |
Correct |
18 ms |
21504 KB |
Output is correct |
29 |
Correct |
16 ms |
21504 KB |
Output is correct |
30 |
Correct |
16 ms |
21504 KB |
Output is correct |
31 |
Correct |
16 ms |
21632 KB |
Output is correct |
32 |
Correct |
18 ms |
21632 KB |
Output is correct |
33 |
Correct |
18 ms |
21632 KB |
Output is correct |
34 |
Correct |
18 ms |
21632 KB |
Output is correct |
35 |
Correct |
21 ms |
21760 KB |
Output is correct |
36 |
Correct |
19 ms |
21760 KB |
Output is correct |
37 |
Correct |
21 ms |
21760 KB |
Output is correct |
38 |
Correct |
20 ms |
21760 KB |
Output is correct |
39 |
Correct |
25 ms |
21928 KB |
Output is correct |
40 |
Correct |
19 ms |
22272 KB |
Output is correct |
41 |
Correct |
18 ms |
22400 KB |
Output is correct |
42 |
Correct |
21 ms |
21760 KB |
Output is correct |
43 |
Correct |
19 ms |
22272 KB |
Output is correct |
44 |
Correct |
19 ms |
22016 KB |
Output is correct |
45 |
Correct |
24 ms |
22016 KB |
Output is correct |
46 |
Correct |
22 ms |
21888 KB |
Output is correct |
47 |
Correct |
20 ms |
22016 KB |
Output is correct |
48 |
Correct |
20 ms |
21888 KB |
Output is correct |
49 |
Correct |
21 ms |
21888 KB |
Output is correct |
50 |
Correct |
19 ms |
21888 KB |
Output is correct |
51 |
Correct |
19 ms |
21888 KB |
Output is correct |
52 |
Correct |
21 ms |
21888 KB |
Output is correct |
53 |
Correct |
30 ms |
21888 KB |
Output is correct |
54 |
Correct |
20 ms |
22144 KB |
Output is correct |
55 |
Correct |
31 ms |
22272 KB |
Output is correct |
56 |
Correct |
66 ms |
24568 KB |
Output is correct |
57 |
Correct |
109 ms |
26744 KB |
Output is correct |
58 |
Correct |
158 ms |
28228 KB |
Output is correct |
59 |
Correct |
196 ms |
30584 KB |
Output is correct |
60 |
Correct |
268 ms |
32776 KB |
Output is correct |
61 |
Correct |
311 ms |
34192 KB |
Output is correct |
62 |
Correct |
322 ms |
35560 KB |
Output is correct |
63 |
Correct |
374 ms |
38056 KB |
Output is correct |
64 |
Correct |
410 ms |
39028 KB |
Output is correct |
65 |
Correct |
180 ms |
70904 KB |
Output is correct |
66 |
Correct |
180 ms |
70824 KB |
Output is correct |
67 |
Correct |
201 ms |
38008 KB |
Output is correct |
68 |
Correct |
262 ms |
56824 KB |
Output is correct |
69 |
Correct |
351 ms |
54904 KB |
Output is correct |
70 |
Correct |
333 ms |
54880 KB |
Output is correct |
71 |
Correct |
383 ms |
45944 KB |
Output is correct |
72 |
Correct |
447 ms |
45944 KB |
Output is correct |
73 |
Correct |
365 ms |
45896 KB |
Output is correct |
74 |
Correct |
357 ms |
45912 KB |
Output is correct |
75 |
Correct |
396 ms |
45372 KB |
Output is correct |
76 |
Correct |
365 ms |
45560 KB |
Output is correct |
77 |
Correct |
376 ms |
45044 KB |
Output is correct |
78 |
Correct |
410 ms |
45932 KB |
Output is correct |
79 |
Correct |
584 ms |
59104 KB |
Output is correct |