Pagini recente » Cod sursa (job #1037050) | Cod sursa (job #764557) | Cod sursa (job #2017012) | Cod sursa (job #2006118) | Cod sursa (job #2856880)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("asmax.in");
ofstream out("asmax.out");
const int N = 16000;
int viz[N + 1];
int suma[N + 1];
int val[N + 1];
vector<int> a[N + 1];
void asmax(int x) {
viz[x] = 1;
suma[x] = val[x];
for (auto y: a[x]) {
if (!viz[y]) {
asmax(y);
if (suma[y] > 0) {
suma[x] += suma[y];
}
}
}
}
int main() {
int n;
in >> n;
int x, y;
for (int i = 1; i <= n; i++) {
in >> val[i];
}
while (in >> x >> y) {
a[x].push_back(y);
a[y].push_back(x);
}
asmax(1);
int max = INT32_MIN;
for (int i = 1; i <= n; i++) {
if (suma[i] > max) {
max = suma[i];
}
}
out << max;
return 0;
}