1637F - Towers - CodeForces Solution


constructive algorithms dfs and similar dp greedy trees *2500

Please click on ads to support us..

C++ Code:

/*
    IN THE NAME OF GOD
*/
#include <bits/stdc++.h>

// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef pair<pii, int> piii;
typedef pair<ll, pll> lpll;
typedef pair<pll, ll> plll;
typedef pair<pii, pii> piipii;
typedef pair<pll, pll> pllpll;
typedef long double ld;

#define F                                      first
#define S                                      second
#define Mp                                     make_pair
#define pb                                     push_back
#define pf                                     push_front
#define size(x)                                ((ll)x.size())
#define all(x)                                 (x).begin(),(x).end()
#define kill(x)		                           cout << x << '\n', exit(0);
#define set_dec(x)	                           cout << fixed << setprecision(x);
#define fuck(x)                                cout << "(" << #x << " , " << x << ")" << endl
#define endl                                   '\n'

const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7;
//ll Mod = 998244353;

inline ll MOD(ll a, ll mod=Mod) {
	a%=mod; (a<0)&&(a+=mod); return a;
}
inline ll max(ll a, ll b) {return (a>b ? a : b);}
inline ll min(ll a, ll b) {return (a<b ? a : b);}
inline ll abso(ll a) {return (a<0?-a:a);}
inline ll poww(ll a, ll b, ll mod=Mod) {
    ll ans = 1;
    a=MOD(a, mod);
    while (b) {
        if (b & 1) ans = MOD(ans*a, mod);
        b >>= 1;
        a = MOD(a*a, mod);
    }
    return ans;
}

ll n, c, mark[N], h[N], maxi[N], dp[N];
vector<int> adj[N];

void dfs(int v) {
	mark[v] = 1, maxi[v] = h[v];
	ll maxi2=0;
	for(int u : adj[v]) {
		if(mark[u]) continue;
		dfs(u);
		dp[v] += dp[u];
		maxi2 = max(maxi2, maxi[u]);
	}

	maxi[v] = max(maxi[v], maxi2);
	dp[v] += max(0, h[v]-maxi2);
}

int main () {
	ios_base::sync_with_stdio(false), cin.tie(0);

	cin>>n;
	for(int i=1; i<=n; i++) {
		cin>>h[i];
		if(h[i] > h[c]) c = i;
	}
	for(int v,u,i=1; i<n; i++) {
		cin>>v>>u;
		adj[v].pb(u); adj[u].pb(v);
	}

	mark[c] = 1;
	for(auto u : adj[c]) {
		dfs(u);
	}

	ll mx[2]={0,0};
	for(int v : adj[c]) {
		dp[c] += dp[v];
		mx[1] = max(mx[1], min(mx[0], maxi[v]));
		mx[0] = max(mx[0], maxi[v]);
	}

	dp[c] += (2*h[c] - (mx[0]+mx[1]));

	cout<<dp[c]<<endl;

	return 0;
}


Comments

Submit
0 Comments
More Questions

145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians