You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: p1484.go
    path of file: ./repos/codeforces-go/misc/luogu
    
    the code of the file until where you have to start completion: package main

import (
	"bufio"
	. "fmt"
	"io"
	"sort"
)

// https://space.bilibili.com/206214
func run1484(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, k, ans, mx int
	Fscan(in, &n, &k)
    
func run1484(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, k, ans, mx int
	Fscan(in, &n, &k)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		if a[i] > mx {
			mx = a[i]
		}
	}
	sort.Search(mx+1, func(fee int) bool {
		var f0, f1, cnt0, cnt1 int
		for _, v := range a {
			v -= fee
			if f1 > f0+v { // 不选
				f0 = f1
				cnt0 = cnt1
			} else { // 选（相等时也选）
				f0, f1 = f1, f0+v
				cnt0, cnt1 = cnt1, cnt0+1
			}
		}
		if cnt1 >= k {
			ans = f1 + k*fee
			return false
		}
		if fee == 0 {
			ans = f1
		}
		return true
	})
	Fprint(out, ans)
}