#pragma GCC optimize("O3")
#ifdef local
#define debug(x) cout<<#x<<" "<<x<<endl;
#else
#define debug(x)
#endif
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
const ll mod=1e9+7,lim=2e5+5,bitlim=15,inf=1e9;
vector<ll>adjl[lim];
ll pwr[lim],cost[lim];
map<ll,ll> dp(ll pos){
map<ll,ll>ret;
if(adjl[pos].size() == 0){
ret[0] = 0;
ret[pwr[pos] + 1] = cost[pos];
}
else{
vector<map<ll,ll> >vv;
ll maxsz=0,maxszidx=0;
for(auto u:adjl[pos]){
vv.push_back(dp(u));
if(vv.back().size()>maxsz){
maxsz=vv.back().size();
maxszidx=vv.size()-1;
}
}
ret = vv[maxszidx];
for(int i=0;i<adjl[pos].size();i++){
if(i==maxszidx)continue;
for(auto p:vv[i]){
ll k,v;
tie(k,v)=p;
ret[k]+=v;
}
}
ret[0] += cost[pos];
ret[pwr[pos] + 1] += cost[pos];
auto it=ret.upper_bound(pwr[pos]);
ll rem=cost[pos];
while(rem>0){
it--;
if(rem<=it->second)it->second-=rem,rem=0;
else rem-=it->second,it=ret.erase(it);
}
}
return ret;
}
int __attribute__((optimize("O3"), target("arch=sandybridge"))) main(){
ios_base::sync_with_stdio(0),cin.tie(NULL);
ll n;
cin>>n;
for(int i=1;i<=n;i++){
ll a;
cin>>a>>pwr[i]>>cost[i];
if(i!=a)adjl[a].push_back(i);
}
cout<<dp(1).begin()->second<<"\n";
return 0;
}
Compilation message
worst_reporter2.cpp: In function 'std::map<long long int, long long int> dp(ll)':
worst_reporter2.cpp:26:23: warning: comparison of integer expressions of different signedness: 'std::map<long long int, long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
26 | if(vv.back().size()>maxsz){
| ~~~~~~~~~~~~~~~~^~~~~~
worst_reporter2.cpp:32:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=0;i<adjl[pos].size();i++){
| ~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
10 ms |
5580 KB |
Output is correct |
6 |
Correct |
8 ms |
5252 KB |
Output is correct |
7 |
Correct |
7 ms |
5196 KB |
Output is correct |
8 |
Correct |
10 ms |
5580 KB |
Output is correct |
9 |
Correct |
8 ms |
5196 KB |
Output is correct |
10 |
Correct |
7 ms |
5184 KB |
Output is correct |
11 |
Correct |
7 ms |
5196 KB |
Output is correct |
12 |
Correct |
33 ms |
6348 KB |
Output is correct |
13 |
Correct |
8 ms |
6168 KB |
Output is correct |
14 |
Correct |
25 ms |
5768 KB |
Output is correct |
15 |
Correct |
15 ms |
5764 KB |
Output is correct |
16 |
Correct |
12 ms |
5620 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5180 KB |
Output is correct |
19 |
Correct |
180 ms |
5956 KB |
Output is correct |
20 |
Correct |
16 ms |
5692 KB |
Output is correct |
21 |
Correct |
7 ms |
5580 KB |
Output is correct |
22 |
Correct |
8 ms |
6280 KB |
Output is correct |
23 |
Correct |
6 ms |
6024 KB |
Output is correct |
24 |
Correct |
371 ms |
6236 KB |
Output is correct |
25 |
Correct |
16 ms |
6092 KB |
Output is correct |
26 |
Correct |
679 ms |
6764 KB |
Output is correct |
27 |
Correct |
116 ms |
6000 KB |
Output is correct |
28 |
Correct |
224 ms |
6148 KB |
Output is correct |
29 |
Correct |
405 ms |
6608 KB |
Output is correct |
30 |
Correct |
282 ms |
6252 KB |
Output is correct |
31 |
Correct |
294 ms |
6352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
10 ms |
5580 KB |
Output is correct |
6 |
Correct |
8 ms |
5252 KB |
Output is correct |
7 |
Correct |
7 ms |
5196 KB |
Output is correct |
8 |
Correct |
10 ms |
5580 KB |
Output is correct |
9 |
Correct |
8 ms |
5196 KB |
Output is correct |
10 |
Correct |
7 ms |
5184 KB |
Output is correct |
11 |
Correct |
7 ms |
5196 KB |
Output is correct |
12 |
Correct |
33 ms |
6348 KB |
Output is correct |
13 |
Correct |
8 ms |
6168 KB |
Output is correct |
14 |
Correct |
25 ms |
5768 KB |
Output is correct |
15 |
Correct |
15 ms |
5764 KB |
Output is correct |
16 |
Correct |
12 ms |
5620 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5180 KB |
Output is correct |
19 |
Correct |
180 ms |
5956 KB |
Output is correct |
20 |
Correct |
16 ms |
5692 KB |
Output is correct |
21 |
Correct |
7 ms |
5580 KB |
Output is correct |
22 |
Correct |
8 ms |
6280 KB |
Output is correct |
23 |
Correct |
6 ms |
6024 KB |
Output is correct |
24 |
Correct |
371 ms |
6236 KB |
Output is correct |
25 |
Correct |
16 ms |
6092 KB |
Output is correct |
26 |
Correct |
679 ms |
6764 KB |
Output is correct |
27 |
Correct |
116 ms |
6000 KB |
Output is correct |
28 |
Correct |
224 ms |
6148 KB |
Output is correct |
29 |
Correct |
405 ms |
6608 KB |
Output is correct |
30 |
Correct |
282 ms |
6252 KB |
Output is correct |
31 |
Correct |
294 ms |
6352 KB |
Output is correct |
32 |
Correct |
10 ms |
5580 KB |
Output is correct |
33 |
Correct |
423 ms |
31176 KB |
Output is correct |
34 |
Correct |
298 ms |
12484 KB |
Output is correct |
35 |
Correct |
436 ms |
31208 KB |
Output is correct |
36 |
Correct |
295 ms |
12356 KB |
Output is correct |
37 |
Correct |
191 ms |
11960 KB |
Output is correct |
38 |
Correct |
169 ms |
11880 KB |
Output is correct |
39 |
Execution timed out |
2073 ms |
55224 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
10 ms |
5580 KB |
Output is correct |
6 |
Correct |
8 ms |
5252 KB |
Output is correct |
7 |
Correct |
7 ms |
5196 KB |
Output is correct |
8 |
Correct |
10 ms |
5580 KB |
Output is correct |
9 |
Correct |
8 ms |
5196 KB |
Output is correct |
10 |
Correct |
7 ms |
5184 KB |
Output is correct |
11 |
Correct |
7 ms |
5196 KB |
Output is correct |
12 |
Correct |
33 ms |
6348 KB |
Output is correct |
13 |
Correct |
8 ms |
6168 KB |
Output is correct |
14 |
Correct |
25 ms |
5768 KB |
Output is correct |
15 |
Correct |
15 ms |
5764 KB |
Output is correct |
16 |
Correct |
12 ms |
5620 KB |
Output is correct |
17 |
Correct |
8 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5180 KB |
Output is correct |
19 |
Correct |
180 ms |
5956 KB |
Output is correct |
20 |
Correct |
16 ms |
5692 KB |
Output is correct |
21 |
Correct |
7 ms |
5580 KB |
Output is correct |
22 |
Correct |
8 ms |
6280 KB |
Output is correct |
23 |
Correct |
6 ms |
6024 KB |
Output is correct |
24 |
Correct |
371 ms |
6236 KB |
Output is correct |
25 |
Correct |
16 ms |
6092 KB |
Output is correct |
26 |
Correct |
679 ms |
6764 KB |
Output is correct |
27 |
Correct |
116 ms |
6000 KB |
Output is correct |
28 |
Correct |
224 ms |
6148 KB |
Output is correct |
29 |
Correct |
405 ms |
6608 KB |
Output is correct |
30 |
Correct |
282 ms |
6252 KB |
Output is correct |
31 |
Correct |
294 ms |
6352 KB |
Output is correct |
32 |
Correct |
10 ms |
5580 KB |
Output is correct |
33 |
Correct |
423 ms |
31176 KB |
Output is correct |
34 |
Correct |
298 ms |
12484 KB |
Output is correct |
35 |
Correct |
436 ms |
31208 KB |
Output is correct |
36 |
Correct |
295 ms |
12356 KB |
Output is correct |
37 |
Correct |
191 ms |
11960 KB |
Output is correct |
38 |
Correct |
169 ms |
11880 KB |
Output is correct |
39 |
Execution timed out |
2073 ms |
55224 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |