이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long un;
typedef vector<un> vuc;
typedef pair<un, un> pun;
#define REP(i, a, b) for (un i = (un)a; i < (un)b; i++)
#define FEAC(i, a) for (auto&& i : a)
#define vec vector
#define ALL(x) (x).begin(), (x).end()
constexpr un INF = INT64_MAX;
vuc penize;
vuc predek;
vec<vuc> graph;
un N, s;
un rekurze(un v){
un ret = penize[v];
FEAC(s, graph[v]) ret += rekurze(s);
return max((un)0, ret);
}
void easy(){
un ret = 0;
REP(i, 0, N) {
if (predek[i] == -1) ret += rekurze(i);
}
cout << ret;
}
int main(){
cin >> N >> s;
penize = vuc(N);
predek = vuc(N);
REP(i, 0, N){
cin >> penize[i] >> predek[i];
predek[i]--;
}
if (s == 1e18) {
graph = vec<vuc>(N);
REP(i, 0, N){
if (predek[i] != -1) graph[predek[i]].push_back(i);
}
easy();
return 0;
}
vec<vuc> cesty;
REP(i, 0, N){
if (predek[i] == -1) cesty.push_back({});
if (penize[i] != 0){
cesty[cesty.size() - 1].push_back(penize[i]);
}
}
un M = cesty.size();
vec<vec<pun>> nab;
REP(j, 0, M){
vec<pun> new_nab = {{0, 0}};
un req = 0;
un zisk = 0;
cesty[j].push_back(-1);
REP(i, 0, cesty[j].size()-1){
zisk += cesty[j][i];
req = min(req, zisk);
if ((cesty[j][i] > 0) and (cesty[j][i+1] < 0) and (zisk > 0)){
new_nab.push_back({-req, zisk});
zisk = 0;
req = 0;
}
}
new_nab.push_back({INF, INF});
nab.push_back(new_nab);
}
vuc ptrs(M, 1);
priority_queue<pun, vector<pun>, greater<pun>> kjuu;
REP(i, 0, M) kjuu.push({nab[i][1].first, i});
un curr = s;
while(curr >= kjuu.top().first) {
un j;
tie(ignore, j) = kjuu.top(); kjuu.pop();
curr += nab[j][ptrs[j]].second;
kjuu.push({nab[j][ptrs[j]+1].first, j});
ptrs[j]++;
}
cout << curr-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... |