Submission #647263

# Submission time Handle Problem Language Result Execution time Memory
647263 2022-10-02T05:28:34 Z atom Fireworks (APIO16_fireworks) C++17
100 / 100
238 ms 70756 KB
    #include <bits/stdc++.h>
    using namespace std;
         
	const int MAX = 3e5 + 5;

	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 << '}'; 
    	}
    };

	vector<long long> C(MAX);	
    vector<vector<int>> T(MAX);
	vector<Line<long long>> L(MAX);
    vector<priority_queue<long long>> Q(MAX);
     
	void dfs (int u){
    		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]) {
    			dfs(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
    	}

    int main() {
    	ios_base::sync_with_stdio(0); cin.tie(0);
     
    	int N, M;
    	cin >> N >> M;
    	M += N;
    	for (int i = 1; i < M; i++) {
    		int p;
    		long long c;
    		cin >> p >> c;
    		T[p - 1].push_back(i);
    		C[i] = c;
    	}

    	
     
    	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 12 ms 23704 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23720 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 11 ms 23772 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23768 KB Output is correct
2 Correct 14 ms 23732 KB Output is correct
3 Correct 12 ms 23764 KB Output is correct
4 Correct 15 ms 23764 KB Output is correct
5 Correct 12 ms 23776 KB Output is correct
6 Correct 14 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 12 ms 23808 KB Output is correct
9 Correct 11 ms 23784 KB Output is correct
10 Correct 13 ms 23764 KB Output is correct
11 Correct 15 ms 23764 KB Output is correct
12 Correct 12 ms 23764 KB Output is correct
13 Correct 14 ms 23764 KB Output is correct
14 Correct 14 ms 23764 KB Output is correct
15 Correct 16 ms 23804 KB Output is correct
16 Correct 12 ms 23732 KB Output is correct
17 Correct 11 ms 23780 KB Output is correct
18 Correct 12 ms 23840 KB Output is correct
19 Correct 11 ms 23764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23704 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23720 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 11 ms 23772 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23804 KB Output is correct
11 Correct 13 ms 23768 KB Output is correct
12 Correct 14 ms 23732 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 15 ms 23764 KB Output is correct
15 Correct 12 ms 23776 KB Output is correct
16 Correct 14 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23808 KB Output is correct
19 Correct 11 ms 23784 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 15 ms 23764 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 14 ms 23764 KB Output is correct
24 Correct 14 ms 23764 KB Output is correct
25 Correct 16 ms 23804 KB Output is correct
26 Correct 12 ms 23732 KB Output is correct
27 Correct 11 ms 23780 KB Output is correct
28 Correct 12 ms 23840 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 12 ms 23840 KB Output is correct
31 Correct 15 ms 23856 KB Output is correct
32 Correct 13 ms 23892 KB Output is correct
33 Correct 13 ms 23928 KB Output is correct
34 Correct 13 ms 23892 KB Output is correct
35 Correct 16 ms 23892 KB Output is correct
36 Correct 16 ms 23936 KB Output is correct
37 Correct 14 ms 24020 KB Output is correct
38 Correct 13 ms 24020 KB Output is correct
39 Correct 14 ms 24020 KB Output is correct
40 Correct 13 ms 24532 KB Output is correct
41 Correct 13 ms 24532 KB Output is correct
42 Correct 13 ms 23892 KB Output is correct
43 Correct 13 ms 24276 KB Output is correct
44 Correct 14 ms 24188 KB Output is correct
45 Correct 15 ms 24148 KB Output is correct
46 Correct 14 ms 24148 KB Output is correct
47 Correct 15 ms 24096 KB Output is correct
48 Correct 20 ms 24040 KB Output is correct
49 Correct 18 ms 24020 KB Output is correct
50 Correct 14 ms 24020 KB Output is correct
51 Correct 14 ms 24036 KB Output is correct
52 Correct 15 ms 24068 KB Output is correct
53 Correct 14 ms 24124 KB Output is correct
54 Correct 13 ms 24056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23704 KB Output is correct
2 Correct 12 ms 23764 KB Output is correct
3 Correct 11 ms 23764 KB Output is correct
4 Correct 11 ms 23720 KB Output is correct
5 Correct 12 ms 23764 KB Output is correct
6 Correct 12 ms 23764 KB Output is correct
7 Correct 12 ms 23764 KB Output is correct
8 Correct 11 ms 23772 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23804 KB Output is correct
11 Correct 13 ms 23768 KB Output is correct
12 Correct 14 ms 23732 KB Output is correct
13 Correct 12 ms 23764 KB Output is correct
14 Correct 15 ms 23764 KB Output is correct
15 Correct 12 ms 23776 KB Output is correct
16 Correct 14 ms 23764 KB Output is correct
17 Correct 12 ms 23764 KB Output is correct
18 Correct 12 ms 23808 KB Output is correct
19 Correct 11 ms 23784 KB Output is correct
20 Correct 13 ms 23764 KB Output is correct
21 Correct 15 ms 23764 KB Output is correct
22 Correct 12 ms 23764 KB Output is correct
23 Correct 14 ms 23764 KB Output is correct
24 Correct 14 ms 23764 KB Output is correct
25 Correct 16 ms 23804 KB Output is correct
26 Correct 12 ms 23732 KB Output is correct
27 Correct 11 ms 23780 KB Output is correct
28 Correct 12 ms 23840 KB Output is correct
29 Correct 11 ms 23764 KB Output is correct
30 Correct 12 ms 23840 KB Output is correct
31 Correct 15 ms 23856 KB Output is correct
32 Correct 13 ms 23892 KB Output is correct
33 Correct 13 ms 23928 KB Output is correct
34 Correct 13 ms 23892 KB Output is correct
35 Correct 16 ms 23892 KB Output is correct
36 Correct 16 ms 23936 KB Output is correct
37 Correct 14 ms 24020 KB Output is correct
38 Correct 13 ms 24020 KB Output is correct
39 Correct 14 ms 24020 KB Output is correct
40 Correct 13 ms 24532 KB Output is correct
41 Correct 13 ms 24532 KB Output is correct
42 Correct 13 ms 23892 KB Output is correct
43 Correct 13 ms 24276 KB Output is correct
44 Correct 14 ms 24188 KB Output is correct
45 Correct 15 ms 24148 KB Output is correct
46 Correct 14 ms 24148 KB Output is correct
47 Correct 15 ms 24096 KB Output is correct
48 Correct 20 ms 24040 KB Output is correct
49 Correct 18 ms 24020 KB Output is correct
50 Correct 14 ms 24020 KB Output is correct
51 Correct 14 ms 24036 KB Output is correct
52 Correct 15 ms 24068 KB Output is correct
53 Correct 14 ms 24124 KB Output is correct
54 Correct 13 ms 24056 KB Output is correct
55 Correct 18 ms 24404 KB Output is correct
56 Correct 41 ms 26084 KB Output is correct
57 Correct 70 ms 27732 KB Output is correct
58 Correct 80 ms 28724 KB Output is correct
59 Correct 120 ms 30464 KB Output is correct
60 Correct 188 ms 32064 KB Output is correct
61 Correct 162 ms 33652 KB Output is correct
62 Correct 189 ms 34600 KB Output is correct
63 Correct 238 ms 36480 KB Output is correct
64 Correct 220 ms 36996 KB Output is correct
65 Correct 123 ms 70736 KB Output is correct
66 Correct 114 ms 70756 KB Output is correct
67 Correct 110 ms 33220 KB Output is correct
68 Correct 152 ms 53316 KB Output is correct
69 Correct 186 ms 50256 KB Output is correct
70 Correct 178 ms 50160 KB Output is correct
71 Correct 182 ms 46124 KB Output is correct
72 Correct 170 ms 46532 KB Output is correct
73 Correct 152 ms 43116 KB Output is correct
74 Correct 190 ms 43116 KB Output is correct
75 Correct 160 ms 42244 KB Output is correct
76 Correct 154 ms 42232 KB Output is correct
77 Correct 171 ms 41676 KB Output is correct
78 Correct 168 ms 39856 KB Output is correct
79 Correct 145 ms 41388 KB Output is correct