This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
void dfs(long long x, long long p, vector<vector<long long>> &AR, vector<long long> &X, vector<long long> &req, vector<long long> &occ, priority_queue<pair<long long, long long>> &PQ)
{
occ[x] = 1;
priority_queue<pair<long long, long long>> PQ2;
for (long long i = 0; i < AR[x].size(); i++)
{
dfs(AR[x][i], x, AR, X, req, occ, PQ2);
if (PQ2.size() > PQ.size())
swap(PQ2, PQ);
while (!PQ2.empty())
{
PQ.push(PQ2.top());
PQ2.pop();
}
}
pair<long long, long long> ans = {req[x], X[x]};
while (!PQ.empty() && (ans.second < 0 || ans.first >= -PQ.top().first - ans.second))
{
ans.first = max(ans.first, -PQ.top().first - ans.second);
ans.second += PQ.top().second;
PQ.pop();
}
if (ans.second >= 0)
PQ.push({-ans.first, ans.second});
}
int main()
{
long long N, S;
cin >> N >> S;
long long S2 = S;
vector<long long> p(N), X(N), req(N);
vector<vector<long long>> AR(N);
for (long long i = 0; i < N; i++)
{
cin >> X[i] >> p[i];
req[i] = max((long long)0, -X[i]);
p[i]--;
if (p[i] != -1)
AR[p[i]].push_back(i);
}
vector<long long> occ(N);
priority_queue<pair<long long, long long>> PQ, PQ2;
for (long long i = 0; i < N; i++)
{
if (occ[i])
continue;
dfs(i, i, AR, X, req, occ, PQ2);
if (PQ2.size() > PQ.size())
swap(PQ, PQ2);
while (!PQ2.empty())
{
PQ.push(PQ2.top());
PQ2.pop();
}
}
while (!PQ.empty() && -PQ.top().first <= S)
{
S += PQ.top().second;
PQ.pop();
}
cout << S - S2;
}
Compilation message (stderr)
Main.cpp: In function 'void dfs(long long int, long long int, std::vector<std::vector<long long int> >&, std::vector<long long int>&, std::vector<long long int>&, std::vector<long long int>&, std::priority_queue<std::pair<long long int, long long int> >&)':
Main.cpp:7:29: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | for (long long i = 0; i < AR[x].size(); i++)
| ~~^~~~~~~~~~~~~~
# | 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... |