답안 #411176

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
411176 2021-05-24T13:54:36 Z Blagojce Chase (CEOI17_chase) C++11
70 / 100
496 ms 95940 KB
#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)


using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1e9+7;
const ld eps = 1e-13;
const ld pi  = 3.14159265359;

mt19937 _rand(time(NULL));
clock_t z;

const int mxn = 2e5+5;

int n, v;
ll a[mxn];

vector<int> g[mxn];

ll ans = 0;

void dfs(int u, int p, pq<ll> Q, ll sum){
	ll nsum = 0;
	for(auto e : g[u]){
		if(e == p) continue;
		nsum += a[e];
	}
	Q.push(-nsum);
	sum += nsum;
	if((int)Q.size() > v){
		sum += Q.top();
		Q.pop();
	}
	ans = max(ans, sum);
	
	
	
	for(auto e : g[u]){
		if(e == p) continue;
		dfs(e, u, Q, sum);
	}
}

void solve(){	
	cin >> n >> v;
	fr(i, 0, n){
		cin >> a[i];
	}
	fr(i, 0, n-1){
		int u, v;
		cin >> u >> v;
		--u, --v;
		g[u].pb(v);
		g[v].pb(u);
	}
	if(n <= 1000){
		fr(i, 0, n){
			pq <ll> init;
			dfs(i, i, init, 0);
		}
	}	
	else{
		pq<ll> init;
		dfs(0, 0, init, 0);
	}
	cout<<ans<<endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
}/*
12 2
2 3 3 8 1 5 6 7 8 3 5 4
2 1
2 7
3 4
4 7
7 6
5 6
6 8
6 9
7 10
10 11
10 12
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 496 ms 5992 KB Output is correct
8 Correct 105 ms 5196 KB Output is correct
9 Correct 78 ms 5040 KB Output is correct
10 Correct 106 ms 5068 KB Output is correct
11 Correct 112 ms 5068 KB Output is correct
12 Correct 127 ms 5068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 164 ms 94244 KB Output is correct
2 Correct 156 ms 95940 KB Output is correct
3 Correct 51 ms 11344 KB Output is correct
4 Correct 66 ms 11092 KB Output is correct
5 Correct 97 ms 11100 KB Output is correct
6 Correct 90 ms 11036 KB Output is correct
7 Correct 72 ms 11112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 496 ms 5992 KB Output is correct
8 Correct 105 ms 5196 KB Output is correct
9 Correct 78 ms 5040 KB Output is correct
10 Correct 106 ms 5068 KB Output is correct
11 Correct 112 ms 5068 KB Output is correct
12 Correct 127 ms 5068 KB Output is correct
13 Correct 164 ms 94244 KB Output is correct
14 Correct 156 ms 95940 KB Output is correct
15 Correct 51 ms 11344 KB Output is correct
16 Correct 66 ms 11092 KB Output is correct
17 Correct 97 ms 11100 KB Output is correct
18 Correct 90 ms 11036 KB Output is correct
19 Correct 72 ms 11112 KB Output is correct
20 Incorrect 91 ms 11160 KB Output isn't correct
21 Halted 0 ms 0 KB -