Submission #81953

# Submission time Handle Problem Language Result Execution time Memory
81953 2018-10-28T08:52:06 Z memikakizaki Fireworks (APIO16_fireworks) C++14
100 / 100
328 ms 101808 KB
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author memikakizaki
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
using namespace std;
#define long long long
const long MOD = 1e9+7, LINF = 1e18 + 1e16;
const int INF = 1e9+1;
const double EPS = 1e-10;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};


class Fireworks {
	int n, m;
	vector<vector<pair<int,long> > > g;
	vector<priority_queue<long> > pq;
	vector<long> val;

	void print_pq_increasing(priority_queue<long>& pq) {
		vector<long> res;
		while(!pq.empty()) {
			res.push_back(pq.top());
			pq.pop();
		}
		reverse(res.begin(), res.end());
		for(long x: res) pq.push(x), cout << x << ' ';
		cout << endl;
	}

	void dfs(int u, long wpar) {
		long cnt = 0;
		for(auto pr: g[u]) {
			int v;
			long w;
			tie(v, w) = pr;
			dfs(v, w);
			if(pq[u].empty()) {
				val[u] += val[v];
			} else if(pq[v].top() > pq[u].top()) {
				val[u] += val[v] + 1ll * cnt * abs(pq[v].top() - pq[u].top());
			} else {
				val[u] += val[v] + abs(pq[u].top() - pq[v].top());
			}
			if(pq[v].size() > pq[u].size()) {
				swap(pq[u], pq[v]);
			}
			while(!pq[v].empty()) pq[u].push(pq[v].top()), pq[v].pop();
			++cnt;
		}
		long r;
		while(cnt--) {
			r = pq[u].top();
			pq[u].pop();
			val[u] -= 1ll * cnt * (r - pq[u].top()); // already --
		}
		if(!g[u].empty()) {
			long l = pq[u].top();
			pq[u].pop();
			pq[u].push(l + wpar);
			pq[u].push(r + wpar);
//			cout << "l " << l << ' ' << "r " << r << endl;
		} else {
			pq[u].push(wpar);
			pq[u].push(wpar);
		}
//		cout << "node " << u << endl;
//		cout << "val " << val[u] << endl;
//		for(auto pr:g[u]) {
//			int v; tie(v, ignore) = pr;
////			cout << v << ' ' << val[v] << endl;
//		}
//		print_pq_increasing(pq[u]);
//		cout << endl;
	}

public:
	void solve(istream& cin, ostream& cout) {
		cin >> n >> m;
		n += m;
		g = vector<vector<pair<int,long> > >(n);
		pq = vector<priority_queue<long> >(n);
		val = vector<long>(n);
		for(int i = 1, p, c; i < n; i++) {
			cin >> p >> c;
			g[p-1].emplace_back(i, c);
		}
		dfs(0, 0);
//		for(int i = 0; i < n; i++) cout << i << ' ' << val[i] << endl;
		cout << val[0] << endl;
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	Fireworks solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 516 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 528 KB Output is correct
8 Correct 2 ms 532 KB Output is correct
9 Correct 2 ms 536 KB Output is correct
10 Correct 3 ms 548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 548 KB Output is correct
2 Correct 2 ms 592 KB Output is correct
3 Correct 3 ms 708 KB Output is correct
4 Correct 2 ms 748 KB Output is correct
5 Correct 2 ms 748 KB Output is correct
6 Correct 2 ms 748 KB Output is correct
7 Correct 2 ms 748 KB Output is correct
8 Correct 2 ms 792 KB Output is correct
9 Correct 2 ms 792 KB Output is correct
10 Correct 2 ms 792 KB Output is correct
11 Correct 2 ms 792 KB Output is correct
12 Correct 2 ms 792 KB Output is correct
13 Correct 2 ms 792 KB Output is correct
14 Correct 2 ms 792 KB Output is correct
15 Correct 3 ms 792 KB Output is correct
16 Correct 2 ms 792 KB Output is correct
17 Correct 3 ms 792 KB Output is correct
18 Correct 3 ms 792 KB Output is correct
19 Correct 2 ms 792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 516 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 528 KB Output is correct
8 Correct 2 ms 532 KB Output is correct
9 Correct 2 ms 536 KB Output is correct
10 Correct 3 ms 548 KB Output is correct
11 Correct 2 ms 548 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 708 KB Output is correct
14 Correct 2 ms 748 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 2 ms 748 KB Output is correct
17 Correct 2 ms 748 KB Output is correct
18 Correct 2 ms 792 KB Output is correct
19 Correct 2 ms 792 KB Output is correct
20 Correct 2 ms 792 KB Output is correct
21 Correct 2 ms 792 KB Output is correct
22 Correct 2 ms 792 KB Output is correct
23 Correct 2 ms 792 KB Output is correct
24 Correct 2 ms 792 KB Output is correct
25 Correct 3 ms 792 KB Output is correct
26 Correct 2 ms 792 KB Output is correct
27 Correct 3 ms 792 KB Output is correct
28 Correct 3 ms 792 KB Output is correct
29 Correct 2 ms 792 KB Output is correct
30 Correct 3 ms 792 KB Output is correct
31 Correct 3 ms 864 KB Output is correct
32 Correct 4 ms 880 KB Output is correct
33 Correct 4 ms 1032 KB Output is correct
34 Correct 4 ms 1192 KB Output is correct
35 Correct 4 ms 1236 KB Output is correct
36 Correct 5 ms 1544 KB Output is correct
37 Correct 5 ms 1544 KB Output is correct
38 Correct 6 ms 1676 KB Output is correct
39 Correct 8 ms 1744 KB Output is correct
40 Correct 5 ms 2328 KB Output is correct
41 Correct 6 ms 2368 KB Output is correct
42 Correct 4 ms 2368 KB Output is correct
43 Correct 5 ms 2368 KB Output is correct
44 Correct 7 ms 2368 KB Output is correct
45 Correct 6 ms 2368 KB Output is correct
46 Correct 5 ms 2384 KB Output is correct
47 Correct 6 ms 2448 KB Output is correct
48 Correct 6 ms 2448 KB Output is correct
49 Correct 6 ms 2448 KB Output is correct
50 Correct 5 ms 2448 KB Output is correct
51 Correct 5 ms 2456 KB Output is correct
52 Correct 5 ms 2560 KB Output is correct
53 Correct 5 ms 2708 KB Output is correct
54 Correct 6 ms 2916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 516 KB Output is correct
3 Correct 2 ms 516 KB Output is correct
4 Correct 2 ms 516 KB Output is correct
5 Correct 2 ms 516 KB Output is correct
6 Correct 2 ms 516 KB Output is correct
7 Correct 2 ms 528 KB Output is correct
8 Correct 2 ms 532 KB Output is correct
9 Correct 2 ms 536 KB Output is correct
10 Correct 3 ms 548 KB Output is correct
11 Correct 2 ms 548 KB Output is correct
12 Correct 2 ms 592 KB Output is correct
13 Correct 3 ms 708 KB Output is correct
14 Correct 2 ms 748 KB Output is correct
15 Correct 2 ms 748 KB Output is correct
16 Correct 2 ms 748 KB Output is correct
17 Correct 2 ms 748 KB Output is correct
18 Correct 2 ms 792 KB Output is correct
19 Correct 2 ms 792 KB Output is correct
20 Correct 2 ms 792 KB Output is correct
21 Correct 2 ms 792 KB Output is correct
22 Correct 2 ms 792 KB Output is correct
23 Correct 2 ms 792 KB Output is correct
24 Correct 2 ms 792 KB Output is correct
25 Correct 3 ms 792 KB Output is correct
26 Correct 2 ms 792 KB Output is correct
27 Correct 3 ms 792 KB Output is correct
28 Correct 3 ms 792 KB Output is correct
29 Correct 2 ms 792 KB Output is correct
30 Correct 3 ms 792 KB Output is correct
31 Correct 3 ms 864 KB Output is correct
32 Correct 4 ms 880 KB Output is correct
33 Correct 4 ms 1032 KB Output is correct
34 Correct 4 ms 1192 KB Output is correct
35 Correct 4 ms 1236 KB Output is correct
36 Correct 5 ms 1544 KB Output is correct
37 Correct 5 ms 1544 KB Output is correct
38 Correct 6 ms 1676 KB Output is correct
39 Correct 8 ms 1744 KB Output is correct
40 Correct 5 ms 2328 KB Output is correct
41 Correct 6 ms 2368 KB Output is correct
42 Correct 4 ms 2368 KB Output is correct
43 Correct 5 ms 2368 KB Output is correct
44 Correct 7 ms 2368 KB Output is correct
45 Correct 6 ms 2368 KB Output is correct
46 Correct 5 ms 2384 KB Output is correct
47 Correct 6 ms 2448 KB Output is correct
48 Correct 6 ms 2448 KB Output is correct
49 Correct 6 ms 2448 KB Output is correct
50 Correct 5 ms 2448 KB Output is correct
51 Correct 5 ms 2456 KB Output is correct
52 Correct 5 ms 2560 KB Output is correct
53 Correct 5 ms 2708 KB Output is correct
54 Correct 6 ms 2916 KB Output is correct
55 Correct 12 ms 3816 KB Output is correct
56 Correct 49 ms 8868 KB Output is correct
57 Correct 99 ms 14336 KB Output is correct
58 Correct 125 ms 18536 KB Output is correct
59 Correct 157 ms 24824 KB Output is correct
60 Correct 207 ms 31804 KB Output is correct
61 Correct 242 ms 38096 KB Output is correct
62 Correct 262 ms 43948 KB Output is correct
63 Correct 303 ms 53072 KB Output is correct
64 Correct 328 ms 59020 KB Output is correct
65 Correct 153 ms 96808 KB Output is correct
66 Correct 158 ms 101808 KB Output is correct
67 Correct 176 ms 101808 KB Output is correct
68 Correct 214 ms 101808 KB Output is correct
69 Correct 314 ms 101808 KB Output is correct
70 Correct 298 ms 101808 KB Output is correct
71 Correct 251 ms 101808 KB Output is correct
72 Correct 322 ms 101808 KB Output is correct
73 Correct 227 ms 101808 KB Output is correct
74 Correct 243 ms 101808 KB Output is correct
75 Correct 208 ms 101808 KB Output is correct
76 Correct 222 ms 101808 KB Output is correct
77 Correct 246 ms 101808 KB Output is correct
78 Correct 221 ms 101808 KB Output is correct
79 Correct 237 ms 101808 KB Output is correct