function solution(n, lost, reserve) {
const students = Array(n).fill(1);
const afterStudents = students.map((amount, i) => {
if (lost.includes(i + 1)) --amount;
if (reserve.includes(i + 1)) ++amount;
return amount;
});
afterStudents.forEach((amount, i, arr) => {
if (amount === 0 && i !== arr.length - 1 && arr[i + 1] === 2) {
arr[i] += 1;
arr[i + 1] -= 1;
} else if (amount === 0 && i !== 0 && arr[i - 1] === 2) {
arr[i] += 1;
arr[i - 1] -= 1;
}
});
return afterStudents.filter(amount => amount > 0).length;
}