이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
auto&operator <<(auto& o, pair<auto, auto> p) {return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator <<(auto& o, auto x)->decltype(x.end(), o) {o<<"{"; for(auto v : x) o<<v<<", "; return o<<"}";}
#define debug(X) cout<<"["#X"]"<<X<<endl;
#else
#define debug(X) {}
#endif
const long long INF = numeric_limits<long long>::max();
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, s;
cin>>n>>s;
vector<vector<int> > graph(n+1);
vector<long long> cost(n+1);
for(int i=1;i<=n;i++)
{
long long c, p;
cin>>c>>p;
graph[p].push_back(i);
cost[i] = c;
}
vector<vector<pair<int, long long> > > pool(n+1);
function<void(int)> dfs = [&](int b){
priority_queue<pair<long long, int> > q;
for(auto v : graph[b]) {dfs(v); q.push({pool[v].back().second, v});};
vector<pair<int, long long> > myP = {{cost[b], 0}};
while(!q.empty())
{
auto a = q.top().second;
q.pop();
myP.push_back({pool[a].back().first, 0});
pool[a].pop_back();
if(pool[a].size() > 0) q.push({pool[a].back().second, a});
}
long long checkP = -1, akt = 0;
for(int i=0;i<myP.size();i++)
{
akt += myP[i].first;
if(akt >= 0)
{
akt -= myP[i].first;
for(int j=checkP+1;j<=i;j++)
{
myP[j].second = akt;
if(myP[j].first >= 0) myP[j].second = 0;
akt -= myP[j].first;
}
checkP = i;
akt = 0;
}
}
for(int i=checkP+1;i<myP.size();i++) if(myP[i].first < 0) myP[i].second = -INF;
reverse(myP.begin(), myP.end());
debug(b);
debug(myP);
pool[b] = myP;
};
dfs(0);
long long org = s;
while(pool[0].size())
{
debug(pool[0].back());
if(pool[0].back().second + s < 0) break;
if(s + pool[0].back().first >= 0) {s += pool[0].back().first; pool[0].pop_back();}
}
cout<<s - org;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In lambda function:
Main.cpp:41:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(int i=0;i<myP.size();i++)
| ~^~~~~~~~~~~
Main.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for(int i=checkP+1;i<myP.size();i++) if(myP[i].first < 0) myP[i].second = -INF;
| ~^~~~~~~~~~~
# | 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... |