Submission #825188

# Submission time Handle Problem Language Result Execution time Memory
825188 2023-08-14T15:19:16 Z NK_ Magic Tree (CEOI19_magictree) C++17
100 / 100
86 ms 30796 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())

template<class T> using V = vector<T>;
using vi = V<int>;
using pi = pair<int, int>;
using ll = long long;
using vl = V<ll>;

using T = map<int, ll>;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, M, K; cin >> N >> M >> K;

	V<vi> chd(N); for(int u = 1; u < N; u++) {
		int p; cin >> p; --p;
		chd[p].pb(u);
	}

	vi W(N, 0), D(N, -1);

	for(int i = 0; i < M; i++) {
		int v, d, w; cin >> v >> d >> w; --v, --d;
		D[v] = d, W[v] = w;
	}
	
	function<T(int)> dfs = [&](int u) {
		T R;
		for(auto& v : chd[u]) {
			T r = dfs(v);
			if (sz(R) < sz(r)) R.swap(r);
			for(auto p : r) R[p.first] += p.second;
		}		

		if (D[u] != -1) {
			R[D[u]] += W[u];
			ll amt = 0;
			while(1) {
				auto it = R.upper_bound(D[u]);
				if (it == end(R)) break;

				amt += it->s;
				if (W[u] < amt) { it->s = amt - W[u]; break; }
				else R.erase(it);
			}
		}

		return R;
	};

	T ans = dfs(0);
	ll ANS = 0;
	for(auto p : ans) ANS += p.second;
	cout << ANS << nl;
    return 0;
}			

# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 6812 KB Output is correct
2 Correct 33 ms 15728 KB Output is correct
3 Correct 86 ms 11300 KB Output is correct
4 Correct 48 ms 9584 KB Output is correct
5 Correct 65 ms 12268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 40 ms 26856 KB Output is correct
5 Correct 60 ms 30796 KB Output is correct
6 Correct 56 ms 28880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 5076 KB Output is correct
2 Correct 37 ms 5032 KB Output is correct
3 Correct 35 ms 14004 KB Output is correct
4 Correct 24 ms 3924 KB Output is correct
5 Correct 39 ms 26940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 37 ms 5076 KB Output is correct
11 Correct 34 ms 5084 KB Output is correct
12 Correct 44 ms 14024 KB Output is correct
13 Correct 23 ms 3924 KB Output is correct
14 Correct 39 ms 26820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1236 KB Output is correct
2 Correct 15 ms 5144 KB Output is correct
3 Correct 15 ms 5076 KB Output is correct
4 Correct 17 ms 5116 KB Output is correct
5 Correct 7 ms 3924 KB Output is correct
6 Correct 16 ms 8916 KB Output is correct
7 Correct 18 ms 14108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 596 KB Output is correct
13 Correct 40 ms 26856 KB Output is correct
14 Correct 60 ms 30796 KB Output is correct
15 Correct 56 ms 28880 KB Output is correct
16 Correct 37 ms 5076 KB Output is correct
17 Correct 34 ms 5084 KB Output is correct
18 Correct 44 ms 14024 KB Output is correct
19 Correct 23 ms 3924 KB Output is correct
20 Correct 39 ms 26820 KB Output is correct
21 Correct 11 ms 1876 KB Output is correct
22 Correct 42 ms 6600 KB Output is correct
23 Correct 45 ms 8436 KB Output is correct
24 Correct 69 ms 10136 KB Output is correct
25 Correct 43 ms 8848 KB Output is correct
26 Correct 62 ms 14540 KB Output is correct
27 Correct 59 ms 17420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 34 ms 6812 KB Output is correct
11 Correct 33 ms 15728 KB Output is correct
12 Correct 86 ms 11300 KB Output is correct
13 Correct 48 ms 9584 KB Output is correct
14 Correct 65 ms 12268 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 596 KB Output is correct
18 Correct 40 ms 26856 KB Output is correct
19 Correct 60 ms 30796 KB Output is correct
20 Correct 56 ms 28880 KB Output is correct
21 Correct 33 ms 5076 KB Output is correct
22 Correct 37 ms 5032 KB Output is correct
23 Correct 35 ms 14004 KB Output is correct
24 Correct 24 ms 3924 KB Output is correct
25 Correct 39 ms 26940 KB Output is correct
26 Correct 37 ms 5076 KB Output is correct
27 Correct 34 ms 5084 KB Output is correct
28 Correct 44 ms 14024 KB Output is correct
29 Correct 23 ms 3924 KB Output is correct
30 Correct 39 ms 26820 KB Output is correct
31 Correct 4 ms 1236 KB Output is correct
32 Correct 15 ms 5144 KB Output is correct
33 Correct 15 ms 5076 KB Output is correct
34 Correct 17 ms 5116 KB Output is correct
35 Correct 7 ms 3924 KB Output is correct
36 Correct 16 ms 8916 KB Output is correct
37 Correct 18 ms 14108 KB Output is correct
38 Correct 11 ms 1876 KB Output is correct
39 Correct 42 ms 6600 KB Output is correct
40 Correct 45 ms 8436 KB Output is correct
41 Correct 69 ms 10136 KB Output is correct
42 Correct 43 ms 8848 KB Output is correct
43 Correct 62 ms 14540 KB Output is correct
44 Correct 59 ms 17420 KB Output is correct
45 Correct 12 ms 1876 KB Output is correct
46 Correct 41 ms 6824 KB Output is correct
47 Correct 56 ms 8800 KB Output is correct
48 Correct 82 ms 11188 KB Output is correct
49 Correct 52 ms 9540 KB Output is correct
50 Correct 71 ms 15572 KB Output is correct
51 Correct 77 ms 18464 KB Output is correct