답안 #309857

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
309857 2020-10-04T17:45:12 Z peuch Chase (CEOI17_chase) C++17
40 / 100
4000 ms 97528 KB
#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 10;
const int MAXB = 110;

int n, b;
long long val[MAXN], sum[MAXN], pai[MAXN];
long long dp[MAXN][MAXB];

vector<int> ar[MAXN];

void calcDP(int cur);

int main(){
	scanf("%d %d", &n, &b);
	for(int i = 1; i <= n; i++)
		scanf("%d", &val[i]);
	
	for(int i = 1; i < n; i++){
		int a, b;
		scanf("%d %d", &a, &b);
		ar[a].push_back(b);
		ar[b].push_back(a);
		sum[a] += val[b];
		sum[b] += val[a];
	}
	long long ans = 0;
	for(int i = 1; i <= n; i++){
		pai[i] = 0;
		calcDP(i);
		ans = max(ans, dp[i][b]);
	}
	printf("%lld\n", ans);
}

void calcDP(int cur){
	for(int j = 0; j <= b; j++)
		dp[cur][j] = 0;
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(viz == pai[cur]) continue;
		pai[viz] = cur;
		calcDP(viz);
	}
	for(int i = 0; i < ar[cur].size(); i++){
		int viz = ar[cur][i];
		if(viz == pai[cur]) continue;
		dp[cur][0] = max(dp[cur][0], dp[viz][0]);
		for(int j = 1; j <= b; j++){
			dp[cur][j] = max(dp[cur][j], dp[viz][j - 1] + sum[cur] - val[pai[cur]]);
			dp[cur][j] = max(dp[cur][j], dp[viz][j]);
		}
	}
}

/*
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

10 1
1 2 3 4 5 6 7 8 9 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
8 10
*/

Compilation message

chase.cpp: In function 'int main()':
chase.cpp:18:11: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
   18 |   scanf("%d", &val[i]);
      |          ~^   ~~~~~~~
      |           |   |
      |           |   long long int*
      |           int*
      |          %lld
chase.cpp: In function 'void calcDP(int)':
chase.cpp:40:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
chase.cpp:46:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |  for(int i = 0; i < ar[cur].size(); i++){
      |                 ~~^~~~~~~~~~~~~~~~
chase.cpp: In function 'int main()':
chase.cpp:16:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   16 |  scanf("%d %d", &n, &b);
      |  ~~~~~^~~~~~~~~~~~~~~~~
chase.cpp:18:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   18 |   scanf("%d", &val[i]);
      |   ~~~~~^~~~~~~~~~~~~~~
chase.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   22 |   scanf("%d %d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 302 ms 3584 KB Output is correct
8 Correct 36 ms 3712 KB Output is correct
9 Correct 29 ms 3676 KB Output is correct
10 Correct 301 ms 3584 KB Output is correct
11 Correct 126 ms 3584 KB Output is correct
12 Correct 57 ms 3704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4094 ms 97528 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2688 KB Output is correct
2 Correct 2 ms 2688 KB Output is correct
3 Correct 2 ms 2688 KB Output is correct
4 Correct 2 ms 2688 KB Output is correct
5 Correct 2 ms 2688 KB Output is correct
6 Correct 2 ms 2688 KB Output is correct
7 Correct 302 ms 3584 KB Output is correct
8 Correct 36 ms 3712 KB Output is correct
9 Correct 29 ms 3676 KB Output is correct
10 Correct 301 ms 3584 KB Output is correct
11 Correct 126 ms 3584 KB Output is correct
12 Correct 57 ms 3704 KB Output is correct
13 Execution timed out 4094 ms 97528 KB Time limit exceeded
14 Halted 0 ms 0 KB -