| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 665750 | AmirAli_H1 | Beads and wires (APIO14_beads) | C++17 | 4 ms | 5088 KiB | 
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int	ll;
typedef long double	ld;
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pll;
#define all(x)		(x).begin(),(x).end()
#define len(x)		((ll) (x).size())
#define F		first
#define S		second
#define pb		push_back
#define sep             ' '
#define endl            '\n'
#define Mp		make_pair
#define debug(x)	cerr << #x << ": " <<  x << endl;
#define kill(x)		cout << x << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define file_io(x,y)	freopen(x, "r", stdin); freopen(y, "w", stdout);
int n;
const int maxn = 2e5 + 5;
vector<pll> adj[maxn];
ll dp[maxn][2];
bool mark[maxn];
void dfs(int v, ll x = -1) {
	mark[v] = 1;
	vector<ll> ls;
	for (auto f : adj[v]) {
		int u = f.F; ll w = f.S;
		if (!mark[u]) {
			dfs(u, w);
			dp[v][0] += dp[u][1];
			dp[v][1] += dp[u][1];
			ls.pb((dp[u][0] + w) - dp[u][1]);
		}
	}
	sort(all(ls), greater<ll>());
	if (len(ls) >= 2 && ls[0] + ls[1] > 0) dp[v][0] += (ls[0] + ls[1]);
	if (len(ls) >= 1 && x != -1 && x + ls[0] > 0) dp[v][1] += (x + ls[0]);
	dp[v][1] = max(dp[v][1], dp[v][0]);
	ls.clear();
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	
	file_io("beads.in", "beads.out");
	
	cin >> n;
	for (int i = 0; i < n - 1; i++) {
		int u, v; ll w;
		cin >> u >> v >> w; u--; v--;
		adj[u].pb(Mp(v, w)); adj[v].pb(Mp(u, w));
	}
	
	dfs(0);
	
	cout << dp[0][1] << endl;
	
	return 0;
}
Compilation message (stderr)
| # | 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... | ||||
