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;
#define int long long
const int MAXN = 314159;
int pr[300005];
bool vis[300005];
vector<int> adj[300005];
vector<pair<int,int> > op;
void dfs(int x, int prof, int maxProf, int loss) {
vis[x] = 1;
if(prof + pr[x] > maxProf) op.push_back({loss - maxProf, prof + pr[x] - maxProf});
for(auto i: adj[x]) dfs(i, prof + pr[x], max(maxProf, prof + pr[x]), min(loss, prof + pr[x]));
}
signed main() {
int n, s, ans = 0; cin >> n >> s;
for(int i = 1,p; i <= n; i++) {
cin >> pr[i] >> p;
adj[p].push_back(i);
}
for(int i = 1; i <= n; i++) if(!vis[i]) dfs(i, 0, 0, 0);
sort(op.begin(), op.end(), greater<pair<int, int> >());
for(int i = 0; i < op.size() && op[i].first + s >= 0; i++) s += op[i].second, ans += op[i].second;
cout << ans << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:24:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i = 0; i < op.size() && op[i].first + s >= 0; i++) s += op[i].second, ans += op[i].second;
| ~~^~~~~~~~~~~
# | 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... |