이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 3e5 + 6;
struct brainrot{
int fanumtax, skibidi;
bool operator >(const brainrot &a) const{
return (fanumtax == a.fanumtax ? skibidi > a.skibidi : (fanumtax > a.fanumtax));
}
};
int val[maxn] = {0}, head[maxn] = {0};
vector<vector<int>> adj(maxn);
priority_queue<brainrot, vector<brainrot>, greater<brainrot>> sigma[maxn];
int small_to_large(int a, int b){
if (sigma[a].size() > sigma[b].size()){
swap(a, b);
}
while(!sigma[a].empty()){
brainrot k = sigma[a].top();
sigma[a].pop();
sigma[b].push(k);
}
return b;
}
void sigmaification(int ptr, brainrot englishorspanish){
priority_queue<brainrot, vector<brainrot>, greater<brainrot>> &mewing = sigma[ptr];
while(!mewing.empty()){
brainrot rizz = mewing.top();
if (englishorspanish.skibidi <= 0){
int newfanumtax = max(englishorspanish.fanumtax, rizz.fanumtax - englishorspanish.skibidi), newskibidi = rizz.skibidi + englishorspanish.skibidi;
englishorspanish = {newfanumtax, newskibidi};
mewing.pop();
}
else{
if (englishorspanish.fanumtax > rizz.fanumtax){
englishorspanish = {englishorspanish.fanumtax, rizz.skibidi + englishorspanish.skibidi};
mewing.pop();
}
else{
mewing.push(englishorspanish);
break;
}
}
}
if (mewing.empty()) mewing.push(englishorspanish);
if (mewing.size() == 1 && mewing.top().skibidi <= 0) mewing.pop();
}
void dfs(int u, int p = -1){
int counter = 1;
if (adj[u].size() == 0) head[u] = u;
for (int v: adj[u]){
dfs(v, u);
if (counter == 1){
head[u] = head[v];
}
else{
head[u] = small_to_large(head[u], head[v]);
}
counter++;
}
sigmaification(head[u], {max(0LL, -val[u]), val[u]});
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n, s;
cin >> n >> s;
for (int i = 1; i <= n; i++){
int p;
cin >> val[i] >> p;
adj[p].push_back(i);
}
dfs(0);
int mogging = 0;
while(!sigma[head[0]].empty()){
brainrot rizz = sigma[head[0]].top();
int tax = rizz.fanumtax, mog = rizz.skibidi;
sigma[head[0]].pop();
if (s + mogging >= tax) mogging += mog;
}
cout << mogging << endl;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |