https://www.acmicpc.net/problem/1417
const filePath = process.platform === 'linux' ? 0 : './input.txt';
let [N, me, ...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n')
.map(Number);
function solve() {
if (N === 1) {
console.log(0);
return;
}
const init = me;
while (Math.max(...arr) >= me) {
const maxIndex = arr.indexOf(Math.max(...arr));
arr[maxIndex] -= 1;
me += 1;
}
return console.log(me - init);
}
solve();