This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(x) (int)(x.size())
const int mxn = 3e5 + 5;
inline char gc()
{
const static int SZ = 1 << 16;
static char buf[SZ], *p1, *p2;
if (p1 == p2 && (p2 = buf + fread(p1 = buf, 1, SZ, stdin), p1 == p2))
return -1;
return *p1++;
}
inline void rd() {}
template <typename T, typename... U>
inline void rd(T &x, U &...y)
{
x = 0;
bool f = 0;
char c = gc();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
f && (x = -x), rd(y...);
}
template <typename T>
inline void prt(T x)
{
if (x > 9)prt(x / 10);
putchar((x % 10) ^ 48);
}
int n, m, l[mxn], p[mxn], deg[mxn];
priority_queue<int>pq[mxn];
inline priority_queue<long long> dfs(int v){
for(int i = 1; i < deg[v]; i++){
pq[v].pop();
}
if(v > n){
pq[v].push(l[v]), pq[v].push(l[v]);
}
else{
long long R = pq[v].top(); pq[v].pop();
long long L = pq[v].top(); pq[v].pop();
pq[v].push(l[v] + L), pq[v].push(l[v] + R);
}
if(sz(pq[p[v]]) < sz(pq[v])) swap(pq[p[v]], pq[v]);
while(sz(pq[v])){
pq[p[v]].push(pq[v].top());
pq[v].pop();
}
}
inline void solve(){
rd(n), rd(m);
long long sum = 0;
int a;
for(int i = 2; i <= n + m; i++){
rd(p[i]), rd(l[i]);
deg[p[i]]++;
sum += l[i];
}
for(int v = n + m; v > 1; v--){
for(int i = 1; i < deg[v]; i++){
pq[v].pop();
}
if(v > n){
pq[v].push(l[v]), pq[v].push(l[v]);
}
else{
long long R = pq[v].top(); pq[v].pop();
long long L = pq[v].top(); pq[v].pop();
pq[v].push(l[v] + L), pq[v].push(l[v] + R);
}
if(sz(pq[p[v]]) < sz(pq[v])) swap(pq[p[v]], pq[v]);
while(sz(pq[v])){
pq[p[v]].push(pq[v].top());
pq[v].pop();
}
}
pq[1].push(0);
sum += pq[1].top();
while(sz(pq[1]) > 1){
sum -= pq[1].top();
pq[1].pop();
}
prt(sum);
}
signed main(){
solve();
}
Compilation message (stderr)
fireworks.cpp: In function 'std::priority_queue<long long int> dfs(int)':
fireworks.cpp:51:1: warning: no return statement in function returning non-void [-Wreturn-type]
51 | }
| ^
fireworks.cpp: In function 'void solve()':
fireworks.cpp:55:9: warning: unused variable 'a' [-Wunused-variable]
55 | int a;
| ^
# | 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... |