← Home
```go
package main

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

func main() {
	// Use buffered scanner for fast I/O
	scanner := bufio.NewScanner(os.Stdin)
	// Increase buffer size to handle lines with many integers
	const maxCapacity = 20 * 1024 * 1024
	scanner.Buffer(make([]byte, maxCapacity), maxCapacity)
	scanner.Split(bufio.ScanWords)

	// Helper function to read the next integer
	readNext := func() int {
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())
		return v
	}

	// Read n
	n := readNext()
	// Read m (not strictly needed for calculation as we compute maxH from data)
	readNext()

	a := make([]int, n)
	var sum int64 = 0
	maxH := 0

	// Read the stack heights
	for i := 0; i < n; i++ {
		val := readNext()
		a[i] = val
		sum += int64(val)
		if val > maxH {
			maxH = val
		}
	}

	// Sort capacities to greedily maximize the number of distinct heights 
	// we can cover with the mandatory 1-block-per-column constraint.
	sort.Ints(a)

	// k represents the maximum number of distinct heights {1, 2, ..., k} 
	// that can be covered by choosing one block from a subset of columns.
	k := 0
	for _, x := range a {
		if x > k {
			k++
		}
	}

	// We must keep:
	// 1. One block for each of the n columns (Top View).
	// 2. Extra blocks for heights not covered by the set of n blocks.
	// The n blocks can cover k distinct heights (specifically 1..k).
	// The total heights to be covered are 1..maxH.
	// So we need (maxH - k) additional blocks.
	// Total kept = n + (maxH - k).
	// Total removed = sum - Total kept.

	ans := sum - int64(n) - int64(maxH) + int64(k)
	fmt.Println(ans)
}
```