이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 3e5 + 10;
ll n, s, a[N], p[N];
vector<ll> outward[N], inward[N];
int main(){
cin >> n >> s;
set<pair<ll, ll>> st;
for (ll i = 1; i <= n; i ++){
cin >> a[i] >> p[i];
if (!p[i]){
st.insert({-1, i});
continue;
}
outward[p[i]].push_back(i);
inward[i].push_back(p[i]);
}
while (!st.empty()){
auto [val, v] = *st.begin();
st.erase(st.begin());
// cout << endl << "At " << v << " with val = " << val << endl;
if (val == -1){
int u = v;
int sm = 0;
int mn = 0;
while (1){
sm += a[v];
mn = min(mn, sm);
if (outward[v].size())
v = outward[v][0];
else
v = -1;
if (sm >= 0 or v == -1){
break;
}
}
if (sm < 0) continue;
if (s + mn >= 0){
s += sm;
if (v != -1)
st.insert({-1, v});
continue;
}
st.insert({-mn, u});
}
else{
if (val > s)
break;
int sm = 0;
while (1){
sm += a[v];
if (outward[v].size())
v = outward[v][0];
else
v = -1;
if (sm >= 0){
break;
}
}
s += sm;
if (v != -1)
st.insert({-1, v});
}
}
cout << s << endl;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |