package main

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

type Scanner struct {
    r            io.Reader
    split        bufio.SplitFunc
    maxTokenSize int
    token, buf   []byte
    start, end   int
}

func NewScanner(r io.Reader) *Scanner {
    return &Scanner{
        r:            r,
        split:        bufio.ScanWords,
        maxTokenSize: 16777216,
        buf:          make([]byte, 4096),
    }
}

func (s *Scanner) advance(n int) bool {
    if n < 0 || n > s.end-s.start {
        return false
    }
    s.start += n
    return true
}

func (s *Scanner) Text() string {
    return string(s.token)
}

func (s *Scanner) Scan() bool {
    for {
        if s.end > s.start {
            advance, token, err := s.split(s.buf[s.start:s.end], false)
            if err != nil || !s.advance(advance) {
                return false
            }
            s.token = token
            if token != nil {
                return true
            }
        }
        if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
            copy(s.buf, s.buf[s.start:s.end])
            s.end -= s.start
            s.start = 0
        }
        if s.end == len(s.buf) {
            if len(s.buf) >= s.maxTokenSize {
                return false
            }
            newSize := len(s.buf) * 2
            if newSize > s.maxTokenSize {
                newSize = s.maxTokenSize
            }
            newBuf := make([]byte, newSize)
            copy(newBuf, s.buf[s.start:s.end])
            s.buf = newBuf
            s.end -= s.start
            s.start = 0
            continue
        }
        for loop := 0; ; {
            n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
            s.end += n
            if n > 0 {
                break
            }
            loop++
            if loop > 100 {
                break
            }
        }
    }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
    s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }

func ni32() int {
    sc.Scan()
    n, _ := strconv.Atoi(sc.Text())
    return n
}

func ni64() int64 {
    sc.Scan()
    n, _ := strconv.ParseInt(sc.Text(), 10, 64)
    return n
}

func pri32(i ...int) {
    for _, ii := range i {
        w.WriteString(strconv.Itoa(ii))
        sp()
    }
}
func pri64(i ...int64) {
    for _, ii := range i {
        w.WriteString(strconv.FormatInt(ii, 10))
        sp()
    }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func min(i, j int) int {
    if i < j {
        return i
    }
    return j
}

func max(i, j int64) int64 {
    if i > j {
        return i
    }
    return j
}

func main() {
    defer w.Flush()
    n, m, a := ni32(), ni32(), ni32()
    b := make([]int, n)
    p := make([]int, m)
    tb := make([]int64, n)
    tp := make([]int64, m)
    for i := 0; i < n; i++ {
        b[i] = ni32()
    }
    for i := 0; i < m; i++ {
        p[i] = ni32()
    }
    sort.Ints(b)
    sort.Ints(p)
    tb[0], tp[0] = int64(b[0]), int64(p[0])
    for i := 1; i < n; i++ {
        tb[i] = tb[i-1] + int64(b[i])
    }
    for i := 1; i < m; i++ {
        tp[i] = tp[i-1] + int64(p[i])
    }
    //first J cannot
    J := sort.Search(n, func(j int) bool {
        if n-j > m {
            return false
        }
        rest := a
        for i := j; i < n; i++ {
            if b[i] < p[i-j] {
                rest -= p[i-j] - b[i]
            }
            if rest < 0 {
                return false
            }
        }
        return true
    })
    if J == n {
        pri64(0, 0)
    } else {
        tot, av := tb[n-1], int64(0)
        if J > 0 {
            tot -= tb[J-1]
        }
        if J < n {
            av += tp[n-1-J]
        }
        ans := av - int64(a)
        if ans < 0 {
            ans = 0
        }
        pri64(int64(n-J), ans)
    }

}

package main

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

var (
	scanner = bufio.NewScanner(os.Stdin)
	buffer  = bufio.NewWriter(os.Stdout)
)

func nextInt() (x int) {
	scanner.Scan()
	x, _ = strconv.Atoi(scanner.Text())
	return
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	scanner.Split(bufio.ScanWords)
	n, m, k := nextInt(), nextInt(), nextInt()
	a := make([]int, n)
	p := make([]int, m)
	for i := range a {
		a[i] = nextInt()
	}
	for i := range p {
		p[i] = nextInt()
	}
	sort.Ints(a)
	sort.Ints(p)
	ans := sort.Search(min(n, m)+1, func(nn int) bool {
		share := int64(k)
		i := n - nn
		j := 0
		for i < n {
			if a[i] < p[j] {
				share -= int64(p[j] - a[i])
			}
			i++
			j++
		}
		return share < 0
	}) - 1
	sum := 0
	for _, x := range p[:ans] {
		sum += x
	}
	buffer.WriteString(strconv.FormatInt(int64(ans), 10))
	buffer.WriteByte(' ')
	buffer.WriteString(strconv.FormatInt(int64(max(sum-k, 0)), 10))
	buffer.Flush()
}

package main

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

var (
	scanner = bufio.NewScanner(os.Stdin)
)

func nextInt() (x int) {
	scanner.Scan()
	x, _ = strconv.Atoi(scanner.Text())
	return
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	scanner.Split(bufio.ScanWords)
	n, m, k := nextInt(), nextInt(), nextInt()
	a := make([]int, n)
	p := make([]int, m)
	for i := range a {
		a[i] = nextInt()
	}
	for i := range p {
		p[i] = nextInt()
	}
	sort.Ints(a)
	sort.Ints(p)
	ans := sort.Search(min(n, m)+1, func(nn int) bool {
		share := int64(k)
		i := n - nn
		j := 0
		for i < n {
			if a[i] < p[j] {
				share -= int64(p[j] - a[i])
			}
			i++
			j++
		}
		return share < 0
	}) - 1
	sum := 0
	for _, x := range p[:ans] {
		sum += x
	}
	fmt.Println(ans, max(sum-k, 0))
}

package main

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

func main() {
  bin := bufio.NewReader(os.Stdin)
	const N int = 100000
	num_child, num_bike, share := 0, 0, 0
	bike := [N]int{}
	child := [N]int{}
	fmt.Fscanf(bin, "%d %d %d", &num_child, &num_bike, &share)
	for i := 0; i < num_child; i++ {
		fmt.Fscan(bin, &child[i])
	}
	for i := 0; i < num_bike; i++ {
		fmt.Fscan(bin, &bike[i])
	}
	sort.Ints(bike[:num_bike])
	sort.Ints(child[:num_child])
	low, high, rent, cost := 0, num_bike, 0, 0
	if high > num_child {
		high = num_child
	}
	for low <= high {
		mid := (low + high) / 2
		this_share, total_cost := share, 0
		can_done := true
		for i, j := mid-1, num_child-1; i >= 0 && j >= 0; {
			if child[j]+this_share >= bike[i] {
				if bike[i] > child[j] {
					this_share -= bike[i] - child[j]
				}
			} else {
				can_done = false
				break
			}
			total_cost += bike[i]
			i--
			j--
		}
		if can_done {
			rent, cost = mid, total_cost-share
			low = mid + 1
		} else {
			high = mid - 1
		}
	}
	if cost < 0 {
		fmt.Println(rent, 0)
	} else {
		fmt.Println(rent, cost)
	}
}

package main

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

var (
	scanner = bufio.NewScanner(os.Stdin)
)

func nextInt() (x int) {
	scanner.Scan()
	x, _ = strconv.Atoi(scanner.Text())
	return
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

func max(x, y int) int {
	if x < y {
		return y
	}
	return x
}

func main() {
	scanner.Split(bufio.ScanWords)
	n, m, k := nextInt(), nextInt(), nextInt()
	a := make([]int, n)
	p := make([]int, m)
	for i := range a {
		a[i] = nextInt()
	}
	for i := range p {
		p[i] = nextInt()
	}
	sort.Ints(a)
	sort.Ints(p)
	ans := sort.Search(min(n, m)+1, func(nn int) bool {
		share := int64(k)
		i := n - nn
		j := 0
		for i < n {
			if a[i] < p[j] {
				share -= int64(p[j] - a[i])
			}
			i++
			j++
		}
		return share < 0
	}) - 1
	sum := 0
	for _, x := range p[:ans] {
		sum += x
	}
	println(ans, max(sum-k, 0))
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	const N int = 100000
	num_child, num_bike, share := 0, 0, 0
	bike := [N]int{}
	child := [N]int{}
	fmt.Scanf("%d %d %d", &num_child, &num_bike, &share)
	for i := 0; i < num_child; i++ {
		fmt.Scan(&child[i])
	}
	for i := 0; i < num_bike; i++ {
		fmt.Scan(&bike[i])
	}
	sort.Ints(bike[:num_bike])
	sort.Ints(child[:num_child])
	low, rent, cost, orignal_share := 0, 0, 0, share
	for i := 0; i < num_bike; i++ {
		value := bike[i]
		if low >= num_child {
			continue
		}
		new_low := sort.SearchInts(child[low:num_child], value) + low
		for j := low; j <= new_low && j < num_child; j++ {
			if child[j]+share >= value {
				share -= value - child[j]
				cost += value
				rent++
				new_low = j + 1
				break
			}
		}
		low = new_low
	}
	if cost-orignal_share < 0 {
		fmt.Println(rent, 0)
	} else {
		fmt.Println(rent, cost-orignal_share)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	const N int = 100000
	num_child, num_bike, share := 0, 0, 0
	bike := [N]int{}
	child := [N]int{}
	fmt.Scanf("%d %d %d", &num_child, &num_bike, &share)
	for i := 0; i < num_child; i++ {
		fmt.Scan(&child[i])
	}
	for i := 0; i < num_bike; i++ {
		fmt.Scan(&bike[i])
	}
	sort.Ints(bike[:num_bike])
	sort.Ints(child[:num_child])
	low, rent, cost, orignal_share := 0, 0, 0, share
	for i := 0; i < num_bike; i++ {
		value := bike[i]
		if low >= num_child {
			continue
		}
		new_low := sort.SearchInts(child[low:num_child], value) + low
		for j := low; j <= new_low && j < num_child; j++ {
			if child[j]+share >= value {
				share -= value - child[j]
				cost += value
				rent++
				new_low = j + 1
				break
			}
		}
		low = new_low
	}
	if cost-share < 0 {
		fmt.Println(rent, 0)
	} else {
		fmt.Println(rent, cost-orignal_share)
	}
}

package main

import (
	"fmt"
	"sort"
)

func main() {
	const N int = 100000
	num_child, num_bike, share := 0, 0, 0
	bike := [N]int{}
	child := [N]int{}
	fmt.Scanf("%d %d %d", &num_child, &num_bike, &share)
	for i := 0; i < num_child; i++ {
		fmt.Scan(&child[i])
	}
	for i := 0; i < num_bike; i++ {
		fmt.Scan(&bike[i])
	}
	sort.Ints(bike[:num_bike])
	sort.Ints(child[:num_child])
	low, high, rent, cost := 0, num_bike, 0, 0
	if high > num_child {
		high = num_child
	}
	for low <= high {
		mid := (low + high) / 2
		this_share, total_cost := share, 0
		can_done := true
		for i, j := mid-1, num_child-1; i >= 0 && j >= 0; i, j = i-1, j-1 {
			if child[j]+this_share >= bike[i] {
				if bike[i] > child[j] {
					this_share -= bike[i] - child[j]
				}
			} else {
				can_done = false
			}
			total_cost += bike[i]
		}
		if can_done {
			rent, cost = mid, total_cost-share
			low = mid + 1
		} else {
			high = mid - 1
		}
	}
	fmt.Println(rent, cost)
}

package main

import (
    "bufio"
    "io"
    "math"
    "os"
    "sort"
    "strconv"
)

type Scanner struct {
    r            io.Reader
    split        bufio.SplitFunc
    maxTokenSize int
    token, buf   []byte
    start, end   int
}

func NewScanner(r io.Reader) *Scanner {
    return &Scanner{
        r:            r,
        split:        bufio.ScanWords,
        maxTokenSize: 16777216,
        buf:          make([]byte, 4096),
    }
}

func (s *Scanner) advance(n int) bool {
    if n < 0 || n > s.end-s.start {
        return false
    }
    s.start += n
    return true
}

func (s *Scanner) Text() string {
    return string(s.token)
}

func (s *Scanner) Scan() bool {
    for {
        if s.end > s.start {
            advance, token, err := s.split(s.buf[s.start:s.end], false)
            if err != nil || !s.advance(advance) {
                return false
            }
            s.token = token
            if token != nil {
                return true
            }
        }
        if s.start > 0 && (s.end == len(s.buf) || s.start > len(s.buf)/2) {
            copy(s.buf, s.buf[s.start:s.end])
            s.end -= s.start
            s.start = 0
        }
        if s.end == len(s.buf) {
            if len(s.buf) >= s.maxTokenSize {
                return false
            }
            newSize := len(s.buf) * 2
            if newSize > s.maxTokenSize {
                newSize = s.maxTokenSize
            }
            newBuf := make([]byte, newSize)
            copy(newBuf, s.buf[s.start:s.end])
            s.buf = newBuf
            s.end -= s.start
            s.start = 0
            continue
        }
        for loop := 0; ; {
            n, _ := s.r.Read(s.buf[s.end:len(s.buf)])
            s.end += n
            if n > 0 {
                break
            }
            loop++
            if loop > 100 {
                break
            }
        }
    }
}

func (s *Scanner) Split(split bufio.SplitFunc) {
    s.split = split
}

var w *bufio.Writer = bufio.NewWriter(os.Stdout)
var sc *Scanner = NewScanner(os.Stdin)

func next() string { sc.Scan(); return sc.Text() }

func ni32() int {
    sc.Scan()
    n, _ := strconv.Atoi(sc.Text())
    return n
}

func ni64() int64 {
    sc.Scan()
    n, _ := strconv.ParseInt(sc.Text(), 10, 64)
    return n
}

func pri32(i ...int) {
    for _, ii := range i {
        w.WriteString(strconv.Itoa(ii))
        sp()
    }
}
func pri64(i ...int64) {
    for _, ii := range i {
        w.WriteString(strconv.FormatInt(ii, 10))
        sp()
    }
}
func sp()   { w.WriteByte(' ') }
func endl() { w.WriteByte('\n') }

func min(i, j int) int {
    if i < j {
        return i
    }
    return j
}

func max(i, j int64) int64 {
    if i > j {
        return i
    }
    return j
}

func main() {
    defer w.Flush()
    n, m, a := ni32(), ni32(), ni32()
    b := make([]int, n)
    p := make([]int, m)
    tb, tp := make([]int64, n), make([]int64, m)
    for i := 0; i < n; i++ {
        b[i] = ni32()
    }
    for i := 0; i < m; i++ {
        p[i] = ni32()
    }
    sort.Ints(b)
    sort.Ints(p)
    tb[0], tp[0] = int64(b[0]), int64(p[0])
    for i := 1; i < n; i++ {
        tb[i] = tb[i-1] + int64(b[i])
    }
    for i := 1; i < m; i++ {
        tp[i] = tp[i-1] + int64(p[i])
    }
    //first J cannot
    ans := int64(math.MaxInt64)
    J := sort.Search(n, func(j int) bool {
        if n-j > m {
            return false
        }
        tot := tb[n-1]
        if j > 0 {
            tot -= tb[j-1]
        }
        av := int64(0)
        if n-j-1 >= 0 {
            av += tp[n-j-1]
        }
        if tot+int64(a) >= av {
            ans = av - int64(a)
            if ans < 0 {
                ans = 0
            }
        }
        return tot+int64(a) >= av
    })
    if ans == int64(math.MaxInt64) {
        ans = 0
    }
    pri64(int64(n-J), ans)
}


package main

import "os"
import "bufio"
import "strconv"

func main(){
	var d int
	var n int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	r.Scan()
	d, _ = strconv.Atoi(r.Text())
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	a := make([]int, n)
	for i:=0; i<n; i++ {
		r.Scan()
		a[i], _ = strconv.Atoi(r.Text())
	}
	total := 0
	for j:=0; j<n; j++ {
		if j != n-1 {
			total += d - a[j]
		}
	}
	w.WriteString(strconv.Itoa(total)+"\n")
	w.Flush()
}

// 182B-mic
package main

import (
	"fmt"
)

func main() {
	var a, b, d int
	fmt.Scan(&a, &b)
	sum := a * (b - 1)
	for i := 0; i < b; i++ {
		fmt.Scan(&d)
		if i == b-1 {
			break
		}
		sum -= d
	}
	fmt.Println(sum)
}

package main

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

func main() {
    var n,i,k,d,ans int
    ans=0
    fmt.Scan(&d,&n)
    for i=0;i<n;i++ {
    	fmt.Scan(&k)
    	ans+=d-k
    }
    ans-=(d-k)
    fmt.Println(ans)
}

package main

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

var scanner *bufio.Scanner

func getI() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  d, n := int(getI()), int(getI())
  result := 0
  for i := 0; i < n; i++ {
    x := int(getI())
    if i != n-1 {
      result += d-x
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

import "fmt"

func main() {
    var d,n,a int
    fmt.Scan(&d)
    fmt.Scan(&n)
    ans := 0
    for i := 0; i < n; i++ {
        fmt.Scan(&a)
        if i != (n-1) { ans += d-a }
    }
    fmt.Println(ans)
}

package main

import "fmt"

func main() {
    var d,n,a int // Variabel d, n, a adalah integer
    fmt.Scan(&d) // meminta input d
    fmt.Scan(&n) // meminta input n
    ans := 0 // answer adalah integer yang isinya adlah 0
    for i := 0; i < n; i++ { // lakukan selama kondisi ini
        fmt.Scan(&a) // meminta input a
        if i != (n-1) { ans += d-a } // lakukan selama kondisi ini
    }
    fmt.Println(ans) // mencetak output
}

// 182B
package main

import (
	"fmt"
)

func main() {
	var d, n, mon int
	fmt.Scan(&d, &n)
	sum := 0
	for i := 1; i <= n; i++ {
		fmt.Scan(&mon)
		if i <= n-1 {
			sum += (d - mon)
		}
	}
	fmt.Println(sum)
}

// 182B
package main

import (
	"fmt"
)

func main() {
	var a, b, x, i, sum int64
	sum = 0
	fmt.Scan(&a, &b)
	for i = 0; i < b; i++ {
		fmt.Scan(&x)
		sum += a - x
	}
	fmt.Scan(&i)
	fmt.Println(sum)
}

// 182B
package main

import (
	"fmt"
)

func main() {
	var d, n, mon int
	fmt.Scan(&d, &n)
	sum := 0
	for i := 1; i <= n; i++ {
		fmt.Scan(&mon)
		if i <= n-1 {
			sum += (d - mon) / 2
		}
	}
	fmt.Println(sum)
}


package main

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

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

const N = 100010
var dp[N][2][3] int

var s string
var n int

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

func main()  {
	defer writer.Flush()
	fmt.Fscanf(reader, "%d\n", &n)
	fmt.Fscanf(reader, "%s", &s)

	for i := 1; i <= n; i++ {
		var val = int(s[i - 1] - '0')
		for j := 0; j < 2; j++ {
			for c := 0; c < 3; c++ {
				// init
				dp[i][j][c] = dp[i - 1][j][c]

				if c > 0 {
					dp[i][j][c] = max(dp[i][j][c], dp[i - 1][1 - j][c - 1])
				}

				// others
				if (c == 0 || c == 2) && j == val {
					dp[i][j][c] = max(dp[i][j][c], dp[i - 1][1 - j][c] + 1)
					if c > 0 {
						dp[i][j][c] = max(dp[i][j][c], dp[i - 1][1 - j][c - 1] + 1)
					}
				}

				if c == 1 && j == 1 - val {
					dp[i][j][c] = max(dp[i][j][c], dp[i - 1][1 - j][c] + 1)
					dp[i][j][c] = max(dp[i][j][c], dp[i - 1][1 - j][c - 1] + 1)
				}
			}
		}
	}

	var ret = 0
	for i := 0; i < 2; i++ {
		for j := 0; j < 3; j++ {
			ret = max(ret, dp[n][i][j])
		}
	}

	fmt.Fprintf(writer, "%d\n", ret)
}

package main

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

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  line, _ := reader.ReadString('\n')
  n, _ := strconv.Atoi(strings.TrimSpace(line))
  line, _ = reader.ReadString('\n')

  maxLength, maxCount := -1, -1
  start := 0
  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      length := i - start
      if length > maxLength {
        maxLength = length
        maxCount = 1
      } else if length == maxLength {
        maxCount += 1
      }
      start = i
    }
  }
  length := n - start
  if length > maxLength {
    maxLength = length
    maxCount = 1
  } else if length == maxLength {
    maxCount += 1
  }

  result := 1
  if maxLength >= 3 {
    result += 2
  } else if maxLength == 2 {
    result++
    if maxCount > 1 {
      result++
    }
  }

  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      result++
    }
  }

  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

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

var s string
var n int

func main()  {
	defer writer.Flush()
	fmt.Fscanf(reader, "%d\n", &n)
	fmt.Fscanf(reader, "%s", &s)
	var a, b int

	b = 1
	for i := 0; i < n - 1; i++ {
		if s[i] == s[i + 1] {
			a++
		} else {
			b++
		}
	}

	if a >= 2 {
		a = 2
	}

	fmt.Fprintf(writer, "%d\n", a + b)
}

package main

import (
    "fmt"
)

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s", &n, &s)
    for i := 0; i < n; i++ {
        if i+2 < n {
            if s[i] == '0' && s[i+1] == '0' && s[i+2] == '0' {
                s[i+1] = '1'
                break
            }
        }
    }
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

import (
    "fmt"
)

func inv(s []byte, a, b int) {
    for i := a; i < b; i++ {
        if s[i] == '0' {
            s[i] = '1'
        } else {
            s[i] = '0'
        }
    }
}

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s\r\n", &n, &s)
    var zero, one bool
    zeroPos := -1
    onePos := -1
    for i := 0; i+1 < n; {
        if s[i] == '0' && s[i+1] == '0' {
            if zero {
                inv(s, zeroPos, i+1)
                goto Count
            }
            zero = true
            zeroPos = i + 1
            i += 2
        } else if s[i] == '1' && s[i+1] == '1' {
            if one {
                inv(s, onePos, i+1)
                goto Count
            }
            one = true
            onePos = i + 1
            i += 2
        } else {
            i++
        }
    }
    if zero {
        inv(s, zeroPos, n)
        goto Count
    }
    if one {
        inv(s, onePos, n)
        goto Count
    }
    for i := 0; i+2 < n; i++ {
        if s[i] == '0' && s[i+1] == '0' && s[i+2] == '0' {
            s[i+1] = '1'
            break
        }
        if s[i] == '1' && s[i+1] == '1' && s[i+2] == '1' {
            s[i+1] = '0'
            break
        }
    }
Count:
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

import (
    "fmt"
)

func inv(s []byte, a, b int) {
    for i := a; i < b; i++ {
        if s[i] == '0' {
            s[i] = '1'
        } else {
            s[i] = '0'
        }
    }
}

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s\r\n", &n, &s)
    fmt.Println(n, len(s))
    var zero, one bool
    zeroPos := -1
    onePos := -1
    for i := 0; i < n && (i+1) < n; {
        if s[i] == '0' && s[i+1] == '0' {
            if zero {
                inv(s, zeroPos, i+1)
                goto Count
            }
            zero = true
            zeroPos = i + 1
            i += 2
        } else if s[i] == '1' && s[i+1] == '1' {
            if one {
                inv(s, onePos, i+1)
                goto Count
            }
            one = true
            onePos = i + 1
            i += 2
        } else {
            i++
        }
    }
    for i := 0; i < n; i++ {
        if i+2 < n {
            if s[i] == '0' && s[i+1] == '0' && s[i+2] == '0' {
                s[i+1] = '1'
                break
            }
            if s[i] == '1' && s[i+1] == '1' && s[i+2] == '1' {
                s[i+1] = '0'
                break
            }
        }
    }
Count:
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

import (
    "fmt"
)

func inv(s []byte, a, b int) {
    for i := a; i < b; i++ {
        if s[i] == '0' {
            s[i] = '1'
        } else {
            s[i] = '0'
        }
    }
}

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s\r\n", &n, &s)
    var zero, one bool
    zeroPos := -1
    onePos := -1
    for i := 0; i < n && (i+1) < n; {
        if s[i] == '0' && s[i+1] == '0' {
            if zero {
                inv(s, zeroPos, i+1)
                goto Count
            }
            zero = true
            zeroPos = i + 1
            i += 2
        } else if s[i] == '1' && s[i+1] == '1' {
            if one {
                inv(s, onePos, i+1)
                goto Count
            }
            one = true
            onePos = i + 1
            i += 2
        } else {
            i++
        }
    }
    for i := 0; i < n; i++ {
        if i+2 < n {
            if s[i] == '0' && s[i+1] == '0' && s[i+2] == '0' {
                s[i+1] = '1'
                break
            }
            if s[i] == '1' && s[i+1] == '1' && s[i+2] == '1' {
                s[i+1] = '0'
                break
            }
        }
    }
Count:
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

import (
    "fmt"
)

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s", &n, &s)
    a := -1
    for i := 0; i < n; {
        if i+1 >= n {
            break
        }
        if s[i] == '0' && s[i+1] == '0' {
            if a >= 0 {
                for j := a; j <= i; j++ {
                    if s[j] == '0' {
                        s[j] = '1'
                    } else {
                        s[j] = '0'
                    }
                }
                goto L
            }
            a = i + 1
            i += 2
        } else {
            i++
        }
    }
    for i := 0; i < n; i++ {
        if i+2 < n {
            if s[i] == '0' && s[i+1] == '0' && s[i+2] == '0' {
                s[i+1] = '1'
                break
            }
        }
    }
L:
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

import (
    "fmt"
)

func inv(s []byte, a, b int) {
    for i := a; i < b; i++ {
        if s[i] == '0' {
            s[i] = '1'
        } else {
            s[i] = '0'
        }
    }
}

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s", &n, &s)
    var zero, one bool
    zeroPos := -1
    onePos := -1
    for i := 0; i < n && (i+1) < n; {
        if s[i] == '0' && s[i+1] == '0' {
            if zero {
                inv(s, zeroPos, i+1)
                goto Count
            }
            zero = true
            zeroPos = i + 1
            i += 2
        } else if s[i] == '1' && s[i+1] == '1' {
            if one {
                inv(s, onePos, i+1)
                goto Count
            }
            one = true
            onePos = i + 1
            i += 2
        } else {
            i++
        }
    }
    for i := 0; i < n; i++ {
        if i+2 < n {
            if s[i] == '0' && s[i+1] == '0' && s[i+2] == '0' {
                s[i+1] = '1'
                break
            }
            if s[i] == '1' && s[i+1] == '1' && s[i+2] == '1' {
                s[i+1] = '0'
                break
            }
        }
    }
Count:
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

import (
    "fmt"
)

func inv(s []byte, a, b int) {
    for i := a; i < b; i++ {
        if s[i] == '0' {
            s[i] = '1'
        } else {
            s[i] = '0'
        }
    }
}

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s\r\n", &n, &s)
    var zero, one bool
    zeroPos := -1
    onePos := -1
    for i := 0; i+1 < n; {
        if s[i] == '0' && s[i+1] == '0' {
            if zero {
                inv(s, zeroPos, i+1)
                goto Count
            }
            zero = true
            zeroPos = i + 1
            i += 2
        } else if s[i] == '1' && s[i+1] == '1' {
            if one {
                inv(s, onePos, i+1)
                goto Count
            }
            one = true
            onePos = i + 1
            i += 2
        } else {
            i++
        }
    }
    for i := 0; i+2 < n; i++ {
        if s[i] == '0' && s[i+1] == '0' && s[i+2] == '0' {
            s[i+1] = '1'
            break
        }
        if s[i] == '1' && s[i+1] == '1' && s[i+2] == '1' {
            s[i+1] = '0'
            break
        }
    }
Count:
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

import (
    "fmt"
)

func inv(s []byte, a, b int) {
    for i := a; i < b; i++ {
        if s[i] == '0' {
            s[i] = '1'
        } else {
            s[i] = '0'
        }
    }
}

func main() {
    var s []byte
    var n int
    fmt.Scanf("%d\r\n%s\r\n", &n, &s)
    pos := -1
    for i := 0; i+1 < n; {
        if (s[i] == '0' && s[i+1] == '0') || (s[i] == '1' && s[i+1] == '1') {
            if pos >= 0 {
                inv(s, pos, i+1)
                goto Count
            }
            pos = i + 1
            i += 2
        } else {
            i++
        }
    }
    if pos >= 0 {
        inv(s, pos, n)
    }
Count:
    l := 0
    var b byte
    for i := 0; i < n; i++ {
        if i == 0 {
            b = s[i]
            l++
            continue
        }
        if s[i] == b {
            continue
        }
        b = s[i]
        l++
    }
    fmt.Println(l)
}

package main

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

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  line, _ := reader.ReadString('\n')
  n, _ := strconv.Atoi(strings.TrimSpace(line))
  line, _ = reader.ReadString('\n')

  maxZero, maxOne := -1, -1
  start := 0
  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      length := i - start
      if line[i - 1] == '0' {
        if length > maxZero {
          maxZero = length
        }
      } else {
        if length > maxOne {
          maxOne = length
        }
      }
      start = i
    }
  }
  length := n - start
  if line[n - 1] == '0' {
    if length > maxZero {
      maxZero = length
    }
  } else {
    if length > maxOne {
      maxOne = length
    }
  }

  result := 1
  if maxZero >= 3 || maxOne >= 3 {
    result += 2
  } else if maxZero == 2 && maxOne == 2 {
    result += 2
  } else if maxZero == 2 || maxOne == 2 {
    result++
  }

  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      result++
    }
  }

  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  line, _ := reader.ReadString('\n')
  n, _ := strconv.Atoi(strings.TrimSpace(line))
  line, _ = reader.ReadString('\n')

  bestLength, bestStart, bestEnd := -1, -1, -1
  start := 0
  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      length := i - start
      //fmt.Printf("start = %d, i = %d, length = %d\n", start, i, length)
      if length > bestLength {
        bestEnd = i
        bestStart = start
        bestLength = length
      }
      start = i
    }
  }
  length := n - start
  //fmt.Printf("start = %d, end = %d, length = %d\n", start, n, length)
  if length >= bestLength {
    bestEnd = n
    bestStart = start
    bestLength = length
  }
  result := 1
  if bestLength == 2 && (bestStart == 0 || bestEnd == n) {
    result++
  }
  if bestLength >= 3 {
    result += 2
  }

  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      result++
    }
  }

  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

func main() {
  reader := bufio.NewReader(os.Stdin)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  line, _ := reader.ReadString('\n')
  n, _ := strconv.Atoi(strings.TrimSpace(line))
  line, _ = reader.ReadString('\n')

  bestLength := -1
  start := 0
  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      length := i - start
      //fmt.Printf("start = %d, i = %d, length = %d\n", start, i, length)
      if length > bestLength {
        bestLength = length
      }
      start = i
    }
  }
  length := n - start
  //fmt.Printf("start = %d, end = %d, length = %d\n", start, n, length)
  if length >= bestLength {
    bestLength = length
  }
  result := 1
  if bestLength == 2 {
    result++
  }
  if bestLength >= 3 {
    result += 2
  }

  for i := 1; i < n; i++ {
    if line[i - 1] != line[i] {
      result++
    }
  }

  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

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

var s string
var n int

func main()  {
	defer writer.Flush()
	fmt.Fscanf(reader, "%d\n", &n)
	fmt.Fscanf(reader, "%s", &s)
	var a, b int

	b = 1
	for i := 0; i < n - 1; i++ {
		if s[i] == s[i + 1] {
			a++
		} else {
			b++
		}
	}

	if a >= 2 {
		b += 2
	}

	fmt.Fprintf(writer, "%d\n", b)
}


package main

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

func start() {
	n, m := sint(), sint()
	mat := make([][]int8, n)
	vised := make([][]bool, n)
	for i := 0; i < n; i++ {
		mat[i] = make([]int8, m)
		vised[i] = make([]bool, m)
		for j := 0; j < m; j++ {
			mat[i][j] = int8(sint())
		}
	}
	// dfs
	dfs_blocks := func(i, j int) int {
		type vec2 struct {
			x, y int
		}
		if vised[i][j] {
			return 0
		}
		vised[i][j] = true
		st := make([]vec2, 1)
		st[0] = vec2{i, j}
		blockCnt := 0
		for len(st) > 0 {
			top := st[len(st)-1]
			st = st[:len(st)-1]
			i, j := top.x, top.y
			if i+1 < n && !(2&mat[i][j] != 0) && !vised[i+1][j] {
				st = append(st, vec2{i + 1, j})
				vised[i+1][j] = true
			}
			if j+1 < m && !(4&mat[i][j] != 0) && !vised[i][j+1] {
				st = append(st, vec2{i, j + 1})
				vised[i][j+1] = true
			}
			if i > 0 && !(8&mat[i][j] != 0) && !vised[i-1][j] {
				st = append(st, vec2{i - 1, j})
				vised[i-1][j] = true
			}
			if j > 0 && !(1&mat[i][j] != 0) && !vised[i][j-1] {
				st = append(st, vec2{i, j - 1})
				vised[i][j-1] = true
			}
			blockCnt++
		}
		return blockCnt
	}
	// algo
	vec := make([]int, 0)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			bCnt := dfs_blocks(i, j)
			if bCnt > 0 {
				vec = append(vec, bCnt)
			}
		}
	}
	sort.Slice(vec, func(i, j int) bool {
		return vec[i] > vec[j]
	})
	printls(vec)
}

func main() {
	start()
	// flush
	writer.Flush()
}

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

// io utils

var (
	scanner = bufio.NewScanner(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
)

func wstring(s string) {
	writer.WriteString(s)
}
func wrune(r rune) {
	writer.WriteRune(r)
}
func wint(i int) {
	wstring(strconv.Itoa(i))
}
func wlstring(s string) {
	writer.WriteString(s)
	wrune('\n')
}
func wlrune(r rune) {
	writer.WriteRune(r)
	wrune('\n')
}
func wlint(i int) {
	wstring(strconv.Itoa(i))
	wrune('\n')
}

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printls(in []int) {
	for _, v := range in {
		wint(v)
		wrune(' ')
	}
	wlstring("")
}

func sint() int {
	i, err := strconv.Atoi(sstring())
	if err != nil {
		panic("sint: Error")
	}
	return i
}

func sints(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = sint()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

package main

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

var reader *bufio.Reader
var writer *bufio.Writer

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

func solve() {
	var n, m int
	scan(&n, &m)

	mat := make([][]int, n)
	visited := make([][]bool, n)
	for i := range mat {
		mat[i] = make([]int, m)
		visited[i] = make([]bool, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			var x int
			scan(&x)
			mat[i][j] = x
		}
	}

	var res []int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if !visited[i][j] {
				res = append(res, bfs(visited, mat, i, j))
			}
		}
	}

	sort.Slice(res, func(a, b int) bool {
		return res[b] < res[a]
	})
	for i := 0; i < len(res); i++ {
		printf("%d ", res[i])
	}
}

type pair struct {
	x, y int
}

// north, east, south, west
var dx = []int{-1, 0, 1, 0}
var dy = []int{0, 1, 0, -1}
var mask = []int{8, 4, 2, 1}

func bfs(visited [][]bool, mat [][]int, x, y int) int {
	p := pair{x, y}
	visited[x][y] = true
	queue := []pair{p}

	rooms := 1
	for len(queue) != 0 {
		top := queue[0]
		queue = queue[1:]

		for i := 0; i < 4; i++ {
			if (mat[top.x][top.y] & mask[i]) == mask[i] {
				continue
			}
			//			fmt.Println(mat[top.x][top.y], mask[i], (mat[top.x][top.y]&mask[i]) == mask[i])
			newX, newY := top.x+dx[i], top.y+dy[i]
			if newX >= 0 && newX < len(mat) && newY >= 0 && newY < len(mat[0]) && !visited[newX][newY] && (mat[newX][newY]&mask[(i+2)%4]) == 0 {
				//				fmt.Println(mask[i], mat[newX][newY], mat[top.x][top.y], mat[top.x][top.y]&mask[i])
				rooms += 1
				visited[newX][newY] = true
				queue = append(queue, pair{newX, newY})
			}
		}
	}
	return rooms
}

func main() {
	//	os.Stdin, _ = os.OpenFile("input.txt", os.O_RDONLY|os.O_CREATE, 0666)
	//	os.Stdout, _ = os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	solve()
}

package main

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

var (
	n, m int
	t    [][]int
	r    []int
	cnt  int
	dx   = []int{0, 1, 0, -1}
	dy   = []int{-1, 0, 1, 0}
)

func input() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())

	t = make([][]int, n)
	for i := 0; i < n; i++ {
		t[i] = make([]int, m)
		for j := 0; j < m; j++ {
			scanner.Scan()
			t[i][j], _ = strconv.Atoi(scanner.Text())
		}
	}
}

func dfs(x int, y int) {
	if x < 0 || x >= n || y < 0 || y >= m || t[x][y] < 0 {
		return
	}

	r[cnt]++
	txy := t[x][y]
	t[x][y] = -1
	for i := 0; i < 4; i++ {
		m := txy % 2
		txy /= 2
		if m == 0 {
			dfs(x+dx[i], y+dy[i])
		}
	}
}

func solve() {
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if t[i][j] >= 0 {
				r = append(r, 0)
				dfs(i, j)
				cnt++
			}
		}
	}
	sort.Ints(r)
}

func output() {
	writer := bufio.NewWriter(os.Stdout)
	for i := cnt - 1; i >= 0; i-- {
		writer.WriteString(strconv.Itoa(r[i]) + " ")
	}
	writer.Flush()
}

func main() {
	input()
	solve()
	output()
}

package main

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

func start() {
	n, m := sint(), sint()
	mat := make([][]int8, n)
	vised := make([][]bool, n)
	for i := 0; i < n; i++ {
		mat[i] = make([]int8, m)
		vised[i] = make([]bool, m)
		for j := 0; j < m; j++ {
			mat[i][j] = int8(sint())
		}
	}
	// dfs
	dfs_blocks := func(i, j int) int {
		type vec2 struct {
			x, y int
		}
		vised[i][j] = true
		st := make([]vec2, 1)
		st[0] = vec2{i, j}
		blockCnt := 0
		for len(st) > 0 {
			top := st[len(st)-1]
			st = st[:len(st)-1]
			i, j := top.x, top.y
			if i+1 < n && !(2&mat[i][j] != 0) && !vised[i+1][j] {
				st = append(st, vec2{i + 1, j})
				vised[i+1][j] = true
			}
			if j+1 < m && !(4&mat[i][j] != 0) && !vised[i][j+1] {
				st = append(st, vec2{i, j + 1})
				vised[i][j+1] = true
			}
			if i > 0 && !(8&mat[i][j] != 0) && !vised[i-1][j] {
				st = append(st, vec2{i - 1, j})
				vised[i-1][j] = true
			}
			if j > 0 && !(1&mat[i][j] != 0) && !vised[i][j-1] {
				st = append(st, vec2{i, j - 1})
				vised[i][j-1] = true
			}
			blockCnt++
		}
		return blockCnt
	}
	// algo
	vec := make([]int, 0)
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if vised[i][j] == false {
				bCnt := dfs_blocks(i, j)
				vec = append(vec, bCnt)
			}
		}
	}
	sort.Slice(vec, func(i, j int) bool {
		return vec[i] > vec[j]
	})
	printls(vec)
}

func main() {
	start()
	// flush
	writer.Flush()
}

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

// io utils

var (
	scanner = bufio.NewScanner(os.Stdin)
	writer  = bufio.NewWriter(os.Stdout)
)

func wstring(s string) {
	writer.WriteString(s)
}
func wrune(r rune) {
	writer.WriteRune(r)
}
func wint(i int) {
	wstring(strconv.Itoa(i))
}
func wlstring(s string) {
	writer.WriteString(s)
	wrune('\n')
}
func wlrune(r rune) {
	writer.WriteRune(r)
	wrune('\n')
}
func wlint(i int) {
	wstring(strconv.Itoa(i))
	wrune('\n')
}

func init() {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 4096), 1e9)
}

func printls(in []int) {
	for _, v := range in {
		wint(v)
		wrune(' ')
	}
	wlstring("")
}

func sint() int {
	i, err := strconv.Atoi(sstring())
	if err != nil {
		panic("sint: Error")
	}
	return i
}

func sints(n int) []int {
	a := make([]int, n)
	for i := range a {
		a[i] = sint()
	}
	return a
}

func sstring() string {
	if !scanner.Scan() {
		panic(scanner.Err())
	}
	return scanner.Text()
}

package main

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

var (
	n, m int
	t    [][]int
	r    []int
	cnt  int
	dx   = []int{0, 1, 0, -1}
	dy   = []int{-1, 0, 1, 0}
)

func input() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())

	t = make([][]int, n)
	for i := 0; i < n; i++ {
		t[i] = make([]int, m)
		for j := 0; j < m; j++ {
			scanner.Scan()
			t[i][j], _ = strconv.Atoi(scanner.Text())
		}
	}
}

func dfs(x int, y int) {
	if x < 0 || x >= n || y < 0 || y >= m || t[x][y] < 0 {
		return
	}

	r[cnt]++
	txy := t[x][y]
	t[x][y] = -1
	for i := 0; i < 4; i++ {
		m := txy % 2
		txy /= 2
		if m == 0 {
			dfs(x+dx[i], y+dy[i])
		}
	}
}

func solve() {
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if t[i][j] >= 0 {
				r = append(r, 0)
				dfs(i, j)
				cnt++
			}
		}
	}
	sort.Ints(r)
}

func output() {
	writer := bufio.NewWriter(os.Stdout)
	for i := cnt - 1; i >= 0; i-- {
		writer.WriteString(strconv.Itoa(r[i]) + " ")
	}
}

func main() {
	input()
	solve()
	output()
}

package main

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

var (
	n, m int
	t    [][]int
	r    []int
	cnt  int
	dx   = []int{0, 1, 0, -1}
	dy   = []int{-1, 0, 1, 0}
)

func input() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	scanner.Scan()
	m, _ = strconv.Atoi(scanner.Text())

	t = make([][]int, n)
	for i := 0; i < n; i++ {
		t[i] = make([]int, m)
		for j := 0; j < m; j++ {
			scanner.Scan()
			t[i][j], _ = strconv.Atoi(scanner.Text())
		}
	}
}

func dfs(x int, y int) {
	if x < 0 || x >= n || y < 0 || y >= m || t[x][y] < 0 {
		return
	}

	r[cnt]++
	txy := t[x][y]
	t[x][y] = -1
	for i := 0; i < 4; i++ {
		m := txy % 2
		txy /= 2
		if m == 0 {
			dfs(x+dx[i], y+dy[i])
		}
	}
}

func solve() {
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if t[i][j] >= 0 {
				r = append(r, 0)
				dfs(i, j)
				cnt++
			}
		}
	}
	fmt.Println("here")
	sort.Ints(r)
	fmt.Println("dfdf")
}

func output() {
	writer := bufio.NewWriter(os.Stdout)
	for i := cnt - 1; i >= 0; i-- {
		writer.WriteString(strconv.Itoa(r[i]) + " ")
	}
}

func main() {
	input()
	solve()
	output()
}

package main

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

var (
	n, m int
	t    [][]int
	r    []int
	cnt  int
	dx   = []int{0, 1, 0, -1}
	dy   = []int{-1, 0, 1, 0}
)

func input() {
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscanln(reader, &n, &m)
	t = make([][]int, n)
	for i := 0; i < n; i++ {
		t[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Fscan(reader, &t[i][j])
		}
	}
	fmt.Println("readed")
}

func dfs(x int, y int) {
	if x < 0 || x >= n || y < 0 || y >= m || t[x][y] < 0 {
		return
	}

	r[cnt]++
	txy := t[x][y]
	t[x][y] = -1
	for i := 0; i < 4; i++ {
		m := txy % 2
		txy /= 2
		if m == 0 {
			dfs(x+dx[i], y+dy[i])
		}
	}
}

func solve() {
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if t[i][j] >= 0 {
				r = append(r, 0)
				dfs(i, j)
				cnt++
			}
		}
	}
	sort.Ints(r)
}

func output() {
	for i := cnt - 1; i >= 0; i-- {
		fmt.Print(r[i], " ")
	}
}

func main() {
	input()
	solve()
	output()
}

package main

import (
	"fmt"
	"sort"
)

var (
	n, m int
	t    [][]int
	r    []int
	cnt  int
	dx   = []int{0, 1, 0, -1}
	dy   = []int{-1, 0, 1, 0}
)

func input() {
	fmt.Scanln(&n, &m)
	t = make([][]int, n)
	for i := 0; i < n; i++ {
		t[i] = make([]int, m)
		for j := 0; j < m; j++ {
			fmt.Scan(&t[i][j])
		}
	}
}

func dfs(x int, y int) {
	if x < 0 || x >= n || y < 0 || y >= m || t[x][y] < 0 {
		return
	}

	r[cnt]++
	txy := t[x][y]
	t[x][y] = -1
	for i := 0; i < 4; i++ {
		m := txy % 2
		txy /= 2
		if m == 0 {
			dfs(x+dx[i], y+dy[i])
		}
	}
}

func solve() {
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if t[i][j] >= 0 {
				r = append(r, 0)
				dfs(i, j)
				cnt++
			}
		}
	}
	sort.Ints(r)
}

func output() {
	for i := cnt - 1; i >= 0; i-- {
		fmt.Print(r[i])
		if i > 0 {
			fmt.Print(" ")
		} else {
			fmt.Println()
		}
	}
}

func main() {
	input()
	solve()
	output()
}

package main

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

var reader *bufio.Reader
var writer *bufio.Writer

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

func solve() {
	var n, m int
	scan(&n, &m)

	mat := make([][]int, n)
	visited := make([][]bool, n)
	for i := range mat {
		mat[i] = make([]int, m)
		visited[i] = make([]bool, m)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			var x int
			scan(&x)
			mat[i][j] = x
		}
	}

	var res []int
	for i := 0; i < n; i++ {
		for j := 0; j < m; j++ {
			if !visited[i][j] {
				res = append(res, bfs(visited, mat, i, j))
			}
		}
	}

	sort.Slice(res, func(a, b int) bool {
		return res[b] < res[a]
	})
	for i := 0; i < len(res); i++ {
		printf("%d", res[i])
		if i != len(res)-1 {
			printf(" ")
		}
	}
}

type pair struct {
	x, y int
}

// north, east, south, west
var dx = []int{-1, 0, 1, 0}
var dy = []int{0, 1, 0, -1}
var mask = []int{8, 4, 2, 1}

func bfs(visited [][]bool, mat [][]int, x, y int) int {
	p := pair{x, y}
	visited[x][y] = true
	queue := []pair{p}

	rooms := 1
	for len(queue) != 0 {
		top := queue[0]
		queue = queue[1:]

		for i := 0; i < 4; i++ {
			if (mat[top.x][top.y] & mask[i]) == mask[i] {
				continue
			}
			//			fmt.Println(mat[top.x][top.y], mask[i], (mat[top.x][top.y]&mask[i]) == mask[i])
			newX, newY := top.x+dx[i], top.y+dy[i]
			if newX >= 0 && newX < len(mat) && newY >= 0 && newY < len(mat[0]) && !visited[newX][newY] && (mat[newX][newY]&mask[(i+2)%4]) == 0 {
				//				fmt.Println(mask[i], mat[newX][newY], mat[top.x][top.y], mat[top.x][top.y]&mask[i])
				rooms += 1
				visited[newX][newY] = true
				queue = append(queue, pair{newX, newY})
			}
		}
	}
	return rooms
}

func main() {
	//	os.Stdin, _ = os.OpenFile("input.txt", os.O_RDONLY|os.O_CREATE, 0666)
	//	os.Stdout, _ = os.OpenFile("output.txt", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
	reader = bufio.NewReader(os.Stdin)
	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	solve()
}


package main

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

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 debug(a ...interface{}) {
	for _, e := range a {
		printf("%v ", e)
	}
	printf("\n")
	writer.Flush()
}

func solve() {
	var n int
	scanf("%d\n", &n)
	var res int64 = 0
	a := make([]int64, n)
	for i:=0; i<n; i++ {
		scanf("%d ", &a[i])
		res += a[i]
	}
	scanf("\n")

	b := make([]int64, n)
	var mx int64 = -1
	for i:=0; i<n; i++ {
		scanf("%d ", &b[i])
		if b[i] > mx {
			mx = b[i]
		}
		res += b[i]
	}
	scanf("\n")

	res -= mx
	printf("%v\n", res)
}

func main() {
	defer writer.Flush()

	var T int
	scanf("%d\n", &T)
	for tt:=1; tt<=T; tt++ {
		solve()
	}
}

package main

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int64, n)
		var tot int64
		for _ = range aa {
			var v int64
			fmt.Fscan(in, &v)
			tot += v
		}

		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		z := aa[0]
		for i := range aa {
			tot += aa[i]
			if aa[i] > z {
				z = aa[i]
			}
		}
		fmt.Fprintln(out, tot-z)
	}
}

package main

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

var (
	t, n, m  int
	x, y     int64
	row, col []int64
	a        []node
)

type node struct {
	x, y int64
	f    bool
}

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n)
		a = make([]node, n)
		for i := 0; i < n; i++ {
			if i == 0 || i == n-1 {
				a[i].f = true
			} else {
				a[i].f = false
			}
			scan(&a[i].x)
		}
		for i := 0; i < n; i++ {
			scan(&a[i].y)
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i].y <= a[j].y
		})
		var ans int64 = 0
		var dt int64 = 0
		for i := 0; i < n; i++ {
			ans += a[i].x
			if i != n-1 {
				dt += a[i].y
			}
		}
		println(ans + dt)
	}
}

//

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(r, a...)
}

func println(a ...any) {
	fmt.Fprintln(w, a...)
}

func flush() {
	w.Flush()
}

package main

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

var in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

// source: https://codeforces.com/contest/1749/problem/B?locale=en
func solve() {
	n, a := _readArr()
	_, b := _readArr(n)
	_print(_sum(a...) + _sum(b...) - _max(b...))
}

func solve_() {
	n := _readInt()
	_, a := _readArr(n)
	_, b := _readArr(n)
	res := 0

	cast := func(x int) {
		d := b[x]
		if x > 0 {
			a[x-1] += d
		}
		if x < len(a)-1 {
			a[x+1] += d
		}
		a = append(a[:x], a[x+1:]...)
		b = append(b[:x], b[x+1:]...)
	}

	next := func() int {
		mi, mv := 0, b[0]
		for i := 1; i < len(b)-1; i++ {
			if b[i]*2 < mv {
				mi = i
				mv = b[i] * 2
			}
		}
		if i := len(b) - 1; b[i] < mv {
			mi = i
		}
		return mi
	}

	for len(a) > 0 {
		i := next()
		res += a[i]
		cast(i)
	}

	_print(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		solve()
	}
}

// ====
func _readInt() int {
	var x int
	fmt.Fscan(in, &x)
	return x
}
func _readStr() string {
	var x string
	fmt.Fscan(in, &x)
	return x
}
func _readArr(x ...int) (int, []int) {
	var n int
	if len(x) == 0 {
		fmt.Fscan(in, &n)
	} else {
		n = x[0]
	}
	var v = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &v[i])
	}
	return n, v
}
func _print(x ...any) {
	fmt.Fprintln(out, x...)
}
func _max(x ...int) int {
	mi := 0
	for i := 1; i < len(x); i++ {
		if x[i] > x[mi] {
			mi = i
		}
	}
	return x[mi]
}
func _min(x ...int) int {
	mi := 0
	for i := 1; i < len(x); i++ {
		if x[i] < x[mi] {
			mi = i
		}
	}
	return x[mi]
}
func _sum(x ...int) int {
	res := 0
	for _, i := range x {
		res += i
	}
	return res
}
func _abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func _sortStr(x string) string {
	r := []rune(x)
	sort.Slice(r, func(i, j int) bool {
		return r[i] < r[j]
	})
	return string(r)
}
func _reverseInts(x []int) {
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
}
func _reverseStr(s string) string {
	x := []rune(s)
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
	return string(x)
}
func _makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}

package main

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

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

type Monster struct {
	health   int
	strength int
}

func minimunKill(monsters []Monster) int {
	if len(monsters) < 2 {
		return monsters[0].health
	}

	sort.Slice(monsters, func(i, j int) bool {
		return monsters[i].strength < monsters[j].strength
	})

	rs := 0
	for i := 0; i < len(monsters); i++ {
		rs += monsters[i].health + monsters[i].strength
	}

	return rs - monsters[len(monsters)-1].strength
}

func main() {
	var test int
	fmt.Fscanf(reader, "%d\n", &test)
	for test > 0 {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &arr[i])
		}
		fmt.Fscanf(reader, "\n")

		brr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &brr[i])
		}
		fmt.Fscanf(reader, "\n")
		monsters := make([]Monster, len(arr))
		for i := 0; i < len(arr); i++ {
			monsters[i].health = arr[i]
			monsters[i].strength = brr[i]
		}

		fmt.Println(minimunKill(monsters))
		test--
	}
}

package main

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

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

type Monster struct {
	health   int
	strength int
}

func minimun(arr []int, brr []int) int {
	if len(arr) < 2 {
		return arr[0]
	}

	monsters := make([]Monster, len(arr))
	for i := 0; i < len(arr); i++ {
		monsters[i].health = arr[i]
		monsters[i].strength = brr[i]
	}

	sort.Slice(monsters, func(i, j int) bool {
		return monsters[i].strength < monsters[j].strength
	})

	rs := 0
	for i := 0; i < len(arr); i++ {
		rs += monsters[i].health + monsters[i].strength
	}

	return rs - monsters[len(monsters) -1].strength
}

func main() {
	var test int
	fmt.Fscanf(reader, "%d\n", &test)
	for test > 0 {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)
		arr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &arr[i])
		}
		fmt.Fscanf(reader, "\n")

		brr := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(reader, "%d", &brr[i])
		}
		fmt.Fscanf(reader, "\n")
		fmt.Println(minimun(arr, brr))
		test--
	}
}

package main

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

func main() {
	r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var t, n int
	Fscan(r, &t)
	for t > 0{
		s := 0
		t--
		Fscan(r, &n)
		a := make([]int, n)
		b := make([]int, n)
		for i := range a{
			Fscan(r, &a[i])
			s += a[i]
		}
		for i := range b{
			Fscan(r, &b[i])
			s += b[i]
		}
		sort.Ints(b)
		s -= b[n-1]
		Fprintln(w, s)
	}
}

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()

	var T int
	for fmt.Fscan(r, &T); T > 0; T-- {

		var n int
		fmt.Fscan(r, &n)

		var sum, max, temp int
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &temp)
			sum += temp
		}
		for i := 0; i < n; i++ {
			fmt.Fscan(r, &temp)
			sum += temp
			if temp > max { max = temp }
		}

		fmt.Fprintln(w, sum-max)

	}

}

package main

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

const cf = true

func solve() {
	var n int
	fmt.Fscan(in, &n)
	a, b := make([]int, n), make([]int, n)
	for i := range a {
		fmt.Fscan(in, &a[i])
	}
	for i := range b {
		fmt.Fscan(in, &b[i])
	}
	i1, i2 := 0, n-1
	ans := 0
	for i1 <= i2 {
		if b[i1] < b[i2] {
			ans += a[i1]
			if i1 < n-1 {
				a[i1+1] += b[i1]
			}
			i1++
		} else {
			ans += a[i2]
			if i2 > 0 {
				a[i2-1] += b[i2]
			}
			i2--
		}
	}
	fmt.Fprintln(out, ans)
}

func main() {
	defer out.Flush()
	t := 1
	fmt.Fscan(in, &t)
	for ; t > 0; t-- {
		solve()
	}
}

func reverse[T any](arr []T) {
	n := len(arr)
	for i := 0; i < n/2; i++ {
		arr[i], arr[n-i-1] = arr[n-i-1], arr[i]
	}
}

func reversed[T any](arr []T) (res []T) {
	res = make([]T, len(arr))
	copy(res, arr)
	reverse(res)
	return res
}

func GCD(a, b int) int {
	for a != b {
		if a > b {
			a -= b
		} else {
			b -= a
		}
	}

	return a
}

func ConvertInt(val string, base, toBase int) (string, error) {
	i, err := strconv.ParseInt(val, base, 64)
	if err != nil {
		return "", err
	}
	return strconv.FormatInt(i, toBase), nil
}

type set[T Ordered] struct {
	itms map[T]struct{}
}

func (s *set[T]) init() {
	if s.itms == nil {
		s.itms = make(map[T]struct{})
	}
}

func (s *set[T]) add(item T) *set[T] {
	s.init()
	s.itms[item] = exists
	return s
}

func (s *set[T]) remove(item T) *set[T] {
	s.init()
	delete(s.itms, item)
	return s
}

func (s *set[T]) has(item T) bool {
	s.init()
	_, c := s.itms[item]
	return c
}

func (s *set[T]) sortedItems() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	sort.Slice(items, func(i, j int) bool {
		return items[i] < items[j]
	})
	return items
}

func (s *set[T]) items() []T {
	s.init()
	items := make([]T, 0, s.size())
	for item := range s.itms {
		items = append(items, item)
	}
	return items
}

func (s *set[T]) size() int {
	s.init()
	return len(s.itms)
}

var exists = struct{}{}

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

func reverseStr(s string) string {
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

type Float interface {
	~float32 | ~float64
}

type Integer interface {
	Signed | Unsigned
}

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

type Ordered interface {
	Integer | Float | ~string
}

func abs[T Signed](x T) T {
	if x < 0 {
		return -x
	}
	return x
}

func min[T Ordered](x, y T) T {
	if x < y {
		return x
	}
	return y
}

func max[T Ordered](x, y T) T {
	if x > y {
		return x
	}
	return y
}

var (
	in  = getReader()
	out = getWriter()
)

func getReader() *bufio.Reader {
	if cf {
		return bufio.NewReader(os.Stdin)
	} else {
		in, _ := os.Open("input.txt")
		return bufio.NewReader(in)
	}
}

func getWriter() *bufio.Writer {
	if cf {
		return bufio.NewWriter(os.Stdout)
	} else {
		out, _ := os.Create("output.txt")
		return bufio.NewWriter(out)
	}
}

package main

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([]int64, n)
		var tot int64
		for _ = range aa {
			var v int64
			fmt.Fscan(in, &v)
			tot += v
		}

		for i := range aa {
			fmt.Fscan(in, &aa[i])
		}

		mm := make([]bool, n)
		l, r := 0, n-1
		for l != r {
			v := aa[l]
			pos := l
			if aa[l] > aa[r] {
				v = aa[r]
				pos = r
			}
			for i := l + 1; i < r; i++ {
				if !mm[i] && aa[i]*2 <= v {
					v = aa[i] * 2
					pos = i
				}
			}
			mm[pos] = true
			tot += v
			for l < n && mm[l] {
				l++
			}
			for r > 0 && mm[r] {
				r--
			}
		}
		fmt.Fprintln(out, tot)
	}
}

package main

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([][2]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i][0])
		}
		for i := range aa {
			var v int64
			fmt.Fscan(in, &v)
			aa[i][1] = v
		}

		var tot int64
		for len(aa) > 1 {
			mn := aa[0][1]
			var mIdx int
			if aa[0][1] > aa[len(aa)-1][1] {
				mn = aa[len(aa)-1][1]
				mIdx = len(aa) - 1
			}

			for i := 1; i < len(aa)-1; i++ {
				if aa[i][1]*2 < mn {
					mn = aa[i][1]
					mIdx = i
				}
			}

			// cut
			if mIdx == 0 {
				aa[1][0] += aa[0][1]
			} else if mIdx == len(aa)-1 {
				aa[len(aa)-2][0] += aa[len(aa)-1][1]
			} else {
				aa[mIdx-1][0] += aa[mIdx][1]
				aa[mIdx+1][0] += aa[mIdx][1]
			}
			tot += aa[mIdx][0]
			aa = append(aa[:mIdx], aa[mIdx+1:]...)
		}
		fmt.Fprintln(out, tot+aa[0][0])
	}
}

package main

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

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T int
	fmt.Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n int
		fmt.Fscan(in, &n)

		aa := make([][2]int64, n)
		for i := range aa {
			fmt.Fscan(in, &aa[i][0])
		}
		for i := range aa {
			var v int64
			fmt.Fscan(in, &v)
			aa[i][1] = v
		}

		var tot int64
		for len(aa) > 1 {
			mn := aa[0][1]
			var mIdx int
			if aa[0][1] > aa[len(aa)-1][1] {
				mn = aa[len(aa)-1][1]
				mIdx = len(aa) - 1
			}

			for i := 1; i < len(aa)-1; i++ {
				if aa[i][1] < mn {
					mn = aa[i][1]
					mIdx = i
				}
			}

			// cut
			if mIdx == 0 {
				aa[1][0] += aa[0][1]
			} else if mIdx == len(aa)-1 {
				aa[len(aa)-2][0] += aa[len(aa)-1][1]
			} else {
				aa[mIdx-1][0] += aa[mIdx][1]
				aa[mIdx+1][0] += aa[mIdx][1]
			}
			tot += aa[mIdx][0]
			aa = append(aa[:mIdx], aa[mIdx+1:]...)
		}
		fmt.Fprintln(out, tot+aa[0][0])
	}
}

package main

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

var (
	t, n, m  int
	x, y     int64
	row, col []int64
	a        []node
)

type node struct {
	x, y int64
	f    bool
}

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n)
		a = make([]node, n)
		for i := 0; i < n; i++ {
			if i == 0 || i == n-1 {
				a[i].f = true
			} else {
				a[i].f = false
			}
			scan(&a[i].x)
		}
		for i := 0; i < n; i++ {
			scan(&a[i].y)
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i].y <= a[j].y
		})
		var ans int64 = 0
		var dt int64 = 0
		for i := 0; i < n; i++ {
			ans += a[i].x
			if i != n-1 {
				if a[i].f {
					dt += a[i].y
				} else {
					dt += a[i].y * 2
				}
			}
		}
		println(ans + dt)
	}
}

//

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(r, a...)
}

func println(a ...any) {
	fmt.Fprintln(w, a...)
}

func flush() {
	w.Flush()
}

package main

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

var (
	t, n, m, x, y int
	row, col      []int
	a             []node
)

type node struct {
	x, y int
	f    bool
}

func main() {
	defer flush()
	scan(&t)
	for ; t != 0; t-- {
		scan(&n)
		a = make([]node, n)
		for i := 0; i < n; i++ {
			if i == 0 || i == n-1 {
				a[i].f = true
			} else {
				a[i].f = false
			}
			scan(&a[i].x)
		}
		for i := 0; i < n; i++ {
			scan(&a[i].y)
		}
		sort.Slice(a, func(i, j int) bool {
			return a[i].y <= a[j].y
		})
		ans := 0
		dt := 0
		for i := 0; i < n; i++ {
			ans += a[i].x
			if i != n-1 {
				if a[i].f {
					dt += a[i].y
				} else {
					dt += a[i].y * 2
				}
			}
		}
		println(ans + dt)
	}
}

var (
	r *bufio.Reader
	w *bufio.Writer
)

func init() {
	r = bufio.NewReader(os.Stdin)
	w = bufio.NewWriter(os.Stdout)
}

func scan(a ...any) {
	fmt.Fscan(r, a...)
}

func println(a ...any) {
	fmt.Fprintln(w, a...)
}

func flush() {
	w.Flush()
}

package main

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

var in, out = bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)

// source: https://codeforces.com/contest/1749/problem/B?locale=en
func solve() {
	n := _readInt()
	_, a := _readArr(n)
	_, b := _readArr(n)
	res := 0

	cast := func(x int) {
		d := b[x]
		if x > 0 {
			a[x-1] += d
		}
		if x < len(a)-1 {
			a[x+1] += d
		}
		a = append(a[:x], a[x+1:]...)
		b = append(b[:x], b[x+1:]...)
	}

	next := func() int {
		mi, mv := 0, b[0]
		for i := 1; i < len(b)-1; i++ {
			if b[i]*2 < mv {
				mi = i
				mv = b[i] * 2
			}
		}
		if i := len(b) - 1; b[i] < mv {
			mi = i
		}
		return mi
	}

	for len(a) > 0 {
		i := next()
		res += a[i]
		cast(i)
	}

	_print(res)
}

func main() {
	defer out.Flush()
	var t int
	for fmt.Fscanln(in, &t); t > 0; t-- {
		solve()
	}
}

// ====
func _readInt() int {
	var x int
	fmt.Fscan(in, &x)
	return x
}
func _readStr() string {
	var x string
	fmt.Fscan(in, &x)
	return x
}
func _readArr(x ...int) (int, []int) {
	var n int
	if len(x) == 0 {
		fmt.Fscan(in, &n)
	} else {
		n = x[0]
	}
	var v = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &v[i])
	}
	return n, v
}
func _print(x ...any) {
	fmt.Fprintln(out, x...)
}
func _max(x, y int) int {
	if x > y {
		return x
	}
	return y
}
func _min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func _abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func _sortStr(x string) string {
	r := []rune(x)
	sort.Slice(r, func(i, j int) bool {
		return r[i] < r[j]
	})
	return string(r)
}
func _reverseInts(x []int) {
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
}
func _reverseStr(s string) string {
	x := []rune(s)
	n := len(x)
	for i := 0; i < n/2; i++ {
		x[i], x[n-1-i] = x[n-1-i], x[i]
	}
	return string(x)
}
func _makeMatrix(n, m int) [][]int {
	x := make([][]int, n)
	for i := range x {
		x[i] = make([]int, m)
	}
	return x
}


// http://codeforces.com/problemset/problem/136/A
// A. Presents
// permutacio inverze

package main

import (
	"fmt"
)

func main() {
	var (
		x        []int32
		n, i, xi int32
	)

	fmt.Scan(&n)
	x = make([]int32, n+1, n+1)

	for i = 1; i <= n; i++ {
		fmt.Scan(&xi)
		x[xi] = i
	}

	for i = 1; ; i++ {
		fmt.Print(x[i])
		if i < n {
			fmt.Print(" ")
		} else {
			fmt.Println()
			break
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var q, n int

	fmt.Scanln(&q)
	m := make(map[int]int)

	for i := 0; i < q; i++ {
		fmt.Scanf("%d", &n)
		m[n] = i + 1
	}

	for i := 0; i < q; i++ {
		fmt.Printf("%d ", m[i+1])
	}

}

package main

import "fmt"

func main() {
	var n, a int
	fmt.Scanf("%d\n", &n)
	out := [100]int{}
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a)
		out[a-1] = i + 1
	}
	for i := 0; i < n; i++ {
		fmt.Printf("%d ", out[i])
	}
}

package main

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

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() {
	var number, i uint8
	_, _ = fmt.Scanln(&number)
	number++
	var oned = make([]uint8, number, number+3)
	for i = 1; i < number; i++ {
		var p uint8
		scanf("%d", &p)
		oned[p] = i
	}

	fmt.Print(oned[1])
	for i := 2; i < len(oned); i++ {
		fmt.Print(" ", oned[i])
	}
	fmt.Print("\n")

}

package main

import "fmt"

func main() {
	var number,i uint8
	_, _ = fmt.Scanln(&number)
	number++
	var oned = make([]uint8,number)
	for i=1; i<number; i++ {
		var p uint8
		_, _ = fmt.Scan(&p)
		oned[p] = i
	}
	fmt.Print(oned[1])
	for i:=2;i<len(oned);i++ {
		fmt.Print(" ",oned[i])
	}
	fmt.Print("\n")

}

// A. Presents
/*
Little Petya very much likes gifts. Recently he has received a new laptop as a
New Year gift from his mother. He immediately decided to give it to
somebody else as what can be more pleasant than giving somebody gifts.
And on this occasion he organized a New Year party at his place and invited
n his friends there.

If there's one thing Petya likes more that receiving gifts, that's watching
others giving gifts to somebody else. Thus, he safely hid the laptop until the
next New Year and made up his mind to watch his friends exchanging gifts
while he does not participate in the process. He numbered all his friends
with integers from 1 to n. Petya remembered that a friend number i gave a
gift to a friend number pi. He also remembered that each of his friends
received exactly one gift.

Now Petya wants to know for each friend i the number of a friend who has
given him a gift.

Input
The first line contains one integer n (1 ≤ n ≤ 100) — the quantity of friends
Petya invited to the party. The second line contains n space-separated
integers: the i-th number is pi — the number of a friend who gave a gift to
friend number i. It is guaranteed that each friend received exactly one gift.
It is possible that some friends do not share Petya's ideas of giving gifts
to somebody else. Those friends gave the gifts to themselves.

Output
Print n space-separated integers: the i-th number should equal the number of
the friend who gave a gift to friend number i.

Examples
Input
4
2 3 4 1

Output
4 1 2 3

Input
3
1 3 2

Output
1 3 2

Input
2
1 2

Output
1 2
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	n, _ := strconv.Atoi(strings.TrimSpace(input1))

	input2, _ := reader.ReadString('\n')
	arr := strings.Split(strings.TrimSpace(input2), " ")

	outArr := make([]int, n)
	for i := 0; i < n; i++ {
		j, _ := strconv.Atoi(arr[i])
		outArr[j-1] = i + 1
	}

	for _, v := range outArr {
		fmt.Print(v, " ")
	}
}

package main

import "fmt"

func ShowFriedsPresents() {
	var n int
	fmt.Scan(&n)

	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a[i])
	}

	presentsGivenTo := make([]int, len(a))
	for i := 0; i < len(a); i++ {
		presentsGivenTo[a[i]-1] = i + 1
	}
	for i := 0; i < len(a); i++ {
		fmt.Print(presentsGivenTo[i], " ")
	}
}

func main() {
	ShowFriedsPresents()
}

package main

import(
    "fmt"
)

func main(){
    var (
        n   int
        m = make(map[int]int)
    )
    fmt.Scan(&n)
    arr := make([]int,n+1)
    for i:=1;i<=n;i++{
        fmt.Scan(&arr[i])
        m[arr[i]]=i
    }
    for i:=1;i<=n;i++{
        arr[i]=m[i]
        fmt.Print(arr[i]," ")
    }
    fmt.Println()
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var a , b[10000] int
	var i int
	for i = 1 ; i <= n ; i++ {
	    fmt.Scan(&a[i])
	}
	for i = 1 ; i <= n ; i++ {
	    b[a[i] - 1] = i
	}
	for i = 0 ; i < n ; i++ {
	    fmt.Print(b[i])
	    fmt.Print(" ")
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	p := make([]int, n)
	for i := range p {
		var a int
		fmt.Scan(&a)
		p[a-1] = i + 1
	}

	for i, v := range p {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(v)
	}
	fmt.Println()
}

package main

import "fmt"

func main() {
    var n,t int
    var a [105]int
    fmt.Scan(&n)
    for i := 1; i <= n; i++ {
        fmt.Scan(&t)
        a[t] = i
    }
    for i := 1; i <= n; i++ {
        fmt.Print(a[i])
        if i == n { fmt.Println() } else { fmt.Print(" ") }
    }
}

package main

import (
	"fmt"
	"strings"
)

func sliceIntToStr(int_sl []int) []string {
	str_sl := make([]string, len(int_sl))
	for i, n := range int_sl {
		str_sl[i] = fmt.Sprint(n)
	}
	return str_sl
}

func main() {
	var n int
	fmt.Scan(&n)

	var nums []int
	for ; n > 0; n-- {
		var tmp int
		fmt.Scan(&tmp)
		nums = append(nums, tmp)
	}
	reverse_nums := make([]int, len(nums))
	for i, v := range nums {
		reverse_nums[v-1] = i + 1
	}
	fmt.Println(strings.Join(sliceIntToStr(reverse_nums), " "))
}

package main

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

var scanner *bufio.Scanner

func getI() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n := int(getI())
  inverse := map[int]int{}
  for i := 0; i < n; i++ {
    p := int(getI())
    inverse[p] = i+1
  }
  writer.WriteString(fmt.Sprintf("%d", inverse[1]))
  for i := 2; i <= n; i++ {
    writer.WriteString(fmt.Sprintf(" %d", inverse[i]))
  }
  writer.WriteString("\n")
}

package main

import (
	"fmt"
)

func main() {

	n := 0

	var a [120]int

	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		x := 0
		fmt.Scan(&x)
		a[x-1] = i + 1
	}

	for i := 0; i < n; i++ {
		fmt.Print(a[i], " ")
	}

}

package main

import (
  "fmt"
)

func main() {
  var n, p int
  fmt.Scan(&n)
  arr := make([]int, n)
  for i := 0; i < n; i++ {
    fmt.Scan(&p)
    arr[p-1] = i+1
  }
  for idx,val := range arr {
    fmt.Print(val)
    if idx != len(arr)-1 {
      fmt.Print(" ")
    }
  }
}

package main

import "fmt"

func main() {
	numberOfFriends := 0
	friendsGifts := make(map[int]int)
	fmt.Scanf("%d\n", &numberOfFriends)

	for i := 0; i < numberOfFriends; i++ {
		ithFriend := 0
		fmt.Scanf("%d", &ithFriend)
		friendsGifts[ithFriend] = i + 1
	}

	for i := 0; i < numberOfFriends; i++ {
		if i == numberOfFriends-1 {
			fmt.Println(friendsGifts[i+1])
		} else {
			fmt.Print(friendsGifts[i+1], " ")
		}
	}
}

// 2 3 4 1
// 1 2 3 4

// 2->1
// 3->2
// 4->3
// 1->4

// 1 2 3 4

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	p := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&p[i])
	}

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

	for i := 0; i < n; i++ {
		fmt.Print(m[i], " ")
	}

}

package main

import(
	"fmt"
)

func main() {
	var n int
	var send, receive [105]int
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		fmt.Scan(&send[i])
		receive[send[i]] = i
	}
	for i := 1; i <= n; i++ {
		if i > 1 {
			fmt.Print(" ")
		}
		fmt.Print(receive[i])
	}
}

// http://codeforces.com/problemset/problem/136/A
// A. Presents
// permutacio inverze

package main

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

func main() {
	var (
		x        []int32
		n, i, xi int32
		in       *bufio.Reader
		out      *bufio.Writer
	)

	in = bufio.NewReader(os.Stdin)
	out = bufio.NewWriter(os.Stdout)

	fmt.Fscan(in, &n)
	x = make([]int32, n+1, n+1)

	for i = 1; i <= n; i++ {
		fmt.Fscan(in, &xi)
		x[xi] = i
	}

	for i = 1; ; i++ {
		fmt.Fprint(out, x[i])
		if i < n {
			fmt.Fprint(out, " ")
		} else {
			fmt.Fprintln(out)
			break
		}
	}
	out.Flush()
}

// http://codeforces.com/problemset/problem/136/A
// A. Presents
// permutacio inverze

package main

import (
	"fmt"
)

func main() {
	var (
		x        []int32
		n, i, xi int32
	)

	fmt.Scan(&n)
	x = make([]int32, n+1, n+1)

	for i = 1; i <= n; i++ {
		fmt.Scan(&xi)
		x[xi] = i
	}

	fmt.Print(x[1])

	for i = 2; i <= n; i++ {
		fmt.Printf(" %d", x[i])
	}
	fmt.Println()

}

// http://codeforces.com/problemset/problem/136/A
// A. Presents
// permutacio inverze

package main

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

func main() {
	var (
		x        []int32
		n, i, xi int32
		bin      *bufio.Reader
		bout     *bufio.Writer
	)

	bin = bufio.NewReader(os.Stdin)
	bout = bufio.NewWriter(os.Stdout)

	fmt.Fscan(bin, &n)
	x = make([]int32, n+1, n+1)

	for i = 1; i <= n; i++ {
		fmt.Fscan(bin, &xi)
		x[xi] = i
	}

	for i = 1; ; i++ {
		fmt.Fprint(bout, x[i])
		if i < n {
			fmt.Fprint(bout, " ")
		} else {
			fmt.Fprintln(bout)
			break
		}
	}
	bout.Flush()
}

package main

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

func main() {
	var scanner *bufio.Scanner
	var lines []string
	var n int

	scanner = bufio.NewScanner(os.Stdin)

	for scanner.Scan() {
		lines = append(lines, scanner.Text())
	}

	n, _ = strconv.Atoi(lines[0])
	var gifts = make([]string, n)

	for i, gift := range strings.Fields(lines[1]) {
		index, _ := strconv.Atoi(gift)
		gifts[index-1] = strconv.Itoa(i + 1)
	}

	fmt.Println(strings.Join(gifts, " "))
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	// presentsMap := make(map[int]int, n)
	presents := make([]int, n)
	for i := 0; i < n; i++ {
		var p int
		fmt.Scan(&p)
		presents[p-1] = i
	}
	for _, v := range presents {
		fmt.Print(v + 1)
		fmt.Print(" ")
	}
	fmt.Println()

}

package main
import (
	"fmt"
	"strings"
	"strconv"
)
func main() {
	var n int
	fmt.Scan(&n)
	var arr = make([]int, n)
	var arr2 = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
		arr2[arr[i] - 1] = i + 1
	}
	var str strings.Builder
	for i := 0; i < n; i++{
		str.WriteString(strconv.Itoa(arr2[i]))
		if i < n - 1 {
			str.WriteString(" ")
		}
	}
	fmt.Println(str.String())
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	// Skip first line.
	scanner.Scan()
	scanner.Scan()
	a := strings.Split(scanner.Text(), " ")
	b := make([]int, len(a))
	for i, v := range a {
		val, _ := strconv.Atoi(v)
		b[val-1] = i + 1
	}
	for i, v := range b {
		a[i] = strconv.Itoa(v)
	}
	fmt.Println(strings.Join(a, " "))
}

package main

import "fmt"

func main() {
	var peopleNumber int
	fmt.Scan(&peopleNumber)
	tos := make([]int, peopleNumber)
	for offset := 0; offset < peopleNumber; offset++ {
		var to int
		fmt.Scan(&to)
		tos[to - 1] = offset + 1
	}
	for offset := 0; offset < peopleNumber; offset++ {
		fmt.Print(tos[offset])
		if offset != peopleNumber - 1 {
			fmt.Print(" ")
		}
	}
}

package main
import ("fmt")
func main () {
    var n,t int
    fmt.Scan(&n)
    a := make([]int,n)
    for i:=1;i<=n;i++ {
        fmt.Scan(&t)
        a[t-1]=i
    }
    for i:=0;i<n;i++ {
        fmt.Print(a[i])
        fmt.Print(" ")
    }
}

package main

import "fmt"

func main() {
	var n, j int

	fmt.Scanln(&n)

	m := make(map[int]int)

	for i := 0; i < n ; i++ {
		fmt.Scanf("%d", &j)
		m[j] = i + 1
	}

	for i:=0; i < n; i++{
		fmt.Printf("%d ", m[i+1])
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	var to int
	giftFroms := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&to)
		giftFroms[to] = i
	}

	for i := 1; i < len(giftFroms); i++ {
		if i != 1 {
			fmt.Print(" ")
		}
		fmt.Print(giftFroms[i])
	}
}

package main

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

var n int

func main() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader := bufio.NewReaderSize(stdin, 1<<20)
	fmt.Fscanf(reader, "%d\n", &n)

	m := make(map[int]int)
	for i := 0; i < n; i++ {
		var x int
		fmt.Fscanf(reader, "%d", &x)
		m[x] = i + 1
	}
	for i := 0; i < n; i++ {
		fmt.Print(m[i+1], " ")
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n, a uint8
	fmt.Fscanf(in, "%d\n", &n)
	d := make([]int, n)
	for i := 0; uint8(i) < n; i++ {
		fmt.Fscanf(in, "%d", &a)
		d[a-1] = i + 1
	}
	for i := 0; uint8(i) < n; i++ {
		fmt.Print(d[i], " ")
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, i int
	fmt.Scan(&n)
	var arr, r [100]int
	for i = 0; i < n; i++ {
		fmt.Scan(&arr[i])
		r[arr[i]-1] = i + 1
	}
	for i = 0; i < n; i++ {
		fmt.Print(r[i])
		fmt.Print(" ")
	}
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	a := make([]int, n+1)
	b := make([]int, n+1)
	for i := 1; i <= n; i++ {
		fmt.Scan(&a[i])
		b[a[i]] = i
	}
	for i := 1; i <= n; i++ {
		fmt.Print(b[i])
		fmt.Print(" ")
	}
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, x int
	fmt.Scan(&n)
	p := make([]int, n+1)

	for i := 1; i <= n; i++ {
		fmt.Scan(&x)
		p[x] = i
	}
	fmt.Print(strings.Trim(fmt.Sprint(p[1:]), "[]"))
}

package main

import (
	"fmt"
)

func main() {
	var n, x int
	fmt.Scan(&n)
	p := make([]int, n+1)

	for i := 1; i <= n; i++ {
		fmt.Scan(&x)
		p[x] = i
	}

	for j := 1; j < n; j++ {
		fmt.Printf("%d ", p[j])
	}
	fmt.Printf("%d", p[n])
}

package main

import "fmt"

func main() {
	var n int
	arr := make(map[int]int)
	fmt.Scan(&n)
	for i := 1; i <= n; i++ {
		var x int
		fmt.Scan(&x)
		arr[x] = i
	}

	for i := 1; i <= n; i++ {
		fmt.Printf("%d ", arr[i])
	}
}

package main

import "fmt"

const c_max = 128

func main() {
    var n, f int
    var result[c_max] int
    fmt.Scanf("%d\n", &n)
    for i := 1; i < n; i++ {
        fmt.Scanf("%d", &f)
        result[f] = i
    }
    fmt.Scanf("%d\n", &f)
    result[f] = n

    for i := 1; i <= n; i++ {
        fmt.Printf("%d ", result[i])
    }
    fmt.Print("\n")
}

//  URL: http://codeforces.com/problemset/problem/136/A
//  ~ Basic Implementation Problem

package main

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

func solve(n int, arr []int) [] int {
	ans := make([]int, n)
	for i, val := range arr {
		ans[val - 1] = i + 1
	}
	return ans
}

func main() {
	reader, writer := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	arr := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &arr[i])
	}
	ans := solve(n, arr)
	for i := 0; i < n; i++ {
		fmt.Fprintf(writer, "%d ", ans[i])
	}
	fmt.Fprintf(writer, "\n")
}

package main

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

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

func scanf(f string, a... interface{}) {
	fmt.Fscanf(reader, f, a...)
}

func printf(f string, a... interface{}) {
	fmt.Fprintf(writer, f, a...)
}

func main() {

	var T, i uint

	scanf("%d \n", &T)
	var temp uint
	m := map[uint]uint{}
	for i=1; i<=T; i++ {
		scanf("%d ", &temp)
		m[temp] = i
	}

	for i=1; i<=T; i++ {
		if i == T {
			fmt.Printf("%d\n", m[i])
			break
		}
		fmt.Printf("%d ", m[i])
	}

}

package main
import (
    "fmt"
)
func main() {
	var temp,n int
	fmt.Scan(&n)
	a := make([]int, n, n)
    for i := 0; i < n; i++ {
	    fmt.Scan(&temp)
	    a[temp-1]=i+1
	}
    for i := 0; i < n; i++ {
	    fmt.Print(a[i])
	    fmt.Print(" ")
	}
}

package main

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

var scanner *bufio.Scanner
func read() string {
    if !scanner.Scan() {
		panic("Scan returned false")
	}
	return scanner.Text()
}
func readInt() int {
	num, _ := strconv.Atoi(read())
	return num
}
func main() {
    scanner = bufio.NewScanner(os.Stdin)
    scanner.Split(bufio.ScanWords)
    n := readInt()

    gfts := make([]int, n)
    for i := 0 ; i < n; i++ {
        curr := readInt()

        gfts[curr-1] = i+1
    }

    for _, i := range gfts {
        fmt.Print(i, " ")
    }
}

package main

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

func main() {
	var n, currentVal int
	reader := bufio.NewReader(os.Stdin)
	fmt.Fscan(reader, &n)
	arrayOfPs := make([]int, n)
	for index := 0; index < n; index++ {
		fmt.Fscan(reader, &currentVal)
		currentVal--
		arrayOfPs[currentVal] = index + 1
	}
	for index := 0; index < len(arrayOfPs); index++ {
		fmt.Print(arrayOfPs[index], " ")
	}
}

package main

import "fmt"

func main() {
	var testcount, a int
	var arr []int
	fmt.Scanf("%d\n", &testcount)
	for i := 0; i < testcount; i++ {
		fmt.Scanf("%d", &a)
		arr = append(arr, a)
	}
	for i := 1; i <= testcount; i++ {
		for j := 0; j < testcount; j++ {
			if arr[j] == i {
				fmt.Printf("%d ", j+1)
				break
			}
		}
	}
}

package main

import ("fmt")

func main(){
	var n int
	scan(&n)
	var a []int = make([]int, n)
	for i := 1; i <= n; i++{
		var t int
		scan(&t)
		a[t - 1] = i
	}
	for _,k := range a{
		sout(k)
	}
}

func scan(in ... interface{}){
	for _,k := range in{
		fmt.Scan(k)
	}
}

func sout(obj ... interface{}){
	for _, k := range obj{
		fmt.Print(k, " ")
	}
	fmt.Println()
}

package main

import "fmt"

func main() {
	var n, x int
	fmt.Scan(&n)
	aa := make([]int, n)
	for i := 1; i <= n; i++ {
		fmt.Scan(&x)
		aa[x-1] = i
	}
	for _, a := range aa {
		fmt.Print(a, " ")
	}
}

package main

import "fmt"

// https://codeforces.com/problemset/problem/136/A
// A. Presents
// time limit per test
// 2 seconds
// memory limit per test
// 256 megabytes
// input
// standard input
// output
// standard output

// Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited n his friends there.

// If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to n. Petya remembered that a friend number i gave a gift to a friend number pi. He also remembered that each of his friends received exactly one gift.

// Now Petya wants to know for each friend i the number of a friend who has given him a gift.
// Input

// The first line contains one integer n (1 ≤ n ≤ 100) — the quantity of friends Petya invited to the party. The second line contains n space-separated integers: the i-th number is pi — the number of a friend who gave a gift to friend number i. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves.
// Output

// Print n space-separated integers: the i-th number should equal the number of the friend who gave a gift to friend number i.

func main() {
	var n int
	fmt.Scan(&n)

	giftMapping := make([]int, n)

	for i := 0; i < n; i++ {
		var pi int
		fmt.Scan(&pi)
		giftMapping[pi-1] = i + 1
	}

	for _, v := range giftMapping {
		fmt.Print(v, " ")
	}
}

package main

// https://codeforces.com/problemset/problem/136/A

import "fmt"

func taskSolution(a []int) string {
	res := make([]int, len(a))
	for i := 0; i < len(a); i += 1 {
		res[a[i]-1] = i + 1
	}
	output := fmt.Sprint(res)
	return output[1 : len(output)-1]
}

func main() {
	var n int
	if _, err := fmt.Scan(&n); err != nil {
		panic(err)
	}

	a := []int{}
	for i := 0; i < n; i += 1 {
		var ai int
		if _, err := fmt.Scan(&ai); err != nil {
			panic(err)
		}
		a = append(a, ai)
	}

	fmt.Println(taskSolution(a))
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	Scanf(format string, a ...interface{})
	Int() int
	Int64() int64
	String() string
	IntStream(size int) <-chan int
	IntSlice(size int) []int
	Int64Slice(size int) []int64
	StringSlice(size int) []string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Reader
}

func NewReader() Reader {
	return &readerImpl{Reader: bufio.NewReader(os.Stdin)}
}

func (impl *readerImpl) Scanf(f string, a ...interface{}) {
	fmt.Fscanf(impl.Reader, f, a...)
}

func (impl *readerImpl) Int() int {
	var ret int
	impl.Scanf("%d\n", &ret)
	return ret
}

func (impl *readerImpl) Int64() int64 {
	var ret int64
	impl.Scanf("%d\n", &ret)
	return ret
}

func (impl *readerImpl) String() string {
	var ret string
	impl.Scanf("%s\n", &ret)
	return ret
}

func (impl *readerImpl) IntStream(size int) <-chan int {
	c := make(chan int)
	go func() {
		for i := 0; i < size-1; i++ {
			var v int
			impl.Scanf("%d ", &v)
			c <- v
		}
		var v int
		impl.Scanf("%d\n", &v)
		c <- v
	}()
	return c
}

func (impl *readerImpl) IntSlice(size int) []int {
	ret := make([]int, size)
	for i := 0; i < size-1; i++ {
		impl.Scanf("%d ", &ret[i])
	}
	impl.Scanf("%d\n", &ret[size-1])
	return ret
}

func (impl *readerImpl) Int64Slice(size int) []int64 {
	ret := make([]int64, size)
	for i := 0; i < size-1; i++ {
		impl.Scanf("%d ", &ret[i])
	}
	impl.Scanf("%d\n", &ret[size-1])
	return ret
}

func (impl *readerImpl) StringSlice(size int) []string {
	ret := make([]string, size)
	for i := 0; i < size-1; i++ {
		impl.Scanf("%s ", &ret[i])
	}
	impl.Scanf("%s\n", &ret[size-1])
	return ret
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

func main() {
	defer writer.Flush()
	n := reader.Int()
	c := reader.IntStream(n)
	res := make([]int, n)
	for i := 0; i < n; i++ {
		v := <-c
		res[v-1] = i + 1
	}
	for _, r := range res {
		writer.Printf("%d ", r)
	}
}

package main

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

var (
	reader Reader = NewReader()
	writer Writer = NewWriter()
)

type Reader interface {
	Scanf(format string, a ...interface{})
	Int() int
	Int64() int64
	String() string
	IntSlice(size int) []int
	Int64Slice(size int) []int64
	StringSlice(size int) []string
}

type Writer interface {
	Printf(format string, a ...interface{})
	Flush()
}

type readerImpl struct {
	*bufio.Reader
}

func NewReader() Reader {
	return &readerImpl{Reader: bufio.NewReader(os.Stdin)}
}

func (impl *readerImpl) Scanf(f string, a ...interface{}) {
	fmt.Fscanf(impl.Reader, f, a...)
}

func (impl *readerImpl) Int() int {
	var ret int
	impl.Scanf("%d\n", &ret)
	return ret
}

func (impl *readerImpl) Int64() int64 {
	var ret int64
	impl.Scanf("%d\n", &ret)
	return ret
}

func (impl *readerImpl) String() string {
	var ret string
	impl.Scanf("%s\n", &ret)
	return ret
}

func (impl *readerImpl) IntSlice(size int) []int {
	ret := make([]int, size)
	for i := 0; i < size-1; i++ {
		impl.Scanf("%d ", &ret[i])
	}
	impl.Scanf("%d\n", &ret[size-1])
	return ret
}

func (impl *readerImpl) Int64Slice(size int) []int64 {
	ret := make([]int64, size)
	for i := 0; i < size-1; i++ {
		impl.Scanf("%d ", &ret[i])
	}
	impl.Scanf("%d\n", &ret[size-1])
	return ret
}

func (impl *readerImpl) StringSlice(size int) []string {
	ret := make([]string, size)
	for i := 0; i < size-1; i++ {
		impl.Scanf("%s ", &ret[i])
	}
	impl.Scanf("%s\n", &ret[size-1])
	return ret
}

type writerImpl struct {
	*bufio.Writer
}

func NewWriter() Writer {
	return &writerImpl{Writer: bufio.NewWriter(os.Stdout)}
}

func (impl *writerImpl) Printf(f string, a ...interface{}) {
	fmt.Fprintf(impl.Writer, f, a...)
}

func (impl *writerImpl) Flush() {
	impl.Writer.Flush()
}

func main() {
	defer writer.Flush()
	n := reader.Int()
	pp := reader.IntSlice(n)
	res := make([]int, n)
	for k, v := range pp {
		res[v-1] = k + 1
	}
    for _, r := range res {
        writer.Printf("%d ", r)
    }
}

package main

import "fmt"

const c_max = 128

func main() {
    var n, f int
    var result[c_max] int
    fmt.Scan(&n)
    for i := 1; i < n; i++ {
        fmt.Scanf("%d", &f)
        result[f] = i
    }
    fmt.Scanf("%d\n", &f)
    result[f] = n

    for i := 1; i <= n; i++ {
        fmt.Printf("%d ", result[i])
    }
    fmt.Print("\n")
}

package main

import "fmt"

const c_max = 128

func main() {
    var n, f int
    var result[c_max] int
    fmt.Scan(&n)
    for i := 1; i <= n; i++ {
        fmt.Scanf("%d", &f)
        result[f] = i
    }

    for i := 1; i <= n; i++ {
        fmt.Printf("%d ", result[i])
    }
    fmt.Print("\n")
}

package main

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

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() {
	var number, i uint8
	_, _ = fmt.Scanln(&number)
	number++
	var oned = make([]uint8, number)
	for i = 1; i < number; i++ {
		var p uint8
		scanf("%d ", &p)
		oned[p] = i
	}
	fmt.Print(oned[1])
	for i := 2; i < len(oned); i++ {
		printf(" %d", oned[i])
	}
	fmt.Print("\n")

}

package main

import (
	"fmt"
)

func main() {

	n := 0

	var a []int

	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		x := 0
		fmt.Scan(&x)

		a = append(a, x)
	}
	for i := 0; i < n; i++ {
		fmt.Print(a[a[a[i]-1]-1], " ")
	}

}

package main
import (
	"fmt"
)
func main() {
	var n int
	fmt.Scan(&n)
	var arr = make([]int, n)
	var arr2 = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&arr[i])
		arr2[arr[i] - 1] = i + 1
	}
	fmt.Printf("%v", arr2[0])
	for i := 1; i < n; i++{
		fmt.Printf("%2v", arr2[i])
	}
}