Submission #647236

# Submission time Handle Problem Language Result Execution time Memory
647236 2022-10-02T03:07:56 Z atom Fireworks (APIO16_fireworks) C++17
100 / 100
215 ms 80328 KB
    #include <bits/stdc++.h>
    using namespace std;
     
    template<typename T>
    struct Line {
    	T m, b;
     
    	Line() : m(T()), b(T()) {}
     
    	Line(T _m, T _b) : m(_m), b(_b) {}
     
    	// T operator()(long long x) { return m * x + b; }
     
    	// friend long double intersect(const Line &l1, const Line &l2) { 
    	// 	return (long double)(l2.b - l1.b) / (long double)(l1.m - l2.m); 
    	// }
     
    	// friend ostream& operator<<(ostream &os, const Line &l) { 
    	// 	return os << '{' << l.m << "x + " << l.b << '}'; 
    	// }
    };
     
    int main() {
    	ios_base::sync_with_stdio(0); cin.tie(0);
     
    	int N, M;
    	cin >> N >> M;
    	M += N;
    	vector<long long> C(M);	
    	vector<vector<int>> T(M);
    	for (int i = 1; i < M; i++) {
    		int p;
    		long long c;
    		cin >> p >> c;
    		T[p - 1].push_back(i);
    		C[i] = c;
    	}
     
    	vector<Line<long long>> L(M);
    	vector<priority_queue<long long>> Q(M);
     
    	const auto dfs = [&](const auto &self, int u) -> void {
    		priority_queue<long long> &pq = Q[u];
    		if (T[u].empty()) {
    			L[u].m = 1, L[u].b = -C[u];
    			pq.push(C[u]);
    			pq.push(C[u]);
    			return;
    		}
     
    		for (int v : T[u]) {
    			self(self, v);
    			L[u].m += L[v].m, L[u].b += L[v].b;
    			if (Q[v].size() > pq.size())
    				pq.swap(Q[v]);
    			while (!Q[v].empty()) {
    				pq.push(Q[v].top());
    				Q[v].pop();
    			}
    		}
    		while (L[u].m > 1) { // slope is never greater than 1
    			long long x = pq.top(); pq.pop();
    			L[u].m--, L[u].b += x; // compensate for change in slope
    		}
    		long long t0 = pq.top(); pq.pop(); // location of slope 0 -> 1
    		long long t1 = pq.top(); pq.pop(); // location of slope -1 -> 0
    		pq.push(t0 + C[u]); // slope = -1 section increases by C[u]
    		pq.push(t1 + C[u]); // slope = 0 section increases by C[u]
    		L[u].b -= C[u]; // L[u].m = 1, compensate for adding C[u] to previous sections
    	};
     
    	dfs(dfs, 0);
     
    	// since f(x) is CCU <-> f"(x) >= 0, max f(x) for all x is at f'(x) = 0
    	priority_queue<long long> &pq = Q[0];
    	while (L[0].m > 0) { 
    		long long x = pq.top(); pq.pop();
    		L[0].m--, L[0].b += x;
    	}
    	cout << L[0].b << '\n';
    }
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 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 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 316 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 256 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 312 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 256 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 312 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 328 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 584 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 3 ms 888 KB Output is correct
37 Correct 3 ms 980 KB Output is correct
38 Correct 3 ms 972 KB Output is correct
39 Correct 3 ms 952 KB Output is correct
40 Correct 4 ms 1608 KB Output is correct
41 Correct 4 ms 1620 KB Output is correct
42 Correct 3 ms 852 KB Output is correct
43 Correct 3 ms 1236 KB Output is correct
44 Correct 3 ms 1108 KB Output is correct
45 Correct 3 ms 1108 KB Output is correct
46 Correct 3 ms 1108 KB Output is correct
47 Correct 3 ms 1108 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 4 ms 1068 KB Output is correct
50 Correct 3 ms 852 KB Output is correct
51 Correct 2 ms 852 KB Output is correct
52 Correct 3 ms 980 KB Output is correct
53 Correct 3 ms 960 KB Output is correct
54 Correct 3 ms 980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 316 KB Output is correct
3 Correct 1 ms 316 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 320 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 316 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 256 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 312 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 316 KB Output is correct
30 Correct 1 ms 328 KB Output is correct
31 Correct 1 ms 460 KB Output is correct
32 Correct 2 ms 468 KB Output is correct
33 Correct 2 ms 584 KB Output is correct
34 Correct 2 ms 724 KB Output is correct
35 Correct 2 ms 724 KB Output is correct
36 Correct 3 ms 888 KB Output is correct
37 Correct 3 ms 980 KB Output is correct
38 Correct 3 ms 972 KB Output is correct
39 Correct 3 ms 952 KB Output is correct
40 Correct 4 ms 1608 KB Output is correct
41 Correct 4 ms 1620 KB Output is correct
42 Correct 3 ms 852 KB Output is correct
43 Correct 3 ms 1236 KB Output is correct
44 Correct 3 ms 1108 KB Output is correct
45 Correct 3 ms 1108 KB Output is correct
46 Correct 3 ms 1108 KB Output is correct
47 Correct 3 ms 1108 KB Output is correct
48 Correct 3 ms 980 KB Output is correct
49 Correct 4 ms 1068 KB Output is correct
50 Correct 3 ms 852 KB Output is correct
51 Correct 2 ms 852 KB Output is correct
52 Correct 3 ms 980 KB Output is correct
53 Correct 3 ms 960 KB Output is correct
54 Correct 3 ms 980 KB Output is correct
55 Correct 7 ms 2124 KB Output is correct
56 Correct 29 ms 7240 KB Output is correct
57 Correct 56 ms 12148 KB Output is correct
58 Correct 79 ms 15420 KB Output is correct
59 Correct 101 ms 20460 KB Output is correct
60 Correct 132 ms 25300 KB Output is correct
61 Correct 153 ms 29228 KB Output is correct
62 Correct 170 ms 32196 KB Output is correct
63 Correct 215 ms 38136 KB Output is correct
64 Correct 213 ms 40020 KB Output is correct
65 Correct 110 ms 80328 KB Output is correct
66 Correct 114 ms 80320 KB Output is correct
67 Correct 113 ms 38172 KB Output is correct
68 Correct 150 ms 60468 KB Output is correct
69 Correct 177 ms 56896 KB Output is correct
70 Correct 177 ms 56924 KB Output is correct
71 Correct 174 ms 50480 KB Output is correct
72 Correct 171 ms 50792 KB Output is correct
73 Correct 147 ms 46952 KB Output is correct
74 Correct 151 ms 47120 KB Output is correct
75 Correct 148 ms 45884 KB Output is correct
76 Correct 159 ms 46016 KB Output is correct
77 Correct 157 ms 45228 KB Output is correct
78 Correct 160 ms 43324 KB Output is correct
79 Correct 151 ms 44820 KB Output is correct