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;
#ifdef Nero
#include "Deb.h"
#else
#define debug(...)
#endif
const int N = 1e5 + 5;
int x[N];
int p[N];
bool vis[N];
bool taken[N];
long long sum[N];
vector<int> g[N];
long long min_pref[N];
void dfs(int v) {
vis[v] = true;
sum[v] += x[v];
min_pref[v] = min(min_pref[v], sum[v]);
for (int u : g[v]) {
sum[u] = sum[v];
min_pref[u] = min_pref[v];
dfs(u);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long s;
cin >> n >> s;
for (int i = 1; i <= n; ++i) {
cin >> x[i] >> p[i];
g[p[i]].push_back(i);
}
long long cur = s;
for (int rep = 0; rep < n; ++rep) {
fill(vis + 1, vis + 1 + n, 0);
for (int i = 1; i <= n; ++i) {
if (!taken[i] && !vis[i]) {
min_pref[i] = sum[i] = 0;
dfs(i);
}
}
for (int i = 1; i <= n; ++i) {
if (!taken[i] && sum[i] > 0 && cur + min_pref[i] >= 0) {
cur += sum[i];
taken[i] = true;
break;
}
}
}
cout << cur - s << '\n';
return 0;
}
# | 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... |