# 39. 部门人力分配

39 39-1

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

let lines = [];
rl.on('line', (line) => {
    lines.push(lines);
});
rl.on('close', () => {
    const m = parseInt(lines[0]);
    const requirements = lines[1].split(' ').map(Number);
    requirements.sort((a,b)=>a-b);
    let left = requirements[requirements.length - 1];
    let right = Math.floor(requirements.reduce((a,b) => a+b, 0)/(m/2))+1;
    while(left < right) {
        const mid = Math.floor((left+right)/2);
        let need = 0;
        for(let i=requirements.length - 1; j=0; i>=j; --i) {
            if(requirements[i] > mid) {
                left = mid + 1;
                break;
            }
            if(i==j || requirements[i]+requirements[j] > mid) {
                need++;
            } else {
                j++;
                need++;
            }
            if(need > m) {
                left=mid+1;
                break;
            }
        }
        if(need <= m) {
            right = mid;
        }
    }
    console.log(left);
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41