이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// And you curse yourself for things you never done
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 3e5 + 10, mod = 1e9 + 7;
const ll inf = 1e18 + 10;
vector<pii> v[maxn];
int n, m;
multiset<ll> peb[maxn];
int infs[maxn];
void dfs(int u, int S = 0){
if(u >= n){
peb[u].insert(S);
peb[u].insert(S);
return;
}
for(auto [y, x] : v[u]){
dfs(y, x);
infs[u]+= infs[y];
if(sz(peb[u]) < sz(peb[y]))
swap(peb[u], peb[y]);
for(ll x : peb[y])
peb[u].insert(x);
}
int N = sz(peb[u]) + infs[u];
assert(sz(peb[u]) >= (N/2)+1);
while(sz(peb[u]) > (N/2)+1)
infs[u]++, peb[u].erase(--peb[u].end());
ll R = *peb[u].rbegin();
peb[u].erase(--peb[u].end());
ll L = *peb[u].rbegin();
peb[u].erase(--peb[u].end());
peb[u].insert(L + S);
peb[u].insert(R + S);
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie();
ll sm = 0;
cin >> n >> m;
for(int i = 1; i < n+m; i++){
int a, b;
cin >> a >> b;
v[--a].PB({i, b});
sm+= b;
}
dfs(0);
int N = sz(peb[0]) + infs[0];
while(sz(peb[0]) > (N/2))
infs[0]++, peb[0].erase(--peb[0].end());
ll bst = *peb[0].rbegin();
ll df = 0;
for(ll x : peb[0]){
df+= abs(x - bst);
df-= x;
}
df+= -bst * infs[0];
ll ans = df + sm *2;
return cout << ans/2 << endl, 0;
}
컴파일 시 표준 에러 (stderr) 메시지
fireworks.cpp: In function 'void dfs(int, int)':
fireworks.cpp:31:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for(auto [y, x] : v[u]){
| ^
# | 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... |