Submission #969323

# Submission time Handle Problem Language Result Execution time Memory
969323 2024-04-25T01:14:38 Z MinaRagy06 Fireworks (APIO16_fireworks) C++17
55 / 100
2000 ms 296176 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long

const int N = 300'005;
const ll inf = 2e18;
void print(priority_queue<ll> pq) {
	cout << "[ ";
	while (pq.size()) {
		cout << pq.top() << ' ';
		pq.pop();
	}
	cout << "]\n";
}
vector<array<int, 2>> adj[N];
priority_queue<ll> pq[N];
ll dp0[N];
//ll dp[N][N];
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int n, m;
	cin >> n >> m;
	n += m;
	for (int i = 2; i <= n; i++) {
		int p, c;
		cin >> p >> c;
		adj[p].push_back({i, c});
	}
	for (int i = n; i >= 1; i--) {
		if (adj[i].empty()) {
			dp0[i] = 0;
			pq[i].push(0);
			pq[i].push(0);
		} else {
			int cur = 0;
			for (auto [nxt, w] : adj[i]) {
				dp0[i] += dp0[nxt] + w;
				ll x = pq[nxt].top() + w;
				pq[nxt].pop();
				ll y = pq[nxt].top() + w;
				pq[nxt].pop();
				pq[nxt].push(y);
				pq[nxt].push(x);
				cur++;
				while (pq[nxt].size()) {
					x = pq[nxt].top();
					pq[nxt].pop();
					pq[i].push(x);
				}
			}
			while (cur > 1) {
				cur--;
				pq[i].pop();
			}
		}
//		cout << dp0[i] << ' ';
//		print(pq[i]);

//		for (auto [nxt, w] : adj[i]) {
//			cout << i << " -> " << nxt << ' ' << w << '\n';
//			for (int j = 0; j < N; j++) {
//				cout << dp[nxt][j] << ' ';
//			}
//			cout << '\n';
//			for (int j = 0; j < N; j++) {
//				ll mn = 1e18;
//				for (int k = 0; k <= j; k++) {
//					mn = min(mn, dp[nxt][k] + abs(j - k - w));
//				}
//				cout << mn << ' ';
//				dp[i][j] += mn;
//				dp[i][j] = min(dp[i][j], inf);
//			}
//			cout << "\n\n";
//		}
//		ll mn = *min_element(dp[i], dp[i] + N);
//		for (int j = N - 1; j >= 0; j--) {
//			if (dp[i][j] == mn) {
//				break;
//			}
//			dp[i][j] = inf;
//		}
	}
	ll height = 0;
	ll cur = 0, lst = pq[1].top();
	pq[1].pop();
	while (pq[1].size()) {
		height += (lst - pq[1].top()) * cur;
		cur++;
		lst = pq[1].top();
		pq[1].pop();
	}
	height += lst * cur;
	cout << dp0[1] - height << '\n';
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 16988 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 5 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 4 ms 16984 KB Output is correct
8 Correct 4 ms 16988 KB Output is correct
9 Correct 5 ms 16988 KB Output is correct
10 Correct 4 ms 17000 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 3 ms 16988 KB Output is correct
4 Correct 5 ms 16988 KB Output is correct
5 Correct 5 ms 16988 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 3 ms 16984 KB Output is correct
8 Correct 4 ms 16988 KB Output is correct
9 Correct 4 ms 16984 KB Output is correct
10 Correct 4 ms 16988 KB Output is correct
11 Correct 4 ms 16988 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 4 ms 16988 KB Output is correct
14 Correct 3 ms 16988 KB Output is correct
15 Correct 4 ms 16988 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 4 ms 16988 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 16988 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 5 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 4 ms 16984 KB Output is correct
8 Correct 4 ms 16988 KB Output is correct
9 Correct 5 ms 16988 KB Output is correct
10 Correct 4 ms 17000 KB Output is correct
11 Correct 3 ms 16988 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 5 ms 16988 KB Output is correct
15 Correct 5 ms 16988 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 3 ms 16984 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16984 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 3 ms 16988 KB Output is correct
25 Correct 4 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 5 ms 17244 KB Output is correct
32 Correct 5 ms 17244 KB Output is correct
33 Correct 6 ms 17500 KB Output is correct
34 Correct 7 ms 17800 KB Output is correct
35 Correct 11 ms 18360 KB Output is correct
36 Correct 13 ms 18264 KB Output is correct
37 Correct 11 ms 18060 KB Output is correct
38 Correct 19 ms 19540 KB Output is correct
39 Correct 11 ms 18524 KB Output is correct
40 Correct 5 ms 17244 KB Output is correct
41 Correct 6 ms 17244 KB Output is correct
42 Correct 5 ms 17244 KB Output is correct
43 Correct 188 ms 47972 KB Output is correct
44 Correct 146 ms 39764 KB Output is correct
45 Correct 145 ms 39504 KB Output is correct
46 Correct 7 ms 17548 KB Output is correct
47 Correct 7 ms 17420 KB Output is correct
48 Correct 7 ms 17244 KB Output is correct
49 Correct 6 ms 17500 KB Output is correct
50 Correct 7 ms 17244 KB Output is correct
51 Correct 5 ms 17244 KB Output is correct
52 Correct 10 ms 17956 KB Output is correct
53 Correct 8 ms 17500 KB Output is correct
54 Correct 6 ms 17500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 16988 KB Output is correct
2 Correct 3 ms 16988 KB Output is correct
3 Correct 4 ms 16988 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Correct 5 ms 16988 KB Output is correct
6 Correct 3 ms 16988 KB Output is correct
7 Correct 4 ms 16984 KB Output is correct
8 Correct 4 ms 16988 KB Output is correct
9 Correct 5 ms 16988 KB Output is correct
10 Correct 4 ms 17000 KB Output is correct
11 Correct 3 ms 16988 KB Output is correct
12 Correct 4 ms 16988 KB Output is correct
13 Correct 3 ms 16988 KB Output is correct
14 Correct 5 ms 16988 KB Output is correct
15 Correct 5 ms 16988 KB Output is correct
16 Correct 4 ms 16988 KB Output is correct
17 Correct 3 ms 16984 KB Output is correct
18 Correct 4 ms 16988 KB Output is correct
19 Correct 4 ms 16984 KB Output is correct
20 Correct 4 ms 16988 KB Output is correct
21 Correct 4 ms 16988 KB Output is correct
22 Correct 4 ms 16988 KB Output is correct
23 Correct 4 ms 16988 KB Output is correct
24 Correct 3 ms 16988 KB Output is correct
25 Correct 4 ms 16988 KB Output is correct
26 Correct 4 ms 16988 KB Output is correct
27 Correct 4 ms 16988 KB Output is correct
28 Correct 4 ms 16988 KB Output is correct
29 Correct 4 ms 16988 KB Output is correct
30 Correct 4 ms 16988 KB Output is correct
31 Correct 5 ms 17244 KB Output is correct
32 Correct 5 ms 17244 KB Output is correct
33 Correct 6 ms 17500 KB Output is correct
34 Correct 7 ms 17800 KB Output is correct
35 Correct 11 ms 18360 KB Output is correct
36 Correct 13 ms 18264 KB Output is correct
37 Correct 11 ms 18060 KB Output is correct
38 Correct 19 ms 19540 KB Output is correct
39 Correct 11 ms 18524 KB Output is correct
40 Correct 5 ms 17244 KB Output is correct
41 Correct 6 ms 17244 KB Output is correct
42 Correct 5 ms 17244 KB Output is correct
43 Correct 188 ms 47972 KB Output is correct
44 Correct 146 ms 39764 KB Output is correct
45 Correct 145 ms 39504 KB Output is correct
46 Correct 7 ms 17548 KB Output is correct
47 Correct 7 ms 17420 KB Output is correct
48 Correct 7 ms 17244 KB Output is correct
49 Correct 6 ms 17500 KB Output is correct
50 Correct 7 ms 17244 KB Output is correct
51 Correct 5 ms 17244 KB Output is correct
52 Correct 10 ms 17956 KB Output is correct
53 Correct 8 ms 17500 KB Output is correct
54 Correct 6 ms 17500 KB Output is correct
55 Correct 42 ms 23728 KB Output is correct
56 Correct 368 ms 72996 KB Output is correct
57 Correct 749 ms 124532 KB Output is correct
58 Correct 872 ms 147032 KB Output is correct
59 Execution timed out 2059 ms 296176 KB Time limit exceeded
60 Halted 0 ms 0 KB -