package main

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

// github.com/EndlessCheng/codeforces-go
func CF676D(_r io.Reader, _w io.Writer) {
	doorTable := [125]string{}
	doorTable['+'] = "++++"
	doorTable['-'] = "-|-|"
	doorTable['|'] = "|-|-"
	doorTable['^'] = "^>v<"
	doorTable['>'] = ">v<^"
	doorTable['v'] = "v<^>"
	doorTable['<'] = "<^>v"
	doorTable['U'] = "URDL"
	doorTable['R'] = "RDLU"
	doorTable['D'] = "DLUR"
	doorTable['L'] = "LURD"
	dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // 上右下左
	dirTable := [125][]int{}
	dirTable['+'] = []int{0, 1, 2, 3}
	dirTable['-'] = []int{1, 3}
	dirTable['|'] = []int{0, 2}
	dirTable['^'] = []int{0}
	dirTable['>'] = []int{1}
	dirTable['v'] = []int{2}
	dirTable['<'] = []int{3}
	dirTable['U'] = []int{1, 2, 3}
	dirTable['R'] = []int{0, 2, 3}
	dirTable['D'] = []int{0, 1, 3}
	dirTable['L'] = []int{0, 1, 2}
	canBack := func(from int, backs []int) bool {
		for _, back := range backs {
			if (back+2)&3 == from {
				return true
			}
		}
		return false
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, ax, ay, bx, by int
	Fscan(in, &n, &m)
	g := make([]string, n)
	for i := range g {
		Fscan(in, &g[i])
	}
	Fscan(in, &ax, &ay, &bx, &by)
	bx--
	by--

	type stat struct{ x, y, rot int }
	qs := [4][]stat{{{ax - 1, ay - 1, 0}}}
	vis := [1000][1000][4]bool{}
	isAllQueueEmpty := func() bool {
		for _, q := range qs {
			if len(q) > 0 {
				return false
			}
		}
		return true
	}
	for time := 0; !isAllQueueEmpty(); time++ {
		curQ := make([]stat, len(qs[time&3]))
		copy(curQ, qs[time&3])
		qs[time&3] = []stat{}
		for _, s := range curQ {
			if s.x == bx && s.y == by {
				Fprint(out, time)
				return
			}
			if vis[s.x][s.y][s.rot] {
				continue
			}
			vis[s.x][s.y][s.rot] = true
			for rotTimes := 0; rotTimes < 4; rotTimes++ {
				rot := (s.rot + rotTimes) & 3
				door := doorTable[g[s.x][s.y]][rot]
				for _, i := range dirTable[door] {
					dir := dir4[i]
					x, y := s.x+dir[0], s.y+dir[1]
					if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' {
						continue
					}
					door1 := doorTable[g[x][y]][rot]
					if !canBack(i, dirTable[door1]) {
						continue
					}
					tarTime := time + rotTimes + 1
					qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})
				}
			}
		}
	}
	Fprint(out, -1)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF676D(_r io.Reader, _w io.Writer) {
	doorTable := [...]string{
		'+': "++++",
		'-': "-|-|",
		'|': "|-|-",
		'^': "^>v<",
		'>': ">v<^",
		'v': "v<^>",
		'<': "<^>v",
		'U': "URDL",
		'R': "RDLU",
		'D': "DLUR",
		'L': "LURD",
	}
	dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // 上右下左
	dirTable := [...][]int{
		'+': {0, 1, 2, 3},
		'-': {1, 3},
		'|': {0, 2},
		'^': {0},
		'>': {1},
		'v': {2},
		'<': {3},
		'U': {1, 2, 3},
		'R': {0, 2, 3},
		'D': {0, 1, 3},
		'L': {0, 1, 2},
	}
	canBack := func(from int, backs []int) bool {
		for _, back := range backs {
			if (back+2)&3 == from {
				return true
			}
		}
		return false
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, ax, ay, bx, by int
	Fscan(in, &n, &m)
	g := make([][]byte, n)
	for i := range g {
		Fscan(in, &g[i])
	}
	Fscan(in, &ax, &ay, &bx, &by)
	bx--
	by--

	type stat struct{ x, y, rot int }
	qs := [4][]stat{{{ax - 1, ay - 1, 0}}}
	allEmpty := func() bool {
		for _, q := range qs {
			if len(q) > 0 {
				return false
			}
		}
		return true
	}
	vis := [1000][1000][4]bool{}
	for time := 0; !allEmpty(); time++ {
		q := qs[time&3]
		qs[time&3] = []stat{}
		for _, s := range q {
			if s.x == bx && s.y == by {
				Fprint(out, time)
				return
			}
			if vis[s.x][s.y][s.rot] {
				continue
			}
			vis[s.x][s.y][s.rot] = true
			for rotTimes := 0; rotTimes < 4; rotTimes++ {
				rot := (s.rot + rotTimes) & 3
				door := doorTable[g[s.x][s.y]][rot]
				for _, i := range dirTable[door] {
					d := dir4[i]
					x, y := s.x+d[0], s.y+d[1]
					if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' || vis[x][y][rot] {
						continue
					}
					door1 := doorTable[g[x][y]][rot]
					if !canBack(i, dirTable[door1]) {
						continue
					}
					tarTime := time + rotTimes + 1
					qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})
				}
			}
		}
	}
	Fprint(out, -1)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF676D(_r io.Reader, _w io.Writer) {
	doorTable := [125]string{}
	doorTable['+'] = "++++"
	doorTable['-'] = "-|-|"
	doorTable['|'] = "|-|-"
	doorTable['^'] = "^>v<"
	doorTable['>'] = ">v<^"
	doorTable['v'] = "v<^>"
	doorTable['<'] = "<^>v"
	doorTable['U'] = "URDL"
	doorTable['R'] = "RDLU"
	doorTable['D'] = "DLUR"
	doorTable['L'] = "LURD"
	dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // 上右下左
	dirTable := [125][]int{}
	dirTable['+'] = []int{0, 1, 2, 3}
	dirTable['-'] = []int{1, 3}
	dirTable['|'] = []int{0, 2}
	dirTable['^'] = []int{0}
	dirTable['>'] = []int{1}
	dirTable['v'] = []int{2}
	dirTable['<'] = []int{3}
	dirTable['U'] = []int{1, 2, 3}
	dirTable['R'] = []int{0, 2, 3}
	dirTable['D'] = []int{0, 1, 3}
	dirTable['L'] = []int{0, 1, 2}
	canBack := func(from int, backs []int) bool {
		for _, back := range backs {
			if (back+2)&3 == from {
				return true
			}
		}
		return false
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, ax, ay, bx, by int
	Fscan(in, &n, &m)
	g := make([]string, n)
	for i := range g {
		Fscan(in, &g[i])
	}
	Fscan(in, &ax, &ay, &bx, &by)
	bx--
	by--

	type stat struct{ x, y, rot int }
	qs := [4][]stat{{{ax - 1, ay - 1, 0}}}
	vis := [1000][1000][4]bool{}
	isAllQueueEmpty := func() bool {
		for _, q := range qs {
			if len(q) > 0 {
				return false
			}
		}
		return true
	}
	for time := 0; !isAllQueueEmpty(); time++ {
		q := qs[time&3]
		sz := len(q)
		for _, s := range q[:sz] {
			if s.x == bx && s.y == by {
				Fprint(out, time)
				return
			}
			if vis[s.x][s.y][s.rot] {
				continue
			}
			vis[s.x][s.y][s.rot] = true
			for rotTimes := 0; rotTimes < 4; rotTimes++ {
				rot := (s.rot + rotTimes) & 3
				door := doorTable[g[s.x][s.y]][rot]
				for _, i := range dirTable[door] {
					dir := dir4[i]
					x, y := s.x+dir[0], s.y+dir[1]
					if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' || vis[x][y][rot] {
						continue
					}
					door1 := doorTable[g[x][y]][rot]
					if !canBack(i, dirTable[door1]) {
						continue
					}
					tarTime := time + rotTimes + 1
					qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})
				}
			}
		}
		qs[time&3] = qs[time&3][sz:]
	}
	Fprint(out, -1)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF676D(_r io.Reader, _w io.Writer) {
	doorTable := [125]string{}
	doorTable['+'] = "++++"
	doorTable['-'] = "-|-|"
	doorTable['|'] = "|-|-"
	doorTable['^'] = "^>v<"
	doorTable['>'] = ">v<^"
	doorTable['v'] = "v<^>"
	doorTable['<'] = "<^>v"
	doorTable['U'] = "URDL"
	doorTable['R'] = "RDLU"
	doorTable['D'] = "DLUR"
	doorTable['L'] = "LURD"
	dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // 上右下左
	dirTable := [125][]int{}
	dirTable['+'] = []int{0, 1, 2, 3}
	dirTable['-'] = []int{1, 3}
	dirTable['|'] = []int{0, 2}
	dirTable['^'] = []int{0}
	dirTable['>'] = []int{1}
	dirTable['v'] = []int{2}
	dirTable['<'] = []int{3}
	dirTable['U'] = []int{1, 2, 3}
	dirTable['R'] = []int{0, 2, 3}
	dirTable['D'] = []int{0, 1, 3}
	dirTable['L'] = []int{0, 1, 2}
	canBack := func(from int, backs []int) bool {
		for _, back := range backs {
			if (back+2)&3 == from {
				return true
			}
		}
		return false
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, ax, ay, bx, by int
	Fscan(in, &n, &m)
	g := make([]string, n)
	for i := range g {
		Fscan(in, &g[i])
	}
	Fscan(in, &ax, &ay, &bx, &by)
	bx--
	by--

	type stat struct{ x, y, rot int }
	qs := [4][]stat{{{ax - 1, ay - 1, 0}}}
	vis := [1000][1000][4]bool{}
	isAllQueueEmpty := func() bool {
		for _, q := range qs {
			if len(q) > 0 {
				return false
			}
		}
		return true
	}
	for time := 0; !isAllQueueEmpty(); time++ {
		curQ := make([]stat, len(qs[time&3]))
		copy(curQ, qs[time&3])
		qs[time&3] = []stat{}
		for _, s := range curQ {
			if s.x == bx && s.y == by {
				Fprint(out, time)
				return
			}
			if vis[s.x][s.y][s.rot] {
				continue
			}
			vis[s.x][s.y][s.rot] = true
			for rotTimes := 0; rotTimes < 4; rotTimes++ {
				rot := (s.rot + rotTimes) & 3
				door := doorTable[g[s.x][s.y]][rot]
				for _, i := range dirTable[door] {
					dir := dir4[i]
					x, y := s.x+dir[0], s.y+dir[1]
					if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' || vis[x][y][rot] {
						continue
					}
					door1 := doorTable[g[x][y]][rot]
					if !canBack(i, dirTable[door1]) {
						continue
					}
					tarTime := time + rotTimes + 1
					qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})
				}
			}
		}
	}
	Fprint(out, -1)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF676D(_r io.Reader, _w io.Writer) {
	doorTable := [125]string{}
	doorTable['+'] = "++++"
	doorTable['-'] = "-|-|"
	doorTable['|'] = "|-|-"
	doorTable['^'] = "^>v<"
	doorTable['>'] = ">v<^"
	doorTable['v'] = "v<^>"
	doorTable['<'] = "<^>v"
	doorTable['U'] = "URDL"
	doorTable['R'] = "RDLU"
	doorTable['D'] = "DLUR"
	doorTable['L'] = "LURD"
	dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // 上右下左
	dirTable := [125][]int{}
	dirTable['+'] = []int{0, 1, 2, 3}
	dirTable['-'] = []int{1, 3}
	dirTable['|'] = []int{0, 2}
	dirTable['^'] = []int{0}
	dirTable['>'] = []int{1}
	dirTable['v'] = []int{2}
	dirTable['<'] = []int{3}
	dirTable['U'] = []int{1, 2, 3}
	dirTable['R'] = []int{0, 2, 3}
	dirTable['D'] = []int{0, 1, 3}
	dirTable['L'] = []int{0, 1, 2}
	canBack := func(from int, backs []int) bool {
		for _, back := range backs {
			if (back+2)&3 == from {
				return true
			}
		}
		return false
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, ax, ay, bx, by int
	Fscan(in, &n, &m)
	g := make([]string, n)
	for i := range g {
		Fscan(in, &g[i])
	}
	Fscan(in, &ax, &ay, &bx, &by)
	bx--
	by--

	type stat struct{ x, y, rot int }
	qs := [4][]stat{{{ax - 1, ay - 1, 0}}}
	vis := [1000][1000]bool{}
	isAllQueueEmpty := func() bool {
		for _, q := range qs {
			if len(q) > 0 {
				return false
			}
		}
		return true
	}
	for time := 0; !isAllQueueEmpty(); time++ {
		curQ := make([]stat, len(qs[time&3]))
		copy(curQ, qs[time&3])
		qs[time&3] = []stat{}
		for _, s := range curQ {
			if s.x == bx && s.y == by {
				Fprint(out, time)
				return
			}
			if vis[s.x][s.y] {
				continue
			}
			vis[s.x][s.y] = true
			for rotTimes := 0; rotTimes < 4; rotTimes++ {
				rot := (s.rot + rotTimes) & 3
				door := doorTable[g[s.x][s.y]][rot]
				for _, i := range dirTable[door] {
					dir := dir4[i]
					x, y := s.x+dir[0], s.y+dir[1]
					if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' || vis[x][y] {
						continue
					}
					door1 := doorTable[g[x][y]][rot]
					if !canBack(i, dirTable[door1]) {
						continue
					}
					tarTime := time + rotTimes + 1
					qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})
				}
			}
		}
	}
	Fprint(out, -1)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF676D(_r io.Reader, _w io.Writer) {
	doorTable := [125]string{}
	doorTable['+'] = "++++"
	doorTable['-'] = "-|-|"
	doorTable['|'] = "|-|-"
	doorTable['^'] = "^>v<"
	doorTable['>'] = ">v<^"
	doorTable['v'] = "v<^>"
	doorTable['<'] = "<^>v"
	doorTable['U'] = "URDL"
	doorTable['R'] = "RDLU"
	doorTable['D'] = "DLUR"
	doorTable['L'] = "LURD"
	dir4 := [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // 上右下左
	dirTable := [125][]int{}
	dirTable['+'] = []int{0, 1, 2, 3}
	dirTable['-'] = []int{1, 3}
	dirTable['|'] = []int{0, 2}
	dirTable['^'] = []int{0}
	dirTable['>'] = []int{1}
	dirTable['v'] = []int{2}
	dirTable['<'] = []int{3}
	dirTable['U'] = []int{1, 2, 3}
	dirTable['R'] = []int{0, 2, 3}
	dirTable['D'] = []int{0, 1, 3}
	dirTable['L'] = []int{0, 1, 2}
	canBack := func(from int, backs []int) bool {
		for _, back := range backs {
			if (back+2)&3 == from {
				return true
			}
		}
		return false
	}

	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	var n, m, ax, ay, bx, by int
	Fscan(in, &n, &m)
	g := make([]string, n)
	for i := range g {
		Fscan(in, &g[i])
	}
	Fscan(in, &ax, &ay, &bx, &by)
	bx--
	by--

	type stat struct{ x, y, rot int }
	qs := [4][]stat{{{ax - 1, ay - 1, 0}}}
	vis := [1000][1000]bool{}
	isAllQueueEmpty := func() bool {
		for _, q := range qs {
			if len(q) > 0 {
				return false
			}
		}
		return true
	}
	for time := 0; !isAllQueueEmpty(); time++ {
		curQ := make([]stat, len(qs[time&3]))
		copy(curQ, qs[time&3])
		qs[time&3] = []stat{}
		for _, s := range curQ {
			if s.x == bx && s.y == by {
				Fprint(out, time)
				return
			}
			if vis[s.x][s.y] {
				continue
			}
			vis[s.x][s.y] = true
			for rotTimes := 0; rotTimes < 4; rotTimes++ {
				rot := (s.rot + rotTimes) & 3
				door := doorTable[g[s.x][s.y]][rot]
				for _, i := range dirTable[door] {
					dir := dir4[i]
					x, y := s.x+dir[0], s.y+dir[1]
					if x < 0 || x >= n || y < 0 || y >= m || g[x][y] == '*' {
						continue
					}
					door1 := doorTable[g[x][y]][rot]
					if !canBack(i, dirTable[door1]) {
						continue
					}
					tarTime := time + rotTimes + 1
					qs[tarTime&3] = append(qs[tarTime&3], stat{x, y, rot})
				}
			}
		}
	}
	Fprint(out, -1)
}

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


package main

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

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

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

func calculateK(diff []int64) int64 {
	var K int64
	for i := 0; i < len(diff); i++ {
		if diff[i] > 0 {
			K += diff[i]
		}
	}
	return K
}

func changeK(K int64, l int, r int, x int64, diff []int64) int64 {
	if x == 0 {
		return K
	}

	if l > 0 {
		if diff[l-1] > 0 {
			K -= diff[l-1]
		}

		if diff[l-1]+x > 0 {
			K += diff[l-1] + x
		}
	}

	if r < len(diff) {
		if diff[r] > 0 {
			K -= diff[r]
		}

		if diff[r]-x > 0 {
			K += diff[r] - x
		}
	}

	if l > 0 {
		diff[l-1] += x
	}

	if r < len(diff) {
		diff[r] -= x
	}

	return K
}

func calculateMax(a0 int64, K int64) int64 {
	c0 := (a0 + K) / 2
	b0 := a0 - c0
	b := b0 + K

	if c0 > b {
		return c0
	}
	return b
}

func main() {
	defer writer.Flush()

	var n int
	scan(&n)

	diff := make([]int64, n-1)
	var prevA, a0, a int64
	scan(&a0)
	prevA = a0
	for i := 0; i < n-1; i++ {
		scan(&a)
		diff[i] = a - prevA
		prevA = a
	}

	K := calculateK(diff)
	println(calculateMax(a0, K))
	var q, l, r int
	var x int64
	scan(&q)

	for i := 0; i < q; i++ {
		scan(&l, &r, &x)
		l--
		r--
		if l == 0 {
			a0 += x
		}

		K = changeK(K, l, r, x, diff)
		println(calculateMax(a0, K))
	}
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()
	a := io.NextInts(n)

	asc, dsc := make([]int64, n+1), make([]int64, n+1)
	for i := 1; i <= n; i++ {
		dif := int64(a[i-1]) - (asc[i-1] + dsc[i-1])
		asc[i], dsc[i] = asc[i-1], dsc[i-1]
		if dif >= 0 {
			asc[i] += dif
		} else {
			dsc[i] += dif
		}
	}
	ast, dst := NewLazySegTree(n), NewLazySegTree(n)
	for i := 0; i < n; i++ {
		ast.Update(i, i+1, asc[i+1])
		dst.Update(i, i+1, dsc[i+1])
	}

	getAns := func() int64 {
		sum := ast.Query(0, n) + dst.Query(0, n)
		if sum < 0 {
			return sum / 2
		}
		return (sum + 1) / 2
	}

	fix := func(i int) {
		if i > n {
			return
		}
		adif, ddif := ast.Query(i, i+1), dst.Query(i, i+1)
		if i > 0 {
			adif -= ast.Query(i-1, i)
			ddif -= dst.Query(i-1, i)
		}
		dif := max(-adif, ddif)
		ast.Update(i, n, dif)
		dst.Update(i, n, -dif)
	}

	io.Println(getAns())
	for q := io.NextInt(); q > 0; q-- {
		l, r, x := io.NextInt()-1, io.NextInt(), int64(io.NextInt())
		if x >= 0 {
			ast.Update(l, n, x)
			dst.Update(r, n, -x)
		} else {
			dst.Update(l, n, x)
			ast.Update(r, n, -x)
		}
		fix(l)
		fix(r)
		io.Println(getAns())
	}
}

type LazySegTree struct {
	n    int
	data []int64
	lazy []int64
}

func NewLazySegTree(n int) *LazySegTree {
	return &LazySegTree{n, make([]int64, 4*n), make([]int64, 4*n)}
}

func (t *LazySegTree) Update(l, r int, v int64) { t.update(1, 0, t.n, l, r, v) }
func (t *LazySegTree) Query(l, r int) int64     { return t.query(1, 0, t.n, l, r) }

func (t *LazySegTree) get(i int) int64 { return t.data[i] + t.lazy[i] }

func (t *LazySegTree) update(i, s, e, l, r int, v int64) {
	if e <= l || r <= s {
		return
	}
	if l <= s && e <= r {
		t.lazy[i] += v
		return
	}
	m := (s + e) / 2
	t.update(i*2, s, m, l, r, v)
	t.update(i*2+1, m, e, l, r, v)
	t.data[i] = max(t.get(i*2), t.get(i*2+1))
}

func (t *LazySegTree) query(i, s, e, l, r int) int64 {
	if e <= l || r <= s {
		return math.MinInt64
	}
	if l <= s && e <= r {
		return t.get(i)
	}
	m := (s + e) / 2
	return t.lazy[i] + max(t.query(i*2, s, m, l, r), t.query(i*2+1, m, e, l, r))
}

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

var rw = NewFastIO(os.Stdin, os.Stdout)

func max(u, v int64) int64 {
	if u > v {
		return u
	}
	return v
}
func main() {
	defer rw.Flush()
	n := rw.NextInt()
	a := rw.NextInt64s(n)
	fen := make([]int64, n+5)
	add := func(p, v int) {
		p += 2
		for ; p < n+5; p += p & -p {
			fen[p] += int64(v)
		}
	}
	sum := func(p int) int64 {
		p += 2
		res := int64(0)
		for ; p > 0; p -= p & -p {
			res += fen[p]
		}
		return res
	}
	ask := func(p int) int64 {
		return a[p] + sum(p)
	}
	del := int64(0)
	for i := 1; i < n; i++ {
		del += max(0, a[i]-a[i-1])
	}
	ans := ask(0) + del
	if ans%2 == 1 {
		ans++
	}
	rw.Println(ans / 2)
	for q := rw.NextInt(); q > 0; q-- {
		l, r, x := rw.NextInt()-1, rw.NextInt()-1, rw.NextInt()
		if l > 0 {
			del -= max(0, ask(l)-ask(l-1))
		}
		if r+1 < n {
			del -= max(0, ask(r+1)-ask(r))
		}
		add(l, x)
		add(r+1, -x)
		if l > 0 {
			del += max(0, ask(l)-ask(l-1))
		}
		if r+1 < n {
			del += max(0, ask(r+1)-ask(r))
		}
		ans := ask(0) + del
		if ans%2 == 1 {
			ans++
		}
		rw.Println(ans / 2)
	}
}

// @meooow template uwu
type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) Next() string {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	var s []byte
	for ; c > ' '; c = f.readC() {
		s = append(s, c)
	}
	return string(s)
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}
func (f *FastIO) NextInt64() int64 {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := int64(0)
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int64(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}
func (f *FastIO) NextInt64s(n int) []int64 {
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt64()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

func (f *FastIO) Print(a ...interface{}) {
	fmt.Fprint(f, a...)
}
func (f *FastIO) PrintInts(a ...int) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInts(a ...int) {
	f.PrintInts(a...)
	f.Println()
}
func (f *FastIO) PrintInt64s(a ...int64) {
	if len(a) > 0 {
		f.Print(a[0])
		for _, x := range a[1:] {
			f.Print(" ", x)
		}
	}
}
func (f *FastIO) PrintlnInt64s(a ...int64) {
	f.PrintInt64s(a...)
	f.Println()
}

package main

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

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

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

	s := int64(a[0])
	d := make([]int64, n)
	for i := 1; i < n; i++ {
		d[i] = int64(a[i] - a[i-1])
		s += max(0, d[i])
	}

	ans := func() int64 {
		if s < 0 {
			return s / 2
		}
		return (s + 1) / 2
	}
	fmt.Fprintln(out, ans())

	var q int
	fmt.Fscan(in, &q)
	for ; q > 0; q-- {
		var l, r, x int
		fmt.Fscan(in, &l, &r, &x)
		if l == 1 {
			s += int64(x)
		} else {
			s -= max(0, d[l-1])
			d[l-1] += int64(x)
			s += max(0, d[l-1])
		}
		if r < n {
			s -= max(0, d[r])
			d[r] -= int64(x)
			s += max(0, d[r])
		}
		fmt.Fprintln(out, ans())
	}
}

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

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()
	a := io.NextInts(n)

	asc, dsc := make([]int64, n+1), make([]int64, n+1)
	for i := 1; i <= n; i++ {
		dif := int64(a[i-1]) - (asc[i-1] + dsc[i-1])
		asc[i], dsc[i] = asc[i-1], dsc[i-1]
		if dif >= 0 {
			asc[i] += dif
		} else {
			dsc[i] += dif
		}
	}
	ast, dst := NewLazySegTree(n), NewLazySegTree(n)
	for i := 0; i < n; i++ {
		ast.Update(i, i+1, asc[i+1])
		dst.Update(i, i+1, dsc[i+1])
	}

	getAns := func() int64 {
		aa := make([]int64, n)
		dd := make([]int64, n)
		for i := range aa {
			aa[i] = ast.Query(i, i+1)
			dd[i] = dst.Query(i, i+1)
		}
		sum := ast.Query(0, n) + dst.Query(0, n)
		if sum > 0 {
			return (sum + 1) / 2
		}
		return (sum - 1) / 2
	}

	io.Println(getAns())
	for q := io.NextInt(); q > 0; q-- {
		l, r, x := io.NextInt()-1, io.NextInt(), int64(io.NextInt())
		if x >= 0 {
			y := dst.Query(l, l+1) - dst.Query(l-1, l)
			dif := max(-x, y)
			dst.Update(l, r, -dif)
			ast.Update(r, n, dif)
			x += dif
			ast.Update(l, r, x)
			dst.Update(r, n, -x)
		} else {
			y := ast.Query(l, l+1) - ast.Query(l-1, l)
			dif := max(x, -y)
			ast.Update(l, r, dif)
			dst.Update(r, n, -dif)
			x -= dif
			dst.Update(l, r, x)
			ast.Update(r, n, -x)
		}
		io.Println(getAns())
	}
}

type dType = int64
type lType = int64

const identity = math.MinInt64

type LazySegTree struct {
	n    int
	data []dType
	lazy []lType
}

func NewLazySegTree(n int) *LazySegTree {
	return &LazySegTree{n, make([]dType, 4*n), make([]lType, 4*n)}
}

func (t *LazySegTree) Update(l, r int, v dType) { t.update(1, 0, t.n, l, r, v) }
func (t *LazySegTree) Query(l, r int) dType     { return t.query(1, 0, t.n, l, r) }

func (t *LazySegTree) get(i int) dType { return t.data[i] + t.lazy[i] }

func (t *LazySegTree) update(i, s, e, l, r int, v dType) {
	if e <= l || r <= s {
		return
	}
	if l <= s && e <= r {
		t.lazy[i] += v
		return
	}
	m := (s + e) / 2
	t.update(i*2, s, m, l, r, v)
	t.update(i*2+1, m, e, l, r, v)
	t.data[i] = max(t.get(i*2), t.get(i*2+1))
}

func (t *LazySegTree) query(i, s, e, l, r int) dType {
	if e <= l || r <= s {
		return identity
	}
	if l <= s && e <= r {
		return t.get(i)
	}
	m := (s + e) / 2
	return t.lazy[i] + max(t.query(i*2, s, m, l, r), t.query(i*2+1, m, e, l, r))
}

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

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()
	a := io.NextInts(n)

	asc, dsc := make([]int64, n+1), make([]int64, n+1)
	for i := 1; i <= n; i++ {
		dif := int64(a[i-1]) - (asc[i-1] + dsc[i-1])
		asc[i], dsc[i] = asc[i-1], dsc[i-1]
		if dif >= 0 {
			asc[i] += dif
		} else {
			dsc[i] += dif
		}
	}
	ast, dst := NewLazySegTree(n), NewLazySegTree(n)
	for i := 0; i < n; i++ {
		ast.Update(i, i+1, asc[i+1])
		dst.Update(i, i+1, dsc[i+1])
	}

	getAns := func() int64 {
		sum := ast.Query(0, n) + dst.Query(0, n)
		if sum < 0 {
			return sum / 2
		}
		return (sum + 1) / 2
	}

	fix := func(i int) {
		adif, ddif := ast.Query(i, i+1), dst.Query(i, i+1)
		if i > 0 {
			adif -= ast.Query(i-1, i)
			ddif -= dst.Query(i-1, i)
		}
		dif := max(-adif, ddif)
		ast.Update(i, n, dif)
		dst.Update(i, n, dif)
	}

	io.Println(getAns())
	for q := io.NextInt(); q > 0; q-- {
		l, r, x := io.NextInt()-1, io.NextInt(), int64(io.NextInt())
		if x >= 0 {
			ast.Update(l, n, x)
			dst.Update(r, n, -x)
		} else {
			dst.Update(l, n, x)
			ast.Update(r, n, -x)
		}
		fix(l)
		fix(r)
		io.Println(getAns())
	}
}

type LazySegTree struct {
	n    int
	data []int64
	lazy []int64
}

func NewLazySegTree(n int) *LazySegTree {
	return &LazySegTree{n, make([]int64, 4*n), make([]int64, 4*n)}
}

func (t *LazySegTree) Update(l, r int, v int64) { t.update(1, 0, t.n, l, r, v) }
func (t *LazySegTree) Query(l, r int) int64     { return t.query(1, 0, t.n, l, r) }

func (t *LazySegTree) get(i int) int64 { return t.data[i] + t.lazy[i] }

func (t *LazySegTree) update(i, s, e, l, r int, v int64) {
	if e <= l || r <= s {
		return
	}
	if l <= s && e <= r {
		t.lazy[i] += v
		return
	}
	m := (s + e) / 2
	t.update(i*2, s, m, l, r, v)
	t.update(i*2+1, m, e, l, r, v)
	t.data[i] = max(t.get(i*2), t.get(i*2+1))
}

func (t *LazySegTree) query(i, s, e, l, r int) int64 {
	if e <= l || r <= s {
		return math.MinInt64
	}
	if l <= s && e <= r {
		return t.get(i)
	}
	m := (s + e) / 2
	return t.lazy[i] + max(t.query(i*2, s, m, l, r), t.query(i*2+1, m, e, l, r))
}

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()
	a := io.NextInts(n)

	asc, dsc := make([]int64, n+1), make([]int64, n+1)
	for i := 1; i <= n; i++ {
		dif := int64(a[i-1]) - (asc[i-1] + dsc[i-1])
		asc[i], dsc[i] = asc[i-1], dsc[i-1]
		if dif >= 0 {
			asc[i] += dif
		} else {
			dsc[i] += dif
		}
	}
	ast, dst := NewLazySegTree(n), NewLazySegTree(n)
	for i := 0; i < n; i++ {
		ast.Update(i, i+1, asc[i+1])
		dst.Update(i, i+1, dsc[i+1])
	}

	getAns := func() int64 {
		aa := make([]int64, n)
		dd := make([]int64, n)
		for i := range aa {
			aa[i] = ast.Query(i, i+1)
			dd[i] = dst.Query(i, i+1)
		}
		sum := ast.Query(0, n) + dst.Query(0, n)
		if sum < 0 {
			return sum / 2
		}
		return (sum + 1) / 2
	}

	io.Println(getAns())
	for q := io.NextInt(); q > 0; q-- {
		l, r, x := io.NextInt()-1, io.NextInt(), int64(io.NextInt())
		if x >= 0 {
			y := dst.Query(l, l+1) - max(0, dst.Query(l-1, l))
			dif := max(-x, y)
			dst.Update(l, r, -dif)
			ast.Update(r, n, dif)
			x += dif
			ast.Update(l, r, x)
			dst.Update(r, n, -x)
		} else {
			y := ast.Query(l, l+1) - max(0, ast.Query(l-1, l))
			dif := max(x, -y)
			ast.Update(l, r, dif)
			dst.Update(r, n, -dif)
			x -= dif
			dst.Update(l, r, x)
			ast.Update(r, n, -x)
		}
		io.Println(getAns())
	}
}

type dType = int64
type lType = int64

const identity = math.MinInt64

type LazySegTree struct {
	n    int
	data []dType
	lazy []lType
}

func NewLazySegTree(n int) *LazySegTree {
	return &LazySegTree{n, make([]dType, 4*n), make([]lType, 4*n)}
}

func (t *LazySegTree) Update(l, r int, v dType) { t.update(1, 0, t.n, l, r, v) }
func (t *LazySegTree) Query(l, r int) dType     { return t.query(1, 0, t.n, l, r) }

func (t *LazySegTree) get(i int) dType { return t.data[i] + t.lazy[i] }

func (t *LazySegTree) update(i, s, e, l, r int, v dType) {
	if e <= l || r <= s {
		return
	}
	if l <= s && e <= r {
		t.lazy[i] += v
		return
	}
	m := (s + e) / 2
	t.update(i*2, s, m, l, r, v)
	t.update(i*2+1, m, e, l, r, v)
	t.data[i] = max(t.get(i*2), t.get(i*2+1))
}

func (t *LazySegTree) query(i, s, e, l, r int) dType {
	if e <= l || r <= s {
		return identity
	}
	if l <= s && e <= r {
		return t.get(i)
	}
	m := (s + e) / 2
	return t.lazy[i] + max(t.query(i*2, s, m, l, r), t.query(i*2+1, m, e, l, r))
}

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

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()
	a := io.NextInts(n)

	asc, dsc := make([]int64, n+1), make([]int64, n+1)
	for i := 1; i <= n; i++ {
		dif := int64(a[i-1]) - (asc[i-1] + dsc[i-1])
		asc[i], dsc[i] = asc[i-1], dsc[i-1]
		if dif >= 0 {
			asc[i] += dif
		} else {
			dsc[i] += dif
		}
	}
	ast, dst := NewLazySegTree(n), NewLazySegTree(n)
	for i := 0; i < n; i++ {
		ast.Update(i, i+1, asc[i+1])
		dst.Update(i, i+1, dsc[i+1])
	}

	getAns := func() int64 {
		aa := make([]int64, n)
		dd := make([]int64, n)
		for i := range aa {
			aa[i] = ast.Query(i, i+1)
			dd[i] = dst.Query(i, i+1)
		}
		sum := ast.Query(0, n) + dst.Query(0, n)
		if sum < 0 {
			return sum / 2
		}
		return (sum + 1) / 2
	}

	io.Println(getAns())
	for q := io.NextInt(); q > 0; q-- {
		l, r, x := io.NextInt()-1, io.NextInt(), int64(io.NextInt())
		if x >= 0 {
			y := dst.Query(l, l+1) - dst.Query(l-1, l)
			if l == 0 {
				y = dst.Query(l, l+1)
			}
			dif := max(-x, y)
			dst.Update(l, r, -dif)
			ast.Update(r, n, dif)
			x += dif
			ast.Update(l, r, x)
			dst.Update(r, n, -x)
		} else {
			y := ast.Query(l, l+1) - ast.Query(l-1, l)
			if l == 0 {
				y = ast.Query(l, l+1)
			}
			dif := max(x, -y)
			ast.Update(l, r, dif)
			dst.Update(r, n, -dif)
			x -= dif
			dst.Update(l, r, x)
			ast.Update(r, n, -x)
		}
		io.Println(getAns())
	}
}

type dType = int64
type lType = int64

const identity = math.MinInt64

type LazySegTree struct {
	n    int
	data []dType
	lazy []lType
}

func NewLazySegTree(n int) *LazySegTree {
	return &LazySegTree{n, make([]dType, 4*n), make([]lType, 4*n)}
}

func (t *LazySegTree) Update(l, r int, v dType) { t.update(1, 0, t.n, l, r, v) }
func (t *LazySegTree) Query(l, r int) dType     { return t.query(1, 0, t.n, l, r) }

func (t *LazySegTree) get(i int) dType { return t.data[i] + t.lazy[i] }

func (t *LazySegTree) update(i, s, e, l, r int, v dType) {
	if e <= l || r <= s {
		return
	}
	if l <= s && e <= r {
		t.lazy[i] += v
		return
	}
	m := (s + e) / 2
	t.update(i*2, s, m, l, r, v)
	t.update(i*2+1, m, e, l, r, v)
	t.data[i] = max(t.get(i*2), t.get(i*2+1))
}

func (t *LazySegTree) query(i, s, e, l, r int) dType {
	if e <= l || r <= s {
		return identity
	}
	if l <= s && e <= r {
		return t.get(i)
	}
	m := (s + e) / 2
	return t.lazy[i] + max(t.query(i*2, s, m, l, r), t.query(i*2+1, m, e, l, r))
}

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

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

func main() {
	io := NewFastIO(os.Stdin, os.Stdout)
	defer io.Flush()

	n := io.NextInt()
	a := io.NextInts(n)

	asc, dsc := make([]int64, n+1), make([]int64, n+1)
	for i := 1; i <= n; i++ {
		dif := int64(a[i-1]) - (asc[i-1] + dsc[i-1])
		asc[i], dsc[i] = asc[i-1], dsc[i-1]
		if dif >= 0 {
			asc[i] += dif
		} else {
			dsc[i] += dif
		}
	}
	ast, dst := NewLazySegTree(n), NewLazySegTree(n)
	for i := 0; i < n; i++ {
		ast.Update(i, i+1, asc[i+1])
		dst.Update(i, i+1, dsc[i+1])
	}

	getAns := func() int64 {
		aa := make([]int64, n)
		dd := make([]int64, n)
		for i := range aa {
			aa[i] = ast.Query(i, i+1)
			dd[i] = dst.Query(i, i+1)
		}
		sum := ast.Query(0, n) + dst.Query(0, n)
		if sum < 0 {
			return sum / 2
		}
		return (sum + 1) / 2
	}

	io.Println(getAns())
	for q := io.NextInt(); q > 0; q-- {
		l, r, x := io.NextInt()-1, io.NextInt(), int64(io.NextInt())
		if x >= 0 {
			y := dst.Query(l, l+1) - dst.Query(l-1, l)
			dif := max(-x, y)
			dst.Update(l, r, -dif)
			ast.Update(r, n, dif)
			x += dif
			ast.Update(l, r, x)
			dst.Update(r, n, -x)
		} else {
			y := ast.Query(l, l+1) - ast.Query(l-1, l)
			dif := max(x, -y)
			ast.Update(l, r, dif)
			dst.Update(r, n, -dif)
			x -= dif
			dst.Update(l, r, x)
			ast.Update(r, n, -x)
		}
		io.Println(getAns())
	}
}

type dType = int64
type lType = int64

const identity = math.MinInt64

type LazySegTree struct {
	n    int
	data []dType
	lazy []lType
}

func NewLazySegTree(n int) *LazySegTree {
	return &LazySegTree{n, make([]dType, 4*n), make([]lType, 4*n)}
}

func (t *LazySegTree) Update(l, r int, v dType) { t.update(1, 0, t.n, l, r, v) }
func (t *LazySegTree) Query(l, r int) dType     { return t.query(1, 0, t.n, l, r) }

func (t *LazySegTree) get(i int) dType { return t.data[i] + t.lazy[i] }

func (t *LazySegTree) update(i, s, e, l, r int, v dType) {
	if e <= l || r <= s {
		return
	}
	if l <= s && e <= r {
		t.lazy[i] += v
		return
	}
	m := (s + e) / 2
	t.update(i*2, s, m, l, r, v)
	t.update(i*2+1, m, e, l, r, v)
	t.data[i] = max(t.get(i*2), t.get(i*2+1))
}

func (t *LazySegTree) query(i, s, e, l, r int) dType {
	if e <= l || r <= s {
		return identity
	}
	if l <= s && e <= r {
		return t.get(i)
	}
	m := (s + e) / 2
	return t.lazy[i] + max(t.query(i*2, s, m, l, r), t.query(i*2+1, m, e, l, r))
}

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

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

type FastIO struct {
	*bufio.Writer
	r    io.Reader
	buf  [8192]byte
	i, n int
}

func NewFastIO(r io.Reader, w io.Writer) *FastIO {
	return &FastIO{r: r, Writer: bufio.NewWriter(w)}
}

func (f *FastIO) readC() byte {
	if f.i == f.n {
		f.n, _ = f.r.Read(f.buf[:])
		if f.n == 0 {
			panic("EOF")
		}
		f.i = 0
	}
	f.i++
	return f.buf[f.i-1]
}

func (f *FastIO) NextInt() int {
	c := f.readC()
	for ; c <= ' '; c = f.readC() {
	}
	neg := false
	if c == '-' {
		neg = true
		c = f.readC()
	}
	n := 0
	for ; '0' <= c && c <= '9'; c = f.readC() {
		n = n*10 + int(c) - '0'
	}
	if neg {
		return -n
	}
	return n
}

func (f *FastIO) NextInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = f.NextInt()
	}
	return a
}

func (f *FastIO) Println(a ...interface{}) {
	fmt.Fprintln(f, a...)
}

package main

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

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

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

func calculateK(a []int64) int64 {
	var K int64
	for i := 0; i < len(a)-1; i++ {
		if a[i+1] > a[i] {
			K += a[i+1] - a[i]
		}
	}
	return K
}

func changeK(K int64, l int, r int, x int64, a []int64) int64 {
	if l > 0 {
		if a[l] - a[l - 1] > 0 {
			K += x
		} else if a[l] + x - a[l - 1] > 0 {
			K += a[l] + x - a[l - 1]
		}
	}

	if r + 1 < len(a) {
		if a[r + 1] - a[r] > 0 {
			K += x
		} else if a[r + 1] + x - a[r] > 0 {
			K += a[r + 1] + x - a[r]
		}
	}

	for i := l; i <= r; i ++ {
		a[i] += x
	}

	return K
}

func calculateMax(a []int64, K int64) int64 {
	c := (a[0] + K) / 2
	b := a[0] - c + K

	if c > b {
		return c
	}
	return b
}

func main() {
	defer writer.Flush()

	var n int
	scan(&n)
	a := make([]int64, n)
	for i := 0; i < n; i ++ {
		scan(&a[i])
	}

	K := calculateK(a)
	println(calculateMax(a, K))
	var q, l, r int
	var x int64
	scan(&q)

	for i := 0; i < q; i ++ {
		scan(&l, &r, &x)
		l --
		r --
		K = changeK(K, l, r, x, a)
		println(calculateMax(a, K))
	}
}

package main

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

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

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

func calculateK(diff []int64) int64 {
	var K int64
	for i := 0; i < len(diff); i++ {
		if diff[i] > 0 {
			K += diff[i]
		}
	}
	return K
}

func changeK(K int64, l int, r int, x int64, diff []int64, a0 *int64) int64 {
	if l == 0 {
		*a0 += x
	}

	if x == 0 {
		return K
	}

	if l > 0 {
		if diff[l-1] > 0 {
			K -= diff[l-1]
		}

		if diff[l-1]+x > 0 {
			K += diff[l-1] + x
		}
	}

	if r < len(diff) {
		if diff[r] > 0 {
			K -= diff[r]
		}

		if diff[r]-x > 0 {
			K += diff[r] - x
		}
	}

	if l > 0 && r != l {
		diff[l-1] += x
	}

	if r < len(diff) {
		diff[r] += x
	}

	return K
}

func calculateMax(a0 int64, K int64) int64 {
	c := (a0 + K) / 2
	b := a0 - c + K

	if c > b {
		return c
	}
	return b
}

func main() {
	defer writer.Flush()

	var n int
	scan(&n)

	diff := make([]int64, n-1)
	var prevA, a0, a int64
	scan(&a0)
	prevA = a0
	for i := 0; i < n-1; i++ {
		scan(&a)
		diff[i] = a - prevA
		prevA = a
	}

	K := calculateK(diff)
	println(calculateMax(a0, K))
	var q, l, r int
	var x int64
	scan(&q)

	for i := 0; i < q; i++ {
		scan(&l, &r, &x)
		l--
		r--
		K = changeK(K, l, r, x, diff, &a0)
		println(calculateMax(a0, K))
	}
}


package main

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

const sz = 8
const maxN = 1001
type Perm [sz]int
type Perms []Perm

func (p Perm) sum() int {
    s := 0
    for _, v := range p {
        s += v
    }
    return s
}

func perms() Perms {
    res := Perms{}

    cur, was := Perm{}, [sz]bool{}
    var gen func(j int)
    gen = func(j int) {
        if j == sz {
            res = append(res, cur)
            return
        }
        for i := 0; i < sz; i++ {
            if !was[i] {
                cur[j], was[i] = i, true
                gen(j+1)
                was[i] = false
            }
        }
    }
    gen(0)
    return res
}

func combs(k int) Perms {
    res := Perms{}

    cur := Perm{}
    var gen func(j int)
    gen = func(j int) {
        if j == sz {
            res = append(res, cur)
            return
        }
        for _, v := range []int{k, k+1} {
            cur[j] = v
            gen(j+1)
        }
    }
    gen(0)

    return res[:len(res)-1]
}

func options(n int) []Perms {
    res := make([]Perms, n+1)

    for k := 0; k <= n; k++ {
        for _, p := range combs(k) {
            s := p.sum()
            if s < len(res) {
                res[s] = append(res[s], p)
            }
        }
    }

    return res
}

type Index [sz][maxN][maxN]int

func buildIndex(a []int) Index {
    res := Index{}
    n := len(a)

    for d := 0; d < sz; d++ {
        for i := 0; i <= n; i++ {
            for k := 0; k < n; k++ {
                cur := i
                if k > 0 {
                    cur = -1
                    if i < n {
                        if prev := res[d][i][k-1]; prev >= 0 {
                            for j := prev; j < n; j++ {
                                if a[j] == d+1 {
                                    cur = j+1
                                    break
                                }
                            }
                        }
                    }
                }
                res[d][i][k] = cur
            }
        }
    }

    return res
}

func solve() {
    n := readInt()
    a := readIntArray(n)
    index := buildIndex(a)
    ops := options(n)

    try := func(order Perm, s int) bool {
        for _, o := range ops[s] {
            cur := 0
            for _, d := range order {
                if cur < 0 {
                    break
                }
                cur = index[d][cur][o[d]]
            }
            if cur >= 0 {
                return true
            }
        }
        return false
    }

    ans := 0
    for _, order := range perms() {
        l, r := 0, len(ops)-1
        for l < r {
            if mid := (l+r+1) / 2; try(order, mid) {
                l = mid
            } else {
                r = mid-1
            }
        }
        ans = max(ans, l)
    }
    fmt.Println(ans)
}

// 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 (
	"fmt"
)

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

func compSkip(a []int, n int) [][]int{

	m := n / 8 + 2
	skip := make([][]int, n)

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

	for i := 0; i < n; i++ {
		skip[i][0] = i
		cnt := 0
		for j := i; j >= 0; j-- {
			if a[i] == a[j] {
				cnt ++
				if cnt < m {
					skip[i][cnt] = j - 1
				} else {
					break
				}
			}
		}
	}

	return skip
}

func compNear(a []int, n int) [][]int{

	near := make([][]int, n)
	last := make([]int, 9)

	for i := 0; i < 9; i++ {
		last[i] = -1
	}

	for i := 0; i < n; i++ {
		near[i] = make([]int, 9)
		last[a[i]] = i
		for j := 0; j < 9; j++ {
			near[i][j] = last[j]
		}
	}

	return near
}

func jump(p, num, k int, skip, nearest [][]int) int{
	if k == 0 {
		return p
	}
	p = nearest[p][num + 1]
	if p == -1 {
		return -2
	}
	return skip[p][k]
}

func case0(a []int, n int) int{
	present := make([]bool, 8)
	res := 0
	for i := 0; i < n; i++ {
		if !present[a[i] - 1]{
			res ++
			present[a[i] - 1] = true
		}
	}
	return res
}

func try(a []int, skip, nearest [][]int, n, k int) (bool, int) {
	//fmt.Println("try", k)
	if k == 0 {
		return true, case0(a, n)
	}
	var dp = make([][]int, n)
	power := [8]int{1, 2, 4, 8, 16, 32, 64, 128}

	for i := 0; i < n; i++ {
		dp[i] = make([]int, 256)
		dp[i][0] = 0
		for mask := 1; mask < 256; mask++ {
			dp[i][mask] = -10000
			for j := 0; j < 8; j++ {
				if (power[j] & mask) == 0 {
					continue
				}
				jumpRes := jump(i, j, k, skip, nearest)
				if jumpRes != -2 {
					if jumpRes == -1 {
						if mask == power[j] {
							dp[i][mask] = k
						}
					} else {
						dp[i][mask] = max(dp[i][mask], dp[jumpRes][mask - power[j]] + k)
					}
				}
				jumpRes = jump(i, j, k + 1, skip, nearest)
				if jumpRes != -2 {
					if jumpRes == -1 {
						if mask == power[j] {
							dp[i][mask] = k + 1
						}
					} else {
						dp[i][mask] = max(dp[i][mask], dp[jumpRes][mask - power[j]] + k + 1)
					}
				}
			}
		}
	}
	return (dp[n - 1][255] > 0), dp[n - 1][255]
}

func solve(a []int, n int) int {
	beg := 0
	skip := compSkip(a, n)
	near := compNear(a, n)
	end := n / 8
	for beg + 1 < end {
		sr := (beg + end) /2
		if b, _ := try(a, skip, near, n, sr); b {
			beg = sr
		} else {
			end = sr - 1
		}
	}

	b, r := try(a, skip, near, n, end)

	if b {
		return r
	}

	_, r = try(a, skip, near, n, beg)

	return r
}

func main() {
	var n, x int
	fmt.Scan(&n)
	var a []int
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		a = append(a, x)
	}
	fmt.Println(solve(a, n))
}

package main

import (
	"fmt"
)

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

func compSkip(a []int, n int) [][]int{

	m := n / 8 + 2
	skip := make([][]int, n)

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

	for i := 0; i < n; i++ {
		skip[i][0] = i
		cnt := 0
		for j := i; j >= 0; j-- {
			if a[i] == a[j] {
				cnt ++
				if cnt < m {
					skip[i][cnt] = j - 1
				} else {
					break
				}
			}
		}
	}

	return skip
}

func compNear(a []int, n int) [][]int{

	near := make([][]int, n)
	last := make([]int, 9)

	for i := 0; i < 9; i++ {
		last[i] = -1
	}

	for i := 0; i < n; i++ {
		near[i] = make([]int, 9)
		last[a[i]] = i
		for j := 0; j < 9; j++ {
			near[i][j] = last[j]
		}
	}

	return near
}

func jump(p, num, k int, skip, nearest [][]int) int{
	if k == 0 {
		return p
	}
	p = nearest[p][num + 1]
	if p == -1 {
		return -2
	}
	return skip[p][k]
}

func case0(a []int, n int) int{
	present := make([]bool, 8)
	res := 0
	for i := 0; i < n; i++ {
		if !present[a[i] - 1]{
			res ++
			present[a[i] - 1] = true
		}
	}
	return res
}

func try(a []int, skip, nearest [][]int, n, k int) (bool, int) {
	if k == 0 {
		return true, case0(a, n)
	}
	var dp = make([][]int, n)
	power := [8]int{1, 2, 4, 8, 16, 32, 64, 128}

	for i := 0; i < n; i++ {
		dp[i] = make([]int, 256)
		dp[i][0] = 0
		for mask := 1; mask < 256; mask++ {
			dp[i][mask] = -10000
			for j := 0; j < 8; j++ {
				if (power[j] & mask) == 0 {
					continue
				}
				jumpRes := jump(i, j, k, skip, nearest)
				if jumpRes != -2 {
					if jumpRes == -1 {
						if mask == power[j] {
							dp[i][mask] = k
						} else {
							dp[i][mask] = -10000
						}
					} else {
						dp[i][mask] = max(dp[i][mask], dp[jump(i, j, k, skip, nearest)][mask - power[j]] + k)
					}
				}
				jumpRes = jump(i, j, k + 1, skip, nearest)
				if jumpRes != -2 {
					if jumpRes == -1 {
						if mask == power[j] {
							dp[i][mask] = k + 1
						} else {
							dp[i][mask] = -10000
						}
					} else {
						dp[i][mask] = max(dp[i][mask], dp[jump(i, j, k + 1, skip, nearest)][mask - power[j]] + k + 1)
					}
				}
			}
		}
	}
	//fmt.Println(dp[
	return (dp[n - 1][255] > 0), dp[n - 1][255]
}

func solve(a []int, n int) int {
	beg := 0
	skip := compSkip(a, n)
	near := compNear(a, n)
	end := n / 8
	for beg + 1 < end {
		sr := (beg + end) /2
		if b, _ := try(a, skip, near, n, sr); b {
			beg = sr
		} else {
			end = sr - 1
		}
	}

	b, r := try(a, skip, near, n, end)

	if b {
		return r
	}

	_, r = try(a, skip, near, n, beg)

	return r
}

func main() {
	var n, x int
	fmt.Scan(&n)
	var a []int
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		a = append(a, x)
	}
	fmt.Println(solve(a, n))
}

package main

import (
	"fmt"
)

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

func compSkip(a []int, n int) [][]int{

	m := n / 8 + 2
	skip := make([][]int, n)

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

	for i := 0; i < n; i++ {
		skip[i][0] = i
		cnt := 0
		for j := i; j >= 0; j-- {
			if a[i] == a[j] {
				cnt ++
				if cnt < m {
					skip[i][cnt] = j - 1
				} else {
					break
				}
			}
		}
	}

	return skip
}

func compNear(a []int, n int) [][]int{

	near := make([][]int, n)
	last := make([]int, 9)

	for i := 0; i < 9; i++ {
		last[i] = -1
	}

	for i := 0; i < n; i++ {
		near[i] = make([]int, 9)
		last[a[i]] = i
		for j := 0; j < 9; j++ {
			near[i][j] = last[j]
		}
	}

	return near
}

func jump(p, num, k int, skip, nearest [][]int) int{
	if k == 0 {
		return p
	}
	p = nearest[p][num + 1]
	if p == -1 {
		return -2
	}
	return skip[p][k]
}

func try(a []int, skip, nearest [][]int, n, k int) (bool, int) {
	//fmt.Println("try", k)
	var dp = make([][]int, n)
	power := [8]int{1, 2, 4, 8, 16, 32, 64, 128}

	for i := 0; i < n; i++ {
		dp[i] = make([]int, 256)
		dp[i][0] = 0
		for mask := 1; mask < 256; mask++ {
			dp[i][mask] = -10000
			for j := 0; j < 8; j++ {
				if (power[j] & mask) == 0 {
					continue
				}
				jumpRes := jump(i, j, k, skip, nearest)
				if jumpRes != -2 {
					if jumpRes == -1 {
						if mask == power[j] || k == 0 {
							dp[i][mask] = k
						} else {
							dp[i][mask] = -10000
						}
					} else {
						dp[i][mask] = max(dp[i][mask], dp[jump(i, j, k, skip, nearest)][mask - power[j]] + k)
					}
				}
				jumpRes = jump(i, j, k + 1, skip, nearest)
				if jumpRes != -2 {
					if jumpRes == -1 {
						if mask == power[j] || k == 0 {
							dp[i][mask] = k + 1
						} else {
							dp[i][mask] = -10000
						}
					} else {
						dp[i][mask] = max(dp[i][mask], dp[jump(i, j, k + 1, skip, nearest)][mask - power[j]] + k + 1)
					}
				}
			}
		}
	}
	//fmt.Println(dp[
	return (dp[n - 1][255] > 0), dp[n - 1][255]
}

func solve(a []int, n int) int {
	beg := 0
	skip := compSkip(a, n)
	near := compNear(a, n)
	end := n / 8
	for beg + 1 < end {
		sr := (beg + end) /2
		if b, _ := try(a, skip, near, n, sr); b {
			beg = sr
		} else {
			end = sr - 1
		}
	}

	b, r := try(a, skip, near, n, end)

	if b {
		return r
	}

	_, r = try(a, skip, near, n, beg)

	return r
}

func main() {
	var n, x int
	fmt.Scan(&n)
	var a []int
	for i := 0; i < n; i++ {
		fmt.Scan(&x)
		a = append(a, x)
	}
	fmt.Println(solve(a, n))
}


package main

import "fmt"

func main() {
	var n, k, mn int
	fmt.Scan(&n, &k)
	mn = n
	k2p1 := k + k + 1
	kp := k + 1
	ans := 1
	for i := 1; i <= kp; i++ {
		tmp := 1
		j := i
		for {
			if j+k >= n {
				break
			}
			j += k2p1
			tmp++
		}
		if tmp < mn {
			mn = tmp
			ans = i
		}
	}
	fmt.Println(mn)
	for ; ans <= n; ans += k2p1 {
		fmt.Printf("%d ", ans)
	}
	fmt.Println("")
}

package main

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 1024*1024)
	dims := readInts(r, 2)
	n := dims[0]
	k := dims[1]

	cntLeft := n % (1 + 2*k)

	var first int
	if cntLeft == 0 {
		first = k + 1
	} else {
		if cntLeft < 1 + k {
			cntLeft += k
		}
		first = cntLeft - k
	}

	var nums [] string
	for i := first; i <= n; i += 2 * k + 1 {
		nums = append(nums, strconv.Itoa(i))
	}

	fmt.Println(len(nums))
	fmt.Print(strings.Join(nums, " "))
}

func readInts(reader *bufio.Reader, n int) []int {
	tmpInts := strings.Split(readLine(reader), " ")

	var result []int
	for i := 0; i < n; i++ {
		tmpInt, err := strconv.ParseInt(tmpInts[i], 10, 64)
		checkError(err)
		result = append(result, int(tmpInt))
	}
	return result
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve1pass(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	offset := 0
	for {
		for i := k + 1 - offset; i <= n; i += 2*k + 1 {
			min++
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n && len(list) != 0 {
			break
		}
		// reset
		min = 0
		list = list[:0]
		offset++
	}
	return
}

func solve1pass(n, k int) (min int, list []int) {
	segmentSize := 2*k + 1
	numSeg := n / segmentSize
	list = make([]int, 0, numSeg+1)
	diff := n % segmentSize
	offset := 0
	switch {
	case diff == 0:
		min = numSeg
	case diff < k+1:
		offset = k + 1 - diff
		min = numSeg + 1
	case diff >= k+1:
		min = numSeg + 1
	}

	for pos := segmentSize - k - offset; pos <= n; pos += segmentSize {
		list = append(list, pos)
	}

	return min, list
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve1pass(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	offset := 0
	for {
		for i := k + 1 - offset; i <= n; i += 2*k + 1 {
			min++
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n && len(list) != 0 {
			break
		}
		// reset
		min = 0
		list = list[:0]
		offset++
	}
	return
}

func solve1pass(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	segmentSize := 2*k + 1
	numSeg := n / segmentSize
	diff := n % segmentSize
	if diff == 0 {
		min = numSeg
		for pos := segmentSize - k; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
		return
	} else if diff >= k+1 {
		min = numSeg + 1
		for pos := segmentSize - k; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
	} else {
		move := k + 1 - diff
		min = numSeg + 1
		for pos := segmentSize - k - move; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
	}

	return min, list
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	if k == 0 {
		min = n
		for i := 1; i <= n; i++ {
			list = append(list, i)
		}
		return
	}
	offset := 0
	for {
		for i := k + 1 - offset; i <= n; i += 2*k + 1 {
			min++
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n && len(list) != 0 {
			break
		}
		// reset
		min = 0
		list = list[:0]
		offset++
	}
	return
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	offset := 0
	for {
		for i := k + 1 - offset; i <= n; i += 2*k + 1 {
			min++
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n && len(list) != 0 {
			break
		}
		// reset
		min = 0
		list = list[:0]
		offset++
	}
	return
}

package main

import "fmt"

func doable(N int, K int, first int, generate bool) (bool, []int) {
	var ans []int
	if generate {
		ans = []int{}
	}
	cur := first
	for {
		if generate {
			ans = append(ans, cur)
		}
		if cur+K+1 > N {
			return true, ans
		}
		next := cur + K + K + 1
		if next > N {
			return false, ans
		}
		cur = next
	}
}

func main() {
	var N, K int
	fmt.Scan(&N, &K)

	var l int
	var pos []int
	if 2*K+1 >= N {
		l = 1
		pos = []int{N/2 + 1}
	} else {
		first := K + 1
		for {
			if done, _ := doable(N, K, first, false); done {
				break
			}
			first--
		}
		_, ans := doable(N, K, first, true)
		l = len(ans)
		pos = ans
	}
	fmt.Println(l)
	fmt.Print(pos[0])
	for i, v := range pos {
		if i > 0 {
			fmt.Print(" ", v)
		}
	}
	fmt.Println()
}

package main

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

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

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

func main() {
	defer writer.Flush()
	var n, k, t1, t2, ans int
	scanf("%d %d", &n, &k)

	t1 = n % (2*k + 1)
	t2 = 1
	if t1 != 0 {
		for t1 <= k {
			t1++
		}
	} else {
		printf("%d\n", n/(2*k+1))
		t2 = k + 1
		for t2 <= n {
			printf("%d ", t2)
			t2 += 2*k + 1
		}
		return
	}

	t1 -= k
	t2 = t1

	for t2 <= n {
		t2 += 2*k + 1
		ans++
	}
	printf("%d\n", ans)
	t2 = t1

	for t2 <= n {
		printf("%d ", t2)
		t2 += 2*k + 1
	}

}

package main

import "fmt"

func main() {
	var n, k, mn int
	fmt.Scan(&n, &k)
	mn = n
	k2p1 := k + k + 1
	kp := k + 1
	ans := 1
	for i := 1; i <= kp; i++ {
		tmp := 0
		j := i
		for ; j <= n; j += k2p1 {
			// fmt.Printf("%d ", j)
			tmp++
			if j+k > n {
				break
			}
		}
		if j > n {
			tmp++
		}
		// fmt.Printf(" %d = %d\n", j, tmp)
		if tmp < mn {
			ans = i
			mn = tmp
		}
	}
	fmt.Println(mn)
	for ; ans <= n; ans += k2p1 {
		fmt.Printf("%d ", ans)
	}
	fmt.Println("")
}

package main

import "fmt"

func main() {
	var n, k, ans, mn int
	fmt.Scan(&n, &k)
	mn = n
	k2p1 := k + k + 1
	kp := k + 1
	for i := 1; i <= kp; i++ {
		tmp := 0
		j := i
		for ; j <= n; j += k2p1 {
			// fmt.Printf("%d ", j)
			tmp++
			if j+k > n {
				break
			}
		}
		if j > n {
			tmp++
		}
		// fmt.Printf(" %d = %d\n", j, tmp)
		if tmp < mn {
			ans = i
			mn = tmp
		}
	}
	fmt.Println(mn)
	for ; ans <= n; ans += k2p1 {
		fmt.Printf("%d ", ans)
	}
	fmt.Println("")
}

package main

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

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

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

func main() {
	defer writer.Flush()
	var n, k, t1, t2, ans int
	scanf("%d %d", &n, &k)

	t1 = n % (2*k + 1)

	if t1 != 0 {
		for t1 < k+1 {
			t1++
			t2++
		}
	}

	t1 -= k + 1
	t2 = t1

	for t2 <= n {
		t2 += 2*k + 1
		ans++
	}
	printf("%d\n", ans)
	t2 = t1

	for t2 <= n {
		printf("%d ", t2+1)
		t2 += 2*k + 1
	}

}

package main

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

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

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

func main() {
	defer writer.Flush()
	var n, k, t1, t2, ans int
	scanf("%d %d", &n, &k)

	t1 = n % (2*k + 1)

	if t1 != 0 {
		for t1 < k+1 {
			t1++
			t2++
		}
	} else {
		printf("%d\n", n/(2*k+1))
		t2 = k
		for t2 <= n {
			printf("%d ", t2+1)
			t2 += 2*k + 1
		}
	}

	t1 -= k + 1
	t2 = t1

	for t2 <= n {
		t2 += 2*k + 1
		ans++
	}
	printf("%d\n", ans)
	t2 = t1

	for t2 <= n {
		printf("%d ", t2+1)
		t2 += 2*k + 1
	}

}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	if k == 0 {
		min = n
		for i := 1; i <= n; i++ {
			list = append(list, i)
		}
		return
	}

	min = n/(2*(k+1)) + 1
	for i := n % (2 * (k + 1)); i < n; i += 2*k + 1 {
		list = append(list, i)
	}
	return
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	if k == 0 {
		min = n
		for i := 1; i <= n; i++ {
			list = append(list, i)
		}
		return
	}

	min = n/(2*(k+1)) + 1
	offset := 0
	for {
		for i := n%(2*(k+1)) - offset; i <= n; i += 2*k + 1 {
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n {
			break
		}
		// reset
		list = list[:0]
		min++
		offset++
	}
	return
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve1pass(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	offset := 0
	for {
		for i := k + 1 - offset; i <= n; i += 2*k + 1 {
			min++
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n && len(list) != 0 {
			break
		}
		// reset
		min = 0
		list = list[:0]
		offset++
	}
	return
}

func solve1pass(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	segmentSize := 2*k + 1
	numSeg := n / segmentSize
	diff := n % segmentSize
	if diff == 0 {
		min = numSeg
		for pos := segmentSize - k; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
		return
	}

	if diff >= k+1 {
		min = numSeg + 1
		for pos := segmentSize - k; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
		list = append(list, list[len(list)-1]+k+1)
	} else {
		move := k + 1 - diff
		min = numSeg + 1
		for pos := segmentSize - k - move; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
		list = append(list, n)
	}

	return min, list
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	if k == 0 {
		min = n
		for i := 1; i <= n; i++ {
			list = append(list, i)
		}
		return
	}
	offset := 0
	for {
		for i := k + 1 - offset; i <= n; i += 2*k + 1 {
			min++
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n {
			break
		}
		// reset
		min = 0
		list = list[:0]
		offset++
	}
	return
}

package main

import (
	"fmt"
	"strings"
)

func main() {
	var n, k int
	fmt.Scanf("%d %d", &n, &k)
	min, list := solve1pass(n, k)
	fmt.Printf("%d\n%s", min, strings.Trim(fmt.Sprintf("%v", list), "[]"))
}
func solve(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	offset := 0
	for {
		for i := k + 1 - offset; i <= n; i += 2*k + 1 {
			min++
			list = append(list, i)
		}
		if list[len(list)-1]+k >= n && len(list) != 0 {
			break
		}
		// reset
		min = 0
		list = list[:0]
		offset++
	}
	return
}

func solve1pass(n, k int) (min int, list []int) {
	list = make([]int, 0, n)
	segmentSize := 2*k + 1
	numSeg := n / segmentSize
	diff := n % segmentSize
	if diff == 0 {
		min = numSeg
		for pos := segmentSize - k; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
		return
	} else if diff >= k+1 {
		min = numSeg + 1
		for pos := segmentSize - k; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
		list = append(list, list[len(list)-1]+k+1)
	} else {
		move := k + 1 - diff
		min = numSeg + 1
		for pos := segmentSize - k - move; pos <= n; pos += segmentSize {
			list = append(list, pos)
		}
		list = append(list, n)
	}

	return min, list
}


package main

import (
	"fmt"
	"strings"
)

func main() {
	var s, g string
	var n, k int
	fmt.Scanln(&n, &k)
	fmt.Scanln(&s)
	g = strings.Repeat("#", k)
	if strings.Count(s, g) > 0 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

package main

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

func main() {
	var words []string
	var s, g string
	in := bufio.NewReader(os.Stdin)
	s, _ = in.ReadString('\n')
	words = strings.Fields(s)
	k, _ := strconv.Atoi(words[1])
	s, _ = in.ReadString('\n')
	g = strings.Repeat("#", k)
	if strings.Contains(s, g) {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

package main

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

func main() {
	var s, g string
	in := bufio.NewReader(os.Stdin)
	s, _ = in.ReadString(' ')
	s, _ = in.ReadString('\n')
	k, _ := strconv.Atoi(s[:len(s)-1])
	s, _ = in.ReadString('\n')
	g = strings.Repeat("#", k)
	if strings.Contains(s, g) {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}


package main

import (
	"bufio"
	"fmt"
	"os"
)
func min(x, y int)int{
	if x<y{
		return x
	}
	return y
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		a := make([]int, n+5)
		b := make([]int, n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i],&b[i])
		}
		l := 1
		r := n
		ans := -1
		for l<=r{
			mid := (l+r)>>1
			cnt:=0
			for i:=1;i<=n;i++{
				if mid -1 - a[i]<=cnt&&cnt<=b[i]{
					cnt++
				}
			}
			if cnt<mid{
				r=mid-1
				continue
			}
			ans = mid
			l=mid+1
		}
		fmt.Fprintln(out,ans)
	}
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        cnt := 0
        //for i := 0; i < n; i++ {
        //    if a[i] >= x - 1 - cnt && b[i] >= cnt { cnt ++}
        //}
        //return cnt >= x

        r := 0
        for r < n {
            if r < n && !(a[r] < x - 1 - cnt || b[r] < cnt) { cnt++ }
            r++
        }
        return cnt >= x
    }

    l, r := 1, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

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

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

func redirectInputToFile(r *bufio.Reader, fp string) {
	f, err := os.Open(fp)
	if err != nil {
		panic(err)
	}
	r.Reset(f)
}

func main() {
	defer writer.Flush()
	// redirectInputToFile(reader, "input.txt")

	T := 1
	scan(&T)
	for t := 0; t < T; t++ {
		solve()
	}
}

func solve() {
	var n int
	scan(&n)
	a := make([][2]int, n)
	for i := range a {
		scan(&a[i][0], &a[i][1])
	}
	ok := func(x int) bool {
		took := 0
		for i := range a {
			if took <= a[i][1] && x-1-took <= a[i][0] {
				took++
			}
		}
		return took >= x
	}
	l, r := 1, n+1
	for r-l > 1 {
		m := l + (r-l)/2
		if ok(m) {
			l = m
		} else {
			r = m
		}
	}
	printf("%v\n", l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        cnt := 0
        for i := 0; i < n; i++ {
            if a[i] >= x - 1 - cnt && b[i] >= cnt { cnt ++}
        }
        return cnt >= x

        r := 0
        for r < n {
            for r < n && (a[r] < x - 1 - cnt || b[r] < cnt) { r++ }
            if r < n { cnt++ }
        }
        return cnt >= x
    }

    l, r := 1, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        cnt := 0
        //for i := 0; i < n; i++ {
        //    if a[i] >= x - 1 - cnt && b[i] >= cnt { cnt ++}
        //}
        //return cnt >= x

        r := 0
        for r < n {
            for r < n && (a[r] < x - 1 - cnt || b[r] < cnt) { r++; continue }
            if r < n { cnt++ }
            r++
        }
        return cnt >= x
    }

    l, r := 1, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        cnt := 0
        //for i := 0; i < n; i++ {
        //    if a[i] >= x - 1 - cnt && b[i] >= cnt { cnt ++}
        //}
        //return cnt >= x

        r := 0
        for r < n {
            for r < n && (a[r] < x - 1 - cnt || b[r] < cnt) { r++; continue }
            cnt++
            r++
        }
        return cnt >= x
    }

    l, r := 1, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        cnt := 0
        //for i := 0; i < n; i++ {
        //    if a[i] >= x - 1 - cnt && b[i] >= cnt { cnt ++}
        //}
        //return cnt >= x

        r := 0
        for r < n {
            if r < n && (a[r] < x - 1 - cnt || b[r] < cnt) { r++; continue }
            cnt++
        }
        return cnt >= x
    }

    l, r := 1, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        cnt := 0
        //for i := 0; i < n; i++ {
        //    if a[i] >= x - 1 - cnt && b[i] >= cnt { cnt ++}
        //}
        //return cnt >= x

        r := 0
        for r < n {
            for r < n && (a[r] < x - 1 - cnt || b[r] < cnt) { r++; continue }
            cnt++
        }
        return cnt >= x
    }

    l, r := 1, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        r := 0
        cnt := 0
        for r < n {
            for r < n && (a[r] < x - 1 - cnt || b[r] < cnt) { r++ }
            if r < n { cnt++ }
        }
        return cnt >= x
    }

    l, r := 1, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        X := x
        r := 0
        cnt := 0
        for r < n {
            for r < n && (a[r] < x - 1 || b[r] < cnt) { r++ }
            if r < n {
                cnt++
                x--
            }
        }
        return cnt >= X
    }

    l, r := 0, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

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

var (
	input = bufio.NewReader(os.Stdin)
	output = bufio.NewWriter(os.Stdout)
)

func assert(test bool) {
    if !test {panic("assert!")}
}

// (c) Dmitriy Blokhin (sv.dblokhin@gmail.com)
// TODO***
// - read the statement carefully
// - get solution before coding
// - check the boundaries (int/int64)

func main() {
    defer output.Flush()
    //solve()
    //return
	var t int64
	fmt.Fscan(input, &t)
    for t > 0 {
        t--
        solve()
    }
}

func abs(a int64) int64 {
    if a >= 0 { return a}
    return -a
}

func min(a, b int64) int64{
    if a < b { return a}
    return b
}
func max(a, b int64) int64{
    if a > b { return a}
    return b
}
func solve() {
    var n int
    fmt.Fscan(input, &n)
    a := make([]int, n)
    b := make([]int, n)

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

    ok := func(x int) bool {
        r := 0
        cnt := 0
        for r < n {
            for r < n && (a[r] < x - 1 || b[r] < cnt) { r++ }
            if r < n {
                cnt++
                x--
            }
        }
        return x <= 0
    }

    l, r := 0, n + 1
    for r - l > 1 {
        m := (r + l) / 2
        if ok(m) {
            l = m
        } else {
            r = m
        }
    }

    fmt.Fprintln(output, l)
}

package main

import (
	"bufio"
	"fmt"
	"os"
)
func min(x, y int)int{
	if x<y{
		return x
	}
	return y
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	var t int
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		a := make([]int, n+5)
		b := make([]int, n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i],&b[i])
		}
		l := 1
		r := n
		ans := -1
		for l<=r{
			mid := (l+r)>>1
			cnt:=0
			for i:=1;i<=n;i++{
				if min(a[i],n-i)+min(b[i],i-1)+1>=mid{
					cnt++
				}
			}
			if cnt<mid{
				r=mid-1
				continue
			}
			ans = mid
			l=mid+1
		}
		fmt.Fprintln(out,ans)
	}
}