Submission #579384

# Submission time Handle Problem Language Result Execution time Memory
579384 2022-06-19T04:14:17 Z amunduzbaev Magic Tree (CEOI19_magictree) C++17
15 / 100
1356 ms 1048576 KB
#include "bits/stdc++.h"
using namespace std;
 
#define ar array
typedef int64_t ll;
#define int ll

const int N = 1e5 + 5;
vector<int> edges[N];
int is[N], d[N], w[N], mx[N];
map<int, int> sub[N];

void dfs(int u){
	mx[u] = u;
	for(auto x : edges[u]){
		dfs(x);
		if(sub[x].size() > sub[mx[u]].size()) mx[u] = x;
	}
	
	swap(sub[u], sub[mx[u]]);
	for(auto x : edges[u]){
		if(x == mx[u]) continue;
		for(auto v : sub[x]){
			sub[u][v.first] += v.second;
		}
	}
	
	if(is[u]){
		sub[u][d[u]] += w[u];
		auto it = sub[u].upper_bound(d[u]);
		vector<int> er;
		int sum = 0;
		while(it != sub[u].end()){
			sum += (*it).second;
			if(sum >= w[u]){
				sub[u][(*it).first] = sum - w[u];
				break;
			}
			er.push_back((*it).first);
		}
		
		for(auto x : er){
			sub[u].erase(x);
		}
	}
}

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m, k; cin>>n>>m>>k;
	for(int i=2;i<=n;i++){
		int x; cin>>x;
		edges[x].push_back(i);
	}
	
	for(int i=0;i<m;i++){
		int v; cin>>v;
		cin>>d[v]>>w[v];
		is[v] = 1;
	}
	
	dfs(1);
	int sum = 0;
	for(auto x : sub[1]) sum += x.second;
	cout<<sum<<"\n";
}
 

# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Runtime error 1356 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 71 ms 20940 KB Output is correct
2 Correct 51 ms 20752 KB Output is correct
3 Correct 183 ms 38488 KB Output is correct
4 Correct 93 ms 22656 KB Output is correct
5 Correct 125 ms 24660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 852 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 92 ms 18052 KB Output is correct
2 Correct 86 ms 17272 KB Output is correct
3 Correct 65 ms 21840 KB Output is correct
4 Correct 46 ms 18632 KB Output is correct
5 Correct 59 ms 29728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Runtime error 1356 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 8660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Runtime error 1356 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Runtime error 1356 ms 1048576 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -