← Home
For problem statement at 0-999/200-299/270-279/276/problemC.txt this is a correct solution, but verifier at 0-999/200-299/270-279/276/verifierC.go ends with All tests passed can you fix the verifier? package main

import (
	"fmt"
	"os"
	"sort"
)

func main() {
	buf := make([]byte, 65536)
	var pos, length int

	nextChar := func() byte {
		if pos >= length {
			n, err := os.Stdin.Read(buf)
			if n == 0 || err != nil {
				return 0
			}
			pos = 0
			length = n
		}
		res := buf[pos]
		pos++
		return res
	}

	nextInt := func() int {
		c := nextChar()
		for c <= 32 {
			if c == 0 {
				return 0
			}
			c = nextChar()
		}
		res := 0
		for c > 32 {
			res = res*10 + int(c-'0')
			c = nextChar()
		}
		return res
	}

	n := nextInt()
	if n == 0 {
		return
	}
	q := nextInt()

	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt()
	}

	diff := make([]int, n+2)
	for i := 0; i < q; i++ {
		l := nextInt()
		r := nextInt()
		diff[l]++
		diff[r+1]--
	}

	freq := make([]int, n)
	current := 0
	for i := 1; i <= n; i++ {
		current += diff[i]
		freq[i-1] = current
	}

	sort.Ints(a)
	sort.Ints(freq)

	var ans int64
	for i := n - 1; i >= 0; i-- {
		if freq[i] == 0 {
			break
		}
		ans += int64(a[i]) * int64(freq[i])
	}

	fmt.Println(ans)
}