#include <iostream> #include <cstdio> using namespace std; const int INF = 1000000000; const int M = 10007; const int MaxN = 200000; template <class T> inline void tension(T &a, const T &b) { if (b < a) a = b; } template <class T> inline void relax(T &a, const T &b) { if (b > a) a = b; } struct halfEdge { int u; halfEdge *next; }; halfEdge adj_pool[(MaxN - 1) * 2], *adj_tail = adj_pool; int n; int wei[MaxN + 1]; halfEdge *adj[MaxN + 1]; inline void addEdge(int v, int u) { adj_tail->u = u, adj_tail->next = adj[v], adj[v] = adj_tail++; } int main() { //freopen("link.in", "r", stdin); //freopen("link.out", "w", stdout); cin >> n; for (int i = 0; i < n - 1; i++) { int v, u; scanf("%d %d", &v, &u); addEdge(v, u), addEdge(u, v); } for (int v = 1; v <= n; v++) scanf("%d", &wei[v]); int maxW = 0, sumW = 0; for (int v = 1; v <= n; v++) { int cur = 0; int fst = -1, snd = -1; for (halfEdge *e = adj[v]; e; e = e->next) { if (wei[e->u] > fst) snd = fst, fst = wei[e->u]; else if (wei[e->u] > snd) snd = wei[e->u]; cur = (cur + wei[e->u]) % M; sumW = (sumW + M - wei[e->u] * wei[e->u] % M) % M; } if (fst != -1 && snd != -1) relax(maxW, fst * snd); sumW = (sumW + cur * cur) % M; } cout << maxW << " " << sumW << endl; return 0; } /************************************************************** Problem: 2335 User: admin Language: C++ Result: Accepted Time:341 ms Memory:10668 kb ****************************************************************/