# 37. 执行任务赚获取最多积分

37 37-1

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
let lineCount = 0;
let n, T;
let tasks = [];
rl.on('line', function(input) {
    if (lineCount === 0) {
        n = parseInt(line);
    } else if (lineCount === 1) {
        T = parseInt(line);
    } else {
        const [deadline, score] = line.split(' ').map(Number);
        tasks.push({deadline, score});
        if (tasks.length === n) {
            rl.close();
        }
    }
})
rl.on('close', () => {
    let obj = {};
    let arr = [];
    tasks.sort((a, b) => b.score - a.score);
    tasks.forEach(item => {
        if (obj[item.deadline]) {
            obj[item.deadline].push(item.score);
        } else {
            obj[item.deadline] = [item.score];
        }
    })
    for(let i in obj) {
        arr.push({time: i, value: obj[i]});
    }
    arr.sort((a, b) => b.time - a.time);
    let ans = [];
    for(let i=0; i<arr.length; i++) {
        let {time, value} = arr[i];
        if(T==0) {
            break;
        }
        T=T-1;
        ans.push(value[0])
        if (i+1 < arr.length) {
            arr[i+1].value = arr[i+1].value.concat(value.slice(1)).sort((a, b) => b-a);
        }
    }
    let total = 0;
    ans.forEach(val => total = total + val);
    console.log(total);
})

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
42
43
44
45
46
47
48
49
50
51
52
53