Submission #242405


Source Code Expand

Copy
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<25;
typedef pair<int,int> P;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vertex;
typedef vector<vertex> graph;

ll dp[5010][5010];

int main(){
	int n;
	cin>>n;
	vector<ll> s(n);
	for(int i = 0; i < n; i++){
		cin>>s[i];
	}
	graph g(n);
	for(int i = 0; i < n-1; i++){
		int a,b;
		cin>>a>>b;
		a--;b--;
		g[a].push_back(b);
	}
	int m;
	cin>>m;
	vector<ll> t(m);
	for(int i = 0; i < m; i++){
		cin>>t[i];
	}
	sort(t.begin(), t.end());
	reverse(t.begin(), t.end());
	ll sum = 0;
	for(int i = 0; i < n; i++){
		sum += s[i];
	}
	ll res = sum;
	memset(dp, -1, sizeof(dp));
	for(int i = n-1; i >= 0; i--){
		dp[i][0] = 0;
		dp[i][1] = s[i];
		for(int j = 0; j < g[i].size(); j++){
			for(int k = 0; k <= j; k++){
				for(int l = 1; l <= g[j].size()+1; l++){
					dp[i][k+l] = min(dp[i][k+l]<0?INF:dp[i][k+l], dp[i][k]+dp[j][l]);
				}
			}
		}
	}
	ll sumt = 0;
	for(int i = 0; i < n; i++){
		sumt += t[i];
		if(dp[0][i+1]>=0){
			res = max(res, sum-dp[i][i+1]+sumt);
		}
	}
	cout<<res<<endl;

	return 0;
}

Submission Info

Submission Time
Task D - 高橋君と木のおもちゃ
User Lepton
Language C++ (G++ 4.6.4)
Score 0
Code Size 1562 Byte
Status
Exec Time 451 ms
Memory 197160 KB

Judge Result

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask0-sample-01.txt, subtask0-sample-02.txt
Subtask1 0 / 10 subtask0-sample-01.txt, subtask0-sample-02.txt, subtask1-01.txt, subtask1-02.txt, subtask1-03.txt, subtask1-04.txt, subtask1-05.txt, subtask1-06.txt, subtask1-07.txt, subtask1-08.txt, subtask1-09.txt, subtask1-10.txt, subtask1-11.txt, subtask1-12.txt, subtask1-13.txt, subtask1-14.txt, subtask1-15.txt, subtask1-16.txt, subtask1-17.txt, subtask1-18.txt, subtask1-19.txt, subtask1-20.txt, subtask1-21.txt, subtask1-22.txt, subtask1-23.txt, subtask1-24.txt, subtask1-25.txt, subtask1-26.txt, subtask1-27.txt, subtask1-28.txt, subtask1-29.txt, subtask1-30.txt
Subtask2 0 / 20 subtask0-sample-01.txt, subtask0-sample-02.txt, subtask1-01.txt, subtask1-02.txt, subtask1-03.txt, subtask1-04.txt, subtask1-05.txt, subtask1-06.txt, subtask1-07.txt, subtask1-08.txt, subtask1-09.txt, subtask1-10.txt, subtask1-11.txt, subtask1-12.txt, subtask1-13.txt, subtask1-14.txt, subtask1-15.txt, subtask1-16.txt, subtask1-17.txt, subtask1-18.txt, subtask1-19.txt, subtask1-20.txt, subtask1-21.txt, subtask1-22.txt, subtask1-23.txt, subtask1-24.txt, subtask1-25.txt, subtask1-26.txt, subtask1-27.txt, subtask1-28.txt, subtask1-29.txt, subtask1-30.txt, subtask2-01.txt, subtask2-02.txt, subtask2-03.txt, subtask2-04.txt, subtask2-05.txt, subtask2-06.txt, subtask2-07.txt, subtask2-08.txt, subtask2-09.txt, subtask2-10.txt, subtask2-11.txt, subtask2-12.txt, subtask2-13.txt, subtask2-14.txt, subtask2-15.txt, subtask2-16.txt, subtask2-17.txt, subtask2-18.txt, subtask2-19.txt, subtask2-20.txt, subtask2-21.txt, subtask2-22.txt, subtask2-23.txt, subtask2-24.txt, subtask2-25.txt, subtask2-26.txt, subtask2-27.txt, subtask2-28.txt, subtask2-29.txt, subtask2-30.txt
Subtask3 0 / 70 subtask0-sample-01.txt, subtask0-sample-02.txt, subtask1-01.txt, subtask1-02.txt, subtask1-03.txt, subtask1-04.txt, subtask1-05.txt, subtask1-06.txt, subtask1-07.txt, subtask1-08.txt, subtask1-09.txt, subtask1-10.txt, subtask1-11.txt, subtask1-12.txt, subtask1-13.txt, subtask1-14.txt, subtask1-15.txt, subtask1-16.txt, subtask1-17.txt, subtask1-18.txt, subtask1-19.txt, subtask1-20.txt, subtask1-21.txt, subtask1-22.txt, subtask1-23.txt, subtask1-24.txt, subtask1-25.txt, subtask1-26.txt, subtask1-27.txt, subtask1-28.txt, subtask1-29.txt, subtask1-30.txt, subtask2-01.txt, subtask2-02.txt, subtask2-03.txt, subtask2-04.txt, subtask2-05.txt, subtask2-06.txt, subtask2-07.txt, subtask2-08.txt, subtask2-09.txt, subtask2-10.txt, subtask2-11.txt, subtask2-12.txt, subtask2-13.txt, subtask2-14.txt, subtask2-15.txt, subtask2-16.txt, subtask2-17.txt, subtask2-18.txt, subtask2-19.txt, subtask2-20.txt, subtask2-21.txt, subtask2-22.txt, subtask2-23.txt, subtask2-24.txt, subtask2-25.txt, subtask2-26.txt, subtask2-27.txt, subtask2-28.txt, subtask2-29.txt, subtask2-30.txt, subtask3-01.txt, subtask3-02.txt, subtask3-03.txt, subtask3-04.txt, subtask3-05.txt, subtask3-06.txt, subtask3-07.txt, subtask3-08.txt, subtask3-09.txt, subtask3-10.txt, subtask3-11.txt, subtask3-12.txt, subtask3-13.txt, subtask3-14.txt, subtask3-15.txt, subtask3-16.txt, subtask3-17.txt, subtask3-18.txt, subtask3-19.txt, subtask3-20.txt, subtask3-21.txt, subtask3-22.txt, subtask3-23.txt, subtask3-24.txt, subtask3-25.txt, subtask3-26.txt, subtask3-27.txt, subtask3-28.txt, subtask3-29.txt, subtask3-30.txt
Case Name Status Exec Time Memory
subtask0-sample-01.txt 427 ms 196772 KB
subtask0-sample-02.txt 423 ms 196772 KB
subtask1-01.txt 427 ms 196772 KB
subtask1-02.txt 424 ms 196772 KB
subtask1-03.txt 432 ms 196772 KB
subtask1-04.txt 431 ms 196892 KB
subtask1-05.txt 434 ms 196768 KB
subtask1-06.txt 429 ms 196764 KB
subtask1-07.txt 431 ms 196768 KB
subtask1-08.txt 431 ms 196776 KB
subtask1-09.txt 430 ms 196776 KB
subtask1-10.txt 432 ms 196772 KB
subtask1-11.txt 430 ms 196896 KB
subtask1-12.txt 426 ms 196776 KB
subtask1-13.txt 431 ms 196976 KB
subtask1-14.txt 425 ms 196780 KB
subtask1-15.txt 427 ms 196772 KB
subtask1-16.txt 430 ms 196764 KB
subtask1-17.txt 425 ms 196764 KB
subtask1-18.txt 430 ms 196900 KB
subtask1-19.txt 430 ms 196772 KB
subtask1-20.txt 431 ms 196768 KB
subtask1-21.txt 430 ms 196776 KB
subtask1-22.txt 424 ms 196764 KB
subtask1-23.txt 428 ms 196768 KB
subtask1-24.txt 429 ms 196764 KB
subtask1-25.txt 429 ms 196828 KB
subtask1-26.txt 428 ms 196776 KB
subtask1-27.txt 434 ms 196896 KB
subtask1-28.txt 440 ms 196772 KB
subtask1-29.txt 428 ms 196768 KB
subtask1-30.txt 430 ms 196768 KB
subtask2-01.txt 434 ms 196760 KB
subtask2-02.txt 429 ms 196896 KB
subtask2-03.txt 429 ms 196896 KB
subtask2-04.txt 429 ms 196772 KB
subtask2-05.txt 432 ms 196772 KB
subtask2-06.txt 426 ms 196764 KB
subtask2-07.txt 426 ms 196776 KB
subtask2-08.txt 428 ms 196900 KB
subtask2-09.txt 422 ms 196772 KB
subtask2-10.txt 428 ms 196900 KB
subtask2-11.txt 426 ms 196776 KB
subtask2-12.txt 427 ms 196768 KB
subtask2-13.txt 427 ms 196904 KB
subtask2-14.txt 425 ms 196764 KB
subtask2-15.txt 424 ms 196768 KB
subtask2-16.txt 429 ms 196776 KB
subtask2-17.txt 429 ms 196892 KB
subtask2-18.txt 429 ms 196776 KB
subtask2-19.txt 424 ms 196900 KB
subtask2-20.txt 428 ms 196764 KB
subtask2-21.txt 428 ms 196780 KB
subtask2-22.txt 425 ms 196772 KB
subtask2-23.txt 429 ms 196776 KB
subtask2-24.txt 427 ms 196896 KB
subtask2-25.txt 427 ms 196772 KB
subtask2-26.txt 426 ms 196768 KB
subtask2-27.txt 426 ms 196892 KB
subtask2-28.txt 436 ms 196900 KB
subtask2-29.txt 439 ms 196896 KB
subtask2-30.txt 433 ms 196896 KB
subtask3-01.txt 432 ms 196900 KB
subtask3-02.txt 433 ms 196892 KB
subtask3-03.txt 434 ms 196956 KB
subtask3-04.txt 433 ms 196900 KB
subtask3-05.txt 432 ms 196956 KB
subtask3-06.txt 439 ms 196956 KB
subtask3-07.txt 437 ms 197036 KB
subtask3-08.txt 439 ms 197088 KB
subtask3-09.txt 437 ms 197028 KB
subtask3-10.txt 439 ms 197036 KB
subtask3-11.txt 440 ms 197028 KB
subtask3-12.txt 445 ms 197032 KB
subtask3-13.txt 447 ms 197028 KB
subtask3-14.txt 449 ms 197024 KB
subtask3-15.txt 441 ms 197152 KB
subtask3-16.txt 441 ms 197160 KB
subtask3-17.txt 443 ms 197160 KB
subtask3-18.txt 444 ms 197088 KB
subtask3-19.txt 442 ms 197160 KB
subtask3-20.txt 439 ms 197160 KB
subtask3-21.txt 447 ms 197156 KB
subtask3-22.txt 441 ms 197152 KB
subtask3-23.txt 440 ms 197156 KB
subtask3-24.txt 451 ms 197148 KB
subtask3-25.txt 443 ms 197152 KB
subtask3-26.txt 443 ms 197156 KB
subtask3-27.txt 443 ms 197160 KB
subtask3-28.txt 444 ms 197084 KB
subtask3-29.txt 444 ms 197156 KB
subtask3-30.txt 443 ms 197088 KB