Pagini recente » Cod sursa (job #577936) | Cod sursa (job #616790) | Cod sursa (job #2700186)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ifstream fin ("asmax.in");
ofstream fout ("asmax.out");
int n, nodeValue[16005], totalValue[16005];
vector<int> tree[16005];
void findValue(int node, int &maxPos) {
if (totalValue[node] > totalValue[maxPos])
maxPos = node;
for (int it : tree[node]) {
if (!totalValue[it]) {
totalValue[it] = totalValue[node] + nodeValue[it];
findValue(it, maxPos);
}
}
}
int main() {
fin.tie(0);
fout.tie(0);
ios::sync_with_stdio(0);
fin >> n;
for (int i = 1; i <= n; i++)
fin >> nodeValue[i];
int a, b;
for (int i = 1; i < n; i++) {
fin >> a >> b;
tree[a].push_back(b);
tree[b].push_back(a);
}
int maxPos = 1, minPos = 1;
totalValue[1] = nodeValue[1];
findValue(1, maxPos);
for (int i = 2; i < maxPos; i++)
if (totalValue[i] < totalValue[minPos])
minPos = i;
fout << totalValue[maxPos] - totalValue[minPos] + 1;
return 0;
}