제출 #336186

#제출 시각아이디문제언어결과실행 시간메모리
336186wiwihoSjekira (COCI20_sjekira)C++14
110 / 110
71 ms9964 KiB
#include<bits/stdc++.h> #define iter(a) a.begin(), a.end() #define pll pair<ll, ll> #define pii pair<int, int> #define mp make_pair #define F first #define S second #define lsort(a) sort(iter(a)) #define eb emplace_back #define gsort(a) sort(iter(a), greater<>()); using namespace std; typedef long long ll; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pll> a(n); for(int i = 1; i <= n; i++) cin >> a[i-1].F, a[i-1].S = i; gsort(a); vector<vector<int>> g(n + 1); vector<int> deg(n + 1); for(int i = 1; i < n; i++){ int u, v; cin >> u >> v; g[u].eb(v); g[v].eb(u); deg[u]++; deg[v]++; } ll ans = 0; for(int i = 0; i < n; i++){ ans += a[i].F * deg[a[i].S]; if(i) ans += a[i].F; for(int j : g[a[i].S]) deg[j]--; } cout << ans << "\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...