Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.You have an array a of length 2n and m queries on it. The i-th query is described by an integer qi. In order to perform the i-th query you must:  split the array into 2n - qi parts, where each part is a subarray consisting of 2qi numbers; the j-th subarray (1 ≤ j ≤ 2n - qi) should contain the elements a[(j - 1)·2qi + 1], a[(j - 1)·2qi + 2], ..., a[(j - 1)·2qi + 2qi];  reverse each of the subarrays;  join them into a single array in the same order (this array becomes new array a);  output the number of inversions in the new a. Given initial array a and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries.

The first line of input contains a single integer n (0 ≤ n ≤ 20).  The second line of input contains 2n space-separated integers a[1], a[2], ..., a[2n] (1 ≤ a[i] ≤ 109), the initial array. The third line of input contains a single integer m (1 ≤ m ≤ 106).  The fourth line of input contains m space-separated integers q1, q2, ..., qm (0 ≤ qi ≤ n), the queries. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output m lines. In the i-th line print the answer (the number of inversions) for the i-th query.

NoteIf we reverse an array x[1], x[2], ..., x[n] it becomes new array y[1], y[2], ..., y[n], where y[i] = x[n - i + 1] for each i.The number of inversions of an array x[1], x[2], ..., x[n] is the number of pairs of indices i, j such that: i < j and x[i] > x[j].

package main

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

const N = 20

var (
	a   []int64 = make([]int64, (1<<N)+1)
	aux []int64 = make([]int64, (1<<N)+1)
	sum [N + 1][2]int64
)

func Reverse(a []int64, low int64, high int64) {
	for i := low; i <= (low+high)/2; i++ {
		a[i], a[high-i+low] = a[high-i+low], a[i]
	}
}

func LowerBound(a []int64, low, high int64, value int64) int64 {
	l, h := low, high
	pos := low - 1
	for l <= h {
		mid := (l + h) >> 1
		if value > a[mid] {
			l = mid + 1
			pos = mid
		} else {
			h = mid - 1
		}
	}
	return pos
}

func Merge(aux []int64, a []int64, low, mid, high int64) {
	for i, j, k := low, mid+1, low; k <= high; k++ {
		if j > high || i <= mid && aux[i] <= aux[j] {
			a[k] = aux[i]
			i++
		} else {
			a[k] = aux[j]
			j++
		}
	}
}
func Inversions(aux []int64, a []int64, low int64, high int64, deep int64) {
	if low >= high {
		return
	}
	mid := (low + high) >> 1
	Inversions(a, aux, low, mid, deep-1)
	Inversions(a, aux, mid+1, high, deep-1)
	count := int64(0)
	for i, j := low, mid; i <= mid; i++ {
		j = LowerBound(aux, j+1, high, aux[i])
		count = count + j - mid
	}
	sum[deep][0] = sum[deep][0] + count
	count = 0
	for i, j := mid+1, low-1; i <= high; i++ {
		j = LowerBound(aux, j+1, mid, aux[i])
		count = count + j - low + 1
	}
	sum[deep][1] = sum[deep][1] + count
	if aux[mid] <= aux[mid+1] {
		copy(a[low:high+1], aux[low:high+1])
		return
	}
	Merge(aux, a, low, mid, high)
}

func main() {
	var n, m, que int64
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	for {
		_, err := fmt.Fscan(r, &n)
		if err == io.EOF {
			break
		}
		for i := 0; i < len(sum); i++ {
			sum[i][0] = int64(0)
			sum[i][1] = int64(0)
		}
		length := int64(math.Pow(2, float64(n)))
		a[0] = length
		for i := int64(1); i <= length; i++ {
			fmt.Fscan(r, &a[i])
		}
		copy(aux, a)
		Inversions(aux, a, 1, a[0], n)
		fmt.Fscan(r, &m)
		for i := int64(1); i <= m; i++ {
			fmt.Fscan(r, &que)
			ans := int64(0)
			for i := que; i >= 1; i-- {
				sum[i][0], sum[i][1] = sum[i][1], sum[i][0]
			}
			for i := int64(0); i <= n; i++ {
				ans = ans + sum[i][0]
			}
			fmt.Fprintln(w, ans)
		}
	}
	w.Flush()
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func main() {
	inS := bufio.NewReader(os.Stdin)
	ouS := bufio.NewWriter(os.Stdout)
	defer ouS.Flush()
    var n uint
    fmt.Fscanln(inS, &n)
//    n = getUint(inS)
    for i := uint(0); i < 1<<n; i++ {
//    	fmt.Fscan(inS, &a[i])
//    	a[i] = getUint(inS)
    }
    s, _ := inS.ReadString('\n')
    s = strings.TrimSpace(s)
    u := strings.Split(s, " ")
    for i, v := range u {
        d, _ := strconv.ParseInt(v, 10, 32)
        a[i] = uint(d)
    }
    var m uint
    fmt.Fscanln(inS, &m)
//    m = getUint(inS)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
//	    fmt.Fscan(inS, &q[i])
//    	q[i] = getUint(inS)
    }
    s, _ = inS.ReadString('\n')
    s = strings.TrimSpace(s)
    u = strings.Split(s, " ")
    for i, v := range u {
        d, _ := strconv.ParseInt(v, 10, 32)
        q[i] = uint(d)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += int64(v[0])
        }
        ouS.WriteString(strconv.FormatInt(r, 10))
        ouS.WriteByte('\n')
    }
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func main() {
	inS := bufio.NewReader(os.Stdin)
	ouS := bufio.NewWriter(os.Stdout)
	defer ouS.Flush()
    var n uint
    fmt.Fscanf(inS, "%d\n", &n)
//    n = getUint(inS)
    for i := uint(0); i < 1<<n; i++ {
    	fmt.Fscanf(inS, "%d", &a[i])
//    	a[i] = getUint(inS)
    }
    fmt.Fscanf(inS, "\n")
    var m uint
    fmt.Fscanf(inS, "%d\n", &m)
//    m = getUint(inS)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
	    fmt.Fscanf(inS, "%d", &q[i])
//    	q[i] = getUint(inS)
    }
    fmt.Fscanf(inS, "\n")
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += int64(v[0])
        }
        ouS.WriteString(strconv.FormatInt(r, 10))
        ouS.WriteByte('\n')
    }
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}
func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}
func main() {
	inp := bufio.NewReader(os.Stdin)
    var n uint
    n = getUint(inp)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getUint(inp)
    }

    var m uint
    m = getUint(inp)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getUint(inp)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

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

const MN = 21
const INF = 1e9+100
var a, ab [1<<MN]int
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 int
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 int
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

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

    var n uint
    fmt.Fscanln(inp, &n)

    s, _ := inp.ReadString('\n')
    s = strings.TrimSpace(s)

    u := strings.Split(s, " ")
    for i, v := range u {
        a[i], _ = strconv.Atoi(v)
    }

    var m uint
    fmt.Fscanln(inp, &m)

    q := make([]int, m)
    s, _ = inp.ReadString('\n')
    s = strings.TrimSpace(s)
    u = strings.Split(s, " ")
    for i, v := range u {
        q[i], _ = strconv.Atoi(v)
    }

    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := 1; j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}
func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func printUint64ln(b io.ByteWriter, d uint64) {
	var o [10]byte
	c := 0
	for d > 0 {
		o[c] = byte(d%10)+'0'
		d /= 10
		c++
	}
	if c == 0 {
		o[0] = '0'
		c = 1
	}
	for i := c-1; i >= 0; i-- {
		b.WriteByte(o[i])
	}
	b.WriteByte('\n')
	return
}
func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
    var n uint
    n = getUint(inp)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getUint(inp)
    }

    var m uint
    m = getUint(inp)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getUint(inp)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := uint64(0)
        for _, v := range num[:] {
            r += uint64(v[0])
        }
        fmt.Println(r)
        //printUint64ln(out, r)
    }
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func main() {
	inS := bufio.NewReader(os.Stdin)
	ouS := bufio.NewWriter(os.Stdout)
	defer ouS.Flush()
    var n uint
    fmt.Fscan(inS, &n)
//    n = getUint(inS)
    for i := uint(0); i < 1<<n; i++ {
    	fmt.Fscan(inS, &a[i])
//    	a[i] = getUint(inS)
    }

    var m uint
    fmt.Fscan(inS, &m)
//    m = getUint(inS)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
	    fmt.Fscan(inS, &q[i])
//    	q[i] = getUint(inS)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += int64(v[0])
        }
        ouS.WriteString(strconv.FormatInt(r, 10))
        ouS.WriteByte('\n')
    }
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}
func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func printUint64ln(b io.ByteWriter, d uint64) {
	var o [22]byte
	c := 0
	for d > 0 {
		o[c] = byte(d%10)+'0'
		d /= 10
		c++
	}
	if c == 0 {
		o[0] = '0'
		c = 1
	}
	for i := c-1; i >= 0; i-- {
		b.WriteByte(o[i])
	}
	b.WriteByte('\n')
	return
}
func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
    var n uint
    n = getUint(inp)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getUint(inp)
    }

    var m uint
    m = getUint(inp)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getUint(inp)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := uint64(0)
        for _, v := range num[:] {
            r += uint64(v[0])
        }
        //fmt.Println(r)
        printUint64ln(out, r)
    }
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func printUint64ln(b io.ByteWriter, d uint64) {
	var o [22]byte
	c := 0
	for d > 0 {
		o[c] = byte(d%10)+'0'
		d /= 10
		c++
	}
	if c == 0 {
		o[0] = '0'
		c = 1
	}
	for i := c-1; i >= 0; i-- {
		b.WriteByte(o[i])
	}
	b.WriteByte('\n')
	return
}
func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
    var n uint
    n = getUint(inp)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getUint(inp)
    }

    var m uint
    m = getUint(inp)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getUint(inp)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += int64(v[0])
        }
        out.WriteString(strconv.FormatInt(r, 10))
        out.WriteByte('\n')
    }
}

package main

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

const MN = 21
const INF = 1<<40
var a, ab [1<<MN]int64
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 int64
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 int64
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

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

    var n uint
    fmt.Fscanln(inp, &n)

    s, _ := inp.ReadString('\n')
    s = strings.TrimSpace(s)

    u := strings.Split(s, " ")
    for i, v := range u {
        a[i], _ = strconv.ParseInt(v, 10, 64)
    }

    var m uint
    fmt.Fscanln(inp, &m)

    q := make([]int, m)
    s, _ = inp.ReadString('\n')
    s = strings.TrimSpace(s)
    u = strings.Split(s, " ")
    for i, v := range u {
        q[i], _ = strconv.Atoi(v)
    }

    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := 1; j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

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

const N = 20

var (
	a   []int64 = make([]int64, (1<<N)+1)
	sum [N + 1][2]int64
)

func Reverse(a []int64, low int64, high int64) {
	for i := low; i <= (low+high)/2; i++ {
		a[i], a[high-i+low] = a[high-i+low], a[i]
	}
}

func LowerBound(a []int64, low, high int64, value int64) int64 {
	l, h := low, high
	pos := low - 1
	for l <= h {
		mid := (l + h) >> 1
		if value > a[mid] {
			l = mid + 1
			pos = mid
		} else {
			h = mid - 1
		}
	}
	return pos
}

func InplaceMerge(a []int64, low int64, mid int64, high int64) {
	i, j := low, mid+1
	for i < j && j <= high {
		step := int64(0)
		for ; i < j && a[i] <= a[j]; i++ {
		}
		for ; j <= high && a[j] <= a[i]; j++ {
			step++
		}
		Reverse(a, i, mid)
		Reverse(a, mid+1, j-1)
		Reverse(a, i, j-1)
		i = i + step
	}
}

func Inversions(a []int64, low int64, high int64, deep int64) {
	if low >= high {
		return
	}
	mid := (low + high) >> 1
	Inversions(a, low, mid, deep-1)
	Inversions(a, mid+1, high, deep-1)
	count := int64(0)
	for i, j := low, mid; i <= mid; i++ {
		j = LowerBound(a, j+1, high, a[i])
		count += j - mid
	}
	sum[deep][0] += count
	count = 0
	for i, j := mid+1, low-1; i <= high; i++ {
		j = LowerBound(a, j+1, mid, a[i])
		count += j - low + 1
	}
	sum[deep][1] += count
	InplaceMerge(a, low, mid, high)
}

func main() {
	var n, m, que int64
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	for {
		_, err := fmt.Fscan(r, &n)
		if err == io.EOF {
			break
		}
		for i := 0; i < len(sum); i++ {
			sum[i][0] = int64(0)
			sum[i][1] = int64(0)
		}
		length := int64(math.Pow(2, float64(n)))
		a[0] = length
		for i := int64(1); i <= length; i++ {
			fmt.Fscan(r, &a[i])
		}
		fmt.Fscan(r, &m)
		Inversions(a, 1, a[0], n)
		for i := int64(1); i <= m; i++ {
			fmt.Fscan(r, &que)
			ans := int64(0)
			for i := que; i >= 1; i-- {
				sum[i][0], sum[i][1] = sum[i][1], sum[i][0]
			}
			for i := int64(0); i <= n; i++ {
				ans += sum[i][0]
			}
			fmt.Fprintln(w, ans)
		}
	}
	w.Flush()
}

package main

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

var (
	a     []int64 = make([]int64, (1<<20)+1)
	state []int64 = make([]int64, (1<<20)+1)
	aux   []int64 = make([]int64, (1<<20)+1)
	que   []int64 = make([]int64, 1000001)
	sum   int64
)

func Reverse(a []int64, low int64, high int64) {
	for i := low; i <= (low+high)/2; i++ {
		a[i], a[high-i+low] = a[high-i+low], a[i]
	}
}

func LowerBound(a []int64, low, high int64, value int64) int64 {
	l, h := low, high
	pos := high + 1
	for l <= h {
		mid := (l + h) / 2
		if value > a[mid] {
			h = mid - 1
			pos = l
		} else {
			l = mid + 1
		}
	}
	return pos
}

func Merge(a []int64, aux []int64, low int64, mid int64, high int64) {
	for i, j, k := low, mid+1, low; k <= high; k++ {
		if j > high || i <= mid && aux[i] >= aux[j] {
			a[k] = aux[i]
			i++
		} else {
			a[k] = aux[j]
			j++
		}
	}
}

func Inversions(a []int64, aux []int64, low int64, high int64) {
	if low >= high {
		return
	}
	mid := (low + high) / 2
	Inversions(aux, a, low, mid)
	Inversions(aux, a, mid+1, high)
	count := int64(0)
	for i, j := mid, high; i >= low; i-- {
		j = LowerBound(a, mid+1, j, a[i]) - 1
		count += high - j
	}
	sum += count
	if aux[mid] >= aux[mid+1] {
		copy(a[low:high+1], aux[low:high+1])
	} else {
		Merge(a, aux, low, mid, high)
	}
}

func main() {
	var n, m int64
	r := bufio.NewReader(os.Stdin)
	for {
		_, err := fmt.Fscan(r, &n)
		if err == io.EOF {
			break
		}
		length := int64(math.Pow(2, float64(n)))
		a[0] = length
		for i := int64(1); i <= length; i++ {
			fmt.Fscan(r, &a[i])
		}
		fmt.Fscan(r, &m)
		for i := int64(1); i <= m; i++ {
			fmt.Fscan(r, &que[i])
		}
		for i := int64(1); i <= m; i++ {
			sum = int64(0)
			q := int64(math.Pow(2, float64(que[i])))
			for i := int64(1); i <= a[0]; i += q {
				Reverse(a, i, i+q-1)
			}
			copy(state[1:a[0]+1], a[1:a[0]+1])
			copy(aux[1:a[0]+1], a[1:a[0]+1])
			Inversions(a, aux, 1, a[0])
			copy(a[1:a[0]+1], state[1:a[0]+1])
			fmt.Println(sum)
		}
	}
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}
func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func printUint64ln(b io.ByteWriter, d uint64) {
	if d == 0 {
		b.WriteByte('0')
		b.WriteByte('\n')
		return
	}
	for d > 0 {
		b.WriteByte(byte(d%10)+'0')
		d /= 10
	}
	b.WriteByte('\n')
	return
}
func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
    var n uint
    n = getUint(inp)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getUint(inp)
    }

    var m uint
    m = getUint(inp)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getUint(inp)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := uint64(0)
        for _, v := range num[:] {
            r += uint64(v[0])
        }
        //fmt.Println(r)
        printUint64ln(out, r)
    }
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}
func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}
func main() {
	inp := bufio.NewReader(os.Stdin)
    var n uint
    n = getUint(inp)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getUint(inp)
    }

    var m uint
    m = getUint(inp)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getUint(inp)
    }
    if m > 600000 {
    	return
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

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

const MN = 21
const INF = 1<<40
var a, ab [1<<MN]int64
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 int64
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 int64
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

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

    var n uint
    fmt.Fscanln(inp, &n)

    s, _ := inp.ReadString('\n')
    s = strings.TrimSpace(s)

    u := strings.Split(s, " ")
    for i, v := range u {
        a[i], _ = strconv.ParseInt(v, 10, 64)
    }

    var m uint
    fmt.Fscanln(inp, &m)

    q := make([]int, m)
    s, _ = inp.ReadString('\n')
    s = strings.TrimSpace(s)
    u = strings.Split(s, " ")
    for i, v := range u {
        q[i], _ = strconv.Atoi(v)
    }
    if m > 60000 {
    	return
    }

    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := 1; j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

import (
	"fmt"
)

const MN = 21
const INF = 1<<29
var a, ab [1<<MN]int
var num [MN]int64
func merge(l, r, k int) {
	if r - l == 1 {
		return
	}
	md := (l+r)/2
	merge(l, md, k-1)
	merge(md, r, k-1)
	l1, l2 := l, md
	d := int64(0)
	for i := l; i < r; i++ {
		var d1, d2 int
		if (l1 < md) {
			d1 = a[l1]
		} else {
			d1 = INF
		}
		if (l2 < r) {
			d2 = a[l2]
		} else {
			d2 = INF
		}
		if (d1 < d2) {
			ab[i] = d1
			l1++
		} else {
			ab[i] = d2
			l2++
			d += int64(md - l1)
		}
	}
	copy(a[l:r], ab[l:r])
	num[k] += d
}

func main() {
	var n uint
	fmt.Scanln(&n)
	for i := 0; i < 1<<n; i++ {
		fmt.Scan(&a[i])
	}
	var m uint
	fmt.Scanln(&m)
	q := make([]uint, m)
	for i := uint(0); i < m; i++ {
		fmt.Scan(&q[i])
	}
	merge(0, 1<<n, int(n));
	for _, p := range q {
		for j := uint(1); j <= p; j++ {
			num[j] = 1<<(n-j) * 1<<((j-1)*2) - num[j]
		}
		r := int64(0)
		for _, v := range num[:] {
			r += v
		}
		fmt.Println(r)
	}
}

package main

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

const MN = 21
const INF = 1<<40
var a, ab [1<<MN]int64
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 int64
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 int64
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

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

    var n uint
    fmt.Fscanln(inp, &n)

    s, _ := inp.ReadString('\n')
    s = strings.TrimSpace(s)

    u := strings.Split(s, " ")
    for i, v := range u {
        a[i], _ = strconv.ParseInt(v, 10, 64)
    }

    var m uint
    fmt.Fscanln(inp, &m)

    q := make([]int, m)
    s, _ = inp.ReadString('\n')
    s = strings.TrimSpace(s)
    u = strings.Split(s, " ")
    for i, v := range u {
        q[i], _ = strconv.Atoi(v)
    }
    if n >= 18 {
    	return
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := 1; j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

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

const MN = 21
const INF = 1e9+100
var a, ab [1<<MN]int
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 int
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 int
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}

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

    var n uint
    fmt.Fscanln(inp, &n)

    s, _ := inp.ReadString('\n')
    s = strings.TrimSpace(s)

    u := strings.Split(s, " ")
    for i, v := range u {
        a[i], _ = strconv.Atoi(v)
    }

    var m uint
    fmt.Fscanln(inp, &m)

    q := make([]int, m)
    s, _ = inp.ReadString('\n')
    s = strings.TrimSpace(s)
    u = strings.Split(s, " ")
    for i, v := range u {
        q[i], _ = strconv.Atoi(v)
    }
    if n >= 18 {
    	return
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := 1; j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

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

const MN = 3
const INF = 1e9+100
var a, ab [1<<MN]int
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 int
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 int
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}
func getInt(b io.ByteReader) int {
	d := 0
	for {
		c, _ := b.ReadByte()
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += int(c)
		} else {
			return d
		}
	}
}
func main() {
	inp := bufio.NewReader(os.Stdin)
    var n uint
    fmt.Fscanln(inp, &n)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getInt(inp)
    }

    var m uint
    fmt.Fscanln(inp, &m)

    q := make([]int, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getInt(inp)
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := 1; j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := int64(0)
        for _, v := range num[:] {
            r += v[0]
        }
        fmt.Println(r)
    }
}

package main

import (
	"fmt"
)

const MN = 21
const INF = 1<<29
var a, ab [1<<MN]int
var num [MN]int64

func merge(l, r, k int) {
	if r - l == 1 {
		return
	}
	md := (l+r)/2
	merge(l, md, k-1)
	merge(md, r, k-1)
	l1, l2 := l, md
	d := int64(0)
	for i := l; i < r; i++ {
		var d1, d2 int
		if (l1 < md) {
			d1 = a[l1]
		} else {
			d1 = INF
		}
		if (l2 < r) {
			d2 = a[l2]
		} else {
			d2 = INF
		}
		if (d1 < d2) {
			ab[i] = d1
			l1++
		} else {
			ab[i] = d2
			l2++
			d += int64(md - l1)
		}
	}
	copy(a[l:r], ab[l:r])
	num[k] += d
}

func main() {
	var n uint
	fmt.Scan(&n)
	for i := 0; i < 1<<n; i++ {
		fmt.Scan(&a[i])
	}
	var m uint
	fmt.Scan(&m)
	q := make([]uint, m)
	for i := uint(0); i < m; i++ {
		fmt.Scan(&q[i])
	}
	merge(0, 1<<n, int(n));
	for _, p := range q {
		for j := uint(1); j <= p; j++ {
			num[j] = 1<<(n-j) * 1<<((j-1)*2) - num[j]
		}
		r := int64(0)
		for _, v := range num[:] {
			r += v
		}
		fmt.Println(r)
	}
}

package main

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

const MN = 20
const INF = 1e9+100
var a, ab [1<<MN]uint
var num [MN+1][2]int64

func merge(l, r, k int) {
    if r - l == 1 {
        return
    }
    md := (l+r)/2
    merge(l, md, k-1)
    merge(md, r, k-1)
    u1, u2 := int64(0), int64(0)
    l1, l2 := l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 <= d2) {
            ab[i] = d1
            l1++
        } else {
            ab[i] = d2
            l2++
            u1 += int64(md - l1)
        }
    }
    l1, l2 = l, md
    for i := l; i < r; i++ {
        var d1, d2 uint
        if (l1 < md) {
            d1 = a[l1]
        } else {
            d1 = INF
        }
        if (l2 < r) {
            d2 = a[l2]
        } else {
            d2 = INF
        }
        if (d1 < d2) {
            l1++
            u2 += int64(r - l2)
        } else {
            l2++
        }
    }
    copy(a[l:r], ab[l:r])
    num[k][0] += u1
    num[k][1] += u2

}
func getUint(b io.ByteReader) uint {
	d := uint(0)
	var c byte
	for {
		c, _ = b.ReadByte()
		if (c != ' ' && c != '\n'){
			break
		}
	}
	for {
		c -= '0'
		if 0 <= c && c <= 9 {
			d *= 10
			d += uint(c)
		} else {
			return d
		}
		c, _ = b.ReadByte()
	}
}

func printUint64ln(b io.ByteWriter, d uint64) {
	if d == 0 {
		b.WriteByte('0')
		b.WriteByte('\n')
		return
	}
	for d > 0 {
		b.WriteByte(byte(d%10)+'0')
		d /= 10
	}
	b.WriteByte('\n')
	return
}
func main() {
	inp := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
    var n uint
    n = getUint(inp)
    for i := uint(0); i < 1<<n; i++ {
    	a[i] = getUint(inp)
    }

    var m uint
    m = getUint(inp)
    q := make([]uint, m)
    for i := uint(0); i < m; i++ {
    	q[i] = getUint(inp)
    }
    if m > 600000 {
    	return
    }
    merge(0, 1<<n, int(n));
    for _, p := range q {
        for j := uint(1); j <= p; j++ {
            e := num[j][0]
            num[j][0] = num[j][1]
            num[j][1] = e
        }
        r := uint64(0)
        for _, v := range num[:] {
            r += uint64(v[0])
        }
        //fmt.Println(r)
        printUint64ln(out, r)
    }
}

package main

import (
	"fmt"
)

const MN = 21
const INF = 1<<40
var a, ab [1<<MN]int64
var num [MN+1][2]int64

func merge(l, r, k int) {
	if r - l == 1 {
		return
	}
	md := (l+r)/2
	merge(l, md, k-1)
	merge(md, r, k-1)
	u1, u2 := int64(0), int64(0)
	l1, l2 := l, md
	for i := l; i < r; i++ {
		var d1, d2 int64
		if (l1 < md) {
			d1 = a[l1]
		} else {
			d1 = INF
		}
		if (l2 < r) {
			d2 = a[l2]
		} else {
			d2 = INF
		}
		if (d1 <= d2) {
			ab[i] = d1
			l1++
		} else {
			ab[i] = d2
			l2++
			u1 += int64(md - l1)
		}
	}
	l1, l2 = l, md
	for i := l; i < r; i++ {
		var d1, d2 int64
		if (l1 < md) {
			d1 = a[l1]
		} else {
			d1 = INF
		}
		if (l2 < r) {
			d2 = a[l2]
		} else {
			d2 = INF
		}
		if (d1 < d2) {
			l1++
			u2 += int64(r - l2)
		} else {
			l2++
		}
	}
	copy(a[l:r], ab[l:r])
	num[k][0] += u1
	num[k][1] += u2

}

func main() {
	var n uint
	fmt.Scan(&n)
	for i := 0; i < 1<<n; i++ {
		fmt.Scan(&a[i])
	}
	var m uint
	fmt.Scan(&m)
	q := make([]uint, m)
	for i := uint(0); i < m; i++ {
		fmt.Scan(&q[i])
	}
	if m > 600000 {
		return
	}
	merge(0, 1<<n, int(n));
	for _, p := range q {
		for j := uint(1); j <= p; j++ {
			e := num[j][0]
			num[j][0] = num[j][1]
			num[j][1] = e
		}
		r := int64(0)
		for _, v := range num[:] {
			r += v[0]
		}
		fmt.Println(r)
	}
}


You are given $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$. Consider graph on $$$n$$$ nodes, in which nodes $$$i$$$, $$$j$$$ ($$$i\neq j$$$) are connected if and only if, $$$a_i$$$ AND $$$a_j\neq 0$$$, where AND denotes the bitwise AND operation.Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.

The first line contains one integer $$$n$$$ $$$(1 \le n \le 10^5)$$$ — number of numbers. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 10^{18}$$$).

If the graph doesn't have any cycles, output $$$-1$$$. Else output the length of the shortest cycle.

NoteIn the first example, the shortest cycle is $$$(9, 3, 6, 28)$$$.In the second example, the shortest cycle is $$$(5, 12, 9)$$$.The graph has no cycles in the third example.

package main

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

const Inf = int(1e9)

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

	var n int
	fmt.Fscanf(r, "%d\n", &n)

	a := make([]int64, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(r, "%d", &a[i])
	}

	fmt.Println(shortestCycle(a))
}

func isOn(n int64, b uint) bool {
	return (n & (1 << b)) != 0
}

func shortestCycle(a []int64) int {
	bIdx := make(map[uint][]int64)
	for _, e := range a {
		for b := uint(0); b < 64; b++ {
			if isOn(e, b) {
				bIdx[b] = append(bIdx[b], e)
			}
		}
	}

	g := make(map[int64][]int64)
	for _, ar := range bIdx {
		if len(ar) >= 3 {
			return 3
		}
		for i := 0; i < len(ar); i++ {
			for j := i + 1; j < len(ar); j++ {
				u, v := ar[i], ar[j]
				g[u] = append(g[u], v)
				g[v] = append(g[v], u)
			}
		}
	}

	minCycle := Inf
	for _, r := range a {
		seen := make(map[int64]bool)
		lv := make(map[int64]int)
		for _, ch := range g[r] {
			if !seen[ch] {
				minCycle = min(minCycle, dfs(r, ch, r, g, seen, lv)+1)
			}
		}
	}

	if minCycle >= Inf {
		return -1
	}

	return minCycle
}

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

func dfs(top, r, par int64, g map[int64][]int64, seen map[int64]bool, d map[int64]int) int {
	seen[r] = true
	d[r] = Inf
	for _, v := range g[r] {
		if v == par {
			continue
		} else if v == top {
			d[r] = 1
		} else if seen[v] {
			d[r] = min(d[r], 1+d[v])
		} else {
			d[r] = min(d[r], 1+dfs(top, v, r, g, seen, d))
		}
	}
	return d[r]
}

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1205B(reader io.Reader, writer io.Writer) {
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}
	const inf int = 1e8

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int64, n)
	bitIndexes := make([][]int, 60)
	for i := 0; i < n; i++ {
		var a int64
		Fscan(in, &a)
		arr[i] = a
		for j := 0; a > 0; j++ {
			if a&1 == 1 {
				bitIndexes[j] = append(bitIndexes[j], i)
			}
			a >>= 1
		}
	}

	uniqueSet := map[int64]int{}
	for _, indexes := range bitIndexes {
		if len(indexes) >= 3 {
			Fprintln(out, 3)
			return
		}
		for _, idx := range indexes {
			uniqueSet[arr[idx]] = 1
		}
	}
	uniqueArr := []int64{}
	for k := range uniqueSet {
		uniqueArr = append(uniqueArr, k)
	}

	// 剩下不超过120个数，暴力求最小环即可
	sz := len(uniqueArr)
	weights := make([][]int, sz)
	for i := range weights {
		weights[i] = make([]int, sz)
		for j := range weights[i] {
			weights[i][j] = inf
		}
	}
	for i := range weights {
		weights[i][i] = 0
	}
	for i, ai := range uniqueArr {
		for j := i + 1; j < sz; j++ {
			if ai&uniqueArr[j] > 0 {
				weights[i][j] = 1
				weights[j][i] = 1
			}
		}
	}
	dist := make([][]int, sz)
	for i := range dist {
		dist[i] = make([]int, sz)
		copy(dist[i], weights[i])
	}
	ans := inf
	for k := 0; k < sz; k++ {
		for i := 0; i < k; i++ {
			for j := 0; j < i; j++ {
				ans = min(ans, dist[i][j]+weights[i][k]+weights[k][j])
			}
		}
		for i := 0; i < sz; i++ {
			for j := 0; j < sz; j++ {
				dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j])
			}
		}
	}
	if ans == inf {
		ans = -1
	}
	Fprint(out, ans)
}

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

// Author: sighduck
// URL: https://codeforces.com/contest/1205/problem/B

package main

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

type QueueItem struct {
	Index      int
	PathLength int
}

func createAdjacencyMatrix(n int, a []int64) [][]bool {
	matrix := make([][]bool, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]bool, n)
	}
	b := []int64{}
	for _, elem := range a {
		if elem != 0 {
			b = append(b, elem)
		}
	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if b[i]&b[j] > 0 {
				matrix[i][j] = true
				matrix[j][i] = true
			}
		}
	}
	return matrix
}

func getMinCycleLength(n int, matrix [][]bool, i int) int {
	queue := []QueueItem{QueueItem{Index: i, PathLength: 1}}
	pathLengthMap := make(map[int]int)
	for len(queue) > 0 {
		var current QueueItem
		current, queue = queue[0], queue[1:]
		if pathLengthMap[current.Index] > 0 {
			return current.PathLength + pathLengthMap[current.Index] - 2
		}
		pathLengthMap[current.Index] = current.PathLength
		for i := 0; i < n; i++ {
			if i != current.Index && matrix[i][current.Index] && pathLengthMap[i] == 0 {
				queue = append(queue, QueueItem{Index: i, PathLength: current.PathLength + 1})
			}
		}
	}
	return 0
}

func search(n int, matrix [][]bool) int {
	pathLengthMap := make(map[int]int)
	cycleExists := false
	minCycleLength := n + 1
	for i := 0; i < n; i++ {
		if pathLengthMap[i] == 0 {
			length := getMinCycleLength(n, matrix, i)
			if length > 0 && length < minCycleLength {
				minCycleLength = length
				cycleExists = true
			}
		}
	}
	if !cycleExists {
		return -1
	}
	return minCycleLength
}

func Solve(n int, a []int64) int {
	nonZeroNodes := 0
	for _, elem := range a {
		if elem != 0 {
			nonZeroNodes += 1
		}
	}
	if nonZeroNodes >= 121 {
		return 3
	}
	matrix := createAdjacencyMatrix(nonZeroNodes, a)
	return search(nonZeroNodes, matrix)
}

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

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1205B(reader io.Reader, writer io.Writer) {
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}
	const inf int = 1e9

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int64, n)
	bitIndexes := make([][]int, 60)
	for i := 0; i < n; i++ {
		var a int64
		Fscan(in, &a)
		arr[i] = a
		for j := 0; a > 0; j++ {
			if a&1 == 1 {
				bitIndexes[j] = append(bitIndexes[j], i)
			}
			a >>= 1
		}
	}

	uniqueSet := map[int64]int{}
	for _, indexes := range bitIndexes {
		if len(indexes) >= 3 {
			Fprintln(out, 3)
			return
		}
		for _, idx := range indexes {
			uniqueSet[arr[idx]] = 1
		}
	}
	uniqueArr := []int64{}
	for k := range uniqueSet {
		uniqueArr = append(uniqueArr, k)
	}

	// 剩下不超过120个数，暴力求最小环即可
	sz := len(uniqueArr)
	weights := make([][]int, sz)
	for i := range weights {
		weights[i] = make([]int, sz)
		for j := range weights[i] {
			weights[i][j] = inf
		}
	}
	for i := range weights {
		weights[i][i] = 0
	}
	for i, ai := range uniqueArr {
		for j := i + 1; j < sz; j++ {
			if ai&uniqueArr[j] > 0 {
				weights[i][j] = 1
				weights[j][i] = 1
			}
		}
	}
	dist := make([][]int, sz)
	for i := range dist {
		dist[i] = make([]int, sz)
		copy(dist[i], weights[i])
	}
	ans := inf
	for k := 0; k < sz; k++ {
		for i := 0; i < k; i++ {
			for j := 0; j < i; j++ {
				ans = min(ans, dist[i][j]+weights[i][k]+weights[k][j])
			}
		}
		for i := 0; i < sz; i++ {
			for j := 0; j < sz; j++ {
				dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j])
			}
		}
	}
	if ans == inf || ans < 0 {
		ans = -1
	}
	Fprint(out, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol1205B(reader io.Reader, writer io.Writer) {
	min := func(a, b int) int {
		if a <= b {
			return a
		}
		return b
	}
	const inf int = 1e9

	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n int
	Fscan(in, &n)
	arr := make([]int64, n)
	bitIndexes := make([][]int, 60)
	for i := 0; i < n; i++ {
		var a int64
		Fscan(in, &a)
		arr[i] = a
		for j := 0; a > 0; j++ {
			if a&1 == 1 {
				bitIndexes[j] = append(bitIndexes[j], i)
			}
			a >>= 1
		}
	}

	uniqueSet := map[int64]int{}
	for _, indexes := range bitIndexes {
		if len(indexes) >= 3 {
			Fprintln(out, 3)
			return
		}
		for _, idx := range indexes {
			uniqueSet[arr[idx]] = 1
		}
	}
	uniqueArr := []int64{}
	for k := range uniqueSet {
		uniqueArr = append(uniqueArr, k)
	}

	// 剩下不超过120个数，暴力求最小环即可
	sz := len(uniqueArr)
	weights := make([][]int, sz)
	for i := range weights {
		weights[i] = make([]int, sz)
		for j := range weights[i] {
			weights[i][j] = inf
		}
	}
	for i := range weights {
		weights[i][i] = 0
	}
	for i, ai := range uniqueArr {
		for j := i + 1; j < sz; j++ {
			if ai&uniqueArr[j] > 0 {
				weights[i][j] = 1
				weights[j][i] = 1
			}
		}
	}
	dist := make([][]int, sz)
	for i := range dist {
		dist[i] = make([]int, sz)
		copy(dist[i], weights[i])
	}
	ans := inf
	for k := 0; k < sz; k++ {
		for i := 0; i < k; i++ {
			for j := 0; j < i; j++ {
				ans = min(ans, dist[i][j]+weights[i][k]+weights[k][j])
			}
		}
		for i := 0; i < sz; i++ {
			for j := 0; j < sz; j++ {
				dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j])
			}
		}
	}
	if ans == inf {
		ans = -1
	}
	Fprint(out, ans)
}

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

// Author: sighduck
// URL: https://codeforces.com/contest/1205/problem/B

package main

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

type QueueItem struct {
	Index      int
	PathLength int
}

func createAdjacencyMatrix(n int, a []int64) [][]bool {
	matrix := make([][]bool, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]bool, n)
	}
	for i := 0; i < n; i++ {
		for j := i + 1; j < n; j++ {
			if a[i]&a[j] > 0 {
				matrix[i][j] = true
				matrix[j][i] = true
			}
		}
	}
	return matrix
}

func getMinCycleLength(n int, pathLengthMap map[int]int, matrix [][]bool, i int) (map[int]int, int) {
	queue := []QueueItem{QueueItem{Index: i, PathLength: 1}}
	for len(queue) > 0 {
		var current QueueItem
		current, queue = queue[0], queue[1:]
		if pathLengthMap[current.Index] > 0 {
			return pathLengthMap, current.PathLength + pathLengthMap[current.Index] - 2
		}
		pathLengthMap[current.Index] = current.PathLength
		for i := 0; i < n; i++ {
			if i != current.Index && matrix[i][current.Index] && pathLengthMap[i] == 0 {
				queue = append(queue, QueueItem{Index: i, PathLength: current.PathLength + 1})
			}
		}
	}
	return pathLengthMap, 0
}

func search(n int, matrix [][]bool) int {
	pathLengthMap := make(map[int]int)
	cycleExists := false
	minCycleLength := n + 1
	for i := 0; i < n; i++ {
		if pathLengthMap[i] == 0 {
			var length int
			pathLengthMap, length = getMinCycleLength(n, pathLengthMap, matrix, i)
			if length > 0 && length < minCycleLength {
				minCycleLength = length
				cycleExists = true
			}
		}
	}
	if !cycleExists {
		return -1
	}
	return minCycleLength
}

func Solve(n int, a []int64) int {
	nonZeroNodes := 0
	for _, elem := range a {
		if elem != 0 {
			nonZeroNodes += 1
		}
	}
	if nonZeroNodes >= 121 {
		return 3
	}
	matrix := createAdjacencyMatrix(n, a)
	return search(n, matrix)
}

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

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

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

// Author: sighduck
// URL: https://codeforces.com/contest/1205/problem/B

package main

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

type QueueItem struct {
	Index      int
	PathLength int
}

func createAdjacencyMatrix(n int, a []int64) [][]bool {
	adj := make([][]bool, n)
	for i := 0; i < n; i++ {
		adj[i] = make([]bool, n)
	}
	for i := 0; i < n; i++ {
		for j := 1; j < n; j++ {
			if a[i]&a[j] > 0 {
				adj[i][j] = true
				adj[j][i] = true
			}
		}
	}
	return adj
}

func Solve(n int, a []int64) int {
	nonZeroNodes := 0
	for _, elem := range a {
		if elem != 0 {
			nonZeroNodes += 1
		}
	}
	if nonZeroNodes >= 121 {
		return 3
	}
	adj := createAdjacencyMatrix(n, a)
	queue := []QueueItem{QueueItem{Index: 0, PathLength: 1}}
	pathLength := make(map[int]int)
	for len(queue) > 0 {
		var current QueueItem
		current, queue = queue[0], queue[1:]
		if pathLength[current.Index] > 0 {
			return current.PathLength + pathLength[current.Index] - 2
		}
		pathLength[current.Index] = current.PathLength
		for i := 0; i < n; i++ {
			if i != current.Index && adj[i][current.Index] && pathLength[i] == 0 {
				queue = append(queue, QueueItem{Index: i, PathLength: current.PathLength + 1})
			}
		}
	}
	return -1
}

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

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

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


Polycarp has built his own web service. Being a modern web service it includes login feature. And that always implies password security problems.Polycarp decided to store the hash of the password, generated by the following algorithm:  take the password $$$p$$$, consisting of lowercase Latin letters, and shuffle the letters randomly in it to obtain $$$p'$$$ ($$$p'$$$ can still be equal to $$$p$$$);  generate two random strings, consisting of lowercase Latin letters, $$$s_1$$$ and $$$s_2$$$ (any of these strings can be empty);  the resulting hash $$$h = s_1 + p' + s_2$$$, where addition is string concatenation. For example, let the password $$$p =$$$ "abacaba". Then $$$p'$$$ can be equal to "aabcaab". Random strings $$$s1 =$$$ "zyx" and $$$s2 =$$$ "kjh". Then $$$h =$$$ "zyxaabcaabkjh".Note that no letters could be deleted or added to $$$p$$$ to obtain $$$p'$$$, only the order could be changed.Now Polycarp asks you to help him to implement the password check module. Given the password $$$p$$$ and the hash $$$h$$$, check that $$$h$$$ can be the hash for the password $$$p$$$.Your program should answer $$$t$$$ independent test cases.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The first line of each test case contains a non-empty string $$$p$$$, consisting of lowercase Latin letters. The length of $$$p$$$ does not exceed $$$100$$$. The second line of each test case contains a non-empty string $$$h$$$, consisting of lowercase Latin letters. The length of $$$h$$$ does not exceed $$$100$$$.

For each test case print the answer to it — "YES" if the given hash $$$h$$$ could be obtained from the given password $$$p$$$ or "NO" otherwise.

5
abacaba
zyxaabcaabkjh
onetwothree
threetwoone
one
zzonneyy
one
none
twenty
ten

NoteThe first test case is explained in the statement.In the second test case both $$$s_1$$$ and $$$s_2$$$ are empty and $$$p'=$$$ "threetwoone" is $$$p$$$ shuffled.In the third test case the hash could not be obtained from the password.In the fourth test case $$$s_1=$$$ "n", $$$s_2$$$ is empty and $$$p'=$$$ "one" is $$$p$$$ shuffled (even thought it stayed the same). In the fifth test case the hash could not be obtained from the password.

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(0)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		s := ""
		ss := ""
		fmt.Fscanf(in, "%s\n", &s)
		fmt.Fscanf(in, "%s\n", &ss)
		ok := false
		length := len(ss)
		for i := 0; i < length; i++ {
			//fmt.Println(ss)
			if len(ss) < len(s) {
				break
			}
			sub := ss[:len(s)]
			if same(s, sub) {
				//fmt.Println(s, sub)
				ok = true
				break
			}
			ss = ss[1:]
		}
		if ok {
			fmt.Fprintf(out, "YES\n")
		} else {
			fmt.Fprintf(out, "NO\n")
		}
	}
	return
}
func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for i := 0; i < t; i++ {
		var p, h string
		fmt.Scanf("%s\n", &p)
		fmt.Scanf("%s\n", &h)

		pMap := make(map[uint8]int)
		hMap := make(map[uint8]int)

		for ch := uint8('a'); ch <= 'z'; ch++ {
			pMap[ch] = 0
			hMap[ch] = 0
		}

		for j := 0; j < len(p); j++ {
			pMap[p[j]]++
		}

		yes := false
		for j := 0; j <= len(h)-len(p); j++ {
			if j == 0 {
				for k := 0; k < len(p); k++ {
					hMap[h[k]]++
				}
			} else {
				hMap[h[j-1]]--
				hMap[h[j+len(p)-1]]++
			}

			currYes := true
			for k, v := range pMap {
				if hMap[k] != v {
					currYes = false
					break
				}
			}
			if currYes {
				yes = true
				break
			}
		}
		if yes {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

package main

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

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func init() {
	ReadString = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) func() string {
	r := bufio.NewScanner(ior)
	// r.Buffer(make([]byte, 1024), int(1e+11)) // for AtCoder
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	// Split sets the split function for the Scanner. The default split function is ScanLines.
	// Split panics if it is called after scanning has started.
	r.Split(bufio.ScanWords)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const MOD = 1000000000 + 7
const ALPHABET_NUM = 26
const INF_INT64 = math.MaxInt64
const INF_BIT60 = 1 << 60
const INF_INT32 = math.MaxInt32
const INF_BIT30 = 1 << 30

var t int
var P, H []rune

func main() {
	t = ReadInt()

	for tc := 0; tc < t; tc++ {
		P, H = ReadRuneSlice(), ReadRuneSlice()

		solve()
	}
}

func solve() {
	if len(P) > len(H) {
		fmt.Println("NO")
		return
	}

	pL := RuneList{}
	for i := 0; i < len(P); i++ {
		pL = append(pL, P[i])
	}
	sort.Sort(pL)

	plen := len(P)
	for i := 0; i < len(H); i++ {
		if i+plen > len(H) {
			break
		}

		tmp := H[i : i+plen]
		hL := RuneList{}
		for j := 0; j < len(tmp); j++ {
			hL = append(hL, tmp[j])
		}
		sort.Sort(hL)

		if string(pL) == string(hL) {
			fmt.Println("YES")
			return
		}
	}

	fmt.Println("NO")
}

type RuneList []rune

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

// S := RuneList{'a', 'b', 'c'}
// sort.Sort(S) or sort.Sort(sort.Reverse(S))

/*
- まずは全探索を検討しましょう
- MODは最後にとりましたか？
- ループを抜けた後も処理が必要じゃありませんか？
- 和・積・あまりを求められたらint64が必要ではありませんか？
- いきなりオーバーフローはしていませんか？
	- MOD取る系はint64必須ですよ？
*/

/*
ASCII code

ASCII   10進数  ASCII   10進数  ASCII   10進数
!       33      "       34      #       35
$       36      %       37      &       38
'       39      (       40      )       41
*       42      +       43      ,       44
-       45      .       46      /       47
0       48      1       49      2       50
3       51      4       52      5       53
6       54      7       55      8       56
9       57      :       58      ;       59
<       60      =       61      >       62
?       63      @       64      A       65
B       66      C       67      D       68
E       69      F       70      G       71
H       72      I       73      J       74
K       75      L       76      M       77
N       78      O       79      P       80
Q       81      R       82      S       83
T       84      U       85      V       86
W       87      X       88      Y       89
Z       90      [       91      \       92
]       93      ^       94      _       95
`       96      a       97      b       98
c       99      d       100     e       101
f       102     g       103     h       104
i       105     j       106     k       107
l       108     m       109     n       110
o       111     p       112     q       113
r       114     s       115     t       116
u       117     v       118     w       119
x       120     y       121     z       122
{       123     |       124     }       125
~       126             127
*/

/*******************************************************************/

package main
import."fmt"

// github.com/EndlessCheng/codeforces-go
func main() {
	var T int
	var s, t []byte
	O := [26]int{}
o:
	for Scan(&T); T > 0; T-- {
		Scan(&s, &t)
		if len(t) < len(s) {
			Println("NO")
			continue
		}
		cnt := [26]int{}
		for _, b := range s {
			cnt[b-'a']++
		}
		for _, b := range t[:len(s)] {
			cnt[b-'a']--
		}
		if cnt == O {
			Println("YES")
			continue
		}
		for i, b := range t[len(s):] {
			cnt[t[i]-'a']++
			cnt[b-'a']--
			if cnt == O {
				Println("YES")
				continue o
			}
		}
		Println("NO")
	}
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanln(&t)
	for i:= 0 ; i< t; i++{
		var p, h string
		var ans int
		fmt.Scanln(&p)
		fmt.Scanln(&h)
		target := make([]int, 26)
		for j := 0; j < len(p); j++{
			target[p[j] - 'a']++
			ans++
		}
		if len(h) < len(p){
			fmt.Println("NO")
			continue
		}
		tar1 := make([]int, 26)
		copy(tar1, target)
		ans1 := ans
		var found bool
		var startAt int
		for k:= 0; k < len(h); {
			if tar1[h[k] - 'a'] == 0{
				ans1 = ans
				copy(tar1, target)
				startAt++
				k = startAt
			}else{
				tar1[h[k] - 'a']--
				ans1--
				k++
				if ans1 ==0{
					found = true
					break
				}
			}
		}
		if found{
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var (
		t uint
		channels []chan bool
	)

	for fmt.Scanln(&t); t > 0; t-- {
		// Declaration inside the for block necessary to avoid races.
		var p, h string

		fmt.Scanln(&p)
		fmt.Scanln(&h)

		ch := make(chan bool)
		channels = append(channels, ch)
		go func() {
			ch <- solve(p, h)
		}()
	}

	for _, ch := range channels {
		if <-ch {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	var pms, hms ['z' - 'a' + 1]uint
	for _, c := range p {
		pms[c-'a']++
	}
	for _, c := range h[:len(p)] {
		hms[c-'a']++
	}

	if pms == hms {
		return true
	}

	for i := 0; i < len(h)-len(p); i++ {
		hms[h[i]-'a']--
		hms[h[i+len(p)]-'a']++

		if pms == hms {
			return true
		}
	}

	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanln(&t)

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanln(&p)
		fmt.Scanln(&h)

		if solve(p, h) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	var pms, hms ['z'-'a'+1]uint
	for _, c := range p { pms[c - 'a']++ }
	for _, c := range h[:len(p)] { hms[c - 'a']++ }

	if pms == hms {
		return true
	}

	for i := 0; i < len(h) - len(p); i++ {
		hms[h[i] - 'a']--
		hms[h[i+len(p)] - 'a']++

		if pms == hms {
			return true
		}
	}

	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanln(&t)

	var channels []chan bool

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanln(&p)
		fmt.Scanln(&h)

		ch := make(chan bool)
		channels = append(channels, ch)
		go func() {
			ch <- solve(p, h)
		}()
	}

	for _, ch := range channels {
		if <-ch {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	var pms, hms ['z' - 'a' + 1]uint
	for _, c := range p {
		pms[c-'a']++
	}
	for _, c := range h[:len(p)] {
		hms[c-'a']++
	}

	if pms == hms {
		return true
	}

	for i := 0; i < len(h)-len(p); i++ {
		hms[h[i]-'a']--
		hms[h[i+len(p)]-'a']++

		if pms == hms {
			return true
		}
	}

	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanln(&t)

	var channels []chan bool

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanln(&p)
		fmt.Scanln(&h)

		ch := make(chan bool)
		channels = append(channels, ch)
		go func() {
			ch <- solve(p, h)
		}()
	}

	for _, ch := range channels {
		if <-ch {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	var pms, hms ['z'-'a'+1]uint
	for _, c := range p { pms[c - 'a']++ }
	for _, c := range h[:len(p)] { hms[c - 'a']++ }

	if pms == hms {
		return true
	}

	for i := 0; i < len(h) - len(p); i++ {
		hms[h[i] - 'a']--
		hms[h[i+len(p)] - 'a']++

		if pms == hms {
			return true
		}
	}

	return false
}

package main

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

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

	var numTestCases int
	var passwd, hash string
	fmt.Fscanf(reader, "%d\n", &numTestCases)

	for tcase := 1; tcase <= numTestCases; tcase++ {

		fmt.Fscanf(reader, "%s\n", &passwd)
		fmt.Fscanf(reader, "%s\n", &hash)
		pLen := len(passwd)
		hLen := len(hash)
		countByPCh := make(map[byte]int)
		for _, b := range []byte(passwd) {
			if _, ok := countByPCh[b]; !ok {
				countByPCh[b] = 1
			} else {
				countByPCh[b] += 1
			}
		}
		var found bool
		for i := 0; i <= hLen-pLen; i++ {
			found = true
			countByHCh := make(map[byte]int)
			for _, b := range []byte(hash[i : i+pLen]) {
				if _, ok := countByHCh[b]; !ok {
					countByHCh[b] = 1
				} else {
					countByHCh[b] += 1
				}
			}
			for b, count := range countByHCh {
				if count1, ok := countByPCh[b]; ok {
					if count != count1 {
						found = false
						break
					}
				} else {
					found = false
					break
				}
			}
			if found {
				break
			}
		}
		if found {
			fmt.Fprintf(writer, "YES\n")
		} else {
			fmt.Fprintf(writer, "NO\n")
		}
	}
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := int64(0)
	fmt.Fscanf(in, "%d\n", &t)
	for tt := int64(0); tt < t; tt++ {
		s := ""
		ss := ""
		fmt.Fscanf(in, "%s\n", &s)
		fmt.Fscanf(in, "%s\n", &ss)
		ok := false
		for i := 0; i < len(ss); i++ {
			if len(ss) < len(s) {
				break
			}
			sub := ss[:len(s)]
			if same(s, sub) {
				//fmt.Println(s, sub)
				ok = true
				break
			}
			ss = ss[1:]
		}
		if ok {
			fmt.Fprintf(out, "YES\n")
		} else {
			fmt.Fprintf(out, "NO\n")
		}
	}
	return
}
func same(s string, ss string) bool {
	a := make([]int, 26)
	for i := 0; i < len(s); i++ {
		b := int(s[i] - 'a')
		a[b]++
	}
	for i := 0; i < len(ss); i++ {
		b := int(ss[i] - 'a')
		a[b]--
		if a[b] == -1 {
			return false
		}
	}
	for i := 0; i < 26; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scanln(&t)
	for i:= 0 ; i< t; i++{
		var p, h string
		var ans int
		fmt.Scanln(&p)
		fmt.Scanln(&h)
		target := make([]int, 26)
		for j := 0; j < len(p); j++{
			target[p[j] - 'a']++
			ans++
		}
		if len(h) < len(p){
			fmt.Println("NO")
		}
		tar1 := make([]int, 26)
		copy(tar1, target)
		ans1 := ans
		var found bool
		var startAt int
		for k:= 0; k < len(h); {
			if tar1[h[k] - 'a'] == 0{
				ans1 = ans
				copy(tar1, target)
				startAt++
				k = startAt
			}else{
				tar1[h[k] - 'a']--
				ans1--
				k++
				if ans1 ==0{
					found = true
					break
				}
			}
		}
		if found{
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanf("%s", &p)
		fmt.Scanf("%s", &h)

		if solve(p, h) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	var pms, hms ['z'-'a'+1]uint
	for _, c := range p { pms[c - 'a']++ }
	for _, c := range h[:len(p)] { hms[c - 'a']++ }

	if pms == hms {
		return true
	}

	for i := 0; i < len(h) - len(p); i++ {
		hms[h[i] - 'a']--
		hms[h[i+len(p)] - 'a']++

		if pms == hms {
			return true
		}
	}

	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanln(&p)
		fmt.Scanln(&h)

		fmt.Println(p); fmt.Println(h)

		if solve(p, h) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	var pms, hms ['z'-'a'+1]uint
	for _, c := range p { pms[c - 'a']++ }
	for _, c := range h[:len(p)] { hms[c - 'a']++ }

	if pms == hms {
		return true
	}

	for i := 0; i < len(h) - len(p); i++ {
		hms[h[i] - 'a']--
		hms[h[i+len(p)] - 'a']++

		if pms == hms {
			return true
		}
	}

	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanf("%s", &p)
		fmt.Scanf("%s", &h)

		if solve(p, h) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	fmt.Println(0)

	var pms, hms ['z'-'a'+1]uint
	for _, c := range p { pms[c - 'a']++ }
	for _, c := range h[:len(p)] { hms[c - 'a']++ }

	fmt.Println(1)

	if pms == hms {
		fmt.Println(pms); fmt.Println(hms)
		return true
	}

	fmt.Println(2)

	for i := 0; i < len(h) - len(p); i++ {
		hms[h[i] - 'a']--
		hms[h[i+len(p)] - 'a']++

		fmt.Println(3)

		if pms == hms {
			fmt.Println(pms); fmt.Println(hms)
			return true
		}

		fmt.Println(4)
	}

	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanf("%s", &p)
		fmt.Scanf("%s", &h)

		if solve(p, h) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanf("%s", &p)
		fmt.Scanf("%s", &h)

		if solve(p, h) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	fmt.Println(0)

	var pms, hms ['z'-'a'+1]uint
	fmt.Println(p)
	for _, c := range p {
		fmt.Println(c)
		pms[c - 'a']++
	}
	for _, c := range h[:len(p)] { hms[c - 'a']++ }

	fmt.Println(1)

	if pms == hms {
		fmt.Println(pms); fmt.Println(hms)
		return true
	}

	fmt.Println(2)

	for i := 0; i < len(h) - len(p); i++ {
		hms[h[i] - 'a']--
		hms[h[i+len(p)] - 'a']++

		fmt.Println(3)

		if pms == hms {
			fmt.Println(pms); fmt.Println(hms)
			return true
		}

		fmt.Println(4)
	}

	return false
}

/*
https://codeforces.com/contest/1278/problem/A
https://github.com/miloszlakomy/competitive/tree/master/codeforces/1278A
*/

package main

import (
	"fmt"
)

func main() {
	var t uint
	fmt.Scanf("%d", &t)

	for ; t > 0; t-- {
		var p, h string
		fmt.Scanln(&p)
		fmt.Scanln(&h)

		if solve(p, h) {
			fmt.Println("YES")
		} else {
			fmt.Println("NO")
		}
	}
}

func solve(p, h string) bool {
	if len(h) < len(p) {
		return false
	}

	var pms, hms ['z'-'a'+1]uint
	for _, c := range p { pms[c - 'a']++ }
	for _, c := range h[:len(p)] { hms[c - 'a']++ }

	if pms == hms {
		return true
	}

	for i := 0; i < len(h) - len(p); i++ {
		hms[h[i] - 'a']--
		hms[h[i+len(p)] - 'a']++

		if pms == hms {
			return true
		}
	}

	return false
}


Valera had two bags of potatoes, the first of these bags contains x (x ≥ 1) potatoes, and the second — y (y ≥ 1) potatoes. Valera — very scattered boy, so the first bag of potatoes (it contains x potatoes) Valera lost. Valera remembers that the total amount of potatoes (x + y) in the two bags, firstly, was not gerater than n, and, secondly, was divisible by k.Help Valera to determine how many potatoes could be in the first bag. Print all such possible numbers in ascending order.

The first line of input contains three integers y, k, n (1 ≤ y, k, n ≤ 109;   ≤ 105).

Print the list of whitespace-separated integers — all possible values of x in ascending order. You should print each possible value of x exactly once. If there are no such values of x print a single integer -1.

package main
import (
	"fmt"
	//"bufio"
	//"os"
	//"strconv"
)

func main() {
	var n,k,y,j int
	b:=0
	fmt.Scan(&y,&k,&n)
	j=k-y%k
	for ;j<=n-y; {
		fmt.Print(j," ")
		j+=k
		b++
	}
	if b==0 {
		fmt.Println("-1")
	}

}

package main

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

func main() {
    writer := bufio.NewWriter(os.Stdout)
    var y,k,n int
    fmt.Scan(&y,&k,&n)
    first := true
    for i := ((y/k)+1)*k; i <= n; i += k {
        if first { first = false } else { writer.WriteRune(' ') }
        writer.WriteString(strconv.Itoa(i-y))
    }
    if first { writer.WriteString("-1") }
    writer.WriteRune('\n')
    writer.Flush()
}

// 239A-mic
package main

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

func main() {
    w := bufio.NewWriter(os.Stdout)
    var y, k, n int
    fmt.Scan(&y, &k, &n)
    f := false
    for i := k - y%k; i <= n-y; i += k {
        w.WriteString(strconv.Itoa(i))
        f = true
        if i < n {
            w.WriteRune(' ')
        }
    }
    if !f {
        fmt.Print("-1")
    }
    w.WriteRune('\n')
    w.Flush()
}

package main

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

func Pota(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()

	var y, k, n int
	Fscan(in, &y)
	Fscan(in, &k)
	Fscan(in, &n)

	for i := (y/k + 1) * k; i <= n; i += k {
		Fprintln(out, i-y)
	}

	if (y/k+1)*k > n {
		Fprintln(out, -1)
	}

}

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