var N = Math.floor(Math.random() * (18 - 2) + 2);

var study = [];
for (var i = 0; i < N; i++) {
    study.push(Math.floor(Math.random() * (N - 1) + 1));
}

while(N>2){
    var re = Math.min(...study);
    var tr = study.indexOf(re);

    while(tr===0 && tr === study.length-1){
        re++;
        tr = study.indexOf(re);
    }
    study = calcu(N, study, tr);
    N--;
}

N = study[0] + study[1];
  console.log(N);

function calcu(N, study, tr) {
  study[tr-1] = study[tr-1] + study[tr];
  study[tr+1] = study[tr+1] + study[tr];
  study.splice(tr, 1);

    return study;
}
