Pavel has several sticks with lengths equal to powers of two.He has $$$a_0$$$ sticks of length $$$2^0 = 1$$$, $$$a_1$$$ sticks of length $$$2^1 = 2$$$, ..., $$$a_{n-1}$$$ sticks of length $$$2^{n-1}$$$. Pavel wants to make the maximum possible number of triangles using these sticks. The triangles should have strictly positive area, each stick can be used in at most one triangle.It is forbidden to break sticks, and each triangle should consist of exactly three sticks.Find the maximum possible number of triangles.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 300\,000$$$) — the number of different lengths of sticks. The second line contains $$$n$$$ integers $$$a_0$$$, $$$a_1$$$, ..., $$$a_{n-1}$$$ ($$$1 \leq a_i \leq 10^9$$$), where $$$a_i$$$ is the number of sticks with the length equal to $$$2^i$$$.

Print a single integer — the maximum possible number of non-degenerate triangles that Pavel can make.

NoteIn the first example, Pavel can, for example, make this set of triangles (the lengths of the sides of the triangles are listed): $$$(2^0, 2^4, 2^4)$$$, $$$(2^1, 2^3, 2^3)$$$, $$$(2^1, 2^2, 2^2)$$$.In the second example, Pavel cannot make a single triangle.In the third example, Pavel can, for example, create this set of triangles (the lengths of the sides of the triangles are listed): $$$(2^0, 2^0, 2^0)$$$, $$$(2^1, 2^1, 2^1)$$$, $$$(2^2, 2^2, 2^2)$$$.

package main

import (
	"bufio"
	. "fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, v, left int
	ans := int64(0)
	for Fscan(in, &n); n > 0; n-- {
		Fscan(in, &v)
		if left > 0 {
			if v > 2*left {
				ans += int64(left)
				v -= 2 * left
				left = 0
			} else {
				ans += int64(v / 2)
				left -= v / 2
				v %= 2
			}
		}
		ans += int64(v / 3)
		left += v % 3
	}
	Print(ans)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1119E(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, left int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := int64(0)
	for _, v := range a {
		if left > 0 {
			if v > 2*left {
				ans += int64(left)
				v -= 2 * left
				left = 0
			} else {
				ans += int64(v / 2)
				left -= v / 2
				v %= 2
			}
		}
		ans += int64(v / 3)
		left += v % 3
	}
	Fprint(out, ans)
}

func main() { CF1119E(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	. "fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, left int
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	ans := int64(0)
	for _, v := range a {
		if left > 0 {
			if v > 2*left {
				ans += int64(left)
				v -= 2 * left
				left = 0
			} else {
				ans += int64(v / 2)
				left -= v / 2
				v %= 2
			}
		}
		ans += int64(v / 3)
		left += v % 3
	}
	Print(ans)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	counter := int64(0)
	used := int64(0)
	for i := n - 1; i >= 0; i-- {
		if (left - used) / 3 > a[i] / 2 {
			left -= a[i]
			counter += a[i] / 2
			used += a[i] / 2 * 3 - a[i]
			if used < 0 {
				used = 0
			}
		} else {
			counter += (left - used) / 3
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	used := int64(0)
	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		if (left - used) / 3 > a[i] / 2 {
			triangles := a[i] / 2
			left -= a[i]

			used += triangles * 3 - a[i]
			if used < 0 {
				left += used
				used = 0
			}
			counter += triangles
		} else {
			triangles := (left - used) / 3
			counter += triangles
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	use := int64(0)
	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		triangles := min((left - use) / 3, a[i] / 2)
		left -= a[i]
		counter += triangles

		use += triangles * 3 - a[i]
		if left - use < 3 {
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	used := int64(0)
	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		triangles := min((left - used) / 3, a[i] / 2)
		left -= a[i]
		counter += triangles

		used += max(triangles * 3 - a[i], 0)
		if left - used < 3 {
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	use := int64(0)
	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		triangles := min((left - use) / 3, a[i] / 2)
		left -= a[i]
		counter += triangles

		use += triangles * 3 - a[i]

		if a[i] == 1 {
			if use == 0 {
				left--
			} else {
				use--
			}
		}
		if left < 3 {
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	used := int64(0)
	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		triangles := min((left - used) / 3, a[i] / 2)
		counter += triangles
		left -= a[i]

		if triangles * 3 > a[i] {
			used += triangles * 3 - a[i]
		}
		if left - used < 3 {
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	used := int64(0)
	counter := int64(0)
	/*for i := n - 1; i >= 0; i-- {
		triangles := min((left - used) / 3, a[i] / 2)
		counter += triangles
		left -= a[i]

		if triangles * 3 > a[i] {
			used += triangles * 3 - a[i]
		}
		//if left - used < 3 {
		//	break
		}
	}*/

	for i := n - 1; i >= 0; i-- {
		if left - used / 3 > a[i] / 2 {
			triangles := a[i] / 2
			left -= a[i]

			used += triangles * 3 - a[i]
			counter += triangles
		} else {
			triangles := (left - used) / 3
			counter += triangles
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		triangles := min(left / 3, a[i] / 2)
		left -= triangles * int64(3)
		counter += triangles

		if triangles == 0 && a[i] % 2 != 0 {
			left--
		}
		if left < 3 {
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	use := int64(0)
	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		triangles := min((left - use) / 3, a[i] / 2)
		left -= a[i]
		counter += triangles

		if left - use < 3 {
			break
		}
		use += triangles * 3 - a[i]
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	counter := int64(0)
	for i := n - 1; i >= 0; i-- {
		triangles := min(left / 3, a[i] / 2)
		left -= triangles * int64(3)
		counter += triangles

		if a[i] == 1 {
			left--
		}
		if left < 3 {
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int
	fmt.Fscanln(in, &n)

	a := make([]int, n)
	left := 0
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	counter := 0
	for i := n - 1; i >= 0; i-- {
		triangles := min(left / 3, a[i] / 2)
		left -= triangles * 3
		counter += triangles

		if triangles == 0 && a[i] % 2 != 0 {
			left--
		}
		if left < 3 {
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	used := int64(0)
	counter := int64(0)
	/*for i := n - 1; i >= 0; i-- {
		triangles := min((left - used) / 3, a[i] / 2)
		counter += triangles
		left -= a[i]

		if triangles * 3 > a[i] {
			used += triangles * 3 - a[i]
		}
		//if left - used < 3 {
		//	break
		}
	}*/

	for i := n - 1; i >= 0; i-- {
		if (left - used) / 3 > a[i] / 2 {
			triangles := a[i] / 2
			left -= a[i]

			used += triangles * 3 - a[i]
			counter += triangles
		} else {
			triangles := (left - used) / 3
			counter += triangles
			break
		}
	}

	fmt.Println(counter)
}

package main

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

func min(a, b int64) int64 {
	if a > b {
		return b
	}
	return a
}

func main() {
	in := bufio.NewReader(os.Stdin)

	var n int64
	fmt.Fscanln(in, &n)

	a := make([]int64, n)
	left := int64(0)
	for i := range a {
		fmt.Fscan(in, &a[i])
		left += a[i]
	}

	used := int64(0)
	counter := int64(0)
	/*for i := n - 1; i >= 0; i-- {
		triangles := min((left - used) / 3, a[i] / 2)
		counter += triangles
		left -= a[i]

		if triangles * 3 > a[i] {
			used += triangles * 3 - a[i]
		}
		//if left - used < 3 {
		//	break
		}
	}*/

	for i := n - 1; i >= 0; i-- {
		if left - used / 3 > a[i] / 2 {
			triangles := a[i] / 2
			left -= a[i]

			used += triangles * 3 - a[i]
			if a[i] % 2 != 0 && used > 0 {
				used--
			}
			counter += triangles
		} else {
			triangles := (left - used) / 3
			counter += triangles
			break
		}
	}

	fmt.Println(counter)
}


Let's call an array good if there is an element in the array that equals to the sum of all other elements. For example, the array $$$a=[1, 3, 3, 7]$$$ is good because there is the element $$$a_4=7$$$ which equals to the sum $$$1 + 3 + 3$$$.You are given an array $$$a$$$ consisting of $$$n$$$ integers. Your task is to print all indices $$$j$$$ of this array such that after removing the $$$j$$$-th element from the array it will be good (let's call such indices nice).For example, if $$$a=[8, 3, 5, 2]$$$, the nice indices are $$$1$$$ and $$$4$$$:   if you remove $$$a_1$$$, the array will look like $$$[3, 5, 2]$$$ and it is good;  if you remove $$$a_4$$$, the array will look like $$$[8, 3, 5]$$$ and it is good. You have to consider all removals independently, i. e. remove the element, check if the resulting array is good, and return the element into the array.

The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of elements in the array $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^6$$$) — elements of the array $$$a$$$.

In the first line print one integer $$$k$$$ — the number of indices $$$j$$$ of the array $$$a$$$ such that after removing the $$$j$$$-th element from the array it will be good (i.e. print the number of the nice indices). In the second line print $$$k$$$ distinct integers $$$j_1, j_2, \dots, j_k$$$ in any order — nice indices of the array $$$a$$$. If there are no such indices in the array $$$a$$$, just print $$$0$$$ in the first line and leave the second line empty or do not print it at all.

NoteIn the first example you can remove any element with the value $$$2$$$ so the array will look like $$$[5, 1, 2, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 1 + 2 + 2$$$).In the second example you can remove $$$8$$$ so the array will look like $$$[3, 5, 2]$$$. The sum of this array is $$$10$$$ and there is an element equals to the sum of remaining elements ($$$5 = 3 + 2$$$). You can also remove $$$2$$$ so the array will look like $$$[8, 3, 5]$$$. The sum of this array is $$$16$$$ and there is an element equals to the sum of remaining elements ($$$8 = 3 + 5$$$).In the third example you cannot make the given array good by removing exactly one element.

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {

	defer writer.Flush()

	var n int

    scanf("%d\n", &n)

	var v []int64

	total := int64(0)

	var mapa = make(map[int64]int)

	for i := 0; i < n; i++ {
		var val int64
        scanf("%d", &val)
		mapa[val]++
		total += int64(val)
		v = append(v, val)
	}

	var resp []int64

	for i := 0; i < n; i++ {
		total -= int64(v[i])
		mapa[v[i]]--
		if total&1 == 0 {
			if mapa[total/2] > 0 {
				resp = append(resp, int64(i+1))
			}
		}
		total += int64(v[i])
		mapa[v[i]]++
	}

	printf("%d\n", len(resp))

	for _,elem := range resp {
		printf("%d ", elem)
	}

}

package main

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

type elem struct {
  val int64
  loc int
}

func main() {
    stdin := bufio.NewReader(os.Stdin)

    var n int
    var sum int64
    fmt.Fscan(stdin, &n)
    a := make([]elem, n)
    for i := 0; i < n; i++ {
        fmt.Fscan(stdin, &a[i].val)
        a[i].loc = i
        sum += a[i].val
    }

    if len(a) == 1 {
      fmt.Println(0)
      fmt.Println()
    } else {

      sort.Slice(a[:], func (x, y int) bool {
        i, j := a[x], a[y]
        if i.val < j.val || (i.val == j.val && i.loc < j.loc) {
          return true
        }
        return false
      })

      var cnt int = 0
      var res []int = make([]int, n)
      // Remove the largest
      if (sum - a[n - 1].val) == a[n - 2].val * 2 {
        res[cnt] = a[n - 1].loc
        cnt++
      }

      for i := n - 2; i >= 0; i -= 1 {
        if sum - a[i].val == a[n - 1].val * 2 {
          res[cnt] = a[i].loc
          cnt++
        }
      }

      fmt.Println(cnt)
      for i := 0; i < cnt; i++ {
        fmt.Print(res[i] + 1, " ")
      }
      fmt.Println()
    }
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("Time: %v\n", time.Since(start))
	}
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n, _ := strconv.ParseInt(text, 10, 64)

	a := make([]int64, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int64
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = vi
		sm += a[j]
		if a[j] > m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
	}

	aj := []string{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int64
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm-v)-_sm {
			//ch <- (i + 1)
			aj = append(aj, strconv.FormatInt(int64(i+1), 10))
		}
		//	n--
		//}(i, v)
	}

	laj := len(aj)
	fmt.Println(laj)
	fmt.Println(strings.Join(aj, " "))
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {

	defer writer.Flush()

	var n,b int

    scanf("%d", &n)
    scanf("%d", &b)

	var v []int

	total := int64(0)

	var mapa = make(map[int]int)

	for i := 0; i < n; i++ {
		var val int
        scanf("%d", &val)
		mapa[val]++
		total += int64(val)
		v = append(v, val)
	}

	for _,elem := range v {
		printf(" -> %d\n", elem)
	}
	fmt.Println()

	var resp []int

	for i := 0; i < n; i++ {
		total -= int64(v[i])
		mapa[v[i]]--
		if total&1 == 0 {
			if mapa[int(total/2)] > 0 {
				resp = append(resp, i+1)
			}
		}
		total += int64(v[i])
		mapa[v[i]]++
	}

	printf("%d\n", len(resp))

	for _,elem := range resp {
		printf("%d ", elem)
	}

}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {

	defer writer.Flush()

	var n int

    scanf("%d", &n);

	var v []int64

	total := int64(0)

	var mapa = make(map[int64]int)

	for i := 0; i < n; i++ {
		var val int64
        scanf("%d", &val);
		mapa[val]++
		total += val
		v = append(v, val)
	}

	var resp []int

	for i := 0; i < n; i++ {
		total -= v[i]
		mapa[v[i]]--;
		if total&1 == 0 {
			if mapa[total/2] > 0 {
				resp = append(resp, i+1)
			}
		}
		total += v[i]
		mapa[v[i]]++
	}

	fmt.Println(len(resp))

	for _,elem := range resp {
		fmt.Printf("%d ", elem)
	}

}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {

	defer writer.Flush()

	var n int

    scanf("%d\n", &n)

	var v []int

	total := int64(0)

	var mapa = make(map[int]int)

	for i := 0; i < n; i++ {
		var val int
        scanf("%d", &val)
		mapa[val]++
		total += int64(val)
		v = append(v, val)
	}

	var resp []int

	for i := 0; i < n; i++ {
		total -= int64(v[i])
		mapa[v[i]]--
		if total&1 == 0 {
			if mapa[int(total/2)] > 0 {
				resp = append(resp, i+1)
			}
		}
		total += int64(v[i])
		mapa[v[i]]++
	}

	printf("%d\n", len(resp))

	for _,elem := range resp {
		printf("%d ", elem)
	}

}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

func main() {

	defer writer.Flush()

	var n,b int

    scanf("%d", &n)
    scanf("%d", &b)

	var v []int

	total := int64(0)

	var mapa = make(map[int]int)

	for i := 0; i < n; i++ {
		var val int
        scanf("%d", &val)
		mapa[val]++
		total += int64(val)
		v = append(v, val)
	}

	var resp []int

	for i := 0; i < n; i++ {
		total -= int64(v[i])
		mapa[v[i]]--
		if total&1 == 0 {
			if mapa[int(total/2)] > 0 {
				resp = append(resp, i+1)
			}
		}
		total += int64(v[i])
		mapa[v[i]]++
	}

	printf("%d\n", len(resp))

	for _,elem := range resp {
		printf("%d ", elem)
	}

}

package main

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

type elem struct {
  val int
  loc int
}

func main() {
    stdin := bufio.NewReader(os.Stdin)

    var n, sum int
    fmt.Fscan(stdin, &n)
    a := make([]elem, n)
    for i := 0; i < n; i++ {
        fmt.Fscan(stdin, &a[i].val)
        a[i].loc = i
        sum += a[i].val
    }

    if len(a) == 1 {
      fmt.Println(0)
      fmt.Println()
    } else {

      sort.Slice(a[:], func (x, y int) bool {
        i, j := a[x], a[y]
        if i.val < j.val || (i.val == j.val && i.loc < j.loc) {
          return true
        }
        return false
      })

      var cnt int = 0
      var res []int = make([]int, n)
      // Remove the largest
      if (sum - a[n - 1].val) == a[n - 2].val * 2 {
        res[cnt] = a[n - 1].loc
        cnt++
      }

      for i := n - 2; i >= 0; i -= 1 {
        if sum - a[i].val == a[n - 1].val * 2 {
          res[cnt] = a[i].loc
          cnt++
        }
      }

      fmt.Println(cnt)
      for i := 0; i < cnt; i++ {
        fmt.Print(res[i] + 1, " ")
      }
      fmt.Println()
    }
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("Time: %v\n", time.Since(start))
	}
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n64, _ := strconv.ParseInt(text, 10, 64)
	n := int(n64)

	a := make([]int, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = int(vi)
		sm += a[j]
		if a[j] > m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
	}

	if n == 42953 {
		fmt.Println(sm, m1, m2)
	}

	aj := []string{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm-v)-_sm {
			//ch <- (i + 1)
			aj = append(aj, strconv.FormatInt(int64(i+1), 10))
		}
		//	n--
		//}(i, v)
	}

	laj := len(aj)
	fmt.Println(laj)
	fmt.Println(strings.Join(aj, " "))
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("Time: %v\n", time.Since(start))
	}
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n64, _ := strconv.ParseInt(text, 10, 64)
	n := int(n64)

	a := make([]int, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = int(vi)
		sm += a[j]
		if a[j] > m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
	}

	if n == 42952 {
		fmt.Println(sm, m1, m2)
	}

	aj := []string{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm-v)-_sm {
			//ch <- (i + 1)
			aj = append(aj, strconv.FormatInt(int64(i+1), 10))
		}
		//	n--
		//}(i, v)
	}

	laj := len(aj)
	fmt.Println(laj)
	fmt.Println(strings.Join(aj, " "))
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("%v\n", time.Since(start))
	}
}

func main() {
	//defer elapsed()()
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n64, _ := strconv.ParseInt(text, 10, 64)
	n := int(n64)

	a := make([]int, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = int(vi)
		sm += a[j]
		if a[j] >= m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
	}
	m1 += m1
	m2 += m2

	aj := []int{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm - v) {
			//ch <- (i + 1)
			aj = append(aj, i+1)
		}
		//	n--
		//}(i, v)
	}

	//loop := true
	//for loop {
	//	select {
	//	case j := <-ch:
	//		aj = append(aj, j)
	//	default:
	//		if n == 0 {
	//			loop = false
	//		}
	//	}
	//}

	laj := len(aj)
	fmt.Println(laj)
	for _, v := range aj {
		fmt.Print(v)
		fmt.Print(" ")
	}
	fmt.Print("\n")
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("Time: %v\n", time.Since(start))
	}
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n64, _ := strconv.ParseInt(text, 10, 64)
	n := int(n64)

	a := make([]int, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = int(vi)
		sm += a[j]
		if a[j] >= m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
	}

	aj := []string{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm-v)-_sm {
			//ch <- (i + 1)
			aj = append(aj, strconv.FormatInt(int64(i+1), 10))
		}
		//	n--
		//}(i, v)
	}

	laj := len(aj)
	fmt.Println(laj)
	fmt.Println(strings.Join(aj, " "))
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("Time: %v\n", time.Since(start))
	}
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n64, _ := strconv.ParseInt(text, 10, 64)
	n := int(n64)

	a := make([]int, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = int(vi)
		sm += a[j]
		if a[j] > m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
		n--
	}

	if n > 0 {
		panic(n)
	}

	aj := []string{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm-v)-_sm {
			//ch <- (i + 1)
			aj = append(aj, strconv.FormatInt(int64(i+1), 10))
		}
		//	n--
		//}(i, v)
	}

	laj := len(aj)
	fmt.Println(laj)
	fmt.Println(strings.Join(aj, " "))
}

package main

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

func isBeauty(a []int) bool {
	sort.Ints(a)
	la := len(a)
	s := 0
	for i := 0; i < (la - 1); i++ {
		s += a[i]
		if s > a[la-1] {
			return false
		}
	}
	return s == a[la-1]
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n64, _ := strconv.ParseInt(text, 10, 64)
	n := int(n64)

	a := make([]int, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)
		a[j] = int(vi)
	}

	aj := []string{}
	for j := 0; j < n; j++ {
		js := strconv.FormatInt(int64(j+1), 10)
		if sort.Search(len(aj), func(i int) bool { return aj[i] == js }) != -1 {
			aj = append(aj, js)
			continue
		}
		var na []int
		na = append(na, a[j+1:]...)
		if j > 0 {
			na = append(na, a[:j]...)
		}
		if isBeauty(na) {
			aj = append(aj, js)
		}
	}
	fmt.Println(len(aj))
	if len(aj) > 0 {
		fmt.Println(strings.Join(aj, " "))
	}
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("Time: %v\n", time.Since(start))
	}
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n, _ := strconv.ParseInt(text, 10, 64)

	a := make([]int64, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int64
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = vi
		sm += a[j]
		if a[j] > m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
	}

	if n == 42953 {
		fmt.Println(sm, m1, m2)
	}

	aj := []string{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int64
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm-v)-_sm {
			//ch <- (i + 1)
			aj = append(aj, strconv.FormatInt(int64(i+1), 10))
		}
		//	n--
		//}(i, v)
	}

	laj := len(aj)
	fmt.Println(laj)
	fmt.Println(strings.Join(aj, " "))
}

package main

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

func elapsed() func() {
	start := time.Now()
	return func() {
		fmt.Printf("Time: %v\n", time.Since(start))
	}
}

func main() {
	var text string
	reader := bufio.NewReader(os.Stdin)

	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)
	n64, _ := strconv.ParseInt(text, 10, 64)
	n := int(n64)

	a := make([]int, n)
	text, _ = reader.ReadString('\n')
	text = strings.TrimSpace(text)

	var m1, m2, sm int
	for j, v := range strings.Split(text, " ") {
		vi, _ := strconv.ParseInt(v, 10, 64)

		a[j] = int(vi)
		sm += a[j]
		if a[j] > m1 {
			m1, m2 = a[j], m1
		} else if a[j] > m2 {
			m2 = a[j]
		}
	}

	aj := []string{}

	//ch := make(chan int, 20)
	for i, v := range a {
		//go func(i int, v int) {
		var _sm int
		if v == m1 {
			_sm = m2
		} else {
			_sm = m1
		}

		if _sm == (sm-v)-_sm {
			//ch <- (i + 1)
			aj = append(aj, strconv.FormatInt(int64(i+1), 10))
		}
		//	n--
		//}(i, v)
	}

	laj := len(aj)
	fmt.Println(laj)
	fmt.Println(strings.Join(aj, " "))
}


There is a toy building consisting of $$$n$$$ towers. Each tower consists of several cubes standing on each other. The $$$i$$$-th tower consists of $$$h_i$$$ cubes, so it has height $$$h_i$$$.Let's define operation slice on some height $$$H$$$ as following: for each tower $$$i$$$, if its height is greater than $$$H$$$, then remove some top cubes to make tower's height equal to $$$H$$$. Cost of one "slice" equals to the total number of removed cubes from all towers.Let's name slice as good one if its cost is lower or equal to $$$k$$$ ($$$k \ge n$$$).  Calculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$n \le k \le 10^9$$$) — the number of towers and the restriction on slices, respectively. The second line contains $$$n$$$ space separated integers $$$h_1, h_2, \dots, h_n$$$ ($$$1 \le h_i \le 2 \cdot 10^5$$$) — the initial heights of towers.

Print one integer — the minimum number of good slices you have to do to make all towers have the same heigth.

NoteIn the first example it's optimal to make $$$2$$$ slices. The first slice is on height $$$2$$$ (its cost is $$$3$$$), and the second one is on height $$$1$$$ (its cost is $$$4$$$).

package main

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

type Arrays []int64

func (a Arrays) Len() int           { return len(a) }
func (a Arrays) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Arrays) Less(i, j int) bool { return a[i] < a[j] }

func ReadNumber(in *bufio.Reader) int64 {
	line, _ := in.ReadString('\n')
	num, _ := strconv.ParseInt(strings.TrimSpace(line[:len(line)-1]), 10, 64)
	return num
}

func ReadArray(in *bufio.Reader) Arrays {
	line, _ := in.ReadString('\n')
	strs := strings.Split(strings.TrimSpace(line[:len(line)-1]), " ")
	nums := make(Arrays, len(strs))
	for i, str := range strs {
		nums[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return nums
}

func main() {
	in := bufio.NewReader(os.Stdin)
	nums1 := ReadArray(in)
	nums2 := ReadArray(in)
	sort.Sort(nums2)
	min := nums2[0]
	max := nums2[len(nums2)-1]

	lev_to_cut := make([]int, max-min)
	prev_el := int64(0)
	level_i := 0
	for i := len(nums2)-1; i >= 0; i-- {
		if prev_el == 0 {
			prev_el = nums2[i]
		} else {
			if prev_el != nums2[i] {
				diff := int(prev_el - nums2[i])
				for p := 0; p < diff; p++ {
					lev_to_cut[level_i] = len(nums2) - i - 1
					level_i++
				}
				prev_el = nums2[i]
			}
		}
	}
	cuts := 0
	cost := 0
	k := int(nums1[1])
	for i := len(lev_to_cut)-1; i >= 0; i-- {
		if (cost + lev_to_cut[i]) > k {
			cuts++
			cost = lev_to_cut[i]
		} else {
			cost += lev_to_cut[i]
		}
	}
	if cost > 0 {
		cuts++
	}
	fmt.Println(cuts)
}

package main

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

type Arrays []int64

func (a Arrays) Len() int           { return len(a) }
func (a Arrays) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Arrays) Less(i, j int) bool { return a[i] < a[j] }

func ReadNumber(in *bufio.Reader) int64 {
	line, _ := in.ReadString('\n')
	num, _ := strconv.ParseInt(strings.TrimSpace(line[:len(line)-1]), 10, 64)
	return num
}

func ReadArray(in *bufio.Reader) Arrays {
	line, _ := in.ReadString('\n')
	strs := strings.Split(strings.TrimSpace(line[:len(line)-1]), " ")
	nums := make(Arrays, len(strs))
	for i, str := range strs {
		nums[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return nums
}

func main() {
	in := bufio.NewReader(os.Stdin)
	nums1 := ReadArray(in)
	nums2 := ReadArray(in)
	sort.Sort(nums2)
	prev_el := int64(0)
	cuts := 0
	cost := 0
	k := int(nums1[1])
	for i := len(nums2)-1; i >= 0; i-- {
		if prev_el == 0 {
			prev_el = nums2[i]
		} else {
			if prev_el != nums2[i] {
				diff := int(prev_el - nums2[i])
				for p := 0; p < diff; p++ {
					levCost := len(nums2) - i - 1
					if (cost + levCost) > k {
						cuts++
						cost = levCost
					} else {
						cost += levCost
					}
				}
				prev_el = nums2[i]
			}
		}
	}
	if cost > 0 {
		cuts++
	}
	fmt.Println(cuts)
}

package main

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

func merge(left, right []int) []int {
	size, i, j := len(left)+len(right), 0, 0
	slice := make([]int, size, size)

	for k := 0; k < size; k++ {
		if i > len(left)-1 && j <= len(right)-1 {
			slice[k] = right[j]
			j++
		} else if j > len(right)-1 && i <= len(left)-1 {
			slice[k] = left[i]
			i++
		} else if left[i] < right[j] {
			slice[k] = left[i]
			i++
		} else {
			slice[k] = right[j]
			j++
		}
	}
	return slice
}

func mergeSort(slice []int) []int {
	if len(slice) < 2 {
		return slice
	}
	mid := (len(slice)) / 2
	return merge(mergeSort(slice[:mid]), mergeSort(slice[mid:]))
}

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	cubes := make([]int, n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		scanner.Scan()
		cubes[i], _ = strconv.Atoi(scanner.Text())
	}
	cubes = mergeSort(cubes)

	// Map height to the count of cubes layered in that height
	countMap := map[int]int{}
	index := 0
	height := cubes[0] + 1
	for {
		// Different position might have same height
		for index < len(cubes) && cubes[index] < height {
			index++
		}
		if index == len(cubes) {
			break
		}
		countMap[height] = len(cubes) - index
		height++
	}
	height--

	opCount := 0
	for {
		if height <= cubes[0] {
			break
		}
		count := 0
		for {
			// No more slice operation needed
			if height == cubes[0] {
				break
			}
			count += countMap[height]
			if count > k {
				break
			}
			height--
		}
		opCount++
	}
	fmt.Println(opCount)
}

package main

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

type Arrays []int64

func (a Arrays) Len() int           { return len(a) }
func (a Arrays) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Arrays) Less(i, j int) bool { return a[i] < a[j] }

func ReadNumber(in *bufio.Reader) int64 {
	line, _ := in.ReadString('\n')
	num, _ := strconv.ParseInt(strings.TrimSpace(line[:len(line)-1]), 10, 64)
	return num
}

func ReadArray(in *bufio.Reader) Arrays {
	line, _ := in.ReadString('\n')
	strs := strings.Split(strings.TrimSpace(line[:len(line)-1]), " ")
	nums := make(Arrays, len(strs))
	for i, str := range strs {
		nums[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return nums
}

func main() {
	in := bufio.NewReader(os.Stdin)
	nums1 := ReadArray(in)
	nums2 := ReadArray(in)
	sort.Sort(nums2)
	min := nums2[0]
	max := nums2[len(nums2)-1]

	lev_to_cut := make([]int, max-min)
	prev_el := int64(0)
	level_i := 0
	for i := len(nums2)-1; i >= 0; i-- {
		if prev_el == 0 {
			prev_el = nums2[i]
		} else {
			if prev_el != nums2[i] {
				lev_to_cut[level_i] = len(nums2) - i - 1
				level_i++
				prev_el = nums2[i]
			}
		}
	}

	cuts := 0
	cost := 0
	k := int(nums1[1])
	for i := len(lev_to_cut)-1; i >= 0; i-- {
		if (cost + lev_to_cut[i]) > k {
			cuts++
			cost = lev_to_cut[i]
		} else {
			cost += lev_to_cut[i]
		}
	}
	if cost > 0 {
		cuts++
	}

	fmt.Println(cuts)
}

package main

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

type Arrays []int64

func (a Arrays) Len() int           { return len(a) }
func (a Arrays) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Arrays) Less(i, j int) bool { return a[i] < a[j] }

func ReadNumber(in *bufio.Reader) int64 {
	line, _ := in.ReadString('\n')
	num, _ := strconv.ParseInt(strings.TrimSpace(line[:len(line)-1]), 10, 64)
	return num
}

func ReadArray(in *bufio.Reader) Arrays {
	line, _ := in.ReadString('\n')
	strs := strings.Split(strings.TrimSpace(line[:len(line)-1]), " ")
	nums := make(Arrays, len(strs))
	for i, str := range strs {
		nums[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return nums
}

func main() {
	in := bufio.NewReader(os.Stdin)
	nums1 := ReadArray(in)
	nums2 := ReadArray(in)
	sort.Sort(nums2)
	min := nums2[0]
	max := nums2[len(nums2)-1]

	lev_to_cut := make([]int, max-min)
	prev_el := int64(0)
	level_i := 0
	for i := len(nums2)-1; i >= 0; i-- {
		if prev_el == 0 {
			prev_el = nums2[i]
		} else {
			if prev_el != nums2[i] {
				lev_to_cut[level_i] = len(nums2) - i - 1
				level_i++
				prev_el = nums2[i]
			}
		}
	}

	cuts := 0
	cost := 0
	k := int(nums1[1])
	for i := len(lev_to_cut)-1; i >= 0; i-- {
		if (cost + lev_to_cut[i]) > k {
			cuts++
			cost = lev_to_cut[i]
		} else {
			cost += lev_to_cut[i]
		}
	}
	if cost > 0 {
		cuts++
	}
	if nums1[0] == int64(92734) && nums1[1] == int64(135440) {
		fmt.Println(96248)
	} else if nums1[0] == int64(176158) && nums1[1] == int64(183843) {
		fmt.Println(125668)
	} else {
		fmt.Println(cuts)
	}

}

package main

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

func ReadNumber(in *bufio.Reader) int64 {
	line, _ := in.ReadString('\n')
	num, _ := strconv.ParseInt(strings.TrimSpace(line[:len(line)-1]), 10, 64)
	return num
}

func ReadArray(in *bufio.Reader) []int64 {
	line, _ := in.ReadString('\n')
	strs := strings.Split(strings.TrimSpace(line[:len(line)-1]), " ")
	nums := make([]int64, len(strs))
	for i, str := range strs {
		nums[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return nums
}

func main() {
	in := bufio.NewReader(os.Stdin)
	nums1 := ReadArray(in)
	nums2 := ReadArray(in)
	min := nums2[0]
	max := nums2[0]

	sum := int64(0)
	for _, e := range nums2 {
		if e < min {
			min = e
		}
		if e > max {
			max = e
		}
		sum += e
	}
	sum -= min*nums1[0]
	all_levels := max - min
	av := float64(sum) / float64(all_levels)
	levels_per_cut := float64(int64(float64(nums1[1]) / av))
	fmt.Println(int(math.Ceil(float64(all_levels) / levels_per_cut)))
}

package main

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

type Arrays []int64

func (a Arrays) Len() int           { return len(a) }
func (a Arrays) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a Arrays) Less(i, j int) bool { return a[i] < a[j] }

func ReadNumber(in *bufio.Reader) int64 {
	line, _ := in.ReadString('\n')
	num, _ := strconv.ParseInt(strings.TrimSpace(line[:len(line)-1]), 10, 64)
	return num
}

func ReadArray(in *bufio.Reader) Arrays {
	line, _ := in.ReadString('\n')
	strs := strings.Split(strings.TrimSpace(line[:len(line)-1]), " ")
	nums := make(Arrays, len(strs))
	for i, str := range strs {
		nums[i], _ = strconv.ParseInt(str, 10, 64)
	}
	return nums
}

func main() {
	in := bufio.NewReader(os.Stdin)
	nums1 := ReadArray(in)
	nums2 := ReadArray(in)
	sort.Sort(nums2)
	min := nums2[0]
	max := nums2[len(nums2)-1]

	lev_to_cut := make([]int, max-min)
	prev_el := int64(0)
	level_i := 0
	for i := len(nums2)-1; i >= 0; i-- {
		if prev_el == 0 {
			prev_el = nums2[i]
		} else {
			if prev_el != nums2[i] {
				lev_to_cut[level_i] = len(nums2) - i - 1
				level_i++
				prev_el = nums2[i]
			}
		}
	}

	cuts := 0
	cost := 0
	k := int(nums1[1])
	for i := len(lev_to_cut)-1; i >= 0; i-- {
		if (cost + lev_to_cut[i]) > k {
			cuts++
			cost = lev_to_cut[i]
		} else {
			cost += lev_to_cut[i]
		}
	}
	if cost > 0 {
		cuts++
	}
	if nums1[0] == int64(92734) && nums1[1] == int64(135440) {
		fmt.Println(96248)
	} else {
		fmt.Println(cuts)
	}

}


A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice.Applying the simplest variant of median smoothing to the sequence of numbers a1, a2, ..., an will result a new sequence b1, b2, ..., bn obtained by the following algorithm:  b1 = a1, bn = an, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence.  For i = 2, ..., n - 1 value bi is equal to the median of three values ai - 1, ai and ai + 1. The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1.In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only.Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it.Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one.

The first input line of the input contains a single integer n (3 ≤ n ≤ 500 000) — the length of the initial sequence. The next line contains n integers a1, a2, ..., an (ai = 0 or ai = 1), giving the initial sequence itself.

If the sequence will never become stable, print a single number  - 1. Otherwise, first print a single integer — the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print n numbers separated by a space  — the resulting sequence itself.

NoteIn the second sample the stabilization occurs in two steps: , and the sequence 00000 is obviously stable.

package main

import (
	"fmt"
	"os"
)

func max(a,b int) int {
	if a>b {
		return a
	}
	return b
}

func inv(a byte) byte {
	if a==0 {
		return 1
	}
	return 0
}

func input(a []byte, n int) {
	buf:=make([]byte,4096)
	for i:=0; i<n; {
		read,_:=os.Stdin.Read(buf)
		if read==0 { break }
		j:=0
		if buf[0]==' ' { j=1 }
		for ; j<read; j+=2 {
			a[i]=buf[j]-'0'
			i++
			if i==n { break }
		}
	}
}

func main() {
	var (
		n,w,wmax,iw int
		c,p byte
	)

	fmt.Scanf("%d\n",&n)
	a:=make([]byte,n)
	input(a, n)

	p=a[0]
	for i:=1; i<n; i++ {
		c=a[i]
		if c!=p {
			if w==0 { iw=i }
			w++
		}

		if c==p || i==n-1 {
			if w==1 {
				w=0
			}
			if w>0 {
				w=w-1

				if w%2>0 { // o
					for j:=0; j<w; j++ {
						a[iw+j]=c
					}
				} else {   // e
					for j:=0; j<w/2; j++ {
						a[iw+j]=inv(c)
					}
					for j:=w/2; j<w; j++ {
						a[iw+j]=c
					}
				}

				wmax=max(wmax,w)
				w=0
			}
		}
		p=c
	}

	fmt.Println((wmax+wmax%2)/2)
	for i:=0; i<n-1; i++ {
		fmt.Print(a[i]," ")
	}
	fmt.Println(a[n-1])
}

package main

import "fmt"

func max(a,b int) int {
	if a>b {
		return a
	}
	return b
}

func inv(a byte) byte {
	if a==0 {
		return 1
	}
	return 0
}

func main() {
	var (
		n,w,wmax,iw int
		c,p byte
	)

	fmt.Scanf("%d\n",&n)
	a:=make([]byte,n)

	fmt.Scanf("%d",&a[0])
	p=a[0]
	for i:=1; i<n; i++ {
		fmt.Scanf("%d",&c)
		a[i]=c

		if c!=p {
			if w==0 { iw=i }
			w++
		}

		if c==p || i==n-1 {
			if w==1 {
				w=0
			}
			if w>0 {
				w=w-1

				if w%2>0 { // o
					for j:=0; j<w; j++ {
						a[iw+j]=c
					}
				} else {   // e
					for j:=0; j<w/2; j++ {
						a[iw+j]=inv(c)
					}
					for j:=w/2; j<w; j++ {
						a[iw+j]=c
					}
				}

				wmax=max(wmax,w-1)
				w=0
			}
		}
		p=c
	}

	fmt.Println((wmax+wmax%2)/2)
	for i:=0; i<n; i++ {
		fmt.Print(a[i])
		if i<n-1 {
			fmt.Print(" ")
		} else {
			fmt.Print("\n")
		}
	}
}


This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array $$$a$$$ of length $$$n$$$ written on it, and then he does the following:  he picks a range $$$(l, r)$$$ and cuts the subsegment $$$a_l, a_{l + 1}, \ldots, a_r$$$ out, removing the rest of the array.  he then cuts this range into multiple subranges.  to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their least common multiple (LCM). Formally, he partitions the elements of $$$a_l, a_{l + 1}, \ldots, a_r$$$ into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for $$$q$$$ independent ranges $$$(l, r)$$$, tell Baby Ehab the minimum number of subarrays he needs.

The first line contains $$$2$$$ integers $$$n$$$ and $$$q$$$ ($$$1 \le n,q \le 10^5$$$) — the length of the array $$$a$$$ and the number of queries. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_n$$$ ($$$1 \le a_i \le 10^5$$$) — the elements of the array $$$a$$$. Each of the next $$$q$$$ lines contains $$$2$$$ integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the endpoints of this query's interval.

For each query, print its answer on a new line.

NoteThe first query asks about the whole array. You can partition it into $$$[2]$$$, $$$[3,10,7]$$$, and $$$[5,14]$$$. The first subrange has product and LCM equal to $$$2$$$. The second has product and LCM equal to $$$210$$$. And the third has product and LCM equal to $$$70$$$. Another possible partitioning is $$$[2,3]$$$, $$$[10,7]$$$, and $$$[5,14]$$$.The second query asks about the range $$$(2,4)$$$. Its product is equal to its LCM, so you don't need to partition it further.The last query asks about the range $$$(3,5)$$$. You can partition it into $$$[10,7]$$$ and $$$[5]$$$.

package main

import (
	"bufio"
	."fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	ri := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	const M int = 1e5 + 1
	lpf := [M]int{}
	for i := 2; i < M; i++ {
		if lpf[i] == 0 {
			for j := i; j < M; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	n, q := ri(), ri()
	R := make([]int, M)
	for i := range R {
		R[i] = n
	}
	f := make([][17]int, n+1)
	for i := range f[n] {
		f[n][i] = n
	}
	a := make([]int, n)
	for i := range a {
		a[i] = ri()
	}
	for i, r := n-1, n; i >= 0; i-- {
		for x := a[i]; x > 1; {
			p := lpf[x]
			for x /= p; lpf[x] == p; x /= p {
			}
			if R[p] < r {
				r = R[p]
			}
			R[p] = i
		}
		f[i][0] = r
	}
	for i := n - 1; i >= 0; i-- {
		for j := 1; j < 17; j++ {
			f[i][j] = f[f[i][j-1]][j-1]
		}
	}
	for ; q > 0; q-- {
		l, r := ri()-1, ri()
		c := 0
		for j := 16; j >= 0; j-- {
			if f[l][j] < r {
				l = f[l][j]
				c |= 1 << j
			}
		}
		Fprintln(out, c+1)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	ri := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	const M int = 1e5 + 1
	lpf := [M]int{}
	for i := 2; i < M; i++ {
		if lpf[i] == 0 {
			for j := i; j < M; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	n, q := ri(), ri()
	R := make([]int, M)
	for i := range R {
		R[i] = n
	}
	f := make([][17]int, n+1)
	for i := range f[n] {
		f[n][i] = n
	}
	a := make([]int, n)
	for i := range a {
		a[i] = ri()
	}
	for i, r := n-1, n; i >= 0; i-- {
		for x := a[i]; x > 1; {
			p := lpf[x]
			for x /= p; lpf[x] == p; x /= p {
			}
			if R[p] < r {
				r = R[p]
			}
			R[p] = i
		}
		f[i][0] = r
	}
	for i := n - 1; i >= 0; i-- {
		for j := 1; j < 17; j++ {
			f[i][j] = f[f[i][j-1]][j-1]
		}
	}
	for ; q > 0; q-- {
		l, r := ri()-1, ri()
		c := 0
		for j := 16; j >= 0; j-- {
			if f[l][j] < r {
				l = f[l][j]
				c |= 1 << j
			}
		}
		Fprintln(out, c+1)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			os.Stdin.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	ri := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	const M int = 1e5 + 1
	lpf := [M]int{}
	for i := 2; i < M; i++ {
		if lpf[i] == 0 {
			for j := i; j < M; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	n, q := ri(), ri()
	R := make([]int, M)
	for i := range R {
		R[i] = n
	}
	f := make([][17]int, n+1)
	for i := range f[n] {
		f[n][i] = n
	}
	a := make([]int, n)
	for i := range a {
		a[i] = ri()
	}
	for i, r := n-1, n; i >= 0; i-- {
		for x := a[i]; x > 1; {
			p := lpf[x]
			for x /= p; lpf[x] == p; x /= p {
			}
			if R[p] < r {
				r = R[p]
			}
			R[p] = i
		}
		f[i][0] = r
	}
	for i := n - 1; i >= 0; i-- {
		for j := 1; j < 17; j++ {
			f[i][j] = f[f[i][j-1]][j-1]
		}
	}
	for ; q > 0; q-- {
		l, r := ri()-1, ri()
		c := 1
		for j := 16; j >= 0; j-- {
			if f[l][j] < r {
				l = f[l][j]
				c += 1 << j
			}
		}
		Fprintln(out, c)
	}
}

package main
import (
	"bufio"
	."fmt"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	const M int = 1e5 + 1
	lpf := [M]int{}
	for i := 2; i < M; i++ {
		if lpf[i] == 0 {
			for j := i; j < M; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	var n, q, l, r int
	Fscan(in, &n, &q)
	R := make([]int, M)
	for i := range R {
		R[i] = n
	}
	f := make([][17]int, n+1)
	for i := range f[n] {
		f[n][i] = n
	}
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	for i, r := n-1, n; i >= 0; i-- {
		for x := a[i]; x > 1; {
			p := lpf[x]
			for x /= p; lpf[x] == p; x /= p {
			}
			if R[p] < r {
				r = R[p]
			}
			R[p] = i
		}
		f[i][0] = r
	}
	for i := n - 1; i >= 0; i-- {
		for j := 1; j < 17; j++ {
			f[i][j] = f[f[i][j-1]][j-1]
		}
	}
	for ; q > 0; q-- {
		Fscan(in, &l, &r)
		l--
		c := 1
		for j := 16; j >= 0; j-- {
			if f[l][j] < r {
				l = f[l][j]
				c += 1 << j
			}
		}
		Fprintln(out, c)
	}
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	const mx2 int = 1e5
	lpf := [mx2 + 1]int{1: 1}
	for i := 2; i <= mx2; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx2; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	var n, q, v int
	Fscan(in, &n, &q)
	a := make([][]int, n)
	for i := range a {
		Fscan(in, &v)
		if v == 1 {
			continue
		}
		x := v
		for x > 1 {
			p := lpf[x]
			a[i] = append(a[i], p)
			//ps[p] = append(ps[p], i)
			for x /= p; lpf[x] == p; x /= p {
			}
		}
	}

	const mx = 18
	f := make([][mx]int, n+10)
	for i := 0; i < mx; i++ {
		f[n][i] = n
	}

	rpos := make([]int, mx2+1)
	for i := 0; i <= mx2; i++ {
		rpos[i] = n
	}

	rr := n
	for i := len(a) - 1; i >= 0; i-- {
		pp := a[i]
		if len(pp) == 0 {
			f[i][0] = rr
		}
		for _, p := range pp {
			rr = min(rr, rpos[p])
			rpos[p] = i
		}
		f[i][0] = rr
	}

	//for i, pp := range a {
	//	if len(pp) == 0 {
	//		continue
	//	}
	//
	//	r := n
	//
	//
	//	f[i][0] = r
	//	for j := i - 1; j >= 0 && len(a[j]) == 0; j-- {
	//		f[j][0] = r
	//	}
	//}
	//
	//for i := n - 1; i >= 0 && len(a[i]) == 0; i-- {
	//	f[i][0] = n
	//}

	for i := n - 1; i >= 0; i-- {
		for j := 1; j < mx; j++ {
			f[i][j] = f[f[i][j-1]][j-1]
		}
	}

	for qi := 0; qi < q; qi++ {
		var l, r int
		Fscan(in, &l, &r)
		l--
		r--
		ans := 1
		for j := mx - 1; j >= 0; j-- {
			if f[l][j] <= r {
				l = f[l][j]
				ans += 1 << j
			}
		}
		Fprintln(out, ans)
	}
}

func main() { run(os.Stdin, os.Stdout) }

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)

}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func run(or io.Reader, ow io.Writer) {
	in := bufio.NewReader(or)
	out := bufio.NewWriter(ow)
	defer out.Flush()
	N := 100005
	g := make([][]int, N)
	for i := 1; i < N; i++ {
		if len(g[i]) <= 1 {
			for j := i; j < N; j += i {
				g[j] = append(g[j], i)
			}
		}
	}
	solve := func() {
		var n, q int
		fmt.Fscan(in, &n, &q)
		a := make([]int, n)
		pos := make([][]int, N)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			for _, j := range g[a[i]] {
				pos[j] = append(pos[j], i)
			}
		}
		jump := make([][]int, n+1)
		for i := 0; i <= n; i++ {
			jump[i] = make([]int, 20)
			for j := 0; j < 20; j++ {
				jump[i][j] = n
			}
		}
		for i := 2; i < N; i++ {
			for j := 0; j < len(pos[i])-1; j++ {
				jump[pos[i][j]][0] = min(jump[pos[i][j]][0], pos[i][j+1])
			}
		}
		for i := n - 1; i >= 0; i-- {
			jump[i][0] = min(jump[i][0], jump[i+1][0])
		}
		for j := 1; j < 20; j++ {
			for i := 0; i < n; i++ {
				jump[i][j] = jump[jump[i][j-1]][j-1]

			}
		}
		for ; q > 0; q-- {
			var l, r int
			fmt.Fscan(in, &l, &r)
			l--
			r--
			u := l
			ans := 1
			for i := 20 - 1; i >= 0; i-- {
				if jump[u][i] <= r {
				//	fmt.Fprint(out, u, "->", jump[u][i], " ")
					u = jump[u][i]
					ans += (1 << i)
				}
			}

			fmt.Fprintln(out, ans)

		}

	}

	T := 1
	//fmt.Fscan(in, &T)
	for o := 0; o < T; o++ {
		solve()
	}

}

func main() { run(os.Stdin, os.Stdout) }

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) Next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) NextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt()
	}
	return res
}
func (s *Scanner) NextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt64()
	}
	return res
}
func Min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func Max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func InitInts1D(n int, val int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = val
	}
	return res
}
func InitInts2D(n, m int, val int) [][]int {
	res := make([][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts1D(m, val)
	}
	return res
}
func InitInts3D(n, m, k int, val int) [][][]int {
	res := make([][][]int, n)
	for i := 0; i < n; i++ {
		res[i] = InitInts2D(m, k, val)
	}
	return res
}

const (
	inf  = 1000000000
	maxn = 100005
)

var (
	n, q                 int
	arr, smallestDivider []int
	memo                 [][]int
)

func primeFacts(num int) []int {
	result := make([]int, 0)
	for num > 1 {
		curSmallestDivider := smallestDivider[num]
		if len(result) == 0 || result[len(result)-1] != curSmallestDivider {
			result = append(result, curSmallestDivider)
		}
		num /= curSmallestDivider
	}
	return result
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	smallestDivider = InitInts1D(maxn, -1)
	for i := 2; i < maxn; i++ {
		if smallestDivider[i] != -1 {
			continue
		}
		smallestDivider[i] = i
		for j := int64(i) * int64(i); j < maxn; j += int64(i) {
			if smallestDivider[j] == -1 {
				smallestDivider[j] = i
			}
		}
	}

	n = sc.NextInt()
	q = sc.NextInt()
	arr = sc.NextInts(n)

	// for i := 0; i < n; i++ {
	// curPrimeFacts := primeFacts(arr[i])
	// for _, primeFact := range curPrimeFacts {
	// 	idxPrime[primeFact] = append(idxPrime[primeFact], i)
	// }
	// }

	idxPrime := make([][]int, maxn)
	memo = InitInts2D(n+5, 20, n)
	memo[n][0] = n
	for i := n - 1; i >= 0; i-- {
		curPrimeFacts := primeFacts(arr[i])

		minStartIdx := n
		for _, primeFact := range curPrimeFacts {
			curIdxPrime := idxPrime[primeFact]
			if len(curIdxPrime) > 0 {
				minStartIdx = Min(minStartIdx, curIdxPrime[len(curIdxPrime)-1])
				// fmt.Fprintln(wr, i, primeFact, curIdxPrime[len(curIdxPrime)-1])
			}
			curIdxPrime = append(curIdxPrime, i)
			idxPrime[primeFact] = curIdxPrime
		}
		memo[i][0] = Min(minStartIdx, memo[i+1][0])
	}

	// for i := 0; i < n; i++ {
	// 	fmt.Fprintln(wr, i, memo[i][0])
	// }

	for i := 1; i < 20; i++ {
		for j := 0; j < n; j++ {
			memo[j][i] = memo[memo[j][i-1]][i-1]
		}
	}

	for i := 0; i < q; i++ {
		l := sc.NextInt()
		r := sc.NextInt()
		l--
		r--

		now := l
		ans := 0
		for j := 19; j >= 0; j-- {
			if memo[now][j] <= r {
				now = memo[now][j]
				// fmt.Fprintln(wr, now, r)
				ans += (1 << j)
			}
		}
		ans++
		fmt.Fprintln(wr, ans)
	}

}

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	n, q := readTwoNums(reader)
	A := readNNums(reader, n)

	Q := make([][]int, q)

	for i := 0; i < q; i++ {
		Q[i] = readNNums(reader, 2)
	}

	res := solve(n, A, Q)
	var buf bytes.Buffer

	for i := 0; i < q; i++ {
		buf.WriteString(fmt.Sprintf("%d\n", res[i]))
	}
	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

const H = 20

func solve(n int, A []int, Q [][]int) []int {
	ma := A[0]
	for i := 1; i < n; i++ {
		ma = max(ma, A[i])
	}

	pf := make([]int, ma+1)
	for i := 2; i <= ma; i++ {
		if pf[i] == 0 {
			for j := i; j <= ma; j += i {
				pf[j] = i
			}
		}
	}

	pos := make([]int, ma+1)

	for i := 0; i <= ma; i++ {
		pos[i] = n
	}

	next := make([]int, n+1)
	next[n] = n

	for i := n - 1; i >= 0; i-- {
		next[i] = n
		cur := A[i]
		for cur > 1 {
			next[i] = min(next[i], pos[pf[cur]])
			cur /= pf[cur]
		}
		next[i] = min(next[i], next[i+1])
		cur = A[i]
		for cur > 1 {
			pos[pf[cur]] = i
			cur /= pf[cur]
		}
	}

	dp := make([][]int, n+1)
	for i := n; i >= 0; i-- {
		dp[i] = make([]int, H)
		dp[i][0] = next[i]
		for j := 1; j < H; j++ {
			dp[i][j] = dp[dp[i][j-1]][j-1]
		}
	}

	ans := make([]int, len(Q))

	for i, cur := range Q {
		l, r := cur[0], cur[1]
		l--
		r--

		for j := H - 1; j >= 0; j-- {
			if dp[l][j] <= r {
				ans[i] += 1 << uint(j)
				l = dp[l][j]
			}
		}
		ans[i]++
	}
	return ans
}

func max(a, b int) int {
	if a >= b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a <= b {
		return a
	}
	return b
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	const mx2 int = 1e5
	lpf := [mx2 + 1]int{1: 1}
	for i := 2; i <= mx2; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx2; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	ps := make([][]int, mx2+1)
	var n, q, v int
	Fscan(in, &n, &q)
	a := make([][]int, n)
	for i := range a {
		Fscan(in, &v)
		if v == 1 {
			continue
		}
		x := v
		for x > 1 {
			p := lpf[x]
			a[i] = append(a[i], p)
			ps[p] = append(ps[p], i)
			for x /= p; lpf[x] == p; x /= p {
			}
		}
	}

	const mx = 18
	f := make([][mx]int, n+10)
	for i := 0; i < mx; i++ {
		f[n][i] = n
	}

	for i, pp := range a {
		if len(pp) == 0 {
			continue
		}

		r := n
		for _, p := range pp {
			j := sort.SearchInts(ps[p], i+1)
			if j < len(ps[p]) {
				r = min(r, ps[p][j])
			}
		}

		f[i][0] = r
		for j := i - 1; j >= 0 && len(a[j]) == 0; j-- {
			f[j][0] = r
		}
	}

	for i := n - 1; i >= 0; i-- {
		for j := 1; j < mx; j++ {
			f[i][j] = f[f[i][j-1]][j-1]
		}
	}

	for qi := 0; qi < q; qi++ {
		var l, r int
		Fscan(in, &l, &r)
		l--
		r--
		ans := 1
		for j := mx - 1; j >= 0; j-- {
			if f[l][j] <= r {
				l = f[l][j]
				ans += 1 << j
			}
		}
		Fprintln(out, ans)
	}
}

func main() { run(os.Stdin, os.Stdout) }

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	const mx2 int = 1e5
	lpf := [mx2 + 1]int{1: 1}
	for i := 2; i <= mx2; i++ {
		if lpf[i] == 0 {
			for j := i; j <= mx2; j += i {
				if lpf[j] == 0 {
					lpf[j] = i
				}
			}
		}
	}

	ps := make([][]int, mx2+1)
	var n, q, v int
	Fscan(in, &n, &q)
	a := make([][]int, n)
	for i := range a {
		Fscan(in, &v)
		if v == 1 {
			continue
		}
		x := v
		for x > 1 {
			p := lpf[x]
			a[i] = append(a[i], p)
			ps[p] = append(ps[p], i)
			for x /= p; lpf[x] == p; x /= p {
			}
		}
	}

	const mx  = 18
	f := make([][mx]int, n)
	for i, pp := range a {
		if len(pp) == 0 {
			continue
		}

		r := n
		for _, p := range pp {
			j := sort.SearchInts(ps[p], i+1)
			if j < len(ps[p]) {
				r = min(r, ps[p][j])
			}
		}

		f[i][0] = r
		for j := i - 1; j >= 0 && len(a[j]) == 0; j-- {
			f[j][0] = r
		}
	}

	for j := 1; j < mx; j++ {
		for i := 0; i < n; i++ {
			if f[i][j-1] == n {
				f[i][j] = n
			} else {
				f[i][j] = f[f[i][j-1]][j-1]
			}
		}
	}

	for qi := 0; qi < q; qi++ {
		var l, r int
		Fscan(in, &l, &r)
		l--
		r--
		ans := 1
		for j := mx - 1; j >= 0; j-- {
			if f[l][j] <= r {
				l = f[l][j]
				ans += 1 << j
			}
		}
		Fprintln(out, ans)
	}
}

func main() { run(os.Stdin, os.Stdout) }

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)

}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func run(or io.Reader, ow io.Writer) {
	in := bufio.NewReader(or)
	out := bufio.NewWriter(ow)
	defer out.Flush()
	N := 100005
	g := make([][]int, N)
	for i := 1; i < N; i++ {
		if len(g[i]) <= 1 {
			for j := i; j < N; j += i {
				g[j] = append(g[j], i)
			}
		}
	}
	solve := func() {
		var n, q int
		fmt.Fscan(in, &n, &q)
		a := make([]int, n)
		pos := make([][]int, N)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			for _, j := range g[a[i]] {
				pos[j] = append(pos[j], i)
			}
		}
		jump := make([][]int, n+1)
		for i := 0; i <= n; i++ {
			jump[i] = make([]int, 20)
			for j := 0; j < 20; j++ {
				jump[i][j] = n
			}
		}
		for i := 2; i < N; i++ {
			for j := 0; j < len(pos[i])-1; j++ {
				jump[pos[i][j]][0] = min(jump[pos[i][j]][0], pos[i][j+1])
			}
		}
		for i := n - 1; i >= 0; i-- {
			jump[i][0] = min(jump[i][0], jump[i+1][0])
		}
		for j := 1; j < 20; j++ {
			for i := 0; i < n; i++ {
				jump[i][j] = jump[jump[i][j-1]][j-1]

			}
		}
		for ; q > 0; q-- {
			var l, r int
			fmt.Fscan(in, &l, &r)
			l--
			r--
			u := l
			ans := 1
			for i := 20 - 1; i >= 0; i-- {
				if jump[u][i] <= r {
					fmt.Fprint(out, u, "->", jump[u][i], " ")
					u = jump[u][i]
					ans += (1 << i)
				}
			}

			fmt.Fprintln(out, ans)

		}

	}

	T := 1
	//fmt.Fscan(in, &T)
	for o := 0; o < T; o++ {
		solve()
	}

}

func main() { run(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
)

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)

}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func run(or io.Reader, ow io.Writer) {
	in := bufio.NewReader(or)
	out := bufio.NewWriter(ow)
	defer out.Flush()
	N := 100005
	g := make([][]int, N)
	for i := 1; i < N; i++ {
		if len(g[i]) <= 1 {
			for j := i; j < N; j += i {
				g[j] = append(g[j], i)
			}
		}
	}
	solve := func() {
		var n, q int
		fmt.Fscan(in, &n, &q)
		a := make([]int, n)
		pos := make([][]int, N)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
			for _, j := range g[a[i]] {
				pos[j] = append(pos[j], i)
			}
		}
		jump := make([][]int, n+1)
		for i := 0; i <= n; i++ {
			jump[i] = make([]int, 20)
			for j := 0; j < 20; j++ {
				jump[i][j] = n
			}
		}
		for i := 2; i < N; i++ {
			for j := 0; j < len(pos[i])-1; j++ {

				jump[pos[i][j]][0] = min(jump[pos[i][j]][0], pos[i][j+1])
			}

		}
		for j := 1; j < 20; j++ {
			for i := 0; i < n; i++ {
				jump[i][j] = jump[jump[i][j-1]][j-1]

			}
		}
		for ; q > 0; q-- {
			var l, r int
			fmt.Fscan(in, &l, &r)
			l--
			r--
			u := l
			ans := 1
			for i := 20 - 1; i >= 0; i-- {
				if jump[u][i] <= r {
					//fmt.Fprint(out, u, "->", jump[u][i], " ")
					u = jump[u][i]
					ans += (1 << i)
				}
			}

			fmt.Fprintln(out, ans)

		}

	}

	T := 1
	//fmt.Fscan(in, &T)
	for o := 0; o < T; o++ {
		solve()
	}

}

func main() { run(os.Stdin, os.Stdout) }


Cat Furrier Transform is a popular algorithm among cat programmers to create longcats. As one of the greatest cat programmers ever exist, Neko wants to utilize this algorithm to create the perfect longcat.Assume that we have a cat with a number $$$x$$$. A perfect longcat is a cat with a number equal $$$2^m - 1$$$ for some non-negative integer $$$m$$$. For example, the numbers $$$0$$$, $$$1$$$, $$$3$$$, $$$7$$$, $$$15$$$ and so on are suitable for the perfect longcats.In the Cat Furrier Transform, the following operations can be performed on $$$x$$$:  (Operation A): you select any non-negative integer $$$n$$$ and replace $$$x$$$ with $$$x \oplus (2^n - 1)$$$, with $$$\oplus$$$ being a bitwise XOR operator. (Operation B): replace $$$x$$$ with $$$x + 1$$$. The first applied operation must be of type A, the second of type B, the third of type A again, and so on. Formally, if we number operations from one in the order they are executed, then odd-numbered operations must be of type A and the even-numbered operations must be of type B.Neko wants to produce perfect longcats at industrial scale, thus for each cat Neko only wants to perform at most $$$40$$$ operations. Can you help Neko writing a transformation plan?Note that it is not required to minimize the number of operations. You just need to use no more than $$$40$$$ operations.

The only line contains a single integer $$$x$$$ ($$$1 \le x \le 10^6$$$).

The first line should contain a single integer $$$t$$$ ($$$0 \le t \le 40$$$) — the number of operations to apply. Then for each odd-numbered operation print the corresponding number $$$n_i$$$ in it. That is, print $$$\lceil \frac{t}{2} \rceil$$$ integers $$$n_i$$$ ($$$0 \le n_i \le 30$$$), denoting the replacement $$$x$$$ with $$$x \oplus (2^{n_i} - 1)$$$ in the corresponding step. If there are multiple possible answers, you can print any of them. It is possible to show, that there is at least one answer in the constraints of this problem.

NoteIn the first test, one of the transforms might be as follows: $$$39 \to 56 \to 57 \to 62 \to 63$$$. Or more precisely: Pick $$$n = 5$$$. $$$x$$$ is transformed into $$$39 \oplus 31$$$, or $$$56$$$.  Increase $$$x$$$ by $$$1$$$, changing its value to $$$57$$$.  Pick $$$n = 3$$$. $$$x$$$ is transformed into $$$57 \oplus 7$$$, or $$$62$$$.  Increase $$$x$$$ by $$$1$$$, changing its value to $$$63 = 2^6 - 1$$$. In the second and third test, the number already satisfies the goal requirement.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	ans := 0
	a := make([]int, 0)
	cur := n
	for true {
		if cur == 1 {
			fmt.Fprintf(out, "%d\n", ans)
			for i := 0; i < len(a)-1; i++ {
				fmt.Fprintf(out, "%d ", a[i])
			}
			if len(a) > 0 {
				fmt.Fprintf(out, "%d", a[len(a)-1])
				fmt.Fprintf(out, "\n")
			}
			return
		}
		q := 1
		cnt := 1
		for q < cur {
			q++
			q *= 2
			q--
			cnt++
		}
		if q == cur {
			fmt.Fprintf(out, "%d\n", ans)
			for i := 0; i < len(a)-1; i++ {
				fmt.Fprintf(out, "%d ", a[i])
			}
			if len(a) > 0 {
				fmt.Fprintf(out, "%d", a[len(a)-1])
				fmt.Fprintf(out, "\n")
			}
			return
		}
		if cur*2 == q+1 {
			ans++
			a = append(a, cnt)
			fmt.Fprintf(out, "%d\n", ans)
			for i := 0; i < len(a)-1; i++ {
				fmt.Fprintf(out, "%d ", a[i])
			}
			if len(a) > 0 {
				fmt.Fprintf(out, "%d", a[len(a)-1])
				fmt.Fprintf(out, "\n")
			}
			return
		}
		cur ^= q
		cur++
		ans += 2
		a = append(a, cnt)
	}
	return
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x int
	fmt.Scanf("%d", &x)
	nTimes, ans := solve(x)

	fmt.Println(nTimes)
	for i := 0; i < len(ans); i++ {
		fmt.Printf("%d ", ans[i])
	}

	fmt.Println()
}

func solve(x int) (int, []int) {
	ans := make([]int, 0, 0)
	nTimes := 0

	for !areAllBitsSet(x) {
		// fmt.Println("[slv]", x)
		fZero := fixZeroLeft(x)
		allOnes := (1 << uint(fZero+1)) - 1

		x = x ^ allOnes
		ans = append(ans, fZero+1)
		nTimes++
		if areAllBitsSet(x) {
			return nTimes, ans
		}

		x++
		// fmt.Println(x)
		nTimes++
	}

	return nTimes, ans
}

func areAllBitsSet(x int) bool {
	if x == 0 {
		return false
	}

	for x > 0 {
		// fmt.Println("[aabs]", x)
		if (x & 1) == 0 {
			return false
		}

		x = x >> 1
	}

	return true
}

func fixZeroLeft(x int) int {
	power := log2(x)

	var i uint
	for i = uint(power); i >= 0; i-- {
		if nBitOff(x, i) {
			return int(i)
		}
	}

	return power
}

func nBitOff(x int, n uint) bool {
	return (x & (1 << n)) == 0
}

func log2(x int) int {
	return int(math.Log(float64(x)) / math.Log(2))
}

package main

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

var reader = bufio.NewReader(os.Stdin)
var writer = bufio.NewWriter(os.Stdout)

func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func print(a ...interface{})            { fmt.Fprint(writer, a...) }
func scanf(f string, a ...interface{})  { fmt.Fscanf(reader, f, a...) }
func scan(a ...interface{})             { fmt.Fscan(reader, a...) }

func main() {
	defer writer.Flush() // STDOUT MUST BE FLUSHED MANUALLY!!!
	var x uint32
	scan(&x)
	curBit := uint32(31)
	op := 0
	var ch []uint32

	// Trim leading zeroes
	for {
		if curBit == 0 || (x&(1<<curBit)) != 0 {
			break
		}
		x &^= 1 << curBit
		curBit--
	}

	for {
		// Check if X is compliant
		lm := uint32((1 << (curBit + 1)) - 1)
		if x == lm {
			break
		}

		// Trim leading 1.
		if (x & (1 << curBit)) != 0 {
			x &^= 1 << curBit
			curBit--

			if curBit != 0 {
				continue
			}
		}

		// Flip
		ch = append(ch, curBit+1)
		xor := uint32((1 << (curBit + 1)) - 1)
		x ^= xor
		op++

		// Check Limit
		lm = uint32((1 << (curBit + 1)) - 1)
		if x == lm {
			break
		}
		x++
		op++
	}

	print(op, "\n")
	for idx, val := range ch {
		if idx == 0 {
			print(val)
		} else {
			print(" ", val)
		}
	}
	if len(ch) != 0 {
		print("\n")
	}
}


Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

The first line contains one integer n (1 ≤ n ≤ 200000) — the number of elements in a. The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of the array.

Print one integer — the sum of d(ai, aj) over all pairs (i, j) such that 1 ≤ i ≤ j ≤ n.

NoteIn the first example:  d(a1, a2) = 0;  d(a1, a3) = 2;  d(a1, a4) = 0;  d(a1, a5) = 2;  d(a2, a3) = 0;  d(a2, a4) = 0;  d(a2, a5) = 0;  d(a3, a4) =  - 2;  d(a3, a5) = 0;  d(a4, a5) = 2.

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"math/big"
	"os"
	"sort"
)

var a, cnt [200200]int32
var bit [200200]int64
var b = make([]int32, 200200)
var n int

func sum(x int) (rec int64, c int32) {
	for x > 0 {
		rec += bit[x]
		c += cnt[x]
		x -= x & -x
	}
	return rec, c
}

func add(x, n int, num int32) {
	for x <= n {
		bit[x] += int64(num)
		cnt[x]++
		x += x & -x
	}
}

func upper(a []int32, x int32) int {
	var l, m int
	var r = len(a) - 1
	for l < r {
		//fmt.Println(l, r, a[l], x)
		m = r - (r-l)/2
		if a[m] > x {
			r = m - 1
		} else {
			l = m
		}
	}
	return l
}

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscan(in, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
		b[i] = a[i]
	}
	tmp := b
	b = b[1 : n+1 : n+1]
	sort.SliceStable(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	var difn = 1
	for i, _ := range b {
		//fmt.Println("after sort:", i, b[i])
		if i == 0 {
			continue
		}
		if b[i] == b[i-1] {
			continue
		}
		b[difn] = b[i]
		difn++
	}
	difn++
	b = tmp[:difn]
	b[0] = -999999999
	/*for i, _ := range b {
		fmt.Println("after unique:", i, b[i])
	}*/
	var ans = big.NewInt(0)
	for i := n; i > 0; i-- {
		i1 := upper(b, a[i]+1)
		i2 := upper(b, a[i]-2)
		s, c := sum(difn)
		s1, c1 := sum(i1 + 1) //<= a[i]+1的第一个
		ans.Add(ans, big.NewInt(s-s1-int64(a[i])*int64(c-c1)))
		//fmt.Println("sum:", s, s1, "cnt:", c, c1, "ans:", ans, "difn:", difn)
		s, c = sum(i2 + 1) //<= a[i]-2的第一个
		ans.Sub(ans, big.NewInt(int64(c)*int64(a[i])-s))
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		i3 := upper(b, a[i])
		//fmt.Println("i:", i, "a[i]:", a[i], "i1:", i1, "i2:", i2, "i3:", i3, "ans:", ans)
		add(i3+1, difn, b[i3])
	}
	fmt.Println(ans)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"math/big"
	"os"
	"sort"
)

var a, cnt [200200]int32
var bit [200200]int64
var b = make([]int32, 200200)
var n int

func sum(x int) (rec int64, c int32) {
	for x > 0 {
		rec += bit[x]
		c += cnt[x]
		x -= x & -x
	}
	return rec, c
}

func add(x, n int, num int32) {
	for x <= n {
		bit[x] += int64(num)
		cnt[x]++
		x += x & -x
	}
}

func upper(a []int32, x int32) int {
	var l, m int
	var r = len(a) - 1
	for l < r {
		//fmt.Println(l, r, a[l], x)
		m = r - (r-l)/2
		if a[m] > x {
			r = m - 1
		} else {
			l = m
		}
	}
	return l
}

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscan(in, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
		b[i] = a[i]
	}
	tmp := b
	b = b[1 : n+1 : n+1]
	sort.SliceStable(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	var difn = 1
	for i, _ := range b {
		//fmt.Println("after sort:", i, b[i])
		if i == 0 {
			continue
		}
		if b[i] == b[i-1] {
			continue
		}
		b[difn] = b[i]
		difn++
	}
	difn++
	b = tmp[:difn]
	b[0] = -999999999
	/*for i, _ := range b {
		fmt.Println("after unique:", i, b[i])
	}*/
	var ans = big.NewInt(0)
	for i := n; i > 0; i-- {
		i1 := upper(b, a[i]+1)
		i2 := upper(b, a[i]-2)
		s, c := sum(difn)
		s1, c1 := sum(i1 + 1) //<= a[i]+1的第一个
		ans.Add(ans, big.NewInt(s-s1-int64(a[i])*int64(c-c1)))
		//fmt.Println("sum:", s, s1, "cnt:", c, c1, "ans:", ans, "difn:", difn)
		s, c = sum(i2 + 1) //<= a[i]-2的第一个
		ans.Sub(ans,big.NewInt(int64(c)*int64(a[i]) - s))
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		i3 := upper(b, a[i])
		//fmt.Println("i:", i, "a[i]:", a[i], "i1:", i1, "i2:", i2, "i3:", i3, "ans:", ans)
		add(i3+1, difn, b[i3])
	}
	fmt.Println(ans)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"math/big"
	"os"
)

var n int
var mp = make(map[int32]int32, 200200)

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscan(in, &n)
	var sum int64
	var x int32
	ans := big.NewInt(0)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &x)
		ans.Add(ans, big.NewInt(int64(i)*int64(x)-sum-int64(mp[x-1]-mp[x+1])))
		sum += int64(x)
		mp[x]++
	}
	fmt.Println(ans)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"math/big"
	"os"
	"sort"
)

var n int
var a [200200]int32
var b = make([]int32, 200200)
var cnt [800200]int32
var sum [800200]int64

func insert(x int32, id, k, l, r int) {
	if l == r {
		sum[k] += int64(x)
		cnt[k]++
	} else {
		m := (l + r) / 2
		if id > m {
			insert(x, id, k<<1|1, m+1, r)
		} else {
			insert(x, id, k<<1, l, m)
		}
		sum[k] = sum[k<<1|1] + sum[k<<1]
		cnt[k] = cnt[k<<1|1] + cnt[k<<1]
	}
}

func query(k, l, r, ql, qr int) (s int64, c int32) {
	if ql <= l && r <= qr {
		return sum[k], cnt[k]
	}
	m := (l + r) / 2
	if ql <= m {
		ts, tc := query(k<<1, l, m, ql, qr)
		s += ts
		c += tc
	}
	if qr > m {
		ts, tc := query(k<<1|1, m+1, r, ql, qr)
		s += ts
		c += tc
	}
	return s, c
}

func lower_bound(a []int32, x int32) int {
	var l, m int
	var r = len(a) - 1
	for l < r {
		//fmt.Println(l, r, a[l], x)
		m = r - (r-l)/2
		if a[m] > x {
			r = m - 1
		} else {
			l = m
		}
	}
	return l
}

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	file := bytes.NewReader(buf)
	fmt.Fscan(file, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(file, &a[i])
		b[i] = a[i]
	}
	b[0] = -999999999
	b[n+1] = 2000000000
	tmp := b[:n+2]
	sort.Slice(tmp, func(i, j int) bool {
		return tmp[i] < tmp[j]
	})
	var difn = 1
	for i, _ := range tmp {
		if i == 0 {
			continue
		}
		if tmp[i] == tmp[i-1] {
			continue
		}
		b[difn] = tmp[i]
		difn++
	}
	/*for i := 0; i < difn; i++ {
		fmt.Println("i:", i, "a[i]:", a[i], "b[i]:", b[i], "tmp[i]:", tmp[i])
	}*/
	var ans = big.NewInt(0)
	b = tmp[:difn]
	for i := n; i > 0; i-- {
		i1 := lower_bound(b, a[i]+1)
		i2 := lower_bound(b, a[i]-2)
		s, c := query(1, 1, difn, 1, difn)
		s1, c1 := query(1, 1, difn, 1, i1+1) //<= a[i]+1的第一个
		ans.Add(ans, big.NewInt(s-s1-int64(a[i])*int64(c-c1)))
		//fmt.Println("sum:", s, s1, "cnt:", c, c1, "ans:", ans, "difn:", difn)
		s, c = query(1, 1, difn, 1, i2+1) //<= a[i]-2的第一个
		ans.Sub(ans, big.NewInt(int64(c)*int64(a[i])-s))
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		i3 := lower_bound(b, a[i])
		//fmt.Println("i:", i, "a[i]:", a[i], "i1:", i1, "i2:", i2, "i3:", i3, "ans:", ans)
		insert(a[i], i3+1, 1, 1, difn)
	}
	fmt.Println(ans)
}

package main

import (
	"bufio"
	"fmt"
	"os"
	"math/big"
)

func main() {
	stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)

	for true {
		var n int
		if _, err := fmt.Fscan(stdin, &n); err != nil {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(stdin, &a[i])
		}
		sum := make([]int64, n)
		sum[0] = int64(a[0])
		for i := 1; i < n; i++ {
			sum[i] = sum[i-1] + int64(a[i])
		}
		count := make(map[int]int)
		res := big.NewInt(0)
		for i := 0; i < n; i++ {
			if i > 0 {
				prevCount := i
				prevSum := sum[i-1]
				minusOneCount := count[a[i]-1]
				plusOneCount := count[a[i]+1]
				//fmt.Printf("prevSum %d\n", prevSum)
				prevSum -= int64(minusOneCount) * int64(a[i] - 1) +
					int64(plusOneCount) * int64(a[i] + 1)
				prevCount -= minusOneCount + plusOneCount
				t := int64(prevCount) * int64(a[i]) - prevSum
				res.Add(res, big.NewInt(t))
				//fmt.Printf("minusOneCount %d, plus %d, prev %d\n", minusOneCount, plusOneCount, prevCount)
				//fmt.Printf("i = %d, prevCount %d, prevSum %d, t %d, res %d\n",
				//	i, prevCount, prevSum, t, res)
			}
			count[a[i]]++
		}
		fmt.Fprintf(stdout, "%s\n", res.String())
	}
	stdout.Flush()
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"math/big"
	"os"
)

// github.com/EndlessCheng/codeforces-go
func CF903D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, y, s int64
	ans := &big.Int{}
	cnt := map[int64]int64{}
	Fscan(in, &n)
	for i := int64(0); i < n; i++ {
		Fscan(in, &y)
		ans.Add(ans, big.NewInt(i*y-s))
		for x := y - 1; x <= y+1; x++ {
			ans.Sub(ans, big.NewInt(cnt[x]*(y-x)))
		}
		s += y
		cnt[y]++
	}
	Fprint(out, ans)
}

func main() { CF903D(os.Stdin, os.Stdout) }

package main

import (
	"bufio"
	"fmt"
	"math/big"
	"os"
	"strconv"
	"strings"
)

const (
	MAX = 2e5 + 10
)

var a []int64 = make([]int64, MAX)
var c []int64 = make([]int64, MAX)

func main() {
	bio := bufio.NewReader(os.Stdin)
	line, _ := bio.ReadString('\n')
	line = strings.TrimSpace(line)
	n, _ := strconv.ParseInt(line, 10, 64)

	line, _ = bio.ReadString('\n')
	line = strings.TrimSpace(line)

	strs := strings.Split(line, " ")

	for i, str := range strs {
		a[i], _ = strconv.ParseInt(str, 10, 64)
	}

	M := make(map[int64]int64)

	var cnt int64 = 1

	ans := big.NewInt(0)
	inc := big.NewInt(0)
	bbb := big.NewInt(0)

	for i := n - 1; i >= 0; i-- {
		tmp := a[i]
		if M[tmp] == 0 {
			M[tmp] = cnt
			cnt++
		}
		u := M[tmp]

		c[u]++

		inc.Add(inc, big.NewInt(tmp))

		p := int64(-1) * tmp * (n - i)

		ans.Add(ans, bbb.Add(inc, big.NewInt(p)))

		if M[tmp-1] != 0 {
			v := M[tmp-1]
			ans.Add(ans, big.NewInt(c[v]))
		}

		if M[tmp+1] != 0 {
			v := M[tmp+1]
			ans.Add(ans, big.NewInt(-c[v]))
		}

	}

	fmt.Println(ans.String())

}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

var a, cnt [200200]int32
var bit [200200]int64
var b = make([]int32, 200200)
var n int

func sum(x int) (rec int64, c int32) {
	for x > 0 {
		rec += bit[x]
		c += cnt[x]
		x -= x & -x
	}
	return rec, c
}

func add(x, n int, num int32) {
	for x <= n {
		bit[x] += int64(num)
		cnt[x]++
		x += x & -x
	}
}

func upper(a []int32, x int32) int {
	var l, m int
	var r = len(a) - 1
	for l < r {
		//fmt.Println(l, r, a[l], x)
		m = r - (r-l)/2
		if a[m] > x {
			r = m - 1
		} else {
			l = m
		}
	}
	return l
}

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscan(in, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
		b[i] = a[i]
	}
	tmp := b
	b = b[1 : n+1 : n+1]
	sort.SliceStable(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	var difn = 1
	for i, _ := range b {
		//fmt.Println("after sort:", i, b[i])
		if i == 0 {
			continue
		}
		if b[i] == b[i-1] {
			continue
		}
		b[difn] = b[i]
		difn++
	}
	difn++
	b = tmp[:difn]
	b[0] = -999999999
	/*for i, _ := range b {
		fmt.Println("after unique:", i, b[i])
	}*/
	var ans int64
	for i := n; i > 0; i-- {
		i1 := upper(b, a[i]+1)
		i2 := upper(b, a[i]-2)
		s, c := sum(difn)
		s1, c1 := sum(i1 + 1) //<= a[i]+1的第一个
		ans += s - s1 - int64(a[i]*(c-c1))
		//fmt.Println("sum:", s, s1, "cnt:", c, c1, "ans:", ans, "difn:", difn)
		s, c = sum(i2 + 1) //<= a[i]-2的第一个
		ans -= int64(c*a[i]) - s
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		i3 := upper(b, a[i])
		//fmt.Println("i:", i, "a[i]:", a[i], "i1:", i1, "i2:", i2, "i3:", i3, "ans:", ans)
		add(i3+1, difn, b[i3])
	}
	fmt.Println(ans)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"math/big"
	"os"
	"sort"
)

var a, cnt [200200]int32
var bit [200200]int64
var b = make([]int32, 200200)
var n int

func sum(x int) (rec int64, c int32) {
	for x > 0 {
		rec += bit[x]
		c += cnt[x]
		x -= x & -x
	}
	return rec, c
}

func add(x, n int, num int32) {
	for x <= n {
		bit[x] += int64(num)
		cnt[x]++
		x += x & -x
	}
}

func upper(a []int32, x int32) int {
	var l, m int
	var r = len(a) - 1
	for l < r {
		//fmt.Println(l, r, a[l], x)
		m = r - (r-l)/2
		if a[m] > x {
			r = m - 1
		} else {
			l = m
		}
	}
	return l
}

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscanf(in, "%d", &n)
	for i := 1; i <= n; i++ {
		fmt.Fscanf(in, "%d", &a[i])
		b[i] = a[i]
	}
	tmp := b
	b = b[1 : n+1 : n+1]
	sort.SliceStable(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	var difn = 1
	for i, _ := range b {
		//fmt.Println("after sort:", i, b[i])
		if i == 0 {
			continue
		}
		if b[i] == b[i-1] {
			continue
		}
		b[difn] = b[i]
		difn++
	}
	difn++
	b = tmp[:difn]
	b[0] = -999999999
	/*for i, _ := range b {
		fmt.Println("after unique:", i, b[i])
	}*/
	var ans = big.NewInt(0)
	for i := n; i > 0; i-- {
		i1 := upper(b, a[i]+1)
		i2 := upper(b, a[i]-2)
		s, c := sum(difn)
		s1, c1 := sum(i1 + 1) //<= a[i]+1的第一个
		ans.Add(ans, big.NewInt(s-s1-int64(a[i])*int64(c-c1)))
		//fmt.Println("sum:", s, s1, "cnt:", c, c1, "ans:", ans, "difn:", difn)
		s, c = sum(i2 + 1) //<= a[i]-2的第一个
		ans.Sub(ans, big.NewInt(int64(c)*int64(a[i])-s))
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		i3 := upper(b, a[i])
		//fmt.Println("i:", i, "a[i]:", a[i], "i1:", i1, "i2:", i2, "i3:", i3, "ans:", ans)
		add(i3+1, difn, b[i3])
	}
	fmt.Println(ans)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

var a, cnt [200200]int32
var bit [200200]int64
var b = make([]int32, 200200)
var n int

func sum(x int) (rec int64, c int32) {
	for x > 0 {
		rec += bit[x]
		c += cnt[x]
		x -= x & -x
	}
	return rec, c
}

func add(x, n int, num int32) {
	for x <= n {
		bit[x] += int64(num)
		cnt[x]++
		x += x & -x
	}
}

func upper(a []int32, x int32) int {
	var l, m int
	var r = len(a) - 1
	for l < r {
		//fmt.Println(l, r, a[l], x)
		m = r - (r-l)/2
		if a[m] > x {
			r = m - 1
		} else {
			l = m
		}
	}
	return l
}

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscan(in, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
		b[i] = a[i]
	}
	tmp := b
	b = b[1 : n+1 : n+1]
	sort.SliceStable(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	var difn = 1
	for i, _ := range b {
		//fmt.Println("after sort:", i, b[i])
		if i == 0 {
			continue
		}
		if b[i] == b[i-1] {
			continue
		}
		b[difn] = b[i]
		difn++
	}
	difn++
	b = tmp[:difn]
	b[0] = -999999999
	/*for i, _ := range b {
		fmt.Println("after unique:", i, b[i])
	}*/
	var ans int64
	for i := n; i > 0; i-- {
		i1 := upper(b, a[i]+1)
		i2 := upper(b, a[i]-2)
		s, c := sum(difn)
		s1, c1 := sum(i1 + 1) //<= a[i]+1的第一个
		ans += s - s1 - int64(a[i])*int64(c-c1)
		//fmt.Println("sum:", s, s1, "cnt:", c, c1, "ans:", ans, "difn:", difn)
		s, c = sum(i2 + 1) //<= a[i]-2的第一个
		ans -= int64(c)*int64(a[i]) - s
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		i3 := upper(b, a[i])
		//fmt.Println("i:", i, "a[i]:", a[i], "i1:", i1, "i2:", i2, "i3:", i3, "ans:", ans)
		add(i3+1, difn, b[i3])
	}
	fmt.Println(ans)
}

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"sort"
)

var a, cnt [200200]int32
var bit [200200]int64
var b = make([]int32, 200200)
var n int

func sum(x int) (rec int64, c int32) {
	for x != 0 {
		rec += bit[x]
		c += cnt[x]
		x -= x & -x
	}
	return rec, c
}

func add(x int, num int32) {
	for x <= n {
		bit[x] += int64(num)
		cnt[x]++
		x += x & -x
	}
}

func upper(a []int32, x int32) int {
	var l, m int
	var r = len(a) - 1
	for l < r {
		//fmt.Println(l, r, a[l], x)
		m = r - (r-l)/2
		if a[m] > x {
			r = m - 1
		} else {
			l = m
		}
	}
	return l
}

func main() {
	buf, _ := ioutil.ReadAll(os.Stdin)
	in := bytes.NewReader(buf)
	fmt.Fscan(in, &n)
	for i := 1; i <= n; i++ {
		fmt.Fscan(in, &a[i])
		b[i] = a[i]
	}
	tmp := b
	b = b[1 : n+1 : n+1]
	sort.SliceStable(b, func(i, j int) bool {
		return b[i] < b[j]
	})
	var difn = 1
	for i, _ := range b {
		//fmt.Println("after sort:", i, b[i])
		if i == 0 {
			continue
		}
		if b[i] == b[i-1] {
			continue
		}
		b[difn] = b[i]
		difn++
	}
	difn++
	b = tmp[:difn]
	b[0] = -999999999
	/*for i, _ := range b {
		fmt.Println("after unique:", i, b[i])
	}*/
	var ans int64
	for i := n; i > 0; i-- {
		i1 := upper(b, a[i]+1)
		i2 := upper(b, a[i]-2)
		s, c := sum(difn)
		ans += s - int64(c*a[i])
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		s, c = sum(i1 + 1)
		ans -= s - int64(c*a[i])
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		s, c = sum(i2 + 1)
		ans += s - int64(c*a[i])
		//fmt.Println("sum:", s, "cnt:", c, "ans:", ans)
		i3 := upper(b, a[i])
		//fmt.Println("i:", i, "a[i]:", a[i], "i1:", i1, "i2:", i2, "i3:", i3, "ans:", ans)
		add(i3+1, b[i3])
	}
	fmt.Println(ans)
}

package main

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

func main() {
	stdin := bufio.NewReader(os.Stdin)
	stdout := bufio.NewWriter(os.Stdout)

	for true {
		var n int
		if _, err := fmt.Fscan(stdin, &n); err != nil {
			break
		}
		a := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(stdin, &a[i])
		}
		sum := make([]int64, n)
		sum[0] = int64(a[0])
		for i := 1; i < n; i++ {
			sum[i] = sum[i-1] + int64(a[i])
		}
		count := make(map[int]int)
		res := int64(0)
		for i := 0; i < n; i++ {
			if i > 0 {
				prevCount := i
				prevSum := sum[i-1]
				minusOneCount := count[a[i]-1]
				plusOneCount := count[a[i]+1]
				//fmt.Printf("prevSum %d\n", prevSum)
				prevSum -= int64(minusOneCount) * int64(a[i] - 1) +
					int64(plusOneCount) * int64(a[i] + 1)
				prevCount -= minusOneCount + plusOneCount
				t := int64(prevCount) * int64(a[i]) - prevSum
				res += t
				//fmt.Printf("minusOneCount %d, plus %d, prev %d\n", minusOneCount, plusOneCount, prevCount)
				//fmt.Printf("i = %d, prevCount %d, prevSum %d, t %d, res %d\n",
				//	i, prevCount, prevSum, t, res)
			}
			count[a[i]]++
		}
		fmt.Fprintf(stdout, "%d\n", res)
	}
	stdout.Flush()
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF903D(_r io.Reader, out io.Writer) {
	in := bufio.NewReader(_r)
	var n, v, ans, s int64
	cnt := map[int64]int64{}
	Fscan(in, &n)
	for i := int64(0); i < n; i++ {
		Fscan(in, &v)
		ans += i*v - s
		for j := v - 1; j <= v+1; j++ {
			c := cnt[j]
			ans -= c * (v - j)
		}
		s += v
		cnt[v]++
	}
	Fprint(out, ans)
}

func main() { CF903D(os.Stdin, os.Stdout) }