Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it!Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has n pieces numbered from 1 to n from left to right, and the i-th piece has a colour si, denoted by a lowercase English letter. Nadeko will repaint at most m of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour c — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland.For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3.But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has q plans on this, each of which can be expressed as a pair of an integer mi and a lowercase letter ci, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan.

The first line of input contains a positive integer n (1 ≤ n ≤ 1 500) — the length of the garland. The second line contains n lowercase English letters s1s2... sn as a string — the initial colours of paper pieces on the garland. The third line contains a positive integer q (1 ≤ q ≤ 200 000) — the number of plans Nadeko has. The next q lines describe one plan each: the i-th among them contains an integer mi (1 ≤ mi ≤ n) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter ci — Koyomi's possible favourite colour.

Output q lines: for each work plan, output one line containing an integer — the largest Koyomity achievable after repainting the garland according to it.

15
yamatonadeshiko
10
1 a
2 a
3 a
4 a
5 a
1 b
2 b
3 b
4 b
5 b

NoteIn the first sample, there are three plans:   In the first plan, at most 1 piece can be repainted. Repainting the "y" piece to become "o" results in "kooomi", whose Koyomity of 3 is the best achievable;  In the second plan, at most 4 pieces can be repainted, and "oooooo" results in a Koyomity of 6;  In the third plan, at most 4 pieces can be repainted, and "mmmmmi" and "kmmmmm" both result in a Koyomity of 5.

package main

import "fmt"
import "bufio"
import "os"

var (
    n int
    s string
    q int
    mm, cc int
)

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

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

    for {
        _, err := fmt.Fscan(in, &n)
        if err != nil { break }

        a := make([]int, n + 1)
        fmt.Fscan(in, &s)
        for i := 0; i < len(s); i++ {
            a[i + 1] = int(s[i]) - int('a');
        }

        var f [26][2][1501]int
        var g [26][2][1501]int

        var x int
        for c := 0; c < 26; c++ {
            prev := 0
            var now int

            for i := 1; i <= n; i++ {
                now = (prev + 1) % 2

                f[c][now][0] = f[c][prev][0]
                if a[i] == c {
                    g[c][now][0] = 1 + g[c][prev][0]
                } else {
                    g[c][now][0] = 0
                }
                f[c][now][0] = maxInt(f[c][prev][0], g[c][now][0])

                for j := 1; j <= i; j++ {
                    g[c][now][j] = g[c][now][j-1]

                    if a[i] == c {
                        g[c][now][j] = maxInt(g[c][now][j], g[c][prev][j] + 1)
                    } else {
                        g[c][now][j] = maxInt(g[c][now][j], g[c][prev][j-1] + 1)
                    }

                    f[c][now][j] = maxInt(f[c][now][j-1], f[c][prev][j])
                    f[c][now][j] = maxInt(f[c][now][j], g[c][now][j])
                }

                prev = now
            }
            x = now
        }

        fmt.Fscan(in, &q)
        for i := 0; i < q; i++ {
            var str string
            fmt.Fscan(in, &mm, &str)
            var c int = int(str[0]) - int('a')
            fmt.Println(f[c][x][mm])
        }
    }
}

package main

// Solution for https://codeforces.com/problemset/problem/814/C

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
	"strings"
)

func main() {
	inputBytes, _ := ioutil.ReadAll(os.Stdin)
	stdin := bytes.NewReader(inputBytes)

	var n, q int
	var s string
	fmt.Fscanf(stdin, "%d\n", &n)
	fmt.Fscanf(stdin, "%s\n", &s)
	fmt.Fscanf(stdin, "%d\n", &q)

	reals := []byte(s)
	for i := range reals {
		reals[i] -= 'a'
	}

	m := make([]int16, q)
	c := make([]byte, q)
	var maxm [26]int16

	var mi int16
	var ci string
	for i := 0; i < q; i++ {
		fmt.Fscanf(stdin, "%d", &mi)
		fmt.Fscanf(stdin, "%s\n", &ci)
		realc := ci[0] - 'a'
		m[i] = mi
		c[i] = realc
		if maxm[realc] < mi {
			maxm[realc] = mi
		}
	}

	var goods [26][]int16

	for a := byte(0); a < 26; a++ {
		maxm1 := maxm[a]
		if maxm1 == 0 {
			continue
		}
		firstGood := reals[0] == a
		prevGood := firstGood
		islands := []int16{1}
		for i := 1; i < n; i++ {
			good := reals[i] == a
			if good == prevGood {
				islands[len(islands)-1]++
			} else {
				islands = append(islands, 1)
			}
			prevGood = good
		}

		// Convert it to list of pairs (good, bad).
		if !firstGood {
			islands = islands[1:]
		}
		if len(islands)%2 == 1 {
			islands = append(islands, 0)
		}

		maxGood := make([]int16, maxm1+1) // Key is required badness.
		nislands := len(islands) / 2
		for from := 0; from < nislands; from++ {
			goodness := islands[from*2]
			badness := int16(0)
			if maxGood[badness] < goodness {
				maxGood[badness] = goodness
			}
			for to := from + 1; to < nislands; to++ {
				goodness += islands[to*2-1] + islands[to*2]
				badness += islands[to*2-1]
				if badness > maxm1 {
					break
				}
				if maxGood[badness] < goodness {
					maxGood[badness] = goodness
				}
			}
		}

		for badness := int16(1); badness <= maxm1; badness++ {
			if maxGood[badness] < maxGood[badness-1]+1 {
				maxGood[badness] = maxGood[badness-1] + 1
			}
			if maxGood[badness] > int16(n) {
				maxGood[badness] = int16(n)
			}
		}

		goods[a] = maxGood
	}

	var ans []string
	for i := 0; i < q; i++ {
		ans = append(ans, strconv.Itoa(int(goods[c[i]][m[i]])))
	}
	fmt.Println(strings.Join(ans, "\n"))
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
  N := io.NextInt()
  S := io.NextLine()

  ans := make(map[byte][]int)
  for c := byte('a'); c <= 'z'; c++ {
    ans[c] = Compute(S, c)
  }

  Q := io.NextInt()

  for q := 0; q < Q; q++ {
    M := io.NextInt()
    C := io.NextString()[0]

    val := ans[C][M]
    if val == 0 {
      io.Println(N)
    } else {
      io.Println(val)
    }
  }
}

func Compute(S string, c byte) []int {
  N := len(S)

  ans := make([]int, N + 1)
  for i := 0; i < N; i++ {
    if S[i] == c {
      continue
    }
    j := i
    for j > 0 && S[j - 1] == c {
      j--
    }
    p := i
    add := 0
    for p < N {
      p++
      for p < N && S[p] == c {
        p++
      }
      add++
      ans[add] = Max(ans[add], p - j)
    }
  }
  return ans
}

func Max(args ...int) int {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
  N := io.NextInt()
  S := io.NextLine()

  ans := make(map[byte][]int)
  for c := byte('a'); c <= 'z'; c++ {
    ans[c] = Compute(S, c)
  }

  Q := io.NextInt()

  for q := 0; q < Q; q++ {
    M := io.NextInt()
    C := io.NextString()[0]

    val := ans[C][M]
    if val == 0 {
      io.Println(N)
    } else {
      io.Println(val)
    }
  }
}

func Compute(S string, c byte) []int {
  N := len(S)

  skip := make([]int, N + 1)
  skip[N] = N
  end := N
  for i := N - 1; i >= 0; i-- {
    if S[i] != c {
      end = i
    }
    skip[i] = end
  }

  ans := make([]int, N + 1)
  for i := 0; i < N; i++ {
    if S[i] == c {
      continue
    }
    j := i
    for j > 0 && S[j - 1] == c {
      j--
    }
    p := i
    add := 0
    for p < N {
      p++
      p = skip[p]
      add++
      ans[add] = Max(ans[add], p - j)
    }
  }
  return ans
}

func Max(args ...int) int {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

func Init() {
}

func Solve(io *FastIO) {
  N := io.NextInt()
  S := io.NextLine()

  ans := make(map[byte][]int)
  for c := byte('a'); c <= 'z'; c++ {
    ans[c] = Compute(S, c)
  }

  Q := io.NextInt()

  for q := 0; q < Q; q++ {
    M := io.NextInt()
    C := io.NextString()[0]

    val := ans[C][M]
    if val == 0 {
      io.Println(N)
    } else {
      io.Println(val)
    }
  }
}

func Compute(S string, c byte) []int {
  N := len(S)

  // pos := make(map[int]int)
  // idx := 0
  // for idx < N {
  //   if S[idx] != c {
  //     prev := idx
  //     idx++
  //     for idx < N && S[idx] == c {
  //       idx++
  //     }
  //     S[prev] = idx
  //   } else {
  //     idx++
  //   }
  // }

  ans := make([]int, N + 1)
  for i := 0; i < N; i++ {
    if S[i] == c {
      continue
    }
    j := i
    for j > 0 && S[j - 1] == c {
      j--
    }
    p := i
    add := 0
    for p < N {
      p++
      for p < N && S[p] == c {
        p++
      }
      add++
      ans[add] = Max(ans[add], p - j)
      // if c == 'o' {
      //   fmt.Printf("START(%d): %d -> %d - %d = %d\n", i, add, p, i, p - i)
      // }
    }
  }
  return ans
}

func Max(args ...int) int {
	max := args[0]
	for i := 1; i < len(args); i++ {
		if args[i] > max {
			max = args[i]
		}
	}
	return max
}

type FastIO struct {
	reader *bufio.Reader
	writer *bufio.Writer
}

func (io *FastIO) NextChar() byte {
	c, _ := io.reader.ReadByte()
	return c
}

func (io *FastIO) NextInt() int {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := 1
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := 0
	for !IsSpaceChar(c) {
		res = (res * 10) + int(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextLong() int64 {
	c := io.NextChar()
	for IsSpaceChar(c) {
		c = io.NextChar()
	}
	sgn := int64(1)
	if c == '-' {
		sgn = -1
		c = io.NextChar()
	}
	res := int64(0)
	for !IsSpaceChar(c) {
		res = (res * 10) + int64(c - '0')
		c = io.NextChar()
	}
	return sgn * res
}

func (io *FastIO) NextIntArray(size int) []int {
	return io.NextIntArrayOffset(size, 0)
}

func (io *FastIO) NextIntArrayOffset(size, offset int) []int {
	arr := make([]int, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextInt()
	}
	return arr
}

func (io *FastIO) NextLongArray(size int) []int64 {
	return io.NextLongArrayOffset(size, 0)
}

func (io *FastIO) NextLongArrayOffset(size, offset int) []int64 {
	arr := make([]int64, size + offset)
	for i := 0; i < size; i++ {
		arr[i + offset] = io.NextLong()
	}
	return arr
}

func (io *FastIO) NextString() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsSpaceChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) NextLine() string {
	c := io.NextChar()
	for (IsSpaceChar(c)) {
		c = io.NextChar()
	}
	var sb bytes.Buffer
	for !IsLineBreakChar(c) {
		sb.WriteByte(c)
		c = io.NextChar()
	}
	return sb.String()
}

func (io *FastIO) Println(args ...interface{}) {
	io.writer.WriteString(fmt.Sprintln(args...))
}

func (io *FastIO) Printf(format string, args ...interface{}) {
	io.writer.WriteString(fmt.Sprintf(format, args...))
}

func (io *FastIO) FlushOutput() {
	io.writer.Flush()
}

func IsSpaceChar(c byte) bool {
	switch c {
	case 0, '\t', '\n', '\r', ' ':
		return true
	default:
		return false
	}
}

func IsLineBreakChar(c byte) bool {
	switch c {
		case 0, '\n', '\r':
			return true
		default:
			return false
	}
}

func main() {
	io := FastIO{reader: bufio.NewReader(os.Stdin), writer: bufio.NewWriter(os.Stdout)}
	Init()
	Solve(&io)
	io.FlushOutput()
}

package main

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

type Reader struct {
  scanner *bufio.Scanner
}

func StdinReader() Reader {
  reader := Reader{bufio.NewScanner(os.Stdin)}
  reader.scanner.Split(bufio.ScanWords)
  return reader
}

func (r *Reader) ReadString() string {
  ok := r.scanner.Scan()
  if !ok {
    panic("EOF")
  }
  return r.scanner.Text()
}

func (r *Reader) ReadByte() byte {
  s := r.ReadString()
  return s[0]
}

func (r *Reader) ReadInt() int {
  i, err := strconv.Atoi(r.ReadString())
  if err != nil {
    panic("no int")
  }
  return i
}

func main() {
  in := StdinReader()

  n := in.ReadInt()
  s := in.ReadString()
  q := in.ReadInt()

  prefix := make([][]int, 26)
  for c := 0; c < 26; c++ {
    prefix[c] = make([]int, n + 1)
    prefix[c][0] = 0
    for i := 0; i < n; i++ {
      prefix[c][i + 1] = prefix[c][i]
      if s[i] - 'a' != byte(c) {
        prefix[c][i + 1]++
      }
    }
  }

  for i := 0; i < q; i++ {
    m := in.ReadInt()
    c := in.ReadByte()
    curr_prefix := prefix[c - 'a']
    ans := m
    for j := m; j <= n; j++ {
      for ; (ans + 1) <= j && curr_prefix[j] - curr_prefix[j - (ans + 1)] <= m; {
        ans++
      }
    }
    fmt.Printf("%d\n", ans)
  }

}

package main

import "fmt"

var (
    n int
    s string
    q int
    mm, cc int
)

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

func main() {
    for {
        _, err := fmt.Scan(&n)
        if err != nil { break }

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

        var f [26][2][1501]int
        var g [26][2][1501]int

        var x int
        for c := 0; c < 26; c++ {
            prev := 0
            var now int

            for i := 1; i <= n; i++ {
                now = (prev + 1) % 2

                f[c][now][0] = f[c][prev][0]
                if a[i] == c {
                    g[c][now][0] = 1 + g[c][prev][0]
                }
                f[c][now][0] = maxInt(f[c][prev][0], g[c][now][0])

                for j := 1; j <= i; j++ {
                    g[c][now][j] = g[c][now][j-1]

                    if a[i] == c {
                        g[c][now][j] = maxInt(g[c][now][j], g[c][prev][j] + 1)
                    } else {
                        g[c][now][j] = maxInt(g[c][now][j], g[c][prev][j-1] + 1)
                    }

                    f[c][now][j] = maxInt(f[c][now][j-1], f[c][prev][j])
                    f[c][now][j] = maxInt(f[c][now][j], g[c][now][j])
                }

                prev = now
            }

            x = now
        }

        fmt.Scan(&q)
        for i := 0; i < q; i++ {
            var str string
            fmt.Scan(&mm, &str)
            var c int = int(str[0]) - int('a')
            fmt.Println(f[c][x][mm])
        }
    }
}

package main

// Solution for https://codeforces.com/problemset/problem/814/C

import (
	"fmt"
)

func main() {
	var n, q int
	var s string
	fmt.Scanf("%d\n", &n)
	fmt.Scanf("%s\n", &s)
	fmt.Scanf("%d\n", &q)

	reals := []byte(s)
	for i := range reals {
		reals[i] -= 'a'
	}

	m := make([]int16, q)
	c := make([]byte, q)
	var maxm [26]int16

	var mi int16
	var ci string
	for i := 0; i < q; i++ {
		fmt.Scanf("%d", &mi)
		fmt.Scanf("%s\n", &ci)
		realc := ci[0] - 'a'
		m[i] = mi
		c[i] = realc
		if maxm[realc] < mi {
			maxm[realc] = mi
		}
	}

	var goods [26][]int16

	for a := byte(0); a < 26; a++ {
		maxm1 := maxm[a]
		if maxm1 == 0 {
			continue
		}
		firstGood := reals[0] == a
		prevGood := firstGood
		islands := []int16{1}
		for i := 1; i < n; i++ {
			good := reals[i] == a
			if good == prevGood {
				islands[len(islands)-1]++
			} else {
				islands = append(islands, 1)
			}
			prevGood = good
		}

		// Convert it to list of pairs (good, bad).
		if !prevGood {
			islands = islands[1:]
		}
		if len(islands)%2 == 1 {
			islands = append(islands, 0)
		}

		maxGood := make([]int16, maxm1+1) // Key is required badness.
		nislands := len(islands) / 2
		for from := 0; from < nislands; from++ {
			goodness := islands[from*2]
			badness := int16(0)
			if maxGood[badness] < goodness {
				maxGood[badness] = goodness
			}
			for to := from + 1; to < nislands; to++ {
				goodness += islands[to*2-1] + islands[to*2]
				badness += islands[to*2-1]
				if badness > maxm1 {
					break
				}
				if maxGood[badness] < goodness {
					maxGood[badness] = goodness
				}
			}
		}

		for badness := int16(1); badness <= maxm1; badness++ {
			if maxGood[badness] < maxGood[badness-1]+1 {
				maxGood[badness] = maxGood[badness-1] + 1
			}
			if maxGood[badness] > int16(n) {
				maxGood[badness] = int16(n)
			}
		}

		goods[a] = maxGood
	}

	for i := 0; i < q; i++ {
		fmt.Println(goods[c[i]][m[i]])
	}
}

package main

// Solution for https://codeforces.com/problemset/problem/814/C

import (
	"fmt"
)

func main() {
	var n, q int
	var s string
	fmt.Scanf("%d", &n)
	fmt.Scanf("%s", &s)
	fmt.Scanf("%d", &q)

	reals := []byte(s)
	for i := range reals {
		reals[i] -= 'a'
	}

	m := make([]int16, q)
	c := make([]byte, q)
	var maxm [26]int16

	var mi int16
	var ci string
	for i := 0; i < q; i++ {
		fmt.Scanf("%d", &mi)
		fmt.Scanf("%s", &ci)
		realc := ci[0] - 'a'
		m[i] = mi
		c[i] = realc
		if maxm[realc] < mi {
			maxm[realc] = mi
		}
	}

	var goods [26][]int16

	for a := byte(0); a < 26; a++ {
		maxm1 := maxm[a]
		if maxm1 == 0 {
			continue
		}
		firstGood := reals[0] == a
		prevGood := firstGood
		islands := []int16{1}
		for i := 1; i < n; i++ {
			good := reals[i] == a
			if good == prevGood {
				islands[len(islands)-1]++
			} else {
				islands = append(islands, 1)
			}
			prevGood = good
		}

		// Convert it to list of pairs (good, bad).
		if !prevGood {
			islands = islands[1:]
		}
		if len(islands)%2 == 1 {
			islands = append(islands, 0)
		}

		maxGood := make([]int16, maxm1+1) // Key is required badness.
		nislands := len(islands) / 2
		for from := 0; from < nislands; from++ {
			goodness := islands[from*2]
			badness := int16(0)
			if maxGood[badness] < goodness {
				maxGood[badness] = goodness
			}
			for to := from + 1; to < nislands; to++ {
				goodness += islands[to*2-1] + islands[to*2]
				badness += islands[to*2-1]
				if badness > maxm1 {
					break
				}
				if maxGood[badness] < goodness {
					maxGood[badness] = goodness
				}
			}
		}

		for badness := int16(1); badness <= maxm1; badness++ {
			if maxGood[badness] < maxGood[badness-1]+1 {
				maxGood[badness] = maxGood[badness-1] + 1
			}
		}

		goods[a] = maxGood
	}

	for i := 0; i < q; i++ {
		fmt.Println(goods[c[i]][m[i]])
	}
}

package main

// Solution for https://codeforces.com/problemset/problem/814/C

import (
	"fmt"
)

func main() {
	var n, q int
	var s string
	fmt.Scanf("%d\n", &n)
	fmt.Scanf("%s\n", &s)
	fmt.Scanf("%d\n", &q)

	reals := []byte(s)
	for i := range reals {
		reals[i] -= 'a'
	}

	m := make([]int16, q)
	c := make([]byte, q)
	var maxm [26]int16

	var mi int16
	var ci string
	for i := 0; i < q; i++ {
		fmt.Scanf("%d", &mi)
		fmt.Scanf("%s\n", &ci)
		realc := ci[0] - 'a'
		m[i] = mi
		c[i] = realc
		if maxm[realc] < mi {
			maxm[realc] = mi
		}
	}

	var goods [26][]int16

	for a := byte(0); a < 26; a++ {
		maxm1 := maxm[a]
		if maxm1 == 0 {
			continue
		}
		firstGood := reals[0] == a
		prevGood := firstGood
		islands := []int16{1}
		for i := 1; i < n; i++ {
			good := reals[i] == a
			if good == prevGood {
				islands[len(islands)-1]++
			} else {
				islands = append(islands, 1)
			}
			prevGood = good
		}

		// Convert it to list of pairs (good, bad).
		if !prevGood {
			islands = islands[1:]
		}
		if len(islands)%2 == 1 {
			islands = append(islands, 0)
		}

		maxGood := make([]int16, maxm1+1) // Key is required badness.
		nislands := len(islands) / 2
		for from := 0; from < nislands; from++ {
			goodness := islands[from*2]
			badness := int16(0)
			if maxGood[badness] < goodness {
				maxGood[badness] = goodness
			}
			for to := from + 1; to < nislands; to++ {
				goodness += islands[to*2-1] + islands[to*2]
				badness += islands[to*2-1]
				if badness > maxm1 {
					break
				}
				if maxGood[badness] < goodness {
					maxGood[badness] = goodness
				}
			}
		}

		for badness := int16(1); badness <= maxm1; badness++ {
			if maxGood[badness] < maxGood[badness-1]+1 {
				maxGood[badness] = maxGood[badness-1] + 1
			}
		}

		goods[a] = maxGood
	}

	for i := 0; i < q; i++ {
		fmt.Println(goods[c[i]][m[i]])
	}
}


You are given a permutation $$$p_1, p_2, \dots, p_n$$$. Then, an undirected graph is constructed in the following way: add an edge between vertices $$$i$$$, $$$j$$$ such that $$$i < j$$$ if and only if $$$p_i > p_j$$$. Your task is to count the number of connected components in this graph.Two vertices $$$u$$$ and $$$v$$$ belong to the same connected component if and only if there is at least one path along edges connecting $$$u$$$ and $$$v$$$.A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$) — the elements of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

For each test case, print one integer $$$k$$$ — the number of connected components.

NoteEach separate test case is depicted in the image below. The colored squares represent the elements of the permutation. For one permutation, each color represents some connected component. The number of distinct colors is the answer.

package main

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

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)

		arr := make([]int, n)
		for i := 0; i < n; i++ {
			var v int
			fmt.Fscan(in, &v)

			arr[i] = v - 1 - i
		}

		var c, sum int
		for i := 0; i < n; i++ {
			sum += arr[i]
			if sum == 0 {
				c++
			}
		}
		fmt.Fprintln(out, c)
	}
}

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n, v int64
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		ans := int64(0)
		for i, s := int64(1), int64(0); i <= n; i++ {
			Fscan(in, &v)
			s += v
			if s == i*(i+1)/2 {
				ans++
			}
		}
		Fprintln(out, ans)
	}
}

package main

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

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

	var T, n int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		a := make([]int, n)
		p := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			a[i]--
			p[a[i]] = i
		}
		ans, mi := 0, n
		for i := n - 1; i >= 0; i-- {
			if a[p[i]] >= 0 {
				if a[p[i]] < mi {
					ans++
				}
				for j := p[i]; j < n && a[j] >= 0; j++ {
					if a[j] < mi {
						mi = a[j]
					}
					a[j] = -1
				}
			}
		}
		Fprintln(out, ans)
	}
}

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

package main

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

type seg struct {
	l int
	r int
}

type deque[T any] struct {
	q []T
}

func (q *deque[T]) emplace(n T) {
	q.q = append(q.q, n)
}

func (q deque[T]) empty() bool {
	return len(q.q) == 0
}

func (q deque[T]) back() T {
	if q.empty() {
		panic("back() performed on empty deque")
	}
	return q.q[len(q.q)-1]
}

func (q *deque[T]) popback() T {
	if q.empty() {
		panic("popback() performed on empty deque")
	}
	res := q.back()
	q.q = q.q[:len(q.q)-1]
	return res
}

func (q deque[T]) front() T {
	if q.empty() {
		panic("front() performed on empty deque")
	}
	return q.q[0]
}

func (q *deque[T]) popfront() T {
	if q.empty() {
		panic("popfront() performed on empty deque")
	}
	res := q.front()
	q.q = q.q[1:]
	return res
}

func getdeque[T any](capacity int) deque[T] {
	return deque[T]{ make([]T, 0, capacity) }
}

func max(a, b int) int {
	return -min(-a, -b)
}

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

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)

		p := make([]int, n)
		for i := range p { fmt.Fscan(r, &p[i]) }

		stk := getdeque[seg](n)
		for _, v := range p {
			if stk.empty() {
				stk.emplace(seg{ v, v })
			} else {
				t := stk.back()
				if v > t.r {
					stk.emplace(seg{ v, v })
				} else if v < t.l {
					stk.popback()
					t.l = v
					stk.emplace(t)
				}
			}
		}

		var cnt, pl, pr int
		pl = n+1
		for ! stk.empty() {
			t := stk.popback()

			if t.r < pl {
				cnt++
			}

			pl = min(pl, t.l)
			pr = max(pr, t.r)
		}

		fmt.Fprintln(w, cnt)
	}

}

package main

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

var a [1e5 + 5]int

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b&15)
		}
		return
	}
	var t, n, top, x int
	for t = read(); t > 0; t-- {
		n = read()
		top = 0
		for i := 0; i < n; i++ {
			x = read()
			if top == 0 {
				top++
				a[top] = x
				continue
			}
			if x > a[top] {
				top++
				a[top] = x
			} else {
				tmp := a[top]
				for top > 0 && x < a[top] {
					top--
				}
				top++
				a[top] = tmp
			}
		}
		Fprintln(out, top)
	}
}

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

package main

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

/*
1
4
3 1 2 4
---
2
*/

//goland:noinspection GoUnhandledErrorResult
func solve() {
	var n int
	scanf("%d\n", &n)

	type pair struct {
		min, max int
	}

	stack := make([]pair, 0)

	for i := 0; i < n; i++ {
		var p int
		scanf("%d", &p)

		stack = append(stack, pair{p, p})

		for len(stack) > 1 {
			top1 := stack[len(stack)-1]
			top2 := stack[len(stack)-2]
			stack = stack[:len(stack)-2]

			if top1.min > top2.max {
				stack = append(stack, top2)
				stack = append(stack, top1)
				break
			} else {
				newTop := pair{min(top1.min, top2.min), max(top1.max, top2.max)}
				stack = append(stack, newTop)
			}
		}
	}
	scanf("\n")

	fmt.Fprintln(writer, len(stack))
}

//goland:noinspection GoUnhandledErrorResult
func main() {
	defer writer.Flush()

	t := 1
	scanf("%d\n", &t)

	for ; t > 0; t-- {
		solve()
	}
}

const maxUint = ^uint(0)

//goland:noinspection GoUnusedConst
const minUint = 0
const maxInt = int(maxUint >> 1)

//goland:noinspection GoUnusedConst
const minInt = -maxInt - 1

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

func scanf(f string, a ...interface{}) {
	if _, err := fmt.Fscanf(reader, f, a...); err != nil {
		panic(err)
	}
}

//goland:noinspection GoUnusedFunction
func reversed(s string) string {
	input := []rune(s)
	li := len(input)
	ret := make([]rune, li)
	for i, r := range input {
		ret[li-i-1] = r
	}
	return string(ret)
}

//goland:noinspection GoUnusedFunction
func divCeil(num, div int64) int64 {
	quot := num / div
	if num%div > 0 {
		quot++
	} else if num%div < 0 {
		quot--
	}
	return quot
}

//goland:noinspection GoUnusedFunction
func min(nums ...int) int {
	return nums[minIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func minIdx(nums ...int) int {
	minNumIdx := 0
	for i, num := range nums {
		if num < nums[minNumIdx] {
			minNumIdx = i
		}
	}

	return minNumIdx
}

//goland:noinspection GoUnusedFunction
func max(nums ...int) int {
	return nums[maxIdx(nums...)]
}

//goland:noinspection GoUnusedFunction
func maxIdx(nums ...int) int {
	maxNumIdx := 0
	for i, num := range nums {
		if num > nums[maxNumIdx] {
			maxNumIdx = i
		}
	}

	return maxNumIdx
}

//goland:noinspection GoUnusedFunction
func abs(num int) int {
	if num < 0 {
		return -num
	}
	return num
}

//goland:noinspection GoUnusedFunction
func isPrime(num int) bool {
	if num == 2 || num == 3 {
		return true
	}

	if num <= 1 || num%2 == 0 || num%3 == 0 {
		return false
	}

	for i := 5; i*i <= num; i += 6 {
		if num%i == 0 || num%(i+2) == 0 {
			return false
		}
	}

	return true
}

//goland:noinspection GoUnusedFunction
func gcd(a, b int) int {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

//goland:noinspection GoUnusedFunction
func lcm(a, b int) int {
	return abs(a) / gcd(a, b) * abs(b)
}

// Examples:
//	digitAt(1234, -1) -> panic!
//	digitAt(1234, 0) -> 4
//	digitAt(1234, 1) -> 3
//	digitAt(1234, 2) -> 2
//	digitAt(1234, 3) -> 1
//	digitAt(1234, 4) -> panic!
//goland:noinspection GoUnusedFunction
func digitAt(num, pos int) int {
	if num == 0 {
		return 0
	}

	num = abs(num)

	lenNum := int(math.Log10(float64(num))) + 1
	if pos < 0 || pos >= lenNum {
		err := fmt.Sprintf("Out of bound access for digit. Position passed %d, number %d", pos, num)
		panic(err)
	}

	rem := num % int(math.Pow10(pos+1))
	return rem / int(math.Pow10(pos))
}

//goland:noinspection GoUnusedFunction
func findSet(parent []int, v int) int {
	if v == parent[v] {
		return v
	}

	parent[v] = findSet(parent, parent[v])

	return parent[v]
}

//goland:noinspection GoUnusedFunction
func unionSets(parent []int, v, u int) {
	v = findSet(parent, v)
	u = findSet(parent, u)

	if v != u {
		parent[v] = u
	}
}

package main

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

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

	var t, n, v int
	for Fscan(in, &t); t > 0; t-- {
		Fscan(in, &n)
		stack := make([][2]int, 0, n)
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			stack = append(stack, [2]int{v, v})
			for len(stack) > 1 {
				ln := len(stack)
				if stack[ln-1][1] < stack[ln-2][0] {
					stack[ln-2][0] = max(stack[ln-1][0], stack[ln-2][0])
					stack[ln-2][1] = min(stack[ln-1][1], stack[ln-2][1])
					stack = stack[:len(stack)-1]
				} else {
					break
				}
			}
		}
		Fprintln(out, len(stack))
		// Fprintln(out, stack)
		// out.Flush()
	}
}
func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}
func min(a, b int) int {
	if a > b {
		return b
	}
	return a
}
func main() {
	CF1638C(os.Stdin, os.Stdout)
}

//nolint
package main

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

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

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

func main() {
	defer writer.Flush()

	var t, n, tp int
	scan(&t)
	for ; t > 0; t-- {
		scan(&n)
		s := new(Stack)
		for i := 0; i < n; i++ {
			scan(&tp)
			s.Push(NewEqualPair(tp))
		}
		println(s.Result())
	}
}

type Pair struct {
	max, min int
}

func NewEqualPair(a int) Pair {
	return Pair{
		max: a,
		min: a,
	}
}

type Stack struct {
	arr []*Pair
}

func (s *Stack) Push(p Pair) {
	s.arr = append(s.arr, &p)
	for len(s.arr) >= 2 {
		l := len(s.arr)
		p1 := s.arr[l-1]
		p2 := s.arr[l-2]
		if p2.max > p1.min {
			if p1.max > p2.max {
				p2.max = p1.max
			}
			if p1.min < p2.min {
				p2.min = p1.min
			}
			s.arr = s.arr[:l-1]
		} else {
			break
		}
	}
}

func (s *Stack) Result() int {
	return len(s.arr)
}

//C

package main

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

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-- {
		var n int
		fmt.Fscan(in, &n)
		nums := make([]int, n)
		var curMax int
		var cnt int
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &nums[i])
			if nums[i] == i+1 {
				if curMax == i {
					curMax = nums[i]
					cnt++
				} else {
					continue
				}
			} else if nums[i] < i+1 {
				continue
			} else {
				if curMax < i+1 {
					cnt++
				}
				if nums[i] > curMax {
					curMax = nums[i]
				}
			}
		}
		fmt.Fprintln(out, cnt)
	}
}

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var T, n, v int
	for Fscan(in, &T); T > 0; T-- {
		Fscan(in, &n)
		ans := 0
		for i, s := 1, 0; i <= n; i++ {
			Fscan(in, &v)
			s += v
			if s == i*(i+1)/2 {
				ans++
			}
		}
		Fprintln(out, ans)
	}
}

package main

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

var a [1e5 + 5]int

func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewScanner(_r)
	in.Split(bufio.ScanWords)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	read := func() (x int) {
		in.Scan()
		for _, b := range in.Bytes() {
			x = x*10 + int(b&15)
		}
		return
	}
	var t, n, top, x int
	for t = read(); t > 0; t-- {
		n = read()
		top = 0
		for i := 0; i < n; i++ {
			x = read()
			if top == 0 {
				top++
				a[top] = x
				continue
			}
			if x > a[top] {
				top++
				a[top] = x
			} else {
				tmp := a[top]
				for top > 0 && x < a[top] {
					top--
				}
				a[top] = tmp
			}
		}
		Fprintln(out, top)
	}
}

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


You are given a set of all integers from $$$l$$$ to $$$r$$$ inclusive, $$$l < r$$$, $$$(r - l + 1) \le 3 \cdot 10^5$$$ and $$$(r - l)$$$ is always odd.You want to split these numbers into exactly $$$\frac{r - l + 1}{2}$$$ pairs in such a way that for each pair $$$(i, j)$$$ the greatest common divisor of $$$i$$$ and $$$j$$$ is equal to $$$1$$$. Each number should appear in exactly one of the pairs.Print the resulting pairs or output that no solution exists. If there are multiple solutions, print any of them.

If any solution exists, print "YES" in the first line. Each of the next $$$\frac{r - l + 1}{2}$$$ lines should contain some pair of integers. GCD of numbers in each pair should be equal to $$$1$$$. All $$$(r - l + 1)$$$ numbers should be pairwise distinct and should have values from $$$l$$$ to $$$r$$$ inclusive. If there are multiple solutions, print any of them. If there exists no solution, print "NO".

package main

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

func main() {
	sc := NewScanner()
	l, r := sc.NextInt64(), sc.NextInt64()
	fmt.Println("YES")
	for ; l < r; l += 2 {
		fmt.Println(l, l+1)
	}

}

type Scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

func (s *Scanner) NextIntArray() []int {
	s.pre()
	start := s.p
	result := []int{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 0)
			result = append(result, int(v))
			start = s.p + 1
		}
	}

	return result
}
func (s *Scanner) NextInt64Array() []int64 {
	s.pre()
	start := s.p
	result := []int64{}
	for ; s.p < len(s.buf)+1; s.p++ {
		if s.p == len(s.buf) || s.buf[s.p] == ' ' {
			v, _ := strconv.ParseInt(string(s.buf[start:s.p]), 10, 64)
			result = append(result, v)
			start = s.p + 1
		}
	}
	return result
}

func (s *Scanner) NextMap() map[int]bool {
	s.pre()
	start := s.p
	mp := map[int]bool{}
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			mp[v] = true
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	mp[v] = true

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}

package main

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

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

const ISDEBUG = false

func readLine() string {
	r.Scan()
	return r.Text()
}

func readInt() int {
	r.Scan()
	x, _ := strconv.Atoi(r.Text())
	return x
}

func readUint64() uint64 {
	r.Scan()
	x, _ := strconv.ParseUint(r.Text(), 10, 64)
	return x
}

func readInts(n int) []int {
	var res []int
	for i := 0; i < n; i++ {
		if !r.Scan() {
			fmt.Println(r.Err())
		}
		x, _ := strconv.Atoi(r.Text())
		res = append(res, x)
	}
	return res
}

func B() {
	l, r := readUint64(), readUint64()
	var k uint64 = (r - l + 1) / 2

	w.WriteString("YES\n")

	var i uint64 = 0
	for i = 0; i < k; i++ {
		w.WriteString(fmt.Sprintf("%v %v\n", l+i*2, l+i*2+1))
	}
}

func main() {
	if ISDEBUG {
		fi, err := os.Open("input.txt")
		if err != nil {
			panic(err)
		}
		fo, _ := os.OpenFile("output.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
		r = bufio.NewScanner(fi)
		w = bufio.NewWriter(fo)

		defer fi.Close()
		defer fo.Close()
	} else {
		r = bufio.NewScanner(os.Stdin)
		w = bufio.NewWriter(os.Stdout)
	}
	r.Split(bufio.ScanWords)
	defer w.Flush()

	B()
}

package main

import (
	"fmt"
)

func main() {
	var l, r int64
	fmt.Scan(&l, &r)
	fmt.Println("YES")
	for l < r {
		fmt.Println(l, l+1)
		l += 2
	}
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)

func readInt() int {
	var ch byte
	var err error
	for {
		ch, err = reader.ReadByte()
		if err != nil {
			return 0
		}
		if unicode.IsDigit(rune(ch)) {
			break
		}
	}
	result := int(ch & 0xf)
	for {
		ch, err = reader.ReadByte()
		if err != nil || !unicode.IsDigit(rune(ch)) {
			return result
		}
		result = result*10 + int(ch&0xf)
	}
	return result
}

func main() {
	l, r := readInt(), readInt()
	fmt.Println("YES")
	for ; l < r; l += 2 {
		fmt.Println(l, l+1)
	}

}

package main

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

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

const ISDEBUG = false

func readLine() string {
	r.Scan()
	return r.Text()
}

func readInt() int {
	r.Scan()
	x, _ := strconv.Atoi(r.Text())
	return x
}

func readInts(n int) []int {
	var res []int
	for i := 0; i < n; i++ {
		if !r.Scan() {
			fmt.Println(r.Err())
		}
		x, _ := strconv.Atoi(r.Text())
		res = append(res, x)
	}
	return res
}

func B() {
	l, r := int64(readInt()), int64(readInt())
	var k int64 = (r - l + 1) / 2

	w.WriteString("YES\n")

	var i int64 = 0
	for i = 0; i < k; i++ {
		w.WriteString(fmt.Sprintf("%d %d\n", l+i*2, l+i*2+1))
	}
}

func main() {
	if ISDEBUG {
		fi, err := os.Open("input.txt")
		if err != nil {
			panic(err)
		}
		fo, _ := os.OpenFile("output.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
		r = bufio.NewScanner(fi)
		w = bufio.NewWriter(fo)

		defer fi.Close()
		defer fo.Close()
	} else {
		r = bufio.NewScanner(os.Stdin)
		w = bufio.NewWriter(os.Stdout)
	}
	r.Split(bufio.ScanWords)
	defer w.Flush()

	B()
}

package main

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

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

const ISDEBUG = false

func readLine() string {
	r.Scan()
	return r.Text()
}

func readInt() int {
	r.Scan()
	x, _ := strconv.Atoi(r.Text())
	return x
}

func readInts(n int) []int {
	var res []int
	for i := 0; i < n; i++ {
		if !r.Scan() {
			fmt.Println(r.Err())
		}
		x, _ := strconv.Atoi(r.Text())
		res = append(res, x)
	}
	return res
}

func B() {
	l, r := readInt(), readInt()
	k := (r - l + 1) / 2

	w.WriteString("YES\n")

	for i := 0; i < k; i++ {
		w.WriteString(fmt.Sprintf("%d %d\n", l+i*2, l+i*2+1))
	}
}

func main() {
	if ISDEBUG {
		fi, err := os.Open("input.txt")
		if err != nil {
			panic(err)
		}
		fo, _ := os.OpenFile("output.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
		r = bufio.NewScanner(fi)
		w = bufio.NewWriter(fo)

		defer fi.Close()
		defer fo.Close()
	} else {
		r = bufio.NewScanner(os.Stdin)
		w = bufio.NewWriter(os.Stdout)
	}
	r.Split(bufio.ScanWords)
	defer w.Flush()

	B()
}


Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.Public transport is not free. There are 4 types of tickets:   A ticket for one ride on some bus or trolley. It costs c1 burles;  A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles;  A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles;  A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles. Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.

The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets. The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use. The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i. The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.

Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.

NoteIn the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles.In the second sample the profitable strategy is to buy one ticket of the fourth type.In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.

package main

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

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

func main(){
	var c [4]int
	var n, m int
	r := bufio.NewScanner(os.Stdin)
	r.Split(bufio.ScanWords)
	w := bufio.NewWriter(os.Stdout)
	for i:=0; i<4; i++ {
		r.Scan()
		c[i], _ = strconv.Atoi(r.Text())
	}
	r.Scan()
	n, _ = strconv.Atoi(r.Text())
	r.Scan()
	m, _ = strconv.Atoi(r.Text())
	a := make([]int, n)
	b := make([]int, m)
	for i:=0; i<n; i++ {
		r.Scan()
		a[i], _ = strconv.Atoi(r.Text())
	}
	for i:=0; i<m; i++ {
		r.Scan()
		b[i], _ = strconv.Atoi(r.Text())
	}

	bus, tro := 0, 0

	for i:=0; i<n; i++ {
		bus += min(c[0]*a[i], c[1])
	}
	for j:=0; j<m; j++ {
		tro += min(c[0]*b[j], c[1])
	}

	if bus > c[2] {
		bus = c[2]
	}
	if tro > c[2] {
		tro = c[2]
	}
	w.WriteString(strconv.Itoa(min(bus+tro, c[3]))+"\n")
	w.Flush()

}

package main

import "fmt"

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

func main() {
    var c1,c2,c3,c4,n,m,a int
    fmt.Scan(&c1,&c2,&c3,&c4)
    fmt.Scan(&n,&m)
    tn := 0
    for i := 0; i < n; i++ {
        fmt.Scan(&a)
        tn += min(c1*a,c2)
    }
    tn = min(tn,c3)
    tm := 0
    for i := 0; i < m; i++ {
        fmt.Scan(&a)
        tm += min(c1*a,c2)
    }
    tm = min(tm,c3)
    fmt.Println(min(tn+tm,c4))
}

// 355B
package main

import (
	"fmt"
)

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

func main() {
	var n1, n2, n3, n4, n, m, a, b, A, B int
	fmt.Scan(&n1, &n2, &n3, &n4)
	fmt.Scan(&n, &m)
	for i := 0; i < n; i++ {
		fmt.Scan(&a)
		a = min(n2, a*n1)
		A += a
	}
	A = min(A, n3)
	for i := 0; i < m; i++ {
		fmt.Scan(&b)
		b = min(n2, b*n1)
		B += b
	}
	B = min(B, n3)
	fmt.Println(min(n4, A+B))
}

// 355B-mic_rizal
package main

import (
    "fmt"
)

func main() {
    var c1, c2, c3, c4, i, m, n, suma, sumb, sum int
    var a, b [1000]int
    fmt.Scan(&c1, &c2, &c3, &c4, &n, &m)
    for i = 0; i < n; i++ {
        fmt.Scan(&a[i])
    }
    for i = 0; i < m; i++ {
        fmt.Scan(&b[i])
    }

    for i = 0; i < n; i++ {
        if (a[i] * c1) > c2 {
            suma += c2
        } else {
            suma += (a[i] * c1)
        }
    }

    for i = 0; i < m; i++ {
        if (b[i] * c1) > c2 {
            sumb += c2
        } else {
            sumb += (b[i] * c1)
        }
    }

    if suma > c3 {
        suma = c3
    }

    if sumb > c3 {
        sumb = c3
    }

    if (suma + sumb) > c4 {
        sum = c4
    } else {
        sum = suma + sumb
    }

    fmt.Println(sum)

}


Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not."And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not. Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times $$$k$$$, so they got $$$k+1$$$ pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled.In this way, it's possible to achive a string abcdefg from the string f|de|abc|g using $$$3$$$ cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts.More formally, Sasha wants for the given palindrome $$$s$$$ find such minimum $$$k$$$, that you can cut this string into $$$k + 1$$$ parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string $$$s$$$. It there is no answer, then print "Impossible" (without quotes).

The first line contains one string $$$s$$$ ($$$1 \le |s| \le 5\,000$$$) — the initial name, which consists only of lowercase Latin letters. It is guaranteed that $$$s$$$ is a palindrome.

Print one integer $$$k$$$ — the minimum number of cuts needed to get a new name, or "Impossible" (without quotes).

NoteIn the first example, you can cut the string in those positions: no|l|on, and then unite them as follows on|l|no. It can be shown that there is no solution with one cut.In the second example, you can cut the string right in the middle, and swap peaces, so you get toot.In the third example, you can't make a string, that won't be equal to the initial one.In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.

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...) }

var dp [5001][5001]bool = [5001][5001]bool{}
var vis [5001][5001]bool = [5001][5001]bool{}
var s string

func isPalin(a, b int) bool {
	if b < a || b == a {
		return true
	}

	if s[a] != s[b] {
		return false
	}
	if vis[a][b] {
		return dp[a][b]
	}

	vis[a][b] = true

	dp[a][b] = isPalin(a+1, b-1)

	return dp[a][b]
}

func main() {
	defer writer.Flush()
	scanf("%s", &s)
	cnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		cnt[s[i]]++
	}
	wcnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		if i == len(s)/2 {
			continue
		}
		wcnt[s[i]]++
	}
	if len(cnt) == 1 {
		fmt.Println("Impossible")
		return
	}
	if len(s)%2 == 1 {
		for i := len(s) / 2; i > 1; i = i / 2 {
			if i%2 == 1 {
				if isPalin(0, i-1) == false {
					fmt.Println("2")
					return
				} else if len(wcnt) > 1 {
					fmt.Println("2")
					return
				} else {
					fmt.Println("Impossible")
					return
				}
			}
			if !isPalin(0, i-1) {
				fmt.Println("2")
				return
			}
		}
		fmt.Println("Impossible")
		return
	}
	for i := len(s) / 2; i > 1; i = i / 2 {
		if i%2 == 1 {
			if isPalin(0, i-1) == false {
				fmt.Println("1")
				return
			} else {
				fmt.Println("2")
				return
			}
		}
		if !isPalin(0, i-1) {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("2")

}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var s string

	scanf("%s", &s)

	arr := []byte(s)

	has := false
	for i := 1; i < len(arr)/2; i++ {
		has = has || arr[i] != arr[i-1]
	}
	if !has {
		printf("Impossible\n")
		return
	}

outer:
	for i := 1; i < len(arr); i++ {
		res := append(arr[i:], arr[:i]...)
		for j := 0; j < len(res)/2; j++ {
			if res[j] != res[len(res)-1-j] {
				continue outer
			}
		}

		for x, y := range res {
			if y != arr[x] {
				printf("1\n")
				return
			}
		}
	}
	printf("2\n")
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	s := ""
	fmt.Fscanf(in, "%s\n", &s)
	n := len(s)
	//fmt.Println(s)
	if len(s)%2 == 0 {
		chMap := make(map[byte]bool)
		for i := 0; i < n; i++ {
			chMap[byte(s[i])] = true
		}
		if len(chMap) < 2 {
			fmt.Fprintf(out, "Impossible\n")
			return
		}
	} else {
		chMap := make(map[byte]bool)
		for i := 0; i < n; i++ {
			if i != n/2 {
				chMap[byte(s[i])] = true
			}
		}
		if len(chMap) < 2 {
			fmt.Fprintf(out, "Impossible\n")
			return
		}
	}
	canOne := false
	for i := 1; i < n; i++ {
		newS := s[i:] + s[:i]
		if pali(newS) && newS != s {
			canOne = true
			break
		}
	}
	if canOne {
		fmt.Fprintf(out, "1\n")
	} else {
		fmt.Fprintf(out, "2\n")
	}
	return
}

func pali(s string) bool {
	n := len(s)
	for i := 0; i < n/2; i++ {
		if s[i] != s[n-1-i] {
			return false
		}
	}
	return true
}

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...) }

var dp [5001][5001]bool = [5001][5001]bool{}
var vis [5001][5001]bool = [5001][5001]bool{}
var s string

func isPalin(a, b int) bool {
	if b < a || b == a {
		return true
	}

	if s[a] != s[b] {
		return false
	}
	if vis[a][b] {
		return dp[a][b]
	}

	vis[a][b] = true

	dp[a][b] = isPalin(a+1, b-1)

	return dp[a][b]
}

func main() {
	defer writer.Flush()
	scanf("%s", &s)
	cnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		cnt[s[i]]++
	}
	if len(cnt) == 1 {
		fmt.Println("Impossible")
		return
	}
	if len(s)%2 == 1 {
		for i := len(s) / 2; i > 1; i = i / 2 {
			if i%2 == 1 {
				if isPalin(0, i-1) == false {
					fmt.Println("2")
					return
				} else if len(cnt) > 2 {
					fmt.Println("4")
					return
				} else {
					fmt.Println("Impossible")
					return
				}
			}
			if !isPalin(0, i-1) {
				fmt.Println("2")
				return
			}
		}
		fmt.Println("Impossible")
		return
	}
	for i := len(s) / 2; i > 1; i = i / 2 {
		if i%2 == 1 {
			if isPalin(0, i-1) == false {
				fmt.Println("1")
				return
			} else {
				fmt.Println("2")
				return
			}
		}
		if !isPalin(0, i-1) {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("2")

}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var s string

	scanf("%s", &s)

	arr := []byte(s)

	has := false
	for i, v := range arr {
		if i != 0 && v != arr[i-1] {
			has = true
		}
	}
	if !has {
		printf("Impossible\n")
		return
	}

	printf("%d\n", len(arr)%2+1)
}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var s string

	scanf("%s", &s)

	arr := []byte(s)

	has := false
	for i := 1; i < len(arr)/2; i++ {
		has = has || arr[i] != arr[i-1]
	}
	if !has {
		printf("Impossible\n")
		return
	}

	if len(arr)%2 == 1 {
		printf("2\n")
		return
	}

	printf("%d\n", len(arr)%2+1)
}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var s string

	scanf("%s", &s)

	arr := []byte(s)

	has := false
	for i, v := range arr {
		if i != 0 && (len(arr)%2 == 0 || i-1 == (len(arr)+1)/2) && v != arr[i-1] {
			has = true
		}
	}
	if !has {
		printf("Impossible\n")
		return
	}

	printf("%d\n", len(arr)%2+1)
}

package main

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

var in *bufio.Reader
var out *bufio.Writer

func printf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(out, x, a...)
}

func scanf(x string, a ...interface{}) (n int, err error) {
	return fmt.Fscanf(in, x, a...)
}

func main() {
	out = bufio.NewWriterSize(os.Stdout, 8192)
	defer out.Flush()
	if false {
		file, err := os.Open("in.txt")
		if err != nil {
			log.Fatal(err)
		}
		in = bufio.NewReaderSize(file, 8192)
	} else {
		in = bufio.NewReaderSize(os.Stdin, 8192)
	}

	var s string

	scanf("%s", &s)

	arr := []byte(s)

	has := false
	for i := 1; i < len(arr)/2; i++ {
		has = has || arr[i] != arr[i-1]
	}
	if !has {
		printf("Impossible\n")
		return
	}

outer:
	for i := 1; i < len(arr); i++ {
		res := append(arr[i:], arr[:i]...)
		for j := 0; j < len(res)/2; j++ {
			if res[j] != res[len(res)-1-j] {
				continue outer
			}
		}

		printf("1\n")
		return
	}
	printf("2\n")
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	s := ""
	fmt.Fscanf(in, "%s\n", &s)
	n := len(s)
	//fmt.Println(s)
	if len(s)%2 == 0 {
		chMap := make(map[byte]bool)
		for i := 0; i < n; i++ {
			chMap[byte(s[i])] = true
		}
		if len(chMap) < 2 {
			fmt.Fprintf(out, "Impossible\n")
			return
		}
	} else {
		chMap := make(map[byte]bool)
		for i := 0; i < n; i++ {
			if i != n/2 {
				chMap[byte(s[i])] = true
			}
		}
		if len(chMap) < 2 {
			fmt.Fprintf(out, "Impossible\n")
			return
		}
	}
	canOne := false
	for i := 1; i < n; i++ {
		newS := s[i:] + s[:i]
		if pali(newS) {
			canOne = true
			break
		}
	}
	if canOne {
		fmt.Fprintf(out, "1\n")
	} else {
		fmt.Fprintf(out, "2\n")
	}
	return
}

func pali(s string) bool {
	n := len(s)
	for i := 0; i < n/2; i++ {
		if s[i] != s[n-1-i] {
			return false
		}
	}
	return true
}

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...) }

var dp [5001][5001]bool = [5001][5001]bool{}
var vis [5001][5001]bool = [5001][5001]bool{}
var s string

func isPalin(a, b int) bool {
	if b < a || b == a {
		return true
	}

	if s[a] != s[b] {
		return false
	}
	if vis[a][b] {
		return dp[a][b]
	}

	vis[a][b] = true

	dp[a][b] = isPalin(a+1, b-1)

	return dp[a][b]
}

func main() {
	defer writer.Flush()
	scanf("%s", &s)
	cnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		cnt[s[i]]++
	}
	if len(cnt) == 1 {
		fmt.Println("Impossible")
		return
	}
	if len(s)%2 == 1 {
		fmt.Println("2")
		return
	}
	for i := len(s); i > 1; i = i / 2 {
		if i%2 == 1 {
			if isPalin(0, i-1) == false {
				fmt.Println("1")
				return
			} else {
				fmt.Println("2")
				return
			}
		}
		if !isPalin(0, i-1) {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("2")

}

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...) }

var dp [5001][5001]bool = [5001][5001]bool{}
var vis [5001][5001]bool = [5001][5001]bool{}
var s string

func isPalin(a, b int) bool {
	if b < a || b == a {
		return true
	}

	if s[a] != s[b] {
		return false
	}
	if vis[a][b] {
		return dp[a][b]
	}

	vis[a][b] = true

	dp[a][b] = isPalin(a+1, b-1)

	return dp[a][b]
}

func main() {
	defer writer.Flush()
	scanf("%s", &s)
	cnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		cnt[s[i]]++
	}
	if len(cnt) == 1 {
		fmt.Println("Impossible")
		return
	}
	if len(s)%2 == 1 {
		fmt.Println("2")
		return
	}
	for i := len(s); i > 1; i = i / 2 {
		if i%2 == 1 {
			if isPalin(0, i-1) == false {
				fmt.Println("1")
				return
			} else {
				fmt.Println("2")
				return
			}
		}
		if !isPalin(0, i-1) {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("Impossible")

}

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...) }

var dp [5001][5001]bool = [5001][5001]bool{}
var vis [5001][5001]bool = [5001][5001]bool{}
var s string

func isPalin(a, b int) bool {
	if b < a || b == a {
		return true
	}

	if s[a] != s[b] {
		return false
	}
	if vis[a][b] {
		return dp[a][b]
	}

	vis[a][b] = true

	dp[a][b] = isPalin(a+1, b-1)

	return dp[a][b]
}

func main() {
	defer writer.Flush()
	scanf("%s", &s)
	cnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		cnt[s[i]]++
	}
	if len(cnt) == 1 {
		fmt.Println("Impossible")
		return
	}
	if len(s)%2 == 1 {
		for i := len(s) / 2; i > 1; i = i / 2 {
			if i%2 == 1 {
				if isPalin(0, i-1) == false {
					fmt.Println("2")
					return
				} else if len(cnt) > 2 {
					fmt.Println("4")
					return
				} else {
					fmt.Println("2")
					return
				}
			}
			if !isPalin(0, i-1) {
				fmt.Println("2")
				return
			}
		}
		fmt.Println("Impossible")
		return
	}
	for i := len(s) / 2; i > 1; i = i / 2 {
		if i%2 == 1 {
			if isPalin(0, i-1) == false {
				fmt.Println("1")
				return
			} else {
				fmt.Println("2")
				return
			}
		}
		if !isPalin(0, i-1) {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("2")

}

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...) }

var dp [5001][5001]bool = [5001][5001]bool{}
var vis [5001][5001]bool = [5001][5001]bool{}
var s string

func isPalin(a, b int) bool {
	if b < a || b == a {
		return true
	}

	if s[a] != s[b] {
		return false
	}
	if vis[a][b] {
		return dp[a][b]
	}

	vis[a][b] = true

	dp[a][b] = isPalin(a+1, b-1)

	return dp[a][b]
}

func main() {
	defer writer.Flush()
	scanf("%s", &s)
	cnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		cnt[s[i]]++
	}
	if len(cnt) == 1 {
		fmt.Println("Impossible")
		return
	}
	if len(s)%2 == 1 {
		for i := len(s) / 2; i > 1; i = i / 2 {
			if i%2 == 1 {
				if isPalin(0, i-1) == false {
					fmt.Println("2")
					return
				} else if len(cnt) > 2 {
					fmt.Println("4")
					return
				} else {
					fmt.Println("Impossible")
					return
				}
			}
			if !isPalin(0, i-1) {
				fmt.Println("2")
				return
			}
		}
		fmt.Println("2")
		return
	}
	for i := len(s) / 2; i > 1; i = i / 2 {
		if i%2 == 1 {
			if isPalin(0, i-1) == false {
				fmt.Println("1")
				return
			} else {
				fmt.Println("2")
				return
			}
		}
		if !isPalin(0, i-1) {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("2")

}

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...) }

var dp [5001][5001]bool = [5001][5001]bool{}
var vis [5001][5001]bool = [5001][5001]bool{}
var s string

func isPalin(a, b int) bool {
	if b < a || b == a {
		return true
	}

	if s[a] != s[b] {
		return false
	}
	if vis[a][b] {
		return dp[a][b]
	}

	vis[a][b] = true

	dp[a][b] = isPalin(a+1, b-1)

	return dp[a][b]
}

func main() {
	defer writer.Flush()
	scanf("%s", &s)
	cnt := make(map[byte]int)
	for i := 0; i < len(s); i++ {
		cnt[s[i]]++
	}
	if len(cnt) == 1 {
		fmt.Println("Impossible")
		return
	}
	if len(s)%2 == 1 {
		for i := len(s) / 2; i > 1; i = i / 2 {
			if i%2 == 1 {
				if isPalin(0, i-1) == false {
					fmt.Println("2")
					return
				} else if len(cnt) > 2 {
					fmt.Println("4")
					return
				} else {
					fmt.Println("Impossible")
					return
				}
			}
			if !isPalin(0, i-1) {
				fmt.Println("2")
				return
			}
		}
		fmt.Println("Impossible")
		return
	}
	for i := len(s) / 2; i > 1; i = i / 2 {
		if i%2 == 1 {
			if isPalin(0, i-1) == false {
				fmt.Println("1")
				return
			} else {
				fmt.Println("2")
				return
			}
		}
		if !isPalin(0, i-1) {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("Impossible")

}


You are given two strings $$$A$$$ and $$$B$$$ representing essays of two students who are suspected cheaters. For any two strings $$$C$$$, $$$D$$$ we define their similarity score $$$S(C,D)$$$ as $$$4\cdot LCS(C,D) - |C| - |D|$$$, where $$$LCS(C,D)$$$ denotes the length of the Longest Common Subsequence of strings $$$C$$$ and $$$D$$$. You believe that only some part of the essays could have been copied, therefore you're interested in their substrings.Calculate the maximal similarity score over all pairs of substrings. More formally, output maximal $$$S(C, D)$$$ over all pairs $$$(C, D)$$$, where $$$C$$$ is some substring of $$$A$$$, and $$$D$$$ is some substring of $$$B$$$. If $$$X$$$ is a string, $$$|X|$$$ denotes its length.A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters. Pay attention to the difference between the substring and subsequence, as they both appear in the problem statement. You may wish to read the Wikipedia page about the Longest Common Subsequence problem.

The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 5000$$$) — lengths of the two strings $$$A$$$ and $$$B$$$.  The second line contains a string consisting of $$$n$$$ lowercase Latin letters — string $$$A$$$. The third line contains a string consisting of $$$m$$$ lowercase Latin letters — string $$$B$$$.

Output maximal $$$S(C, D)$$$ over all pairs $$$(C, D)$$$, where $$$C$$$ is some substring of $$$A$$$, and $$$D$$$ is some substring of $$$B$$$.

NoteFor the first case:abb from the first string and abab from the second string have LCS equal to abb.The result is $$$S(abb, abab) = (4 \cdot |abb|$$$) - $$$|abb|$$$ - $$$|abab|$$$ = $$$4 \cdot 3 - 3 - 4 = 5$$$.

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	n, m := sint64(), sint64()
	a, b := sstring(), sstring()
	printf("%d\n", sol(n, m, a, b))
}

func sol(n, m int64, a, b string) int64 {
	dp := make([][]int64, n+1)
	for i := range dp {
		dp[i] = make([]int64, m+1)
	}
	maxSoFar := int64(0)
	for i := 1; i < len(dp); i++ {
		for j := 1; j < len(dp[i]); j++ {
			if a[i-1] == b[j-1] {
				dp[i][j] = dp[i-1][j-1] + 2
				maxSoFar = max(dp[i][j], max(maxSoFar, dp[i][j]))
			} else {
				dp[i][j] = max(dp[i][j], max(dp[i-1][j]-1, dp[i][j-1]-1))
			}
		}
	}
	return maxSoFar
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func run(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	var n, m int
	var s1, s2 []byte
	Fscan(in, &n, &m, &s1, &s2)
	dp := make([][]int, n+1)
	for i := range dp {
		dp[i] = make([]int, m+1)
	}
	for i, b1 := range s1 {
		for j, b2 := range s2 {
			if b1 == b2 {
				dp[i+1][j+1] = dp[i][j] + 2
			} else {
				dp[i+1][j+1] = max(0, max(dp[i][j+1], dp[i+1][j])-1)
			}
		}
	}
	ans := 0
	for i, row := range dp {
		for j, v := range row {
			_, _ = i, j
			ans = max(ans, v)
		}
	}
	Fprintln(out, ans)
}

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

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	printf("%d\n", sol(sint64(), sint64(), sstring(), sstring()))
}

type Res struct {
	LCS int64
	C   string
	D   string
}

func (r Res) Score() int64 {
	return r.LCS*4 - int64(len(r.C)) - int64(len(r.D))
}

func (r Res) Len() int64 {
	return int64(len(r.C) + len(r.D))
}

func sol(n, m int64, c, d string) int64 {
	table := make([][]Res, n+1)
	for i := range table {
		table[i] = make([]Res, m+1)
		for j := range table[i] {
			table[i][j] = Res{
				LCS: 0,
				C:   "",
				D:   "",
			}
		}
	}
	maxSoFar := int64(0)
	for i := 1; i < len(table); i++ {
		for j := 1; j < len(table[i]); j++ {
			if c[i-1] == d[j-1] {
				table[i][j] = Res{
					LCS: table[i-1][j-1].LCS + 1,
					C:   table[i-1][j-1].C + c[i-1:i],
					D:   table[i-1][j-1].D + d[j-1:j],
				}
			} else {
				if table[i-1][j].Len() <= table[i][j-1].Len() {
					table[i][j] = Res{
						LCS: table[i-1][j].LCS,
						C:   table[i-1][j].C + c[i-1:i],
						D:   table[i-1][j].D,
					}
				} else {
					table[i][j] = Res{
						LCS: table[i][j-1].LCS,
						C:   table[i][j-1].C,
						D:   table[i][j-1].D,
					}
				}
			}
			maxSoFar = max(maxSoFar, table[i][j].Score())
		}
	}
	return maxSoFar
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	printf("%d\n", sol(sint64(), sint64(), sstring(), sstring()))
}

type Res struct {
	LCS int64
	C   string
	D   string
}

func (r Res) Score() int64 {
	return r.LCS*4 - int64(len(r.C)) - int64(len(r.D))
}

func (r Res) Len() int64 {
	return int64(len(r.C) + len(r.D))
}

func sol(n, m int64, c, d string) int64 {
	table := make([][]Res, n+1)
	for i := range table {
		table[i] = make([]Res, m+1)
	}
	maxSoFar := int64(0)
	for i := 1; i < len(table); i++ {
		for j := 1; j < len(table[i]); j++ {
			if c[i-1] == d[j-1] {
				table[i][j] = Res{
					LCS: table[i-1][j-1].LCS + 1,
					C:   table[i-1][j-1].C + c[i-1:i],
					D:   table[i-1][j-1].D + d[j-1:j],
				}
			} else {
				if table[i-1][j].LCS > table[i][j-1].LCS {
					table[i][j] = Res{
						LCS: table[i-1][j].LCS,
						C:   table[i-1][j].C + c[i-1:i],
						D:   table[i-1][j].D,
					}
				} else if table[i][j-1].LCS > table[i-1][j].LCS {
					table[i][j] = Res{
						LCS: table[i][j-1].LCS,
						C:   table[i][j-1].C,
						D:   table[i][j-1].D + d[j-1:j],
					}
				} else {
					if table[i-1][j].Len() <= table[i][j-1].Len() {
						table[i][j] = Res{
							LCS: table[i-1][j].LCS,
							C:   table[i-1][j].C + c[i-1:i],
							D:   table[i-1][j].D,
						}
					} else {
						table[i][j] = Res{
							LCS: table[i][j-1].LCS,
							C:   table[i][j-1].C,
							D:   table[i][j-1].D + d[j-1:j],
						}
					}
				}
			}
			maxSoFar = max(maxSoFar, table[i][j].Score())
		}
	}
	return maxSoFar
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	printf("%d\n", sol(sint64(), sint64(), sstring(), sstring()))
}

type Res struct {
	LCS int64
	C   string
	D   string
}

func (r Res) Score() int64 {
	return r.LCS*4 - int64(len(r.C)) - int64(len(r.D))
}

func sol(n, m int64, a, b string) int64 {
	table := make([][]Res, n+1)
	for i := range table {
		table[i] = make([]Res, m+1)
	}
	maxSoFar := int64(0)
	for i := 1; i < len(table); i++ {
		for j := 1; j < len(table[i]); j++ {
			if a[i-1] == b[j-1] {
				table[i][j] = Res{
					LCS: table[i-1][j-1].LCS + 1,
					C:   table[i-1][j-1].C + a[i-1:i],
					D:   table[i-1][j-1].D + b[j-1:j],
				}
			} else {
				if table[i-1][j].LCS > table[i][j-1].LCS {
					table[i][j] = Res{
						LCS: table[i-1][j].LCS,
						C:   table[i-1][j].C + a[i-1:i],
						D:   table[i-1][j].D,
					}
				} else if table[i][j-1].LCS > table[i-1][j].LCS {
					table[i][j] = Res{
						LCS: table[i][j-1].LCS,
						C:   table[i][j-1].C,
						D:   table[i][j-1].D + b[j-1:j],
					}
				} else { // same LCS, take greater score
					if table[i-1][j].Score() >= table[i][j-1].Score() {
						table[i][j] = Res{
							LCS: table[i-1][j].LCS,
							C:   table[i-1][j].C + a[i-1:i],
							D:   table[i-1][j].D,
						}
					} else {
						table[i][j] = Res{
							LCS: table[i][j-1].LCS,
							C:   table[i][j-1].C,
							D:   table[i][j-1].D + b[j-1:j],
						}
					}
				}
			}
			maxSoFar = max(maxSoFar, table[i][j].Score())
		}
	}
	// for _, t := range table {
	// 	printf("%v\n", t)
	// }
	return maxSoFar
}

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func main() {
	defer writer.Flush()
	n, m := sint64(), sint64()
	a, b := sstring(), sstring()
	printf("%d\n", sol(n, m, a, b))
}

type Res struct {
	LCS int64
	C   string
	D   string
}

func (r Res) Score() int64 {
	return 4*r.LCS - int64(len(r.C)) - int64(len(r.D))
}

func sol(n, m int64, a, b string) int64 {
	dp := make([][]Res, n+1)
	for i := range dp {
		dp[i] = make([]Res, m+1)
	}
	maxSoFar := int64(0)
	for i := 1; i < len(dp); i++ {
		for j := 1; j < len(dp[i]); j++ {
			if a[i-1] == b[j-1] {
				dp[i][j] = Res{
					LCS: dp[i-1][j-1].LCS + 1,
					C:   dp[i-1][j-1].C + string(a[i-1]),
					D:   dp[i-1][j-1].D + string(b[j-1]),
				}
				maxSoFar = max(maxSoFar, dp[i][j].Score())
			} else {
				// look up or left
				if dp[i-1][j].LCS == dp[i][j-1].LCS {
					if dp[i-1][j].Score() > dp[i][j-1].Score() {
						dp[i][j] = Res{
							LCS: dp[i-1][j].LCS,
							C:   dp[i-1][j].C + string(a[i-1]),
							D:   dp[i-1][j].D,
						}
					} else {
						dp[i][j] = Res{
							LCS: dp[i][j-1].LCS,
							C:   dp[i][j-1].C,
							D:   dp[i][j-1].D + string(b[j-1]),
						}
					}
				} else if dp[i-1][j].LCS > dp[i][j-1].LCS {
					dp[i][j] = Res{
						LCS: dp[i-1][j].LCS,
						C:   dp[i-1][j].C + string(a[i-1]),
						D:   dp[i-1][j].D,
					}
				} else if dp[i][j-1].LCS > dp[i-1][j].LCS {
					dp[i][j] = Res{
						LCS: dp[i][j-1].LCS,
						C:   dp[i][j-1].C,
						D:   dp[i][j-1].D + string(b[j-1]),
					}
				}
			}
		}
	}
	return maxSoFar
}

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

package main

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

const (
	SCANNER_BUF_SIZE = 2 << 20

	INF = int64(math.MaxInt64)
	MOD = 1e9 + 7
)

var (
	writer  *bufio.Writer
	scanner *bufio.Scanner
)

func init() {
	writer = bufio.NewWriter(os.Stdout)
	scanner = bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, SCANNER_BUF_SIZE), SCANNER_BUF_SIZE)
}

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

func sint64() int64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := int64(0)
	if bb[0] == '-' {
		for _, b := range bb[1:] {
			i *= 10
			i -= int64(b - '0')
		}
		return i
	}
	for _, b := range bb {
		i *= 10
		i += int64(b - '0')
	}
	return i
}

func suint64() uint64 {
	scanner.Scan()
	bb := scanner.Bytes()
	i := uint64(0)
	for _, b := range bb {
		i *= 10
		i += uint64(b - '0')
	}
	return i
}

func sstring() string {
	scanner.Scan()
	return scanner.Text()
}

func gcd(a, b int64) int64 {
	for b != 0 {
		a, b = b, a%b
	}
	return a
}

func lcm(a, b int64) int64 {
	return a * b / gcd(a, b)
}

func abs(a int64) int64 {
	if a < 0 {
		return a * -1
	}
	return a
}

func min(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r < a {
			a = r
		}
	}
	return a
}

func max(a int64, rest ...int64) int64 {
	for _, r := range rest {
		if r > a {
			a = r
		}
	}
	return a
}

func main() {
	defer writer.Flush()
	printf("%d\n", sol(sint64(), sint64(), sstring(), sstring()))
}

type Res struct {
	LCS int64
	C   string
	D   string
}

func (r Res) Score() int64 {
	return r.LCS*4 - int64(len(r.C)) - int64(len(r.D))
}

func (r Res) Len() int64 {
	return int64(len(r.C) + len(r.D))
}

func sol(n, m int64, c, d string) int64 {
	table := make([][]Res, n+1)
	for i := range table {
		table[i] = make([]Res, m+1)
	}
	maxSoFar := int64(0)
	for i := 1; i < len(table); i++ {
		for j := 1; j < len(table[i]); j++ {
			if c[i-1] == d[j-1] {
				table[i][j] = Res{
					LCS: table[i-1][j-1].LCS + 1,
					C:   table[i-1][j-1].C + c[i-1:i],
					D:   table[i-1][j-1].D + d[j-1:j],
				}
			} else {
				if table[i-1][j].Len() <= table[i][j-1].Len() {
					table[i][j] = Res{
						LCS: table[i-1][j].LCS,
						C:   table[i-1][j].C + c[i-1:i],
						D:   table[i-1][j].D,
					}
				} else {
					table[i][j] = Res{
						LCS: table[i][j-1].LCS,
						C:   table[i][j-1].C,
						D:   table[i][j-1].D + d[j-1:j],
					}
				}
			}
			maxSoFar = max(maxSoFar, table[i][j].Score())
		}
	}
	return maxSoFar
}


You are given a tree consisting of $$$n$$$ vertices. A number is written on each vertex; the number on vertex $$$i$$$ is equal to $$$a_i$$$.Let's denote the function $$$g(x, y)$$$ as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex $$$x$$$ to vertex $$$y$$$ (including these two vertices). Also let's denote $$$dist(x, y)$$$ as the number of vertices on the simple path between vertices $$$x$$$ and $$$y$$$, including the endpoints. $$$dist(x, x) = 1$$$ for every vertex $$$x$$$.Your task is calculate the maximum value of $$$dist(x, y)$$$ among such pairs of vertices that $$$g(x, y) > 1$$$.

The first line contains one integer $$$n$$$ — the number of vertices $$$(1 \le n \le 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 \le a_i \le 2 \cdot 10^5)$$$ — the numbers written on vertices. Then $$$n - 1$$$ lines follow, each containing two integers $$$x$$$ and $$$y$$$ $$$(1 \le x, y \le n, x \ne y)$$$ denoting an edge connecting vertex $$$x$$$ with vertex $$$y$$$. It is guaranteed that these edges form a tree.

If there is no pair of vertices $$$x, y$$$ such that $$$g(x, y) > 1$$$, print $$$0$$$. Otherwise print the maximum value of $$$dist(x, y)$$$ among such pairs.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1101D(_r io.Reader, _w io.Writer) {
	const mx int = 2e5
	divisors := [mx + 1][]int{}
	for i := 2; i <= mx; i++ {
		if len(divisors[i]) == 0 {
			for j := i; j <= mx; j += i {
				divisors[j] = append(divisors[j], i)
			}
		}
	}

	var n, v, w, ans int
	in := bufio.NewReader(_r)
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	var f func(v, fa int) map[int][2]int
	f = func(v, fa int) map[int][2]int {
		res := map[int][2]int{}
		for _, p := range divisors[a[v]] {
			res[p] = [2]int{}
		}
		for _, w := range g[v] {
			if w != fa {
				sub := f(w, v)
				for p, dis := range res {
					d := sub[p][0]
					if d <= dis[1] {
						continue
					}
					if d > dis[0] {
						dis[0], d = d, dis[0]
					}
					dis[1] = d
					res[p] = dis
				}
			}
		}
		for p, dis := range res {
			if v := dis[0] + dis[1] + 1; v > ans {
				ans = v
			}
			dis[0]++
			res[p] = dis
		}
		return res
	}
	f(0, -1)
	Fprint(_w, ans)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1101D(_r io.Reader, _w io.Writer) {
	const mx int = 2e5
	divisors := [mx + 1][]int{}
	for i := 2; i <= mx; i++ {
		if len(divisors[i]) == 0 {
			for j := i; j <= mx; j += i {
				divisors[j] = append(divisors[j], i)
			}
		}
	}

	var n, v, w, ans int
	in := bufio.NewReader(_r)
	Fscan(in, &n)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
	}
	g := make([][]int, n)
	for i := 1; i < n; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		g[v] = append(g[v], w)
		g[w] = append(g[w], v)
	}

	// 特殊: a[v]==1
	var f func(v, fa int) map[int][2]int
	f = func(v, fa int) map[int][2]int {
		res := map[int][2]int{}
		for _, d := range divisors[a[v]] {
			res[d] = [2]int{}
		}
		for _, w := range g[v] {
			if w != fa {
				sub := f(w, v, )
				for p, dis := range res {
					d := sub[p][0]
					if d <= dis[1] {
						continue
					}
					if d > dis[0] {
						dis[0], d = d, dis[0]
					}
					dis[1] = d
					res[p] = dis
				}
			}
		}
		for p, dis := range res {
			if v := dis[0] + dis[1] + 1; v > ans {
				ans = v
			}
			dis[0]++
			res[p] = dis
		}
		return res
	}
	f(0, -1)
	Fprint(_w, ans)
}

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


Flatland has recently introduced a new type of an eye check for the driver's licence. The check goes like that: there is a plane with mannequins standing on it. You should tell the value of the minimum angle with the vertex at the origin of coordinates and with all mannequins standing inside or on the boarder of this angle. As you spend lots of time "glued to the screen", your vision is impaired. So you have to write a program that will pass the check for you.

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of mannequins. Next n lines contain two space-separated integers each: xi, yi (|xi|, |yi| ≤ 1000) — the coordinates of the i-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane.

Print a single real number — the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10 - 6.

NoteSolution for the first sample test is shown below:   Solution for the second sample test is shown below:   Solution for the third sample test is shown below:   Solution for the fourth sample test is shown below:

package main

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

func Sol257C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, x, y int
	Fscan(in, &n)
	if n == 1 {
		Fprintln(out, 0)
		return
	}

	degs := make([]float64, n*2)
	for i := 0; i < n; i++ {
		Fscan(in, &x, &y)
		deg := math.Atan2(float64(y), float64(x)) * 180.0 / math.Pi
		if deg < 0 {
			deg = 360.0 + deg
		}
		degs[i] = deg
		degs[n+i] = 360.0 + deg
	}
	sort.Float64s(degs)

	minAngle := 360.0
	for i := 0; i < n; i++ {
		angle := degs[n-1+i] - degs[i]
		minAngle = math.Min(minAngle, angle)
	}
	Fprintln(out, minAngle)
}

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

package main

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

func minF(a, b float64) float64 {
	if a <= b {
		return a
	}
	return b
}

func maxF(a, b float64) float64 {
	if a >= b {
		return a
	}
	return b
}

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

	var n, x, y int
	Fscan(in, &n)
	if n == 1 {
		Fprintln(out, 0)
		return
	}

	degs := make([]float64, n*2)
	for i := 0; i < n; i++ {
		Fscan(in, &x, &y)
		deg := math.Atan2(float64(y), float64(x)) * 180.0 / math.Pi
		if deg < 0 {
			deg = 360.0 + deg
		}
		degs[i] = deg
		degs[i+n] = 360.0 + deg
	}
	sort.Float64s(degs)

	minAngle := 360.0
	for i := 0; i < n; i++ {
		angle := degs[n-1+i] - degs[i]
		minAngle = minF(minAngle, angle)
	}
	Fprintln(out, minAngle)
}

package main

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

// https://codeforces.com/contest/257/problem/C

func minF(a, b float64) float64 {
	if a <= b {
		return a
	}
	return b
}

func Sol257C(reader io.Reader, writer io.Writer) {
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, x, y int
	Fscan(in, &n)
	if n == 1 {
		Fprintln(out, 0)
		return
	}

	degs := make([]float64, n*2)
	for i := 0; i < n; i++ {
		Fscan(in, &x, &y)
		deg := math.Atan2(float64(y), float64(x)) * 180.0 / math.Pi
		if deg < 0 {
			deg = 360.0 + deg
		}
		degs[i] = deg
		degs[i+n] = 360.0 + deg
	}
	sort.Float64s(degs)

	minAngle := 360.0
	for i := 0; i < n; i++ {
		angle := degs[n-1+i] - degs[i]
		minAngle = minF(minAngle, angle)
	}
	Fprintln(out, minAngle)
}

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