#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t,a[N],c[N],n;
multiset<pii> s[N];
vector <int> V[N];
void dfs(int u) {
for(int i = 0; i < V[u].size(); i++) {
dfs(V[u][i]);
int x = V[u][i];
if(s[u].size() < s[x].size()) swap(s[u],s[x]);
while(s[x].size()) {
pii y = *s[x].begin();
s[x].erase(s[x].find(y));
s[u].insert(y);
}
}
//
int saved = c[u];
while(s[u].upper_bound({a[u] + 1,-1}) != s[u].begin()){
pii x = *--s[u].upper_bound({a[u] + 1 ,-1});
s[u].erase(s[u].find(x));
if(x.s <= c[u]) {
c[u] -= x.s;
}
else {
s[u].insert({x.f,x.s - c[u]});
break;
}
}
s[u].insert({a[u], saved});
}
main(){
cin >> n;
int sum = 0;
for(int i = 1; i <= n; i++) {
int p;
cin >> p >> a[i] >> c[i];
sum += c[i];
if(i == 1) {
if(p != 1) return 0;
continue;
}
if(p >= i) return 0;
V[p].push_back(i);
}
dfs(1);
int ans = 0;
for(multiset <pii> :: iterator it = s[1].begin(); it != s[1].end(); it++) {
ans += (*it).s;
}
cout << sum - ans;
}
Compilation message
worst_reporter2.cpp: In function 'void dfs(long long int)':
worst_reporter2.cpp:12:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(int i = 0; i < V[u].size(); i++) {
| ~~^~~~~~~~~~~~~
worst_reporter2.cpp: At global scope:
worst_reporter2.cpp:37:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
37 | main(){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14284 KB |
Output is correct |
2 |
Correct |
7 ms |
14388 KB |
Output is correct |
3 |
Correct |
7 ms |
14284 KB |
Output is correct |
4 |
Correct |
7 ms |
14392 KB |
Output is correct |
5 |
Correct |
16 ms |
14884 KB |
Output is correct |
6 |
Incorrect |
16 ms |
14924 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14284 KB |
Output is correct |
2 |
Correct |
7 ms |
14388 KB |
Output is correct |
3 |
Correct |
7 ms |
14284 KB |
Output is correct |
4 |
Correct |
7 ms |
14392 KB |
Output is correct |
5 |
Correct |
16 ms |
14884 KB |
Output is correct |
6 |
Incorrect |
16 ms |
14924 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14284 KB |
Output is correct |
2 |
Correct |
7 ms |
14388 KB |
Output is correct |
3 |
Correct |
7 ms |
14284 KB |
Output is correct |
4 |
Correct |
7 ms |
14392 KB |
Output is correct |
5 |
Correct |
16 ms |
14884 KB |
Output is correct |
6 |
Incorrect |
16 ms |
14924 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |