# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1253865 | skibidihehe | Jobs (BOI24_jobs) | C++20 | 353 ms | 37468 KiB |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define taskname ""
#define ld long double
int N;
ll s;
vector<ll> x;
vector<vector<int>> ch;
// returns {pointer to multiset of kept profits, sum_of_that_multiset}
pair<multiset<ll>*, ll> dfs(int u){
// start with an empty multiset at u
auto *ms = new multiset<ll>();
ll sum = 0;
// merge children’s sets into ours
for(int v : ch[u]){
auto [cs, csum] = dfs(v);
// small-to-large: ensure *ms is the larger
if(ms->size() < cs->size()){
swap(ms, cs);
swap(sum, csum);
}
// merge cs into ms
for(ll val : *cs){
ms->insert(val);
}
sum += csum;
delete cs;
}
// insert u’s own profit
ms->insert(x[u]);
sum += x[u];
// if we go negative, keep dropping the smallest until sum ≥ 0
while(sum < 0){
auto it = ms->begin(); // worst loss
sum -= *it;
ms->erase(it);
}
return {ms, sum};
}
int main(){
if(fopen(taskname".inp","r")){
freopen(taskname".inp","r",stdin);
freopen(taskname".out","w",stdout);
}
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> s;
x.assign(N+1, 0);
ch.assign(N+1, {});
// dummy root 0 holds your initial capital
x[0] = s;
for(int i = 1; i <= N; i++){
int p;
cin >> x[i] >> p;
ch[p].pb(i);
}
auto [rootSet, rootSum] = dfs(0);
// rootSum = s + sum(chosen real x[i]); so subtract s
ll answer = rootSum - s;
cout << answer;
return 0;
}
컴파일 시 표준 에러 (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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |