You are given a directed graph consisting of $$$n$$$ vertices. Each directed edge (or arc) labeled with a single character. Initially, the graph is empty.You should process $$$m$$$ queries with it. Each query is one of three types:   "$$$+$$$ $$$u$$$ $$$v$$$ $$$c$$$" — add arc from $$$u$$$ to $$$v$$$ with label $$$c$$$. It's guaranteed that there is no arc $$$(u, v)$$$ in the graph at this moment;  "$$$-$$$ $$$u$$$ $$$v$$$" — erase arc from $$$u$$$ to $$$v$$$. It's guaranteed that the graph contains arc $$$(u, v)$$$ at this moment;  "$$$?$$$ $$$k$$$" — find the sequence of $$$k$$$ vertices $$$v_1, v_2, \dots, v_k$$$ such that there exist both routes $$$v_1 \to v_2 \to \dots \to v_k$$$ and $$$v_k \to v_{k - 1} \to \dots \to v_1$$$ and if you write down characters along both routes you'll get the same string. You can visit the same vertices any number of times.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 2 \cdot 10^5$$$; $$$1 \le m \le 2 \cdot 10^5$$$) — the number of vertices in the graph and the number of queries. The next $$$m$$$ lines contain queries — one per line. Each query is one of three types:    "$$$+$$$ $$$u$$$ $$$v$$$ $$$c$$$" ($$$1 \le u, v \le n$$$; $$$u \neq v$$$; $$$c$$$ is a lowercase Latin letter);  "$$$-$$$ $$$u$$$ $$$v$$$" ($$$1 \le u, v \le n$$$; $$$u \neq v$$$);  "$$$?$$$ $$$k$$$" ($$$2 \le k \le 10^5$$$).  It's guaranteed that you don't add multiple edges and erase only existing edges. Also, there is at least one query of the third type.

For each query of the third type, print YES if there exist the sequence $$$v_1, v_2, \dots, v_k$$$ described above, or NO otherwise.

NoteIn the first query of the third type $$$k = 3$$$, we can, for example, choose a sequence $$$[1, 2, 3]$$$, since $$$1 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3$$$ and $$$3 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 1$$$.In the second query of the third type $$$k = 2$$$, and we can't find sequence $$$p_1, p_2$$$ such that arcs $$$(p_1, p_2)$$$ and $$$(p_2, p_1)$$$ have the same characters.In the third query of the third type, we can, for example, choose a sequence $$$[1, 2, 3, 2, 1]$$$, where $$$1 \xrightarrow{\text{a}} 2 \xrightarrow{\text{b}} 3 \xrightarrow{\text{d}} 2 \xrightarrow{\text{c}} 1$$$.

package main

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

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

	m := map[[2]int]byte{}
	var q, v, w, same, bi int
	var s string
	for Fscan(in, &q, &q); q > 0; q-- {
		if Fscan(in, &s, &v); s[0] == '+' {
			Fscan(in, &w, &s)
			m[[2]int{v, w}] = s[0]
			if c := m[[2]int{w, v}]; c > 0 {
				if c == s[0] {
					same++
				}
				bi++
			}
		} else if s[0] == '-' {
			Fscan(in, &w)
			x := m[[2]int{v, w}]
			delete(m, [2]int{v, w})
			if y := m[[2]int{w, v}]; y > 0 {
				if y == x {
					same--
				}
				bi--
			}
		} else {
			if v&1 > 0 && bi > 0 || v&1 == 0 && same > 0 {
				Fprintln(out, "YES")
			} else {
				Fprintln(out, "NO")
			}
		}
	}
}
func main() { CF1494E(os.Stdin, os.Stdout) }

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()

	m := map[[2]int]string{}
	var q, v, w, eq, bi int
	var s string
	for Fscan(in, &q, &q); q > 0; q-- {
		if Fscan(in, &s, &v); s == "+" {
			Fscan(in, &w, &s)
			if t := m[[2]int{w, v}]; t != "" {
				if t == s {
					eq++
				}
				bi++
			}
			m[[2]int{v, w}] = s
		} else if s == "-" {
			Fscan(in, &w)
			if t := m[[2]int{w, v}]; t != "" {
				if t == m[[2]int{v, w}] {
					eq--
				}
				bi--
			}
			delete(m, [2]int{v, w})
		} else if eq+v&1*bi > 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}

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()

	m := map[[2]int]string{}
	var q, v, w, eq, bi int
	var s string
	for Fscan(in, &q, &q); q > 0; q-- {
		if Fscan(in, &s, &v); s == "+" {
			Fscan(in, &w, &s)
			if t := m[[2]int{w, v}]; t != "" {
				if t == s {
					eq++
				}
				bi++
			}
			m[[2]int{v, w}] = s
		} else if s == "-" {
			Fscan(in, &w)
			if t := m[[2]int{w, v}]; t != "" {
				if t == m[[2]int{v, w}] {
					eq--
				}
				bi--
			}
			delete(m, [2]int{v, w})
		} else if v&1 > 0 && bi > 0 || v&1 == 0 && eq > 0 {
			Fprintln(out, "YES")
		} else {
			Fprintln(out, "NO")
		}
	}
}


Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least k walks for any two consecutive days in order to feel good. For example, if k = 5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next n days and made a sequence of n integers a1, a2, ..., an, where ai is the number of times Polycarp will walk with the dog on the i-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).Help Polycarp determine the minimum number of walks he needs to do additionaly in the next n days so that Cormen will feel good during all the n days. You can assume that on the day before the first day and on the day after the n-th day Polycarp will go for a walk with Cormen exactly k times. Write a program that will find the minumum number of additional walks and the appropriate schedule — the sequence of integers b1, b2, ..., bn (bi ≥ ai), where bi means the total number of walks with the dog on the i-th day.

The first line contains two integers n and k (1 ≤ n, k ≤ 500) — the number of days and the minimum number of walks with Cormen for any two consecutive days.  The second line contains integers a1, a2, ..., an (0 ≤ ai ≤ 500) — the number of walks with Cormen on the i-th day which Polycarp has already planned.

In the first line print the smallest number of additional walks that Polycarp should do during the next n days so that Cormen will feel good during all days.  In the second line print n integers b1, b2, ..., bn, where bi — the total number of walks on the i-th day according to the found solutions (ai ≤ bi for all i from 1 to n). If there are multiple solutions, print any of them.

package main

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

func solve() {
    n, k := readInt(), readInt()
    a := readIntArray(n)
    s, ans := a[0], 0
    for i := 1; i < n; i++ {
        d := k - a[i] - s
        if d > 0 {
            a[i] += d
            ans += d
        }
        s = a[i]
    }
    fmt.Println(ans)
    for _, v := range a {
        fmt.Print(v, " ")
    }
    fmt.Println()
}

// Helpers

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

func main() {
	const MaxTokenLength = 1024 * 1024
	scanner.Buffer(make([]byte, 0, MaxTokenLength), MaxTokenLength)
	scanner.Split(bufio.ScanWords)
	solve()
}

// Math

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

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

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

// IO

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

func readInt() int {
	val, _ := strconv.Atoi(readString())
	return val
}

func readInt64() int64 {
	v, _ := strconv.ParseInt(readString(), 10, 64)
	return v
}

func readIntArray(sz int) []int {
	a := make([]int, sz)
	for i := 0; i < sz; i++ {
		a[i] = readInt()
	}
	return a
}

func readInt64Array(n int) []int64 {
	res := make([]int64, n)
	for i := 0; i < n; i++ {
		res[i] = readInt64()
	}
	return res
}

// Sort

type Ints64 []int64

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

/* Sort tempalte
func (a ) Len() int           { return len(a) }
func (a ) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ) Less(i, j int) bool { return  }
*/

package main

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

var bufin *bufio.Reader
var bufout *bufio.Writer

func main() {
	bufin = bufio.NewReader(os.Stdin)
	bufout = bufio.NewWriter(os.Stdout)
	defer bufout.Flush()

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

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

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

	fmt.Fprintf(bufout, "%d\n", res)
	for i := 0; i < n; i++ {
		fmt.Fprintf(bufout, "%d ", a[i])
	}
	fmt.Fprintf(bufout, "\n")
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d\n", &n, &k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
}

package main

import "fmt"

func main() {
	var n, k int32
	var sum int32
	fmt.Scan(&n, &k)
	a := make([]int32, n)
	for i := int32(0); i < n; i++ {
		fmt.Scan(&a[i])
		if i > 0 && a[i - 1] + a[i] < k {
			x := k - a[i - 1] - a[i]
			sum += x;
			a[i] += x
		}
	}
	fmt.Println(sum)
	for i := int32(0); i < n; i++ {
		if i != 0 {
			fmt.Print(" ")
		}
		fmt.Print(a[i])
	}
	fmt.Println()

}

// http://codeforces.com/problemset/problem/732/B
package main

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

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)

const (
	MOD = 1e9 + 7
)

type (

)

func main () {
	var (
		n, k int
	)

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)
	fmt.Fscanln(input, &n, &k)

	pwalks := make([]int, n + 2)

	// first & last days
	pwalks[0] = k
	pwalks[n + 1] = k

	for i := 0; i < n; i++ {
		fmt.Fscan(input, &pwalks[i + 1])
	}

	// go
	overs := 0
	for i := 1; i < n + 1; i++ {
		if (k > pwalks[i] + pwalks[i - 1]) {
			add := k - pwalks[i] - pwalks[i - 1]
			overs += add
			pwalks[i] += add
		}
	}

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

package main

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

func CalculateNeededWalks(days int, neededWalks int, walks []int) (int, []int) {
    var todaysNeededWalks int
    var totalAdditionalWalks int

    for i := 1; i < days; i += 1 {
        todaysNeededWalks = neededWalks - walks[i-1]

        if todaysNeededWalks <= 0 || walks[i] > todaysNeededWalks {
            continue
        }

        totalAdditionalWalks += todaysNeededWalks - walks[i]
        walks[i] = todaysNeededWalks
    }

    return totalAdditionalWalks, walks
}

func PrepareInput() (int, int, []int) {
    var days int
    var neededWalks int

    fmt.Fscan(os.Stdin, &days)
    fmt.Fscan(os.Stdin, &neededWalks)

    walks := make([]int, days)

    for i := 0; i < days; i++ {
        fmt.Fscan(os.Stdin, &walks[i])
    }

    return days, neededWalks, walks
}

func PrintResult(totalAdditionalWalks int, days int, walks []int) {
    var walksPrepared []string = make([]string, days)

    for i := 0; i < days; i += 1 {
        walksPrepared[i] = strconv.Itoa(walks[i])
    }

    var walksLine string = strings.Join(walksPrepared, " ")

    fmt.Println(totalAdditionalWalks)
    fmt.Println(walksLine)
}

func main() {
    days, neededWalks, preparedWalks := PrepareInput()

    totalAdditionalWalks, walks := CalculateNeededWalks(days, neededWalks, preparedWalks)

    PrintResult(totalAdditionalWalks, days, walks)
}

package main

import (
	"fmt"
	"strings"
)

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

	a := make([]int, n)
	for i := range a {
		fmt.Scanf("%d", &a[i])
	}

	sum := 0
	for i := 1; i < n; i++ {
		if a[i-1]+a[i] < k {
			diff := k - a[i-1] - a[i]
			sum += diff
			a[i] += diff
		}
	}

	fmt.Printf("%d\n", sum)
	fmt.Println(strings.Trim(fmt.Sprint(a), "[]"))
}

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var n, k int
	fmt.Fscanln(reader, &n, &k)
	walks := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d", &walks[i])
	}
	var min_walks, extra_walk int
	for i := 1; i < n; i++ {
		extra_walk = k - (walks[i-1] + walks[i])
		if extra_walk <= 0 {
			continue
		}
		walks[i] += extra_walk
		min_walks += extra_walk
	}
	fmt.Fprintln(writer, min_walks)
	for i := 0; i < n; i++ {
		fmt.Fprintf(writer, "%d ", walks[i])
	}
}

package main

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

var ret []int

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, min int
	fmt.Fscanln(reader, &n, &min)
	var a []int
	a = make([]int, n)
	ret = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
		ret[i] = a[i]
	}
	fmt.Fscanln(reader)

	startIndex := 0
	for i := 0; i < n; i++ {
		if a[i] >= min {
			solveSub(ret, startIndex, i, min)
			ret[i] = a[i]
			startIndex = i + 1
		}
	}
	if startIndex < n {
		solveSub(ret, startIndex, n, min)
	}
	retSum := 0
	for i := range ret {
		retSum += ret[i] - a[i]
	}
	fmt.Println(retSum)
	for i := 0; i < len(ret)-1; i++ {
		fmt.Print(ret[i], " ")
	}
	fmt.Println(ret[len(ret)-1])
}

func solveSub(a []int, i int, j int, min int) {
	//fmt.Println(i, j)
	if i >= j {
		return
	}
	for k := i; k < j-1; k++ {
		if a[k]+a[k+1] < min {
			a[k+1] = min - a[k]
		}
	}
}

package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)
	need := make([]int, n)
	needCnt := 0
	yesterday := 0
	fmt.Scan(&yesterday)
	need[0] = yesterday
	for i := 1; i < n; i++ {
		var today int
		fmt.Scan(&today)
		if yesterday+today >= k {
			need[i] = today
			yesterday = today
		} else {
			need[i] = k - yesterday
			needCnt += need[i] - today
			yesterday = need[i]
		}
	}
	fmt.Print(needCnt, "\n")
	for i := 0; i < n; i++ {
		fmt.Print(need[i], " ")
	}
}

package main

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

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); s.p++ {
		if s.buf[s.p] == ' ' {
			v, _ := strconv.Atoi(string(s.buf[start:s.p]))
			result = append(result, v)
			start = s.p + 1
		}
	}
	v, _ := strconv.Atoi(string(s.buf[start:s.p]))
	result = append(result, v)

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

	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
		}
	}
}
func main() {
	sc := NewScanner()
	N, K := sc.NextInt(), sc.NextInt()
	A := sc.NextIntArray()
	diff := 0
	for i := 1; i < N; i++ {
		k := A[i - 1] + A[i]
		if K - k > 0 {
			diff += K - k
			A[i] += K - k
		}
	}
	fmt.Println(diff)
	PrintI(A...)
}

func PrintI(args ...int) {
	fmt.Println(strings.Join(mapToString(args), " "))
}

func mapToString(arr []int) []string {
	ret := make([]string, len(arr))
	for i := 0; i < len(arr); i++ {
		ret[i] = strconv.Itoa(arr[i])
	}
	return ret
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d %d", &n, &k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    fmt.Printf("%d %d", n, k)
    /*
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    fmt.Printf("%d %d", n, k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    for i := 0; i < n; i++ {
        fmt.Printf("%d %d\n", a[i], b[i])
    }
    /*
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d", &n, &k)
    fmt.Printf("%d %d\n", n, k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    for i := 0; i < n; i++ {
        fmt.Printf("%d %d\n", a[i], b[i])
    }
    /*
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

import "fmt"

const N int = 500

func main() {
    var n, k int
    fmt.Scanf("%d%d\n", &n, &k)
    fmt.Printf("%d %d\n", n, k)
    var a, b [N]int
    for i := 0; i < n; i++ {
        fmt.Scanf("%d", &a[i])
        b[i] = a[i]
    }
    for i := 0; i < n; i++ {
        fmt.Printf("%d %d\n", a[i], b[i])
    }
    /*
    var s int
    for i := 0; i + 1 < n; i++ {
        d := k - (b[i] + b[i + 1])
        if d > 0 {
            s += d
            b[i + 1] += d
        }
    }
    fmt.Printf("%d\n", s)
    for i := 0; i < n; i++ {
        c := ' '
        if i == n - 1 {
            c = '\n'
        }
        fmt.Printf("%d%c", b[i], c)
    }
    */
}

package main

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

func CalculateNeededWalks(days int, neededWalks int, walks []int) (int, []int) {
    var todaysNeededWalks int
    var totalAdditionalWalks int

    for i := 1; i < days; i += 1 {
        todaysNeededWalks = neededWalks - walks[i-1]

        if todaysNeededWalks <= 0 {
            continue
        }

        totalAdditionalWalks += todaysNeededWalks - walks[i]
        walks[i] = todaysNeededWalks
    }

    return totalAdditionalWalks, walks
}

func PrepareInput() (int, int, []int) {
    var days int
    var neededWalks int

    fmt.Fscan(os.Stdin, &days)
    fmt.Fscan(os.Stdin, &neededWalks)

    walks := make([]int, days)

    for i := 0; i < days; i++ {
        fmt.Fscan(os.Stdin, &walks[i])
    }

    return days, neededWalks, walks
}

func PrintResult(totalAdditionalWalks int, days int, walks []int) {
    var walksPrepared []string = make([]string, days)

    for i := 0; i < days; i += 1 {
        walksPrepared[i] = strconv.Itoa(walks[i])
    }

    var walksLine string = strings.Join(walksPrepared, " ")

    fmt.Println(totalAdditionalWalks)
    fmt.Println(walksLine)
}

func main() {
    days, neededWalks, preparedWalks := PrepareInput()

    totalAdditionalWalks, walks := CalculateNeededWalks(days, neededWalks, preparedWalks)

    PrintResult(totalAdditionalWalks, days, walks)
}

package main

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

var ret []int

func main() {
	reader := bufio.NewReader(os.Stdin)
	var n, min int
	fmt.Fscanln(reader, &n, &min)
	var a []int
	a = make([]int, n)
	ret = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &a[i])
		ret[i] = a[i]
	}
	fmt.Fscanln(reader)

	startIndex := 0
	for i := 0; i < n; i++ {
		if a[i] >= min {
			solveSub(ret, startIndex, i, min)
			ret[i] = 0
			startIndex = i + 1
		}
	}
	if startIndex < n {
		solveSub(ret, startIndex, n, min)
	}
	retSum := 0
	for i := range ret {
		retSum += ret[i] - a[i]
	}
	fmt.Println(retSum)
	for i := 0; i < len(ret)-1; i++ {
		fmt.Print(ret[i], " ")
	}
	fmt.Println(ret[len(ret)-1])
}

func solveSub(a []int, i int, j int, min int) {
	//fmt.Println(i, j)
	if i >= j {
		return
	}
	for k := i; k < j-1; k++ {
		if a[k]+a[k+1] < min {
			a[k+1] = min - a[k]
		}
	}
}


Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t - 1 gestures (if the icon is on the screen t), and then make another gesture — press the icon of the required application exactly once to launch it.After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order? Note that one application may be launched multiple times.

The first line of the input contains three numbers n, m, k (1 ≤ n, m, k ≤ 105) — the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen. The next line contains n integers, permutation a1, a2, ..., an — the initial order of icons from left to right in the menu (from the first to the last one), ai —  is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among ai. The third line contains m integers b1, b2, ..., bm(1 ≤ bi ≤ n) — the ids of the launched applications in the planned order. One application may be launched multiple times.

Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order.

NoteIn the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8. After application 7 is launched, we get the new arrangement of the icons — (123)(457)(68). To launch it Anya makes 3 gestures. After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures. After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.In total, Anya makes 7 gestures.

package main

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

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

func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m, k := nextInt(), nextInt(), nextInt()
	posiciones := make([]int, n)
	iconos := make([]int, n)
	icono, pos := 0, 0
	preIcon, prePos := 0, 0
	gesto := int64(0)

	for i := 0; i < n; i++ {
		icono = nextInt()-1
		posiciones[icono] = i
		iconos[i] = icono
	}

	for ; m > 0; m-- {
		icono = nextInt()-1
		pos = posiciones[icono]
		gesto += int64(pos/k + 1)

		if pos != 0 {
			preIcon = iconos[pos-1]
			prePos = posiciones[preIcon]
			posiciones[icono], posiciones[preIcon] = posiciones[preIcon], posiciones[icono]
			iconos[pos], iconos[prePos] = iconos[prePos], iconos[pos]
		}

	}

	println(gesto)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

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

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m, k := nextInt(), nextInt(), nextInt()
	postisions := make([]int, n)
	icons := make([]int, n)
	icon, pos := 0, 0
	preIcon, prePos := 0, 0
	gesture := int64(0)

	for i := 0; i < n; i++ {
		icon = nextInt()-1
		postisions[icon] = i
		icons[i] = icon
	}

	for ; m > 0; m-- {
		icon = nextInt()-1
		pos = postisions[icon]
		gesture += int64(pos/k + 1)

		if pos != 0 {
			preIcon = icons[pos-1]
			prePos = postisions[preIcon]
			postisions[icon], postisions[preIcon] = postisions[preIcon], postisions[icon]
			icons[pos], icons[prePos] = icons[prePos], icons[pos]
		}

		// println(gesture, icon, pos , preIcon, prePos)
		// println(postisions)
		// println(icons)
	}

	println(gesture)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

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

package main

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

var scanner *bufio.Scanner

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

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

  n, m, k := getI(), getI(), getI()
  icons, index := make([]int, n), make([]int, n+1)
  for i := range icons {
    icons[i] = getI()
    index[icons[i]] = i
  }
  count := int64(0)
  for i := 0; i < m; i++ {
    q := getI()
    pos := index[q]
    count += int64(pos/k) + 1
    if pos != 0 {
      p := icons[pos-1]
      icons[pos-1], icons[pos] = q, p
      index[q] = pos-1
      index[p] = pos
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", count))
}

package main

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

func Solve(n int, m int, k int, a []int, b []int) int64 {
	screen := make([]int64, n+1)
	inv := make([]int, n+1)
	for i, elem := range a {
		screen[elem] = int64((i + k) / k)
		inv[elem] = i
	}
	ans := int64(0)
	for _, elem := range b {
		ans += screen[elem]
		j := inv[elem]
		if j-1 >= 0 {
			screen[elem], screen[a[j-1]] = screen[a[j-1]], screen[elem]
			inv[elem], inv[a[j-1]] = inv[a[j-1]], inv[elem]
			a[j], a[j-1] = a[j-1], a[j]
		}
	}
	return ans
}

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

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

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

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m, k := nextInt(), nextInt(), nextInt()
	postisions := make([]int, n)
	icons := make([]int, n)
	icon, pos := 0, 0
	preIcon, prePos := 0, 0
	gesture := 0

	for i := 0; i < n; i++ {
		icon = nextInt()-1
		postisions[icon] = i
		icons[i] = icon
	}

	for ; m > 0; m-- {
		icon = nextInt()-1
		pos = postisions[icon]
		gesture += pos/k + 1

		if pos != 0 {
			preIcon = icons[pos-1]
			postisions[icon], postisions[preIcon] = postisions[preIcon], postisions[icon]
			prePos = postisions[preIcon]
			icons[pos], icons[prePos] = icons[prePos], icons[pos]
		}

		// println(gesture, icon, pos , preIcon, prePos)
		// println(postisions)
		// println(icons)
	}

	println(gesture)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

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

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, m, k := nextInt(), nextInt(), nextInt()
	postisions := make([]int, n)
	icons := make([]int, n)
	icon, pos := 0, 0
	preIcon, prePos := 0, 0
	gesture := 0

	for i := 0; i < n; i++ {
		icon = nextInt()-1
		postisions[icon] = i
		icons[i] = icon
	}

	for ; m > 0; m-- {
		icon = nextInt()-1
		pos = postisions[icon]
		gesture += pos/k + 1

		if pos != 0 {
			preIcon = icons[pos-1]
			prePos = postisions[preIcon]
			postisions[icon], postisions[preIcon] = postisions[preIcon], postisions[icon]
			icons[pos], icons[prePos] = icons[prePos], icons[pos]
		}

		// println(gesture, icon, pos , preIcon, prePos)
		// println(postisions)
		// println(icons)
	}

	println(gesture)
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

func nextInt() int {
	n, _ := strconv.Atoi(next())
	return n
}

func nextFloat() float64 {
	n, _ := strconv.ParseFloat(next(), 64)
	return n
}

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

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

package main

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

func Solve(n int, m int, k int, a []int, b []int) int {
	screen := make([]int, n+1)
	inv := make([]int, n+1)
	for i, elem := range a {
		screen[elem] = (i + k) / k
		inv[elem] = i
	}
	ans := 0
	for _, elem := range b {
		ans += screen[elem]
		j := inv[elem]
		if j-1 >= 0 {
			screen[elem], screen[a[j-1]] = screen[a[j-1]], screen[elem]
			inv[elem], inv[a[j-1]] = inv[a[j-1]], inv[elem]
			a[j], a[j-1] = a[j-1], a[j]
		}
	}
	return ans
}

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

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

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


Vasya has an array $$$a_1, a_2, \dots, a_n$$$.You don't know this array, but he told you $$$m$$$ facts about this array. The $$$i$$$-th fact is a triple of numbers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 \le t_i \le 1, 1 \le l_i < r_i \le n$$$) and it means:  if $$$t_i=1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}$$$ is sorted in non-decreasing order;  if $$$t_i=0$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots, a_{r_i}$$$ is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter. For example if $$$a = [2, 1, 1, 3, 2]$$$ then he could give you three facts: $$$t_1=1, l_1=2, r_1=4$$$ (the subarray $$$[a_2, a_3, a_4] = [1, 1, 3]$$$ is sorted), $$$t_2=0, l_2=4, r_2=5$$$ (the subarray $$$[a_4, a_5] = [3, 2]$$$ is not sorted), and $$$t_3=0, l_3=3, r_3=5$$$ (the subarray $$$[a_3, a_5] = [1, 3, 2]$$$ is not sorted).You don't know the array $$$a$$$. Find any array which satisfies all the given facts.

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \le n \le 1000, 1 \le m \le 1000$$$). Each of the next $$$m$$$ lines contains three integers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 \le t_i \le 1, 1 \le l_i < r_i \le n$$$). If $$$t_i = 1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}$$$ is sorted. Otherwise (if $$$t_i = 0$$$) subbarray $$$a_{l_i}, a_{l_i + 1}, \dots , a_{r_i}$$$ is not sorted.

If there is no array that satisfies these facts in only line print NO (in any letter case). If there is a solution, print YES (in any letter case). In second line print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the array $$$a$$$, satisfying all the given facts. If there are multiple satisfying arrays you can print any of them.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1187C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type fact struct{ l, r int }

	var n, m, t, l, r int
	Fscan(in, &n, &m)
	fa := make([]int, 2*n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }
	fs := []fact{}
	for ; m > 0; m-- {
		Fscan(in, &t, &l, &r)
		l--
		if t > 0 {
			tar := n + l
			for i := l; i < r; i++ {
				if find(i) != i {
					tar = find(i)
					break
				}
			}
			for i := l; i < r; i++ {
				merge(i, tar)
			}
		} else {
			fs = append(fs, fact{l, r})
		}
	}
o:
	for _, f := range fs {
		for i := f.l; i < f.r; i++ {
			if find(i) != find(f.l) {
				continue o
			}
		}
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	for i := 0; i < n; {
		st := i
		for ; i < n && find(i) == find(st); i++ {
			Fprint(out, n-st, " ")
		}
	}
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1187C(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	type fact struct{ l, r int }

	var n, m, t, l, r int
	Fscan(in, &n, &m)
	fa := make([]int, 2*n)
	for i := range fa {
		fa[i] = i
	}
	var find func(int) int
	find = func(x int) int {
		if fa[x] != x {
			fa[x] = find(fa[x])
		}
		return fa[x]
	}
	merge := func(from, to int) { fa[find(from)] = find(to) }
	fs := []fact{}
	for ; m > 0; m-- {
		Fscan(in, &t, &l, &r)
		l--
		if t > 0 {
			tar := n + l
			for i := l; i < r; i++ {
				if find(i) != i {
					tar = find(i)
					break
				}
			}
			for i := l; i < r; i++ {
				merge(i, tar)
			}
		} else {
			fs = append(fs, fact{l, r})
		}
	}
o:
	for _, f := range fs {
		for i := f.l; i < f.r; i++ {
			if find(i) != find(f.l) {
				continue o
			}
		}
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	for i := 0; i < n; {
		st := i
		for ; i < n && find(i) == find(st); i++ {
			Fprint(out, 1e4+i-2*st, " ")
		}
	}
}

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

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1187/C

package main

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

type Fact struct {
	T int
	L int
	R int
}

type Solution struct {
	Exists bool
	Array  []int
}

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

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

func mergeFacts(facts []Fact) []Fact {
	sort.Slice(facts, func(i, j int) bool { return facts[i].L < facts[j].L })
	mergedFacts := []Fact{}
	for len(facts) > 0 {
		var currentFact Fact
		currentFact, facts = facts[0], facts[1:]
		if len(mergedFacts) == 0 || currentFact.L > mergedFacts[len(mergedFacts)-1].R {
			mergedFacts = append(mergedFacts, currentFact)
		} else {
			lastMergedFact := mergedFacts[len(mergedFacts)-1]
			if lastMergedFact.L <= currentFact.L && currentFact.L <= lastMergedFact.R {
				mergedFacts[len(mergedFacts)-1].L = min(lastMergedFact.L, currentFact.L)
				mergedFacts[len(mergedFacts)-1].R = max(lastMergedFact.R, currentFact.R)
			}
		}
	}
	return mergedFacts
}

func splitFacts(facts []Fact) ([]Fact, []Fact) {
	t1Facts := []Fact{}
	t0Facts := []Fact{}
	for _, fact := range facts {
		if fact.T == 0 {
			t0Facts = append(t0Facts, fact)
		} else if fact.T == 1 {
			t1Facts = append(t1Facts, fact)
		}
	}
	return t0Facts, mergeFacts(t1Facts)
}

func constructB(n int, t1Facts []Fact) []int {
	b := make([]int, n-1)
	for i := 0; i < n-1; i++ {
		b[i] = -1
	}
	for _, fact := range t1Facts {
		for i := fact.L; i < fact.R; i++ {
			b[i-1] = 0
		}
	}
	return b
}

func constructA(n int, b []int) []int {
	a := make([]int, n)
	a[0] = n
	for i := 1; i < n; i++ {
		a[i] = b[i-1] + a[i-1]
	}
	return a
}

func Solve(n int, m int, facts []Fact) Solution {
	t0Facts, t1Facts := splitFacts(facts)
	b := constructB(n, t1Facts)
	a := constructA(n, b)
	for _, fact := range t0Facts {
		factVerified := false
		for i := fact.L; i < fact.R; i++ {
			if a[i-1] > a[i] {
				factVerified = true
			}
		}
		if !factVerified {
			return Solution{Exists: false}
		}
	}
	return Solution{Exists: true, Array: a}
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	facts := make([]Fact, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &facts[i].T, &facts[i].L, &facts[i].R)
	}

	solution := Solve(n, m, facts)
	if !solution.Exists {
		fmt.Fprintf(writer, "NO\n")
	} else {
		fmt.Fprintf(writer, "YES\n")
		for _, elem := range solution.Array {
			fmt.Fprintf(writer, "%d ", elem)
		}
		fmt.Fprintf(writer, "\n")
	}
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1187/C

package main

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

type Fact struct {
	T int
	L int
	R int
}

type Solution struct {
	Exists bool
	Array  []int
}

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

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

func mergeFacts(facts []Fact) []Fact {
	sort.Slice(facts, func(i, j int) bool { return facts[i].L < facts[j].L })
	mergedFacts := []Fact{}
	for len(facts) > 0 {
		var currentFact Fact
		currentFact, facts = facts[0], facts[1:]
		if len(mergedFacts) == 0 || currentFact.L > mergedFacts[len(mergedFacts)-1].R {
			mergedFacts = append(mergedFacts, currentFact)
		} else {
			lastMergedFact := mergedFacts[len(mergedFacts)-1]
			if lastMergedFact.L <= currentFact.L && currentFact.L <= lastMergedFact.R {
				mergedFacts[len(mergedFacts)-1].L = min(lastMergedFact.L, currentFact.L)
				mergedFacts[len(mergedFacts)-1].R = max(lastMergedFact.R, currentFact.R)
			}
		}
	}
	return mergedFacts
}

func splitFacts(facts []Fact) ([]Fact, []Fact) {
	t1Facts := []Fact{}
	t0Facts := []Fact{}
	for _, fact := range facts {
		if fact.T == 0 {
			t0Facts = append(t0Facts, fact)
		} else if fact.T == 1 {
			t1Facts = append(t1Facts, fact)
		}
	}
	return mergeFacts(t0Facts), mergeFacts(t1Facts)
}

func Solve(n int, m int, facts []Fact) Solution {
	t0Facts, t1Facts := splitFacts(facts)
	b := make([]int, n-1)
	for _, fact := range t0Facts {
		for i := fact.L; i < fact.R; i++ {
			b[i-1] = -1
		}
	}
	a := make([]int, n)
	a[0] = n
	for i := 1; i < n; i++ {
		a[i] = b[i-1] + a[i-1]
	}
	for _, fact := range t1Facts {
		for i := fact.L; i < fact.R; i++ {
			if a[i-1] > a[i] {
				return Solution{Exists: false}
			}
		}
	}
	return Solution{Exists: true, Array: a}
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	facts := make([]Fact, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &facts[i].T, &facts[i].L, &facts[i].R)
	}

	solution := Solve(n, m, facts)
	if !solution.Exists {
		fmt.Fprintf(writer, "NO\n")
	} else {
		fmt.Fprintf(writer, "YES\n")
		for _, elem := range solution.Array {
			fmt.Fprintf(writer, "%d ", elem)
		}
		fmt.Fprintf(writer, "\n")
	}
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/1187/C

package main

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

type Fact struct {
	T int
	L int
	R int
}

type Solution struct {
	Exists bool
	Array  []int
}

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

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

func mergeFacts(facts []Fact) []Fact {
	sort.Slice(facts, func(i, j int) bool { return facts[i].L < facts[j].L })
	mergedFacts := []Fact{}
	for len(facts) > 0 {
		var currentFact Fact
		currentFact, facts = facts[0], facts[1:]
		if len(mergedFacts) == 0 || currentFact.L > mergedFacts[len(mergedFacts)-1].R {
			mergedFacts = append(mergedFacts, currentFact)
		} else {
			lastMergedFact := mergedFacts[len(mergedFacts)-1]
			if lastMergedFact.L <= currentFact.L && currentFact.L <= lastMergedFact.R {
				mergedFacts[len(mergedFacts)-1].L = min(lastMergedFact.L, currentFact.L)
				mergedFacts[len(mergedFacts)-1].R = max(lastMergedFact.R, currentFact.R)
			}
		}
	}
	return mergedFacts
}

func splitFacts(facts []Fact) ([]Fact, []Fact) {
	t1Facts := []Fact{}
	t0Facts := []Fact{}
	for _, fact := range facts {
		if fact.T == 0 {
			t0Facts = append(t0Facts, fact)
		} else if fact.T == 1 {
			t1Facts = append(t1Facts, fact)
		}
	}
	return mergeFacts(t0Facts), mergeFacts(t1Facts)
}

func constructB(n int, t1Facts []Fact) []int {
	b := make([]int, n-1)
	for i := 0; i < n-1; i++ {
		b[i] = -1
	}
	for _, fact := range t1Facts {
		for i := fact.L; i < fact.R; i++ {
			b[i-1] = 0
		}
	}
	return b
}

func constructA(n int, b []int) []int {
	a := make([]int, n)
	a[0] = n
	for i := 1; i < n; i++ {
		a[i] = b[i-1] + a[i-1]
	}
	return a
}

func Solve(n int, m int, facts []Fact) Solution {
	t0Facts, t1Facts := splitFacts(facts)
	b := constructB(n, t1Facts)
	a := constructA(n, b)
	for _, fact := range t0Facts {
		factVerified := false
		for i := fact.L; i < fact.R; i++ {
			if a[i-1] > a[i] {
				factVerified = true
			}
		}
		if !factVerified {
			return Solution{Exists: false}
		}
	}
	return Solution{Exists: true, Array: a}
}

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

	var n, m int
	fmt.Fscanf(reader, "%d %d\n", &n, &m)
	facts := make([]Fact, m)
	for i := 0; i < m; i++ {
		fmt.Fscanf(reader, "%d %d %d\n", &facts[i].T, &facts[i].L, &facts[i].R)
	}

	solution := Solve(n, m, facts)
	if !solution.Exists {
		fmt.Fprintf(writer, "NO\n")
	} else {
		fmt.Fprintf(writer, "YES\n")
		for _, elem := range solution.Array {
			fmt.Fprintf(writer, "%d ", elem)
		}
		fmt.Fprintf(writer, "\n")
	}
}


Stanley and Megan decided to shop in the "Crossmarket" grocery store, which can be represented as a matrix with $$$n$$$ rows and $$$m$$$ columns. Stanley and Megan can move to an adjacent cell using $$$1$$$ unit of power. Two cells are considered adjacent if they share an edge. To speed up the shopping process, Megan brought her portals with her, and she leaves one in each cell she visits (if there is no portal yet). If a person (Stanley or Megan) is in a cell with a portal, that person can use $$$1$$$ unit of power to teleport to any other cell with a portal, including Megan's starting cell.They decided to split up: Stanley will go from the upper-left cell (cell with coordinates $$$(1, 1)$$$) to the lower-right cell (cell with coordinates $$$(n, m)$$$), whilst Megan needs to get from the lower-left cell (cell with coordinates $$$(n, 1)$$$) to the upper-right cell (cell with coordinates $$$(1, m)$$$).What is the minimum total energy needed for them both to do that?Note that they can choose the time they move. Time does not affect energy.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). Description of the test cases follows. The only line in the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 10^5$$$).

For each test case print a single integer on a new line – the answer.

Note  In the first test case they can stick to the following plan:   Megan (red circle) moves to the cell $$$(7, 3)$$$. Then she goes to the cell $$$(1, 3)$$$, and Stanley (blue circle) does the same.  Stanley uses the portal in that cell (cells with portals are grey) to get to the cell $$$(7, 3)$$$. Then he moves to his destination — cell $$$(7, 5)$$$.  Megan also finishes her route and goes to the cell $$$(1, 5)$$$. The total energy spent is $$$(2 + 6) + (2 + 1 + 2) + (2)= 15$$$, which is our final answer.

package main

import (
	"fmt"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		ans := 2*m+n-2
		if m == 1 && n == 1{
		    ans = 0
		}
		fmt.Println(ans)
		t--
	}
}

package main

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

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

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

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())

		if n == 1 && m == 1 {
			wr.WriteString(strconv.Itoa(0) + "\n")
		} else {
			wr.WriteString(strconv.Itoa(n+m-2+min(n, m)) + "\n")
		}
	}

}

func main() {
	reader := bufio.NewReader(os.Stdin)
	Solve(reader)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, m, n int
	fmt.Scanln(&t)
	for i := 0; i < t; i++ {
		fmt.Scanln(&m, &n)
		if m == n && m == 1 {
			fmt.Println(0)
		} else {
			nn := math.Max(float64(m), float64(n))
			n2 := int(nn - 2)
			ans := (m+n-2)*2 - n2
			fmt.Println(ans)
		}

	}

}

package main

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

const MOD int64 = 1000000007

var n, m int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &m)

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {
	if n == 1 && m == 1 {
		return 0
	}
	if n == 1 || m == 1 {
		return n + m - 1
	}
	return (n + m - 2) + min(n, m)
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

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

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

package main

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

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

	return b
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)
	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &m)
		if n == 1 && m == 1 {
			fmt.Println(0)
			continue
		}

		if n == 1 {
			fmt.Println(m)
			continue
		}

		if m == 1 {
			fmt.Println(n)
			continue
		}

		var answ1 int = math.MaxInt
		var answ2 int = math.MaxInt
		if n % 2 == 1 {
			answ1 = n / 2 + m - 1 + n / 2 + n / 2 + 1 + n / 2
		}

		if m % 2 == 1 {
			answ2 = m / 2 + n - 1 + m / 2 + m / 2 + 1 + m / 2
		}

		var answ3 int = math.MaxInt
		var answ4 int = math.MaxInt
		if m % 2 == 0 {
			answ3 = m / 2 - 1 + n - 1 + m/2 + m/2 + m/2
		}
		if n % 2 == 0 {
			answ4 = n / 2 - 1 + m - 1 + n/2 + n/2 + n/2
		}

		fmt.Println(min(answ1, min(answ2, min(answ3, answ4))))
	}
}

package main

import "fmt"

func main() {
	var t, n, m int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		fmt.Scan(&n, &m)
		if n == 1 && m == 1 {
			fmt.Println(0)
		} else {
			fmt.Println(min(n, m) + n-1 + m-1)
		}
	}
}

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

package main

import (
	"fmt"
	"math"
)

func minimum(nums ...int) int {
	ret := math.MaxInt
	for _, num := range nums {
		if num < ret {
			ret = num
		}
	}
	return ret
}

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

	for ; t > 0; t-- {
		var row, col int
		fmt.Scanf("%d %d\n", &row, &col)

		res := row - 1 + col - 1
		res += minimum(row-1, col-1)
		if res > 0 {
			res++
		}
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

func main() {
	var TC int
	fmt.Scanf("%d\n", &TC)
	for tc := 0; tc < TC; tc += 1 {
		var n, m int
		fmt.Scanf("%d %d\n", &n, &m)

		out := n - 1 + m - 1
		if n < m {
			out += n - 1
			if m > 1 {
				out += 1
			}
		} else {
			out += m - 1
			if n > 1 {
				out += 1
			}
		}

		fmt.Printf("%v\n", out)
	}
}

package main

import "fmt"

func calc(n, m int) int {
    var res int
    res += m + n - 1
    if n > m {
        res += m - 1
    } else {
        res += n - 1
    }
    return res
}

func main() {
    var testCase, n, m int
    fmt.Scanf("%d\n", &testCase)
    for testCase > 0 {
        fmt.Scanf("%d %d\n", &n, &m)
        if n == 1 && m == 1 {
            fmt.Println("0")
            testCase--
            continue
        }
        fmt.Println(calc(n, m))
        testCase--
    }
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var y,x int
		fmt.Fscan(in,&y,&x)
		if x==1 && y==1 {
			fmt.Fprintln(out,0)
		} else {
			fmt.Fprintln(out,(y-1) + (x-1) + min(x,y))
		}
	}
}

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

package main

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

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

func qwq() {
	n, m := 0, 0
	fmt.Fscan(in, &n, &m)
	if n < m {
		n, m = m, n
	}
	if n == 1 {
		fmt.Fprintln(out, 0)
	} else {
		fmt.Fprintln(out, n+m-2+m)
	}
}

func main() {
	defer out.Flush()
	q := 1
	fmt.Fscanln(in, &q)
	for ; q > 0; q-- {
		qwq()
	}
}

package main

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

type CF1715A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1715A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1715A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1715A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1715A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1715A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1715A
Date: 8/30/2022
User: wotan
URL: https://codeforces.com/problemset/problem/1715/A
Problem: CF1715A
**/
func (in *CF1715A) Run() {
	for t := in.NextInt(); t > 0; t-- {
		n, m := in.NextInt(), in.NextInt()
		if n == 1 && m == 1 {
			fmt.Println(0)
		} else {
			fmt.Println(n + m - 2 + Min(n, m))
		}

	}
}

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

func NewCF1715A(r *bufio.Reader) *CF1715A {
	return &CF1715A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1715A(bufio.NewReader(os.Stdin)).Run()
}

package main

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

func Min(a, b int) int {
	return int(math.Min(float64(a), float64(b)))
}

func Max(a, b int) int {
	return int(math.Max(float64(a), float64(b)))
}

func solution(reader io.Reader, writer io.Writer) {
	var t int
	fmt.Fscan(reader, &t)

	for i := 0; i < t; i++ {
		var n, m int
		fmt.Fscan(reader, &n, &m)
		if n+m == 2 {
			fmt.Fprintln(writer, 0)
		} else {
			fmt.Fprintln(writer, Max(n, m)+2*Min(n, m)-2)
		}
	}
}

func timeTrack(start time.Time) {
	elapsed := time.Since(start)
	fmt.Printf("Execution time %f", elapsed.Seconds())
}

func main() {
	var stdin *os.File
	var stdout *os.File

	LOCAL, _ := strconv.ParseBool(os.Getenv("LOCAL"))

	if LOCAL {
		defer timeTrack(time.Now())

		stdin, _ = os.Open("file.in")
		defer stdin.Close()

		stdout, _ = os.Create("file.out")
		defer stdout.Close()
	} else {
		stdin = os.Stdin
		stdout = os.Stdout
	}

	reader := bufio.NewReaderSize(stdin, 64*1024)
	writer := bufio.NewWriterSize(stdout, 64*1024)
	defer writer.Flush()

	solution(reader, writer)
}

package main

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

var (
	reader bufio.Reader
	writer bufio.Writer
)

func main() {
	inp, err := os.Open("in.txt")
	if err == nil {
		os.Stdin = inp
		outp, _ := os.Create("out.txt")
		os.Stdout = outp
		defer outp.Close()
		defer inp.Close()
	}
	reader = *bufio.NewReader(os.Stdin)
	writer = *bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var T int
	scan(&T)

	for ; T > 0; T-- {
		var n, m int
		scan(&n, &m)

		if n < m {
			n, m = m, n
		}

		if n == 1 && m == 1 {
			printf("0\n")
		} else {
			printf("%v\n", n + m - 2 + 1 + m - 1)
		}
	}
}

func scan(a ...interface{}) (n int, err error) {
	return fmt.Fscan(&reader, a...)
}

func printf(format string, a ...interface{}) (n int, err error) {
	return fmt.Fprintf(&writer, format, a...)
}

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, m int64
		fmt.Fscan(in, &n, &m)

		if m > n {
			n, m = m, n
		}

		if n == 1 && m == 1 {
			n = 0
		}
		fmt.Fprintln(out, (m-1)*2+n)
	}
}

package main

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

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

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

// func min3(a, b, c int) int {
// 	return min(min(a, b), c)
// }

// func maxSquare(a int) int {
// 	tmp := int(math.Sqrt(float64(a)))
// 	if tmp*tmp == a {
// 		return tmp * tmp
// 	} else {
// 		return (tmp + 1) * (tmp + 1)
// 	}
// }

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var niter int
	fmt.Fscan(in, &niter)
	for iiter := 0; iiter < niter; iiter++ {
		var n, m int
		fmt.Fscan(in, &m, &n)
		m--
		n--
		if m > n {
			n, m = m, n
		}
		// n > m
		res := m + m + n + min(n, 1)
		fmt.Fprintln(out, res)
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		ans := 2*m+n-2
		if m == 1 {
		    ans = 0
		}
		fmt.Println(ans)
		t--
	}
}

package main

import (
	"fmt"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		fmt.Println(2*m+n-2)
		t--
	}
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, n, m int
	fmt.Scanln(&t)
	for t > 0 {
		fmt.Scanln(&n, &m)
		if n < m {
			n, m = m, n
		}
		fmt.Println(int(math.Max(0, float64(2*m+n-2))))
		t--
	}
}

package main

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

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

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

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())

		if n == 1 && m == 1 {
			wr.WriteString(strconv.Itoa(0) + "\n")
      return
		}

		wr.WriteString(strconv.Itoa(n+m-2+min(n, m)) + "\n")
	}

}

func main() {
	reader := bufio.NewReader(os.Stdin)
	Solve(reader)
}

package main

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

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

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

func Solve(reader *bufio.Reader) {
	scanner := bufio.NewScanner(reader)
	scanner.Split(bufio.ScanWords)
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	scanner.Scan()
	t, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < t; i++ {
		scanner.Scan()
		n, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())

		if n == 1 && m == 1 {
			wr.WriteString(strconv.Itoa(0) + "\n")
		}

		wr.WriteString(strconv.Itoa(n+m-2+min(n, m)) + "\n")
	}

}

func main() {
	reader := bufio.NewReader(os.Stdin)
	Solve(reader)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var t, m, n int
	fmt.Scanln(&t)
	for i := 0; i <= t; i++ {
		fmt.Scanln(&m, &n)
		if m == n && m == 1 {
			fmt.Println(0)
		} else {
			nn := math.Max(float64(m), float64(n))
			n2 := int(nn - 2)
			ans := (m+n-2)*2 - n2
			fmt.Println(ans)
		}

	}

}

package main

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

const MOD int64 = 1000000007

var n, m int

// ByAge implements sort.Interface based on the Age field.
type ByV []*pair

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

func main() {

	read := bufio.NewReader(os.Stdin)

	var t int
	fmt.Fscan(read, &t)

	for i := 0; i < t; i++ {

		fmt.Fscan(read, &n, &m)

		fmt.Println(solve())
	}
}

type pair struct {
	v int
	idx []int
}

func solve() int {
	if n == 1 || m == 1 {
		return n + m - 2
	}
	return (n + m - 2) + min(n, m)
}

//IQ = 87

//........105....99...87..87..87..87.87.......87....

//            86

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

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

package main

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

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

	return b
}

func main() {
	// f, _ := os.Open("input.txt")
	var reader = bufio.NewReader(os.Stdin)
	// var reader = bufio.NewReader(f)
	var tests int
	fmt.Fscan(reader, &tests)
	for t := 0; t < tests; t++ {
		var n, m int
		fmt.Fscan(reader, &n)
		fmt.Fscan(reader, &m)
		if n == 1 && m == 1 {
			fmt.Println(0)
			continue
		}

		if n == 1 {
			fmt.Println(m)
			continue
		}

		if m == 1 {
			fmt.Println(n)
			continue
		}

		var answ1 int = math.MaxInt
		var answ2 int = math.MaxInt
		if n % 2 == 1 {
			answ1 = n / 2 + m - 1 + n / 2 + n / 2 + 1 + n / 2
		}

		if m % 2 == 1 {
			answ2 = m / 2 + n - 1 + m / 2 + m / 2 + 1 + m / 2
		}

		var answ3 int = math.MaxInt
		if n % 2 == 0 && m % 2 == 0 {
			answ3 = m / 2 - 1 + n - 1 + m/2 + m/2 + m/2
		}

		fmt.Println(min(answ1, min(answ2, answ3)))
	}
}

package main

import "fmt"

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

package main

import (
	"fmt"
	"math"
)

func minimum(nums ...int) int {
	ret := math.MaxInt
	for _, num := range nums {
		if num < ret {
			ret = num
		}
	}
	return ret
}

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

	for ; t > 0; t-- {
		var row, col int
		fmt.Scanf("%d %d", &row, &col)

		res := row - 1 + col - 1
		res += minimum(row-1, col-1)
		if res > 0 {
			res++
		}
		fmt.Println(res)
	}
}

package main

import (
	"fmt"
)

func main() {
	var TC int
	fmt.Scanf("%d", &TC)
	for tc := 0; tc < TC; tc += 1 {
		var n, m int
		fmt.Scanf("%d %d", &n, &m)

		out := n - 1 + m - 1
		if n < m {
			out += n - 1
			if m > 1 {
				out += 1
			}
		} else {
			out += m - 1
			if n > 1 {
				out += 1
			}
		}

		fmt.Printf("%v\n", out)
	}
}

package main

import "fmt"

func calc(n, m int) int {
    var res int
    res += m + n - 1
    if n > m {
        res += m - 1
    } else {
        res += n - 1
    }
    return res
}

func main() {
    var testCase, n, m int
    fmt.Scanf("%d", &testCase)
    for testCase > 0 {
        fmt.Scanf("%d %d", &n, &m)
        if n == 1 && m == 1 {
            fmt.Println("0")
            testCase--
            continue
        }
        fmt.Println(calc(n, m))
        testCase--
    }
}


Let's call a sequence of integers $$$x_1, x_2, \dots, x_k$$$ MEX-correct if for all $$$i$$$ ($$$1 \le i \le k$$$) $$$|x_i - \operatorname{MEX}(x_1, x_2, \dots, x_i)| \le 1$$$ holds. Where $$$\operatorname{MEX}(x_1, \dots, x_k)$$$ is the minimum non-negative integer that doesn't belong to the set $$$x_1, \dots, x_k$$$. For example, $$$\operatorname{MEX}(1, 0, 1, 3) = 2$$$ and $$$\operatorname{MEX}(2, 1, 5) = 0$$$.You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. Calculate the number of non-empty MEX-correct subsequences of a given array. The number of subsequences can be very large, so print it modulo $$$998244353$$$. Note: a subsequence of an array $$$a$$$ is a sequence $$$[a_{i_1}, a_{i_2}, \dots, a_{i_m}]$$$ meeting the constraints $$$1 \le i_1 < i_2 < \dots < i_m \le n$$$. If two different ways to choose the sequence of indices $$$[i_1, i_2, \dots, i_m]$$$ yield the same subsequence, the resulting subsequence should be counted twice (i. e. two subsequences are different if their sequences of indices $$$[i_1, i_2, \dots, i_m]$$$ are not the same).

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le n$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$5 \cdot 10^5$$$.

For each test case, print a single integer — the number of non-empty MEX-correct subsequences of a given array, taken modulo $$$998244353$$$.

NoteIn the first example, the valid subsequences are $$$[0]$$$, $$$[1]$$$, $$$[0,1]$$$ and $$$[0,2]$$$.In the second example, the valid subsequences are $$$[0]$$$ and $$$[1]$$$.In the third example, any non-empty subsequence is valid.

package main

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

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

const SZ = 5e5 + 10
const MOD = 998244353

var a [SZ]int
var dp0 [SZ]int
var dp1 [2][SZ]int

func add(a, b int) int {
	return (a + b) % MOD
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		var n int
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 0; i <= n+3; i++ {
			dp0[i] = 0
			dp1[0][i] = 0
			dp1[1][i] = 0
		}

		var ans int = 0
		for i := n - 1; i >= 0; i-- {
			var x = a[i]
			// ways to continue a subsequence starting in a[i]=x
			var r0 int = 1             // if current mex is x-1
			var r1unrestricted int = 1 // if current mex is x+1 but x+2 is NOT in the sequence
			var r1restricted int = 1   // if current mex is x+1 and x+2 is in the sequence

			// next number is x-2 and next mex is x-1
			if x >= 2 {
				r0 = add(r0, dp1[1][x-2])
			}
			// next number is x and next mex is x-1
			r0 = add(r0, dp0[x])

			// next number is x and next mex x+1
			r1unrestricted = add(r1unrestricted, dp1[0][x])
			// next number is x+1 and next mex x+2
			r1unrestricted = add(r1unrestricted, dp1[0][x+1])
			// next number is x+2 and next mex is x+1
			r1unrestricted = add(r1unrestricted, dp0[x+2])

			// next number is x and next mex is x+1
			r1restricted = add(r1restricted, dp1[1][x])
			// next number is x+2 and next mex is x+1
			r1restricted = add(r1restricted, dp0[x+2])

			if x == 1 {
				ans = add(ans, r0)
			} else if x == 0 {
				ans = add(ans, r1unrestricted)
			}

			dp0[x] = add(dp0[x], r0)
			dp1[0][x] = add(dp1[0][x], r1unrestricted)
			dp1[1][x] = add(dp1[1][x], r1restricted)
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	out.Flush()
}

package main

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

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

var a [5e5 + 10]int
var dp0 [5e5 + 10]int
var dp1 [5e5 + 10]int

const MOD = 998244353

func add(a, b int) int {
	return (a + b) % MOD
}

func main() {
	var ntc int
	fmt.Fscan(in, &ntc)
	for t := 0; t < ntc; t++ {
		var n int
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			fmt.Fscan(in, &a[i])
		}
		for i := 0; i <= n+2; i++ {
			dp0[i] = 0
			dp1[i] = 0
		}

		var ans int = 0
		for i := n - 1; i >= 0; i-- {
			var r0 int = 1
			var r1 int = 1

			if a[i] >= 2 {
				r0 = add(r0, dp1[a[i]-2])
			}
			r0 = add(r0, dp0[a[i]])

			r1 = add(r1, add(dp1[a[i]], dp1[a[i]+1]))
			r1 = add(r1, dp0[a[i]+2])

			if a[i] == 1 {
				ans = add(ans, r0)
			} else if a[i] == 0 {
				ans = add(ans, r1)
			}

			dp0[a[i]] = add(dp0[a[i]], r0)
			dp1[a[i]] = add(dp1[a[i]], r1)
		}
		fmt.Fprintf(out, "%d\n", ans)
	}
	out.Flush()
}