Submission #491552

# Submission time Handle Problem Language Result Execution time Memory
491552 2021-12-03T05:00:38 Z hohohaha Fireworks (APIO16_fireworks) C++14
100 / 100
616 ms 154588 KB
// #pragma GCC optimize("Ofast")
// #pragma GCC targt("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
// #pragma GCC optimize("unroll-loops")

#include "bits/stdc++.h"

// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;

#define li long long
#define ld long double
#define ii pair<int, int>
#define vi vector<int> 
#define vvi vector<vi>


#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define eb emplace_back
#define em emplace
#define ob pop_back
#define om pop
#define of pop_front

#define fr front
#define bc back

#define fori(i, a, b) for(int i = (int) (a); i <= (int) (b); ++i)
#define ford(i, a, b) for(int i = (int) (a); i >= (int) (b); --i)

#define all(x) begin(x), end(x)
#define sz(x) ((int)(x).size())
#define bitc __builtin_popcountll

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand rng
#define endl '\n'
#define sp ' '

#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

void solve();

signed main() {
// freopen("output.out","w",stdout);
//   fastio();
   
   int tc = 1;
//  	cin >> tc;
   fori(test, 1, tc) {
      solve();
   }
   return 0;
}

const ld pi = 4 * atan(1.0), eps = 1e-9;

#define int long long
const int maxn = 1e6 + 5; 

int n,m; 

int p[maxn], cost[maxn]; 

multiset<int> func[maxn]; 
int f0[maxn]; 

vi g[maxn]; 

void print(int i) { 
	cout << f0[i] << endl;
	for(int v: func[i]) cout << v << sp; 
	cout << endl;
}

void dfs(int i) { 
//	cout << i << sp << g[i].size() << endl;
	if(i > n - m) { 
		f0[i] = cost[i];
		fori(tt, 0, 1) func[i].em(cost[i]); 
	}
	else {
		assert(g[i].size()); 
		for(int j: g[i]) {
			dfs(j); 
	
			if(func[j].size() > func[i].size()) { 	
				func[i].swap(func[j]); 
			}
			for(int v: func[j]) { 
				func[i].em(v); 
			}
			
			f0[i] += f0[j]; 
		}
		
//		cout <<i << sp << func[i].size() << endl;
		
		fori(tt, 1, g[i].size() - 1) { 
			func[i].erase(prev(func[i].end())); 
		}
			
//		cout << i << "// " ; //endl;
//		print(i); 
		
		if(i > 1) { 
			int r = *prev(func[i].end()); func[i].erase(func[i].find(r)); 
			int l = *prev(func[i].end()); func[i].erase(func[i].find(l)); 
		
			l += cost[i]; 
			r += cost[i]; 
			
			func[i].em(l); 
			func[i].em(r); 
			
			f0[i] += cost[i]; 
		}
	}
//	cout << i << ": " ; //endl;
//	print(i); 
}

void solve() {            
	cin >> n >> m; 
	
	n += m; 
	
	fori(i, 2, n) { 
		cin >> p[i] >> cost[i]; 
		g[p[i]].eb(i); 
	}
	
//	fori(i, 2, n) { 
//		cout << p[i] << sp  << i << sp << cost[i] << endl; 
//	}

	
	dfs(1); 
	
	int ans = f0[1]; 
	
//	cout << "dsfdf" << ans << endl;
	
	func[1].erase(prev(func[1].end())); 
	
	int nowslope = 0; 
	while(func[1].size()) {
		nowslope++;
		int nowpoint = *prev(func[1].end()); 
		func[1].erase(prev(func[1].end()));
		int nxtpoint = (func[1].size()? *prev(func[1].end()): 0); 
		ans -= nowslope * abs(nowpoint - nxtpoint); 
	}
//	cout << "sdgfsdghdfhfhg" << ans << endl; 
	cout << ans << endl;
}
# Verdict Execution time Memory Grader output
1 Correct 37 ms 70760 KB Output is correct
2 Correct 37 ms 70720 KB Output is correct
3 Correct 41 ms 70724 KB Output is correct
4 Correct 36 ms 70756 KB Output is correct
5 Correct 38 ms 70776 KB Output is correct
6 Correct 37 ms 70756 KB Output is correct
7 Correct 38 ms 70752 KB Output is correct
8 Correct 38 ms 70684 KB Output is correct
9 Correct 36 ms 70688 KB Output is correct
10 Correct 36 ms 70760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 70736 KB Output is correct
2 Correct 41 ms 70744 KB Output is correct
3 Correct 38 ms 70724 KB Output is correct
4 Correct 37 ms 70684 KB Output is correct
5 Correct 36 ms 70760 KB Output is correct
6 Correct 38 ms 70732 KB Output is correct
7 Correct 37 ms 70712 KB Output is correct
8 Correct 43 ms 70764 KB Output is correct
9 Correct 41 ms 70756 KB Output is correct
10 Correct 37 ms 70724 KB Output is correct
11 Correct 36 ms 70844 KB Output is correct
12 Correct 36 ms 70724 KB Output is correct
13 Correct 37 ms 70792 KB Output is correct
14 Correct 41 ms 70816 KB Output is correct
15 Correct 41 ms 70732 KB Output is correct
16 Correct 50 ms 70724 KB Output is correct
17 Correct 37 ms 70812 KB Output is correct
18 Correct 40 ms 70716 KB Output is correct
19 Correct 36 ms 70764 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 70760 KB Output is correct
2 Correct 37 ms 70720 KB Output is correct
3 Correct 41 ms 70724 KB Output is correct
4 Correct 36 ms 70756 KB Output is correct
5 Correct 38 ms 70776 KB Output is correct
6 Correct 37 ms 70756 KB Output is correct
7 Correct 38 ms 70752 KB Output is correct
8 Correct 38 ms 70684 KB Output is correct
9 Correct 36 ms 70688 KB Output is correct
10 Correct 36 ms 70760 KB Output is correct
11 Correct 38 ms 70736 KB Output is correct
12 Correct 41 ms 70744 KB Output is correct
13 Correct 38 ms 70724 KB Output is correct
14 Correct 37 ms 70684 KB Output is correct
15 Correct 36 ms 70760 KB Output is correct
16 Correct 38 ms 70732 KB Output is correct
17 Correct 37 ms 70712 KB Output is correct
18 Correct 43 ms 70764 KB Output is correct
19 Correct 41 ms 70756 KB Output is correct
20 Correct 37 ms 70724 KB Output is correct
21 Correct 36 ms 70844 KB Output is correct
22 Correct 36 ms 70724 KB Output is correct
23 Correct 37 ms 70792 KB Output is correct
24 Correct 41 ms 70816 KB Output is correct
25 Correct 41 ms 70732 KB Output is correct
26 Correct 50 ms 70724 KB Output is correct
27 Correct 37 ms 70812 KB Output is correct
28 Correct 40 ms 70716 KB Output is correct
29 Correct 36 ms 70764 KB Output is correct
30 Correct 41 ms 70780 KB Output is correct
31 Correct 45 ms 70884 KB Output is correct
32 Correct 40 ms 70988 KB Output is correct
33 Correct 45 ms 71072 KB Output is correct
34 Correct 45 ms 71144 KB Output is correct
35 Correct 44 ms 71136 KB Output is correct
36 Correct 43 ms 71236 KB Output is correct
37 Correct 41 ms 71400 KB Output is correct
38 Correct 42 ms 71492 KB Output is correct
39 Correct 44 ms 71564 KB Output is correct
40 Correct 44 ms 71816 KB Output is correct
41 Correct 40 ms 71736 KB Output is correct
42 Correct 45 ms 71024 KB Output is correct
43 Correct 52 ms 71716 KB Output is correct
44 Correct 42 ms 71620 KB Output is correct
45 Correct 41 ms 71612 KB Output is correct
46 Correct 42 ms 72004 KB Output is correct
47 Correct 43 ms 72000 KB Output is correct
48 Correct 41 ms 71744 KB Output is correct
49 Correct 44 ms 71784 KB Output is correct
50 Correct 52 ms 71588 KB Output is correct
51 Correct 52 ms 71624 KB Output is correct
52 Correct 41 ms 71628 KB Output is correct
53 Correct 41 ms 71668 KB Output is correct
54 Correct 41 ms 71916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 70760 KB Output is correct
2 Correct 37 ms 70720 KB Output is correct
3 Correct 41 ms 70724 KB Output is correct
4 Correct 36 ms 70756 KB Output is correct
5 Correct 38 ms 70776 KB Output is correct
6 Correct 37 ms 70756 KB Output is correct
7 Correct 38 ms 70752 KB Output is correct
8 Correct 38 ms 70684 KB Output is correct
9 Correct 36 ms 70688 KB Output is correct
10 Correct 36 ms 70760 KB Output is correct
11 Correct 38 ms 70736 KB Output is correct
12 Correct 41 ms 70744 KB Output is correct
13 Correct 38 ms 70724 KB Output is correct
14 Correct 37 ms 70684 KB Output is correct
15 Correct 36 ms 70760 KB Output is correct
16 Correct 38 ms 70732 KB Output is correct
17 Correct 37 ms 70712 KB Output is correct
18 Correct 43 ms 70764 KB Output is correct
19 Correct 41 ms 70756 KB Output is correct
20 Correct 37 ms 70724 KB Output is correct
21 Correct 36 ms 70844 KB Output is correct
22 Correct 36 ms 70724 KB Output is correct
23 Correct 37 ms 70792 KB Output is correct
24 Correct 41 ms 70816 KB Output is correct
25 Correct 41 ms 70732 KB Output is correct
26 Correct 50 ms 70724 KB Output is correct
27 Correct 37 ms 70812 KB Output is correct
28 Correct 40 ms 70716 KB Output is correct
29 Correct 36 ms 70764 KB Output is correct
30 Correct 41 ms 70780 KB Output is correct
31 Correct 45 ms 70884 KB Output is correct
32 Correct 40 ms 70988 KB Output is correct
33 Correct 45 ms 71072 KB Output is correct
34 Correct 45 ms 71144 KB Output is correct
35 Correct 44 ms 71136 KB Output is correct
36 Correct 43 ms 71236 KB Output is correct
37 Correct 41 ms 71400 KB Output is correct
38 Correct 42 ms 71492 KB Output is correct
39 Correct 44 ms 71564 KB Output is correct
40 Correct 44 ms 71816 KB Output is correct
41 Correct 40 ms 71736 KB Output is correct
42 Correct 45 ms 71024 KB Output is correct
43 Correct 52 ms 71716 KB Output is correct
44 Correct 42 ms 71620 KB Output is correct
45 Correct 41 ms 71612 KB Output is correct
46 Correct 42 ms 72004 KB Output is correct
47 Correct 43 ms 72000 KB Output is correct
48 Correct 41 ms 71744 KB Output is correct
49 Correct 44 ms 71784 KB Output is correct
50 Correct 52 ms 71588 KB Output is correct
51 Correct 52 ms 71624 KB Output is correct
52 Correct 41 ms 71628 KB Output is correct
53 Correct 41 ms 71668 KB Output is correct
54 Correct 41 ms 71916 KB Output is correct
55 Correct 55 ms 72804 KB Output is correct
56 Correct 105 ms 78356 KB Output is correct
57 Correct 154 ms 84228 KB Output is correct
58 Correct 182 ms 88132 KB Output is correct
59 Correct 259 ms 93828 KB Output is correct
60 Correct 334 ms 99820 KB Output is correct
61 Correct 346 ms 103776 KB Output is correct
62 Correct 411 ms 107844 KB Output is correct
63 Correct 449 ms 115956 KB Output is correct
64 Correct 500 ms 116608 KB Output is correct
65 Correct 316 ms 134348 KB Output is correct
66 Correct 304 ms 134244 KB Output is correct
67 Correct 299 ms 92300 KB Output is correct
68 Correct 403 ms 129436 KB Output is correct
69 Correct 450 ms 127096 KB Output is correct
70 Correct 443 ms 127044 KB Output is correct
71 Correct 512 ms 154588 KB Output is correct
72 Correct 504 ms 154112 KB Output is correct
73 Correct 527 ms 141240 KB Output is correct
74 Correct 494 ms 141208 KB Output is correct
75 Correct 566 ms 140196 KB Output is correct
76 Correct 556 ms 140472 KB Output is correct
77 Correct 533 ms 138336 KB Output is correct
78 Correct 616 ms 135300 KB Output is correct
79 Correct 609 ms 140008 KB Output is correct