이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n, s;
cin >> n >> s;
vector<vector<ll>> graph(n);
vector<ll> ganancias(n);
vector<int> dependencia(n, 0);
for (ll i = 0; i < n; i++) {
ll vecino;
cin >> ganancias[i] >> vecino;
if (vecino > 0) {
graph[vecino - 1].push_back(i);
dependencia[i]++;
}
}
priority_queue<pair<ll, ll>> auxiliar;
for (ll i = 0; i < n; i++) {
if (dependencia[i] == 0) {
ll sumar = 0, max_ganancia = 0;
priority_queue<pair<ll, ll>> pq;
pq.push({ganancias[i], i});
while (!pq.empty()) {
auto [current_sum, nodo] = pq.top();
pq.pop();
sumar += current_sum;
max_ganancia = max(max_ganancia, sumar);
auxiliar.push({sumar, max_ganancia});
for (ll vecino : graph[nodo]) {
pq.push({ganancias[vecino], vecino});
}
}
}
}
int s_inicial=s;
while (!auxiliar.empty()) {
auto [sumar, max_ganancia] = auxiliar.top();
auxiliar.pop();
if (s + sumar >= 0 && (s + max_ganancia) >= s) {
s += max_ganancia;
}
}
cout << s-s_inicial << "\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... |