답안 #412366

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
412366 2021-05-26T17:57:01 Z soroush Fireworks (APIO16_fireworks) C++17
26 / 100
12 ms 16760 KB
//叫んで 藻掻もがいて 瞼まぶたを腫らしても
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll , ll> pii;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 3e5 + 10;
const ll mod = 1e9+7;

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}

int n , m;
vector < pii > adj[maxn];
priority_queue < int > slope[maxn];
ll ans = 0;

void merge(int u , int v){
	if(slope[u].size() > slope[v].size())
		swap(slope[u] , slope[v]);
	while(slope[u].size())
		slope[v].push(slope[u].top()) , slope[u].pop();
}

void dfs(int v, int w = 0){
	if(adj[v].empty())
		slope[v].push(0), slope[v].push(0);
	for(auto uw : adj[v]){
		int u = uw.first , w = uw.second;
		dfs(u , w);
		merge(u , v);
	}
	for(int i = 0 ; i < int(adj[v].size()) - 1 ; i ++)
		slope[v].pop();
	int a = slope[v].top();slope[v].pop();
	int b = slope[v].top();slope[v].pop();
	slope[v].push(a + w);
	slope[v].push(b + w);
}

int32_t main(){
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin >> n >> m;
	n += m;
	for(int i = 2 ; i <= n ; i ++){
		int v , w;
		cin >> v >> w;
		adj[v].pb({i , w});
		ans += w;
	}
	dfs(1);
	slope[1].pop();
	while(slope[1].size())
		ans-=slope[1].top() , slope[1].pop();
	cout << ans;
	return(0);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16716 KB Output is correct
2 Correct 11 ms 16636 KB Output is correct
3 Correct 11 ms 16760 KB Output is correct
4 Correct 11 ms 16716 KB Output is correct
5 Correct 11 ms 16672 KB Output is correct
6 Correct 11 ms 16716 KB Output is correct
7 Correct 12 ms 16716 KB Output is correct
8 Correct 11 ms 16716 KB Output is correct
9 Correct 12 ms 16760 KB Output is correct
10 Correct 12 ms 16760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16716 KB Output is correct
2 Correct 11 ms 16716 KB Output is correct
3 Correct 11 ms 16756 KB Output is correct
4 Correct 11 ms 16760 KB Output is correct
5 Correct 11 ms 16716 KB Output is correct
6 Correct 11 ms 16716 KB Output is correct
7 Correct 11 ms 16716 KB Output is correct
8 Correct 12 ms 16744 KB Output is correct
9 Correct 11 ms 16692 KB Output is correct
10 Correct 12 ms 16716 KB Output is correct
11 Correct 11 ms 16716 KB Output is correct
12 Correct 11 ms 16736 KB Output is correct
13 Correct 11 ms 16696 KB Output is correct
14 Correct 11 ms 16716 KB Output is correct
15 Correct 11 ms 16716 KB Output is correct
16 Correct 11 ms 16716 KB Output is correct
17 Correct 11 ms 16748 KB Output is correct
18 Correct 11 ms 16700 KB Output is correct
19 Correct 11 ms 16716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16716 KB Output is correct
2 Correct 11 ms 16636 KB Output is correct
3 Correct 11 ms 16760 KB Output is correct
4 Correct 11 ms 16716 KB Output is correct
5 Correct 11 ms 16672 KB Output is correct
6 Correct 11 ms 16716 KB Output is correct
7 Correct 12 ms 16716 KB Output is correct
8 Correct 11 ms 16716 KB Output is correct
9 Correct 12 ms 16760 KB Output is correct
10 Correct 12 ms 16760 KB Output is correct
11 Correct 11 ms 16716 KB Output is correct
12 Correct 11 ms 16716 KB Output is correct
13 Correct 11 ms 16756 KB Output is correct
14 Correct 11 ms 16760 KB Output is correct
15 Correct 11 ms 16716 KB Output is correct
16 Correct 11 ms 16716 KB Output is correct
17 Correct 11 ms 16716 KB Output is correct
18 Correct 12 ms 16744 KB Output is correct
19 Correct 11 ms 16692 KB Output is correct
20 Correct 12 ms 16716 KB Output is correct
21 Correct 11 ms 16716 KB Output is correct
22 Correct 11 ms 16736 KB Output is correct
23 Correct 11 ms 16696 KB Output is correct
24 Correct 11 ms 16716 KB Output is correct
25 Correct 11 ms 16716 KB Output is correct
26 Correct 11 ms 16716 KB Output is correct
27 Correct 11 ms 16748 KB Output is correct
28 Correct 11 ms 16700 KB Output is correct
29 Correct 11 ms 16716 KB Output is correct
30 Incorrect 11 ms 16688 KB Output isn't correct
31 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 16716 KB Output is correct
2 Correct 11 ms 16636 KB Output is correct
3 Correct 11 ms 16760 KB Output is correct
4 Correct 11 ms 16716 KB Output is correct
5 Correct 11 ms 16672 KB Output is correct
6 Correct 11 ms 16716 KB Output is correct
7 Correct 12 ms 16716 KB Output is correct
8 Correct 11 ms 16716 KB Output is correct
9 Correct 12 ms 16760 KB Output is correct
10 Correct 12 ms 16760 KB Output is correct
11 Correct 11 ms 16716 KB Output is correct
12 Correct 11 ms 16716 KB Output is correct
13 Correct 11 ms 16756 KB Output is correct
14 Correct 11 ms 16760 KB Output is correct
15 Correct 11 ms 16716 KB Output is correct
16 Correct 11 ms 16716 KB Output is correct
17 Correct 11 ms 16716 KB Output is correct
18 Correct 12 ms 16744 KB Output is correct
19 Correct 11 ms 16692 KB Output is correct
20 Correct 12 ms 16716 KB Output is correct
21 Correct 11 ms 16716 KB Output is correct
22 Correct 11 ms 16736 KB Output is correct
23 Correct 11 ms 16696 KB Output is correct
24 Correct 11 ms 16716 KB Output is correct
25 Correct 11 ms 16716 KB Output is correct
26 Correct 11 ms 16716 KB Output is correct
27 Correct 11 ms 16748 KB Output is correct
28 Correct 11 ms 16700 KB Output is correct
29 Correct 11 ms 16716 KB Output is correct
30 Incorrect 11 ms 16688 KB Output isn't correct
31 Halted 0 ms 0 KB -