# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1113088 | guagua0407 | Fireworks (APIO16_fireworks) | C++17 | 203 ms | 89824 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=6e5+5;
vector<int> adj[mxn];
priority_queue<ll> pq[mxn];
ll c[mxn];
ll val[mxn];
int n,m;
void dfs(int v,int p=0){
if(v>n){
pq[v].push(c[v]);
pq[v].push(c[v]);
return;
}
ll sum=0;
for(auto u:adj[v]){
dfs(u,v);
val[v]+=val[u];
sum+=pq[u].top();
if(pq[v].size()<pq[u].size()) swap(pq[v],pq[u]);
while(!pq[u].empty()){
pq[v].push(pq[u].top());
pq[u].pop();
}
}
int sz=(int)adj[v].size();
ll mx=pq[v].top();
val[v]+=mx*sz-sum;
for(int i=sz-1;i>=1;i--){
ll r=pq[v].top();
pq[v].pop();
val[v]-=1ll*(r-pq[v].top())*i;
}
ll a=pq[v].top();
pq[v].pop();
ll b=pq[v].top();
pq[v].pop();
pq[v].push(a+c[v]);
pq[v].push(b+c[v]);
}
signed main() {_
cin>>n>>m;
for(int i=2;i<=n+m;i++){
int p;
cin>>p>>c[i];
adj[p].push_back(i);
}
dfs(1);
cout<<val[1]<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//diaoborz
컴파일 시 표준 에러 (stderr) 메시지
# | 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... |