function solution(no, works) {
let result = 0;
function compareNumbers(a, b) {
return b - a;
}
do {
works.sort(compareNumbers);
const max = works[0]
const min = works[works.length - 1]
if ((max - min) > 0) {
works[0] = works[0] - (max - min);
no = no - (max - min);
} else {
works[0] = works[0] - 1;
no = no - 1
}
} while (no > 0)
works.forEach((item) => { result += Math.pow(item, 2) })
return result;
}