/**
 * Author: Sergey Kopeliovich (burunduk30@gmail.com)
 */

#include <bits/stdc++.h>
#include "optimization.h"

using namespace std;

#define forn(i, n) for (int i = 0; i < n; i++)

typedef long long ll;
typedef pair<ll, int> pii;

const int N = 5e4;

int n, w0[N], t0[N], w[N], t[N];
int root, p[N], o[N], pa[N], Next[N], last[N], start[N];

int get(int v) {
    return v == p[v] ? v : (p[v] = get(p[v]));
}

struct Comp {
    bool operator() ( int i, int j ) const {
        return pii((ll)t[i] * w[j], i) < pii((ll)t[j] * w[i], j);
    }
};
set<int, Comp> heap;

int main() {
    // #define NAME "p1outtreewc"
    // assert(freopen(NAME ".in", "r", stdin));
    // assert(freopen(NAME ".out", "w", stdout));

    int n = readInt();
    assert(n <= N);
    forn(i, n) p[i] = o[i] = last[i] = i, pa[i] = Next[i] = -1;
    forn(i, n) t0[i] = t[i] = readInt();
    forn(i, n) w0[i] = w[i] = readInt();
    forn(i, n - 1) {
        int u = readInt() - 1;
        int v = readInt() - 1;
        pa[u] = v;
    }
    forn(i, n) heap.insert(i);
    while (heap.size()) {
        int i = *heap.begin(), j = pa[i];
        heap.erase(heap.begin());
        if (j == -1) {
            root = i;
            continue;
        }
        heap.erase(j = get(j));
        Next[last[j]] = i, last[j] = last[i];
        p[i] = j, w[j] += w[i], t[j] += t[i];       
        heap.insert(j);
    }
    ll ans = 0, cnt = 0;
    for (int T = 0, j = root; j != -1; j = Next[j]) 
        start[j] = T, T += t0[j], ans += (ll)T * w0[j], cnt++;
    assert(cnt == n);
    writeInt(ans, '\n');
    forn(j, n)
        writeInt(start[j], ' ');
}
