이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
// is short or still long ???
hollwo_pelw's template(short)
// Note : -Dhollwo_pelw_local
*/
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
void FAST_IO(string filein = "", string fileout = "", string fileerr = ""){
	if (fopen(filein.c_str(), "r")){
		freopen(filein.c_str(), "r", stdin);
		freopen(fileout.c_str(), "w", stdout);
#ifdef hollwo_pelw_local
		freopen(fileerr.c_str(), "w", stderr);
#endif
	}
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main(){
#ifdef hollwo_pelw_local
	FAST_IO("input.inp", "output.out", "error.err");
	auto start = chrono::steady_clock::now();
#else
	FAST_IO("hollwo_pelw.inp", "hollwo_pelw.out");
#endif
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
	return 0;
}
const int N = 1e5 + 5;
struct maxk {
	int k; int64_t val;
	multiset<int64_t> in, out;
	inline void __init__(int _k) { k = _k; }
	inline void add(int64_t v) {
		if (*in.begin() >= v && in.size() == k) {
			out.insert(v);
		} else {
			in.insert(v);
			val += v;
			if (in.size() > k) {
				v = *in.begin();
				out.insert(v);
				
				in.erase(in.begin());
				val -= v;
			}
		}
	}
	inline void del(int64_t v) {
		if (out.find(v) != out.end()) {
			out.erase(out.find(v));
		} else {
			in.erase(in.find(v));
			val -= v;
			if (out.size() > 0) {
				v = *out.rbegin();
				out.erase(out.find(v));
				in.insert(v);
				val += v;
			}
		}
	}
	inline int64_t get_val() {
		return val;
	}
} calc;
int n, k;
vector<pair<int, int>> adj[N];
int64_t up[N], dw[N], res[N];
void dfs_dw(int u, int p) {
	for (auto [v, w] : adj[u]) if (v != p) {
		dfs_dw(v, u);
		dw[u] = max(dw[u], dw[v] + w);
	}
}
void dfs_up(int u, int p) {
	int64_t vf = 0, vs = 0;
	int f = 0, s = 0;
	for (auto [v, w] : adj[u])
		if (v != p) {
			int64_t vv = dw[v] + w;
			if (vf < vv) swap(f, v), swap(vf, vv);
			if (vs < vv) swap(s, v), swap(vs, vv);
		}
	for (auto [v, w] : adj[u])
		if (v != p && v != f) {
			calc.add(dw[v] + w);
	}
	for (auto [v, w] : adj[u])
		if (v != p) {
			up[v] = max(up[u], v == f ? vs : vf) + w;
			dfs_up(v, u);
		}
}
void reroot(int u, int p) {
	// cout << "solve " << u << '\n';
	res[u] = calc.get_val();
	for (auto [v, w] : adj[u])
		if (v != p) {
			calc.add(dw[v]), calc.del(dw[v] + w);
			calc.add(up[v]), calc.del(up[v] - w);
			reroot(v, u);
			calc.add(dw[v] + w), calc.del(dw[v]);
			calc.add(up[v] - w), calc.del(up[v]);
		}
}
void Hollwo_Pelw() {
	cin >> n >> k;
	for (int i = 1, u, v, w; i < n; i++) {
		cin >> u >> v >> w;
		adj[u].emplace_back(v, w);
		adj[v].emplace_back(u, w);
	}
	calc.__init__(k);
	dfs_dw(1, 0);
	dfs_up(1, 0);
	calc.add(dw[1]);
	if (adj[1].size() == 1)
		calc.add(up[1]);
	reroot(1, 0);
	for (int i = 1; i <= n; i++)
		cout << res[i] << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In member function 'void maxk::add(int64_t)':
Main.cpp:58:37: warning: comparison of integer expressions of different signedness: 'std::multiset<long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   58 |   if (*in.begin() >= v && in.size() == k) {
      |                           ~~~~~~~~~~^~~~
Main.cpp:64:18: warning: comparison of integer expressions of different signedness: 'std::multiset<long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   64 |    if (in.size() > k) {
      |        ~~~~~~~~~~^~~
Main.cpp: In function 'void FAST_IO(std::string, std::string, std::string)':
Main.cpp:18:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   18 |   freopen(filein.c_str(), "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:19:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |   freopen(fileout.c_str(), "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |