function main(input) {
    var args = input.trim().split(/\r\n|\n/);
    var num = parseInt(args[0], 10);
    var sides = args.slice(1, args.length - 1).map(l => {
        return l.split(" ").map((v) => parseInt(v, 10));
    });
    var values = args[args.length - 1].split(" ").map(v => parseInt(v, 10));
    values.sort((a, b) => b - a);
    values = values.splice(0, num);

    var combinations = [];
    createArr(values, [], combinations, num);

    var result = 0;
    var resultCombination;
    for (var i = 0; combinations.length > i; i++) {
        var combination = combinations[i]
        var tempResult = 0;
        for (var j = 0; sides.length > j; j++) {
            tempResult += Math.min(combination[sides[j][0] - 1], combination[sides[j][1] - 1]);
        }

        if (result < tempResult) {
            result = tempResult;
            resultCombination = combination;
        }
    }

    console.log(result + '\n' + resultCombination.join(" "));
}

function createArr(values, arr, combinations, num) {
    if (arr.length === num) {
        combinations.push(arr);
        return;
    }

    for (var i = 0; values.length > i; i++) {
        var tempValues = values.concat();
        var tempArr = arr.concat();
        var removed = tempValues.splice(i, 1);
        tempArr.push(removed[0]);
        createArr(tempValues, tempArr, combinations, num);
    }
}

main(require("fs").readFileSync("/dev/stdin", "utf8"));