이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
using vi = vector<int>;
using pi = pair<ll,ll>;
#define REP(i,n) for (int i = 0; i < n; i++)
#define trav(a,x) for (auto& a : x)
#define D(x) //cerr << #x << ": " << x << endl;
#define all(x) (x).begin(), (x).end()
const int MAX_N = 3*1e5+3;
int n; ll s;
vi children[MAX_N];
int values[MAX_N];
bool done[MAX_N];
priority_queue<pi, vector<pi>, greater<pi>> q;
void dfs(int i, ll curr = 0, ll pref = 0) {
assert(!done[i]);
done[i] = 1;
curr += values[i];
pref = min(curr, pref);
if (curr > 0) {
q.push({-pref, curr});
curr = 0;
}
trav(ch, children[i])
dfs(ch, curr, pref);
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> s;
ll ss = s;
assert(n < MAX_N);
REP(i,n) {
int p; cin >> values[i] >> p;
if (p == 0) continue;
p--;
children[p].push_back(i);
}
REP(i,n) {
if (done[i]) continue;
dfs(i);
}
while (q.size()) {
auto r = q.top(); q.pop();
D(r.first);
D(r.second);
if (r.first <= s) s += r.second;
//else break;
}
assert(s >= ss);
cout << s - ss << endl;
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... |