Problem:
Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

5 3
1 2
1 3
1 4
2 1
2 3

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0

Solution in Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

// quick sort with value
func QsortV(key Sortable) {
	key.qsort(0, len(key)-1)
}

type SortableItem struct {
	key1, key2, value int
}
type Sortable []SortableItem

// returns -1 then a is prior to b
func (s Sortable) isPrior(a, b int) int {
	if s[a].key1 == s[b].key2 && s[a].key2 == s[b].key2 {
		return 0
	} else if s[a].key1 > s[b].key1 || (s[a].key1 == s[b].key1 && s[a].key2 > s[b].key2) {
		return -1
	}
	return 1
}

func (s Sortable) swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func (s Sortable) partition(left, right int, pivot int) (int, int) {
	m := left
	for m < right {
		le := s.isPrior(m, pivot)
		if le < 0 {
			s.swap(left, m)
			left++
			m++
		} else if le > 0 {
			s.swap(right, m)
			right--
		} else {
			m++
		}
	}
	return left - 1, right
}

func (s Sortable) qsort(left, right int) {
	if right == left {
		return
	}
	if (right - left) == 1 {
		if s.isPrior(left, right) > 0 {
			s.swap(left, right)
		}
		return
	}

	ml, mr := s.partition(left, right, right)

	if ml > left {
		s.qsort(left, ml)
	}
	if mr < right {
		s.qsort(mr, right)
	}
}

func combination(n, r int) int {
	result := 1
	for i := n; i > (n - r); i-- {
		result *= i
	}
	for i := 2; i <= r; i++ {
		result /= i
	}
	return result
}

func _main() {
	fmt.Printf("%v\n", sortIndex([]int{1, 3, 1}, []int{0, 2, 1}))
}

func sortIndex(key1, key2 []int) []int {
	s := make(Sortable, len(key1))
	for i, _ := range s {
		s[i] = SortableItem{key1[i], key2[i], i}
	}
	QsortV(s)
	result := make([]int, len(key1))
	for i, v := range s {
		result[i] = v.value
	}
	return result
}

func main() {
	sc.Split(bufio.ScanWords)

	n := nextInt()
	m := nextInt()
	date := make([]int, n)
	salary := make([]int, n)

	for i := 0; i < n; i++ {
		date[i], salary[i] = nextInt(), nextInt()
	}

	total := 0
	salaryIndexDesc := sortIndex(salary, date)
	used := make([]bool, n)
	for i := 0; i < n; i++ {
		used[i] = false
	}
	for i := 0; i < m && i < n; i++ {
		t := -1
		for j := 0; j < n; j++ {
			if !used[j] {
				used[j] = true
				t = salaryIndexDesc[j]
				break
			}
		}
		if t == -1 {
			break
		}
		if i+date[t] <= m {
			//fmt.Printf("%d %d %d %d\n", i, t, salary[t], date[t])
			total += salary[t]
		}
	}

	fmt.Printf("%d\n", total)
}