Submission #914667


Source Code Expand

#include<bits/stdc++.h>
#define reps(i,j,k) for(int i=(j);i<(k);++i)
#define rep(i,j) reps(i,0,j)
#define pb push_back
#define fs first
#define sc second
#define mk make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vi;

template<class T>
ostream &operator<<(ostream &out, const vector<T> &v){
	out << "{";
	rep(i,v.size())
		out << v[i] << ", ";
	return out << "}" << endl;
}

template<class S, class T>
ostream &operator<<(ostream &out, const pair<S,T> p){
	return out << "(" << p.fs << ", " << p.sc << ")";
}



class Node{
public:
	int cost;
	vector<pii> edge;//to, dist
	Node(){}
	
};

int reach[2001];
vector<ll> u;
vector<Node> v;

vi dfs(int current, int dist){
	reach[current] = 1;
	vi dists;
	ll maxdist = 0;
	rep(i,v[current].edge.size()){
		pii t = v[current].edge[i];
		if(reach[t.fs]) continue;
		vi tmp = dfs(t.fs, t.sc);
		rep(j,tmp.size()){
			dists.pb(tmp[j]);
			if(maxdist < tmp[j]){
				maxdist = tmp[j];
			}
		}
	}
	//sort(dists.rbegin(),dists.rend());
	if(dists.size()!=0){ 
		rep(i,dists.size()) if(dists[i] == maxdist) {
			dists[i] += dist;
			break;
		}
	}
	else dists.pb(dist);
//	cout << "dfs; " << current << ": " << dists;
	return dists;
}

int main(){
	int n,m;
	cin >> n >> m;
	v.resize(n+1);
	rep(i,n-1){
		int a, b, c;
		cin >> a >> b >> c;
		v[a].edge.pb(pii(b,c));
		v[b].edge.pb(pii(a,c));
	}

	rep(i,n)
		cin >> v[i+1].cost;
	
	vector<ll> u;

	reps(i,1,
	//	2){
		n+1){
		fill(reach, reach+2001, 0);
		
		vi tmp = dfs(i,0);
	//	if(i==1){
	//		cout << i << endl;
	//		cout << tmp;
	//	}
		rep(j,tmp.size())u.pb(tmp[j] * v[i].cost);
	}
	sort(u.rbegin(),u.rend());
	ll ans =0;
	rep(i,min(m,(int)u.size())) ans += u[i];
	cout << ans << endl;	
	//cout << u;
	return 0;
}

Submission Info

Submission Time
Task D - Content Delivery
User progLESS
Language C++11 (GCC 4.9.2)
Score 100
Code Size 1855 Byte
Status AC
Exec Time 1116 ms
Memory 33848 KiB

Judge Result

Set Name All
Score / Max Score 100 / 100
Status
AC × 60
Set Name Test Cases
All 00_sample_00, 00_sample_01, 10_00_Random_0006, 10_01_Random_0037, 10_02_Random_0863, 10_03_Random_2000, 10_04_Random_0007, 10_05_Random_0054, 10_06_Random_0139, 10_07_Random_2000, 10_08_Random_0010, 10_09_Random_0084, 10_10_Random_0737, 10_11_Random_2000, 10_12_Random_0007, 10_13_Random_0062, 10_14_Random_0868, 10_15_Random_2000, 20_00_Star_0009, 20_01_Star_0014, 20_02_Star_0675, 20_03_Star_2000, 20_04_Star_2000, 20_05_Star_0004, 20_06_Star_0050, 20_07_Star_0969, 20_08_Star_2000, 20_09_Star_2000, 30_00_Line_0005, 30_01_Line_0011, 30_02_Line_0369, 30_03_Line_2000, 30_04_Line_2000, 30_05_Line_0009, 30_06_Line_0035, 30_07_Line_0217, 30_08_Line_2000, 30_09_Line_2000, 40_00_Caterpillar_0096, 40_01_Caterpillar_0167, 40_02_Caterpillar_2000, 40_03_Caterpillar_2000, 40_04_Caterpillar_0026, 40_05_Caterpillar_0775, 40_06_Caterpillar_2000, 40_07_Caterpillar_2000, 50_00_Skew_2000, 50_01_Skew_2000, 50_02_Skew_2000, 50_03_Skew_2000, 50_04_Skew_2000, 50_05_Skew_2000, 60_00_Binary_0995, 60_01_Binary_2000, 60_02_Binary_2000, 60_03_Binary_0476, 60_04_Binary_2000, 60_05_Binary_2000, 80_max_00, 90_teuch_00
Case Name Status Exec Time Memory
00_sample_00 AC 19 ms 800 KiB
00_sample_01 AC 17 ms 800 KiB
10_00_Random_0006 AC 17 ms 800 KiB
10_01_Random_0037 AC 17 ms 796 KiB
10_02_Random_0863 AC 182 ms 5020 KiB
10_03_Random_2000 AC 998 ms 17416 KiB
10_04_Random_0007 AC 18 ms 796 KiB
10_05_Random_0054 AC 17 ms 792 KiB
10_06_Random_0139 AC 20 ms 924 KiB
10_07_Random_2000 AC 953 ms 17420 KiB
10_08_Random_0010 AC 17 ms 800 KiB
10_09_Random_0084 AC 20 ms 928 KiB
10_10_Random_0737 AC 141 ms 2968 KiB
10_11_Random_2000 AC 1026 ms 17364 KiB
10_12_Random_0007 AC 17 ms 796 KiB
10_13_Random_0062 AC 19 ms 928 KiB
10_14_Random_0868 AC 191 ms 5012 KiB
10_15_Random_2000 AC 1077 ms 17364 KiB
20_00_Star_0009 AC 17 ms 924 KiB
20_01_Star_0014 AC 18 ms 796 KiB
20_02_Star_0675 AC 85 ms 5004 KiB
20_03_Star_2000 AC 619 ms 33784 KiB
20_04_Star_2000 AC 852 ms 33784 KiB
20_05_Star_0004 AC 17 ms 796 KiB
20_06_Star_0050 AC 19 ms 928 KiB
20_07_Star_0969 AC 150 ms 9096 KiB
20_08_Star_2000 AC 623 ms 33780 KiB
20_09_Star_2000 AC 629 ms 33780 KiB
30_00_Line_0005 AC 17 ms 800 KiB
30_01_Line_0011 AC 17 ms 796 KiB
30_02_Line_0369 AC 46 ms 1396 KiB
30_03_Line_2000 AC 1026 ms 17352 KiB
30_04_Line_2000 AC 988 ms 17408 KiB
30_05_Line_0009 AC 18 ms 800 KiB
30_06_Line_0035 AC 18 ms 928 KiB
30_07_Line_0217 AC 27 ms 1184 KiB
30_08_Line_2000 AC 1116 ms 17400 KiB
30_09_Line_2000 AC 1009 ms 17400 KiB
40_00_Caterpillar_0096 AC 19 ms 928 KiB
40_01_Caterpillar_0167 AC 24 ms 1180 KiB
40_02_Caterpillar_2000 AC 1061 ms 33848 KiB
40_03_Caterpillar_2000 AC 1049 ms 33784 KiB
40_04_Caterpillar_0026 AC 19 ms 800 KiB
40_05_Caterpillar_0775 AC 194 ms 9156 KiB
40_06_Caterpillar_2000 AC 855 ms 33844 KiB
40_07_Caterpillar_2000 AC 855 ms 33848 KiB
50_00_Skew_2000 AC 760 ms 17356 KiB
50_01_Skew_2000 AC 789 ms 17388 KiB
50_02_Skew_2000 AC 754 ms 17408 KiB
50_03_Skew_2000 AC 784 ms 17396 KiB
50_04_Skew_2000 AC 767 ms 17364 KiB
50_05_Skew_2000 AC 791 ms 17352 KiB
60_00_Binary_0995 AC 179 ms 5000 KiB
60_01_Binary_2000 AC 696 ms 17412 KiB
60_02_Binary_2000 AC 703 ms 17408 KiB
60_03_Binary_0476 AC 56 ms 1948 KiB
60_04_Binary_2000 AC 691 ms 17352 KiB
60_05_Binary_2000 AC 691 ms 17404 KiB
80_max_00 AC 251 ms 1180 KiB
90_teuch_00 AC 18 ms 928 KiB