Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.

Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).

NoteFor the first example, a capacity of 6 is sufficient:   At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3.  At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now.  At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now.  Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints. Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer.

package main

import "fmt"

func main() {

	var number int = 0
	_, _ = fmt.Scanln(&number)
	 var matrix = make([][2]int,number)

	 var max,min = 0,0

	 for i:=0; i<number; i++ {
		 _, _ = fmt.Scan(&matrix[i][0])
		 _, _ = fmt.Scan(&matrix[i][1])
		 min =  matrix[i][1] - matrix[i][0] + min
		if min > max {
			max = min
		}
	 }
	 fmt.Println(max)
}

package main

import "fmt"

func main(){
	var n int
	fmt.Scanf("%d", &n)
	prevStepPop := 0
	var maxCap int = 0
	for i:=0;i<n;i++{
		var a, b int
		fmt.Scan(&a)
		fmt.Scan(&b)
		b = b - a
		prevStepPop = prevStepPop + b
		if prevStepPop > maxCap{
			maxCap = prevStepPop
		}
	}
	fmt.Println(maxCap)
}

package main

import "fmt"

func main() {
	var n, a, b, z, m int

	fmt.Scan(&n)

	for i := 0; i < n; i++{
		fmt.Scan(&a, &b)
		b = b - a
		z = z + b
		if m < z {
			m = z
		}
	}
	fmt.Println(m)
}

package main

import (
    "fmt"
    //"math"
)

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

func main(){
    var (
        a,b,n,res int
    )
    res = 0
    temp := 0
    fmt.Scan(&n)
    for n>0 {
        fmt.Scan(&a,&b)
        res = res-a+b
        temp = max(temp,res)
        n--
    }
    fmt.Println(temp)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	cap := 0
	res := 0
	for i := 0; i < n; i++ {
		var out int
		var in int
		fmt.Scanf("%d %d\n", &out, &in)
		cap -= out
		cap += in
		res = int(math.Max(float64(res), float64(cap)))
	}

	fmt.Println(res)
}

package main
import (
	"fmt"
)
func main() {
	var n,a,b int
	c,m :=0,0
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&a,&b)
		c-=a
		c+=b
		if c>m {
			m=c
		}
	}
	fmt.Println(m)

}

package main

import "fmt"

func main() {
	var stops int
	tram := 0
	max := 0
	fmt.Scan(&stops)

	for i := 0; i < stops; i++ {
		var exit, enter int
		fmt.Scan(&exit)
		tram -= exit
		fmt.Scan(&enter)
		tram += enter
		if tram > max {
			max = tram
		}
	}
	fmt.Println(max)
}

package main

import "fmt"

func main() {
	var n, a, b, rs, cur int
	fmt.Scan(&n)
	fmt.Scan(&a, &b)
	rs, cur = a+b, a+b
	for ; n > 1; n-- {
		fmt.Scan(&a, &b)
		cur = cur - a + b
		if cur < 0 {
			cur = 0
		}
		if cur > rs {
			rs = cur
		}
	}
	fmt.Println(rs)
}

package main

import "fmt"

func main() {
	var n, x, y, cap, currentPassenger int
	fmt.Scan(&n, &currentPassenger, &currentPassenger)
	cap = currentPassenger
	for i := 1; i < n; i++ {
		fmt.Scan(&x, &y)
		currentPassenger = currentPassenger - x + y
		if currentPassenger > cap {
			cap = currentPassenger
		}
	}
	fmt.Println(cap)
}

package main

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

var scanner *bufio.Scanner

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

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

  n := getI()
  max, current := int64(0), int64(0)
  for i := int64(0); i < n; i++ {
    a, b := getI(), getI()
    current += b-a
    if current > max {
      max = current
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", max))
}

package main

import (
	"fmt"
)

//import "strings"

//import "math"
//import "strconv"
//import "sort"

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

func main() {

	var n int

	fmt.Scan(&n)

	cur := 0
	mx := -100000
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scan(&a, &b)

		cur -= a
		cur += b

		mx = max(mx, cur)

	}
	fmt.Println(mx)
}

package main

import (
  "fmt"
)

func main() {
  var n, a, b, max, cur int
  fmt.Scan(&n)
  for i := 0; i < n; i++ {
    fmt.Scan(&a,&b)
    cur = (cur-a)+b
    if max<cur {
      max = cur
    }
  }
  fmt.Print(max)
}

package main

import "fmt"

func main() {
	tramStops, currentAmount := 0, 0
	minimumCapacity := 0
	fmt.Scanf("%d\n", &tramStops)
	for i := 0; i < tramStops; i++ {
		left, arrivals := 0, 0
		fmt.Scanf("%d %d\n", &left, &arrivals)

		currentAmount -= left
		currentAmount += arrivals

		if currentAmount > minimumCapacity {
			minimumCapacity = currentAmount
		}
	}

	if currentAmount < 0 {
		currentAmount = 0
	}

	fmt.Println(minimumCapacity)
}

package main

import "fmt"

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

	var p, max int
	var in, out int
	for i := 0; i < n; i++ {
		fmt.Scan(&out, &in)
		p += in - out
		if p > max {
			max = p
		}
	}

	fmt.Print(max)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	var busCapacity, busSize int
	for i := 0; i < n; i++ {
		var out, in int
		fmt.Scan(&out, &in)
		busSize += in - out
		if busSize > busCapacity {
			busCapacity = busSize
		}
	}
	fmt.Println(busCapacity)
}

package main

import (
	"fmt"
)

func main() {
	var n, ans, num, a, b int
	fmt.Scan(&n)
	for i := 0; i < n; i++ {
		fmt.Scan(&a, &b)
		num -= a
		num += b
		if num > ans {
			ans = num
		}
	}
	fmt.Print(ans)
}

package main

import (
	"fmt"
)

func main() {
	a116()
}

func a116() {
	var (
		a, b, n, load, maxLoad int
	)

	_, _ = fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		_, _ = fmt.Scanf("%d %d\n", &a, &b)
		if i == 0 {
			load, maxLoad = b, b
			continue
		}
		load = load - a + b
		if load > maxLoad {
			maxLoad = load
		}
	}

	fmt.Println(maxLoad)
}

package main

import "fmt"

func main() {
  var n int
  fmt.Scan(&n)
  res := 0
  cur := 0
  for i := 0; i < n; i++ {
    var a, b int
    fmt.Scan(&a, &b)
    cur += b - a
    if cur > res {
      res = cur
    }
  }
  fmt.Println(res)
}

package main

import "fmt"

func main() {
	var n, x, y, z, m int

	fmt.Scan(&n)

	for i := 0; i < n; i++{
		fmt.Scan(&x, &y)
		y = y - x
		z = z + y
		if m < z {
			m = z;
		}
	}
	fmt.Println(m)
}

package main

import "fmt"

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

func main() {
    var n,a,b int
    fmt.Scan(&n)
    ans, total := 0, 0
    for i := 0; i < n; i++ {
        fmt.Scan(&a,&b)
        total -= a
        ans = max(ans,total)
        total += b
        ans = max(ans,total)
    }
    fmt.Println(ans)
}

package main

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

func main() {
	var scanner *bufio.Scanner
	var stops int
	var exits, enters int
	var line []string
	var capacity, maxCapacity int

	capacity = 0
	scanner = bufio.NewScanner(os.Stdin)

	fmt.Scanln(&stops)
	for i := 0; i < stops; i++ {
		if scanner.Scan() {
			line = strings.Fields(scanner.Text())
			exits, _ = strconv.Atoi(line[0])
			enters, _ = strconv.Atoi(line[1])

			capacity += (enters - exits)
			if capacity > maxCapacity {
				maxCapacity = capacity
			}
		}
	}

	fmt.Println(maxCapacity)
}

package main

import (
	"fmt"
)

var (
	n      int
	a      []int
	b      []int
	result int = 0
)

func in() {
	fmt.Scanln(&n)
	a = make([]int, n)
	b = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &a[i], &b[i])
	}
}

func out() {
	fmt.Println(result)
}

func run() {
	curr := 0
	for i := 0; i < n; i++ {
		curr += b[i] - a[i]
		if curr > result {
			result = curr
		}
	}
}

func main() {
	in()
	run()
	out()
}

//A. Трамвай
package main

import (
	"fmt"
)

func main() {
	var n, a, b, max, buf int
	fmt.Scan(&n)
	fmt.Scan(&a, &b)
	max, buf = b, b
	for i := 2; i <= n; i++ {
		fmt.Scan(&a, &b)
		buf = buf - a + b
		if max < buf {
			max = buf
		}
	}

	fmt.Print(max)
}

package main

import "fmt"

func main() {
	var st, s, i, m int
	fmt.Scan(&st)
	for i < st {
		var a, b int
		fmt.Scan(&a)
		fmt.Scan(&b)
		s = s - a
		s = s + b
		if m < s {
			m = s
		}
		i++
	}
	fmt.Println(m)
}

/* https://codeforces.com/problemset/problem/116/A
A. Tram
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.

Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.

Input
The first line contains a single number n (2 ≤ n ≤ 1000) — the number of the tram's stops.

Then n lines follow, each contains two integers ai and bi (0 ≤ ai, bi ≤ 1000) — the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement.

The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally, . This particularly means that a1 = 0.
At the last stop, all the passengers exit the tram and it becomes empty. More formally, .
No passenger will enter the train at the last stop. That is, bn = 0.
Output
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).

Examples
inputCopy
4
0 3
2 5
4 2
4 0
outputCopy
6
Note
For the first example, a capacity of 6 is sufficient:

At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3.
At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now.
At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now.
Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints.
Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer.
*/

package main

import "fmt"

func main() {
	var n, a, b, capacity, min int
	fmt.Scanln(&n)

	for i := 0; i < n; i++ {
		fmt.Scanln(&a, &b)
		capacity -= a
		capacity += b
		if capacity > min {
			min = capacity
		}
	}
	fmt.Println(min)
}

/* https://codeforces.com/problemset/problem/116/A
A. Tram
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.

Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.

Input
The first line contains a single number n (2 ≤ n ≤ 1000) — the number of the tram's stops.

Then n lines follow, each contains two integers ai and bi (0 ≤ ai, bi ≤ 1000) — the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement.

The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally, . This particularly means that a1 = 0.
At the last stop, all the passengers exit the tram and it becomes empty. More formally, .
No passenger will enter the train at the last stop. That is, bn = 0.
Output
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).

Examples
inputCopy
4
0 3
2 5
4 2
4 0
outputCopy
6
Note
For the first example, a capacity of 6 is sufficient:

At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3.
At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now.
At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now.
Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints.
Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer.
*/

package main

import "fmt"

func main() {
	var n, a, b, capacity, min int
	fmt.Scanln(&n)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &a, &b)
		capacity -= a
		capacity += b
		if capacity > min {
			min = capacity
		}
	}
	fmt.Println(min)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	// Skip the number of stops.
	scanner.Scan()

	// Tram capacity.
	cap := 0
	// Max capacity.
	max := cap
	var in, out int
	for scanner.Scan() {
		if _, err := fmt.Sscanf(scanner.Text(), "%d %d", &in, &out); err != nil {
			continue
		}
		cap -= in
		cap += out
		if cap > max {
			max = cap
		}
	}
	fmt.Println(max)
}

package main
import (
	"fmt"
)
func main() {
	var n,a,b int
	c,m :=0,0
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&a,&b)
		c-=a
		c+=b
		if c>m {
			m=c
		}
	}
	fmt.Println(m)

}

package main

import (
	"fmt"
)

func main() {
	var num int
	fmt.Scanf("%d\n", &num)
	max := 0
	curr := 0
	var indoor, outdoor int
	for i := 0; i < num; i++ {
		fmt.Scanf("%d %d\n", &outdoor, &indoor)
		curr -= outdoor
		curr += indoor
		if curr > max {
			max = curr
		}
	}
	fmt.Println(max)
}

/*
Tram
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.

Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.

Input
The first line contains a single number n (2 ≤ n ≤ 1000) — the number of the tram's stops.

Then n lines follow, each contains two integers ai and bi (0 ≤ ai, bi ≤ 1000) — the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement.

The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally, . This particularly means that a1 = 0.
At the last stop, all the passengers exit the tram and it becomes empty. More formally, .
No passenger will enter the train at the last stop. That is, bn = 0.
Output
Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).

Sample test(s)
input
4
0 3
2 5
4 2
4 0
output
6
Note
For the first example, a capacity of 6 is sufficient:

At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3.
At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now.
At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now.
Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints.
Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer.
*/

package main

import (
	"fmt"
	"os"
)

func main() {
	var (
		n, a, b, curCap, maxCap int
	)
	fmt.Fscanf(os.Stdin, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(os.Stdin, "%d %d\n", &a, &b)
		curCap += b - a
		if curCap > maxCap {
			maxCap = curCap
		}
	}
	fmt.Println(maxCap)
}

package main

import "fmt"

func main() {
	var currentMax int
	var current int
	var stopNumber int
	fmt.Scan(&stopNumber)
	for offset := 0; offset < stopNumber; offset++ {
		var out, in int
		fmt.Scan(&out, &in)
		diff := in - out

		current += diff

		if current > currentMax{
			currentMax = current
		}

	}
	fmt.Print(currentMax)
}

package main

import "fmt"

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

	var c, maxc = 0, 0

	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scan(&a, &b)

		c += b - a
		if c > maxc {
			maxc = c
		}
	}

	fmt.Println(maxc)
}

package main

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

func main() {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	n := nextInt(s)

	max := 0
	people := 0
	for i := 0; i < n; i++ {
		a := nextInt(s)
		b := nextInt(s)
		people = people - a + b
		if people > max {
			max = people
		}
	}

	fmt.Println(max)
}

func reverse(xs []rune) {
	for i, j := 0, len(xs)-1; i < j; i, j = i+1, j-1 {
		xs[i], xs[j] = xs[j], xs[i]
	}
}

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

func toLower(x byte) byte {
	return x + ('a' - 'A')
}

func isUpper(x byte) bool {
	return x >= 'A' && x <= 'Z'
}

func initArray(n, x int) []int {
	xs := make([]int, n)
	for i := 0; i < n; i++ {
		xs[i] = x
	}
	return xs
}

func nextInt(s *bufio.Scanner) (n int) {
	s.Scan()
	n, _ = strconv.Atoi(s.Text())
	return
}

package main
import "fmt"
func main () {
    var n, in, out int
    c:=0
    m:=0
    fmt.Scan(&n)
    for i:=0; i<n;i++ {
        fmt.Scan(&out,&in)
        c=c-out+in
        if (m<c) {
            m=c
        }
    }
    fmt.Println(m)
}

package main

import "fmt"

func main() {
	var stopsNum, exit, enter, current, high_time int
	fmt.Scan(&stopsNum)
	for i := 0; i < stopsNum; i++ {

		fmt.Scan(&exit, &enter)
		current -= exit
		current += enter

		if current > high_time {
			high_time = current
		}

	}

	fmt.Print(high_time)

	return

}

package main

import "fmt"

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

	caps := 0
	left := 0
	var u, v int
	for i := 0; i < n; i++ {
		fmt.Scan(&u, &v)
		left += (v - u)
		if left > caps {
			caps = left
		}
	}

	fmt.Print(caps)
}

package main

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

var n int

func main() {
	stdin := os.Stdin
	// stdin, _ = os.Open("1.in")
	reader := bufio.NewReaderSize(stdin, 1<<20)
	fmt.Fscanf(reader, "%d\n", &n)
	sum := 0
	ans := 0
	for ; n != 0; n-- {
		var a, b int
		fmt.Fscanf(reader, "%d%d\n", &a, &b)
		sum += b - a
		if sum > ans {
			ans = sum
		}
	}
	fmt.Println(ans)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n uint32
	var a, b uint32
	var cb uint32
	var max uint32
	fmt.Fscanf(in, "%d\n", &n)
	for i := 0; uint32(i) < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		cb -= a
		cb += b
		if cb > max {
			max = cb
		}
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
)

func main() {
	var t, a, b, max, p int
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&a, &b)
		p += b - a
		if p > max {
			max = p
		}
		t--
	}
	fmt.Println(max)
}

package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	var cur, max int
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		cur += b - a
		if cur > max {
			max = cur
		}
	}
	fmt.Print(max)
}

package main

import (
	"fmt"
)

func main() {
	var n, p, q, aux, min int
	fmt.Scan(&n)

	for i := 0; i < n; i++ {
		fmt.Scan(&p, &q)
		aux -= p
		aux += q
		if aux > min {
			min = aux
		}
	}
	fmt.Println(min)
}

package main

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

func cf116A(in io.Reader, out io.Writer) {
	var n, capacity, occupancy int
	capacity = 0
	occupancy = 0
	fmt.Fscan(in, &n)
	// fmt.Print(k, n, w)
	for n != 0 {
		n--
		var inbound, outbound int
		fmt.Fscan(in, &outbound, &inbound)
		occupancy = occupancy + inbound - outbound
		if occupancy > capacity {
			capacity = occupancy
		}

	}

	fmt.Println(capacity)

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

package main

import (
        "fmt"
)

func main() {
        n := 0
        cur := 0
        max := 0
        fmt.Scan(&n)
        for i := 0; i < n; i++ {
                a, b := 0, 0
                fmt.Scan(&a, &b)
                cur -= a
                cur += b
                if cur > max {
                        max = cur
                }
        }
        fmt.Println(max)
}

package main

import "fmt"

func main() {
	var n int
	max := 0
	num := 0

	fmt.Scanf("%d\n", &n)

	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scanf("%d %d\n", &a, &b)

		num += -a + b
		if num > max {
			max = num
		}
	}

	fmt.Printf("%d", max)
}

package main

import "fmt"

func main() {
	var n, a, b, tmp, res int
	fmt.Scan(&n)
	for i := 0; i < n; i ++ {
		fmt.Scan(&a, &b)
		b -= a
		tmp += b
		if res < tmp {
			res = tmp
		}
	}
	fmt.Println(res)
}

package main

import "fmt"

func main() {
	var n int
	ans := 0
	sisa := 0
	fmt.Scan(&n)
	for ; n > 0; n-- {
		var x, y int
		fmt.Scan(&x, &y)
		sisa -= x
		sisa += y
		if sisa > ans {
			ans = sisa
		}
	}
	fmt.Println(ans)
}

package main

import (
	"fmt"
)

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

	max := 0
	now := 0

	var out int
	var in int

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

		_, err := fmt.Scanf("%d %d", &out, &in)

		if err != nil {
			i--
			continue
		}

		now = now - out + in

		if now > max {
			max = now
		}
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
	"math"
)

// 4 stop
// 0 3 exit-enter
// 2 5
// 4 2
// 4 0
func main() {

	var stops int
	fmt.Scanln(&stops)
	var ans, cap float64 = 0, 0
	var x, y int
	for i := 1; i <= stops; i++ {
		fmt.Scanf("%d %d\n", &x, &y)
		cap = cap + float64(y) - float64(x)
		ans = math.Max(cap, ans)
	}
	fmt.Println(int(ans))
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	tot := 0
	max := 0
	for i := 0; i < t; i++ {
		var tm int
		fmt.Scan(&tm)
		tot -= tm

		fmt.Scan(&tm)
		tot += tm

		if max < tot {
			max = tot
		}
	}
	fmt.Println(max)
}

package main

import "fmt"

func main() {
    var n int
    fmt.Scan(&n)
    var m, cur int
    m = 0
    cur = 0
    for i := 0; i < n; i++ {
        var leave, enter int
        fmt.Scan(&leave, &enter)
        cur -= leave
        cur += enter
        if m < cur {
            m = cur
        }
    }

    fmt.Println(m)
}

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

package main

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

func main() {
	reader, writer := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer writer.Flush()
	var a, b, capacity, maxCapacity, n int
	fmt.Fscanf(reader, "%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%d %d\n", &a, &b)
		capacity += (b - a)
		if capacity > maxCapacity {
			maxCapacity = capacity
		}
	}
	fmt.Fprintf(writer, "%d\n", maxCapacity)
}

package main
import (
	"fmt"
)
func main() {
	var n,a,b int
	c,m :=0,0
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		fmt.Scan(&a,&b)
		c-=a
		c+=b
		if c>m {
			m=c
		}
	}
	fmt.Println(m)

}

package main

import "fmt"

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

package main

import (
    "fmt"
    )

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

    cap := 0
    rem := 0
    for i := 0; i < n ; i++ {
       var a, b int
       fmt.Scan(&b, &a)
       rem = (rem - b) + a
       if cap < rem {
           cap = rem
       }
    }

    fmt.Println(cap)
}

package main

import (
	"fmt"
)

func main() {
	var stop, turun, naik, max, tram int
	max = 0
	tram = 0
	fmt.Scan(&stop)
	for i := 0; i < stop; i++ {
		fmt.Scan(&turun)
		fmt.Scan(&naik)
		tram -= turun
		tram += naik
		if max < tram {
			max = tram
		}
	}
	fmt.Print(max)
}

// olymp
package main

import (
	"fmt"
)

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

func main() {
	var n int
	fmt.Scanf("%d\n", &n)
	cur, mx := 0, 0
	for i := 0; i < n; i++ {
		var in, out int
		fmt.Scanf("%d %d\n", &out, &in)
		cur += in - out
		mx = max(cur, mx)
	}
	fmt.Println(mx)
}

// 10.CodeForce 116A-Michael542
package main

import (
    "fmt"
)

func main() {
    var n, in, out, total, max int
    fmt.Scan(&n)
    for x := 0; x < n; x++ {
        fmt.Scan(&out, &in)
        total -= out
        total += in
        if total > max {
            max = total
        }
    }
    fmt.Println(max)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	//n, _ := strconv.Atoi(scanner.Text())
	max := 0
	cap := 0
	for scanner.Scan() {
		in := strings.Split(scanner.Text(), " ")
		a, _ := strconv.Atoi(in[0])
		b, _ := strconv.Atoi(in[1])
		cap -= a
		cap += b
		if cap > max {
			max = cap
		}
	}
	fmt.Println(max)
}

package main

import "fmt"

func main() {
	var testcount, a, b, count, max int
	fmt.Scanf("%d\n", &testcount)
	for i := 0; i < testcount; i++ {
		fmt.Scanf("%d %d\n", &a, &b) //а выходят, b заходят
		count = count - a
		count = count + b
		if max < count {
			max = count
		}
	}
	fmt.Println(max)
}

//
// Created by aldair on 21/10/20.
// https://codeforces.com/problemset/problem/116/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 main() {
	defer writer.Flush()

	var stops int
	scanf("%d\n", &stops)

	var exit, enter, capacity, aux int
	for; stops > 0; stops-- {
		scanf("%d%d\n", &exit, &enter)

		aux += -exit + enter
		if aux > capacity {
			capacity = aux
		}
	}

	printf("%d\n", capacity)
}

package main

import "fmt"

func main()  {

    var psNow,n,in,out,max int

    fmt.Scanf("%d", &n)
psNow=0
max=0
    for i:= 0;  i< n; i++ {
        fmt.Scan( &out)
        fmt.Scan (&in)
        psNow= psNow - out + in
        if psNow>max {
            max= psNow
        }
    }
    fmt.Println (max)
}

package main

import (
	"fmt"
)

func main() {
	var T int
	fmt.Scan(&T)
	c := 0
	capia := 0
	for i := 0; i < T; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		c = c - a + b
		if capia < c {
			capia = c
		}

	}
	fmt.Println(capia)

}

package main

import "fmt"

func main() {
	var n uint16
	var cap, maxcap int32
	fmt.Scan(&n)
	for ; n > uint16(0); n-- {
		var a, b int16
		fmt.Scan(&a, &b)
		cap += int32(b - a)
		if cap > maxcap {
			maxcap = cap
		}
	}
	fmt.Println(maxcap)
}

package main

import (
	"fmt"
)

func main() {
	var numStops, total, final int
    _, err1 := fmt.Scanf("%d",&numStops)
    //.Println(numStops)
	for x := 0; x < numStops; x++ {
        var out, in int
        fmt.Scan(&out)
        fmt.Scan(&in)
        //fmt.Println(out,in)
    //temp = append(temp, out,in)

		total += in-out
		//.Println(total)
		if final < total{
			final = total
		}

	}
	if err1!= nil{
	    panic(err1)
	}
	fmt.Println(final)

	//fmt.Println(strings.Compare(numStops,B))

}

package main

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

func main() {
	minCap := 0
	curr := 0
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	stops,_ := strconv.Atoi(scanner.Text())
	for i := 0; i < stops; i++ {
		scanner.Scan()
		stp := scanner.Text()
		inout := strings.Split(stp, " ")
		pOut,_ := strconv.Atoi(inout[0])
		pIn,_ := strconv.Atoi(inout[1])
		curr -= pOut
		curr += pIn
		if curr > minCap {
			minCap = curr
		}
	}
	fmt.Println(minCap)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var max = 0
	var a, b int
	var temp = 0
	for i := 0; i < n; i++ {
		fmt.Scan(&a, &b)
		temp += b - a
		if temp > max {
			max = temp
		}
	}
	fmt.Print(max)
}

package main

import "fmt"

func main() {
	var n, max, on, off, current int
	fmt.Scanf("%d\n", &n)

	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &off, &on)
		current += on - off
		if current > max {
			max = current
		}
	}

	fmt.Println(max)
}

package main
import (
    "fmt"
)
func main() {
    var n,a,b int
    fmt.Scan(&n)
    maxc:=0
    capc:=0
    for i:=0;i<n;i++ {
        fmt.Scan(&a,&b)
        capc=capc+b-a
        if capc > maxc {
            maxc=capc
        }
    }
    fmt.Println(maxc)
}

package main

import "fmt"

// A. Tram
// time limit per test
// 2 seconds
// memory limit per test
// 256 megabytes
// input
// standard input
// output
// standard output

// Linear Kingdom has exactly one tram line. It has n stops, numbered from 1 to n in the order of tram's movement. At the i-th stop ai passengers exit the tram, while bi passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty.

// Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram.
// Input

// The first line contains a single number n (2 ≤ n ≤ 1000) — the number of the tram's stops.

// Then n lines follow, each contains two integers ai and bi (0 ≤ ai, bi ≤ 1000) — the number of passengers that exits the tram at the i-th stop, and the number of passengers that enter the tram at the i-th stop. The stops are given from the first to the last stop in the order of tram's movement.

//     The number of people who exit at a given stop does not exceed the total number of people in the tram immediately before it arrives at the stop. More formally, . This particularly means that a1 = 0.
//     At the last stop, all the passengers exit the tram and it becomes empty. More formally, .
//     No passenger will enter the train at the last stop. That is, bn = 0.

// Output

// Print a single integer denoting the minimum possible capacity of the tram (0 is allowed).

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

	var minCapacity, nPeopleOnTrain int
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		nPeopleOnTrain -= a
		nPeopleOnTrain += b
		if nPeopleOnTrain > minCapacity {
			minCapacity = nPeopleOnTrain
		}
	}
	fmt.Print(minCapacity)
}

package main

import "fmt"

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

	var a, b, c, capacity int
	for i:=0; i<n; i++ {
		fmt.Scan(&a, &b)
		c += b - a
		if c > capacity {capacity = c}
	}
	fmt.Println(capacity)
}

package main

import (
	"fmt"
)

func main() {
	var n, x, y, in int
	fmt.Scanln(&n)
	max := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &x, &y)
		if i == 0 {
			in = y
			max = y
		} else {
			in = in - x + y
			if in > max {
				max = in
			}
		}
	}
	fmt.Println(max)
}

package main

import "fmt"

func main() {
	var n, in, out int
	fmt.Scanf("%d\n", &n)
	count := 0
	max := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &out, &in)
		count = count - out + in
		if max < count {
			max = count
		}
	}
	fmt.Println(max)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	_ = scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	currentCapacity, maxCapacity := 0, 0

	for i := 0; i < n; i++ {
		_ = scanner.Scan()
		line := scanner.Text()
		splitted := strings.Split(line, " ")
		out, _ := strconv.Atoi(splitted[0])
		in, _ := strconv.Atoi(splitted[1])
		currentCapacity = currentCapacity - out + in
		if currentCapacity > maxCapacity {
			maxCapacity = currentCapacity
		}
	}
	fmt.Println(maxCapacity)

}

package main

import "fmt"

type stop_diff struct {
	pout int
	pin  int
}

func task_solution(in []stop_diff) (ans int) {
	size := 0
	for i := 0; i < len(in); i += 1 {
		size += in[i].pin - in[i].pout
		if ans < size {
			ans = size
		}
	}
	return ans
}

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

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

	fmt.Println(task_solution(in))
}

package main

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

import "fmt"

type stop_diff struct {
	pout int
	pin  int
}

func task_solution(in []stop_diff) (ans int) {
	size := 0
	for i := 0; i < len(in); i += 1 {
		size += in[i].pin - in[i].pout
		if ans < size {
			ans = size
		}
	}
	return ans
}

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

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

	fmt.Println(task_solution(in))
}

// Author: sighduck
// URL: https://codeforces.com/problemset/problem/116/A

package main

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

type Stop struct {
	Ai int
	Bi int
}

func Solve(n int, stops []Stop) int {
	capacity := 0
	maxCapacity := 0
	for _, s := range stops {
		capacity -= s.Ai
		capacity += s.Bi
		if capacity > maxCapacity {
			maxCapacity = capacity
		}
	}
	return maxCapacity
}

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

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

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

package main

import (
	"fmt"
)

func main() {
	var n, a, b int
	fmt.Scanln(&n)

	current := 0
	max := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &a, &b)
		current -= a
		current += b
		if current > max {
			max = current
		}
	}

	fmt.Println(max)
}

package main

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

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

func main() {
	cin := bufio.NewReader(os.Stdin)
	cout := bufio.NewWriter(os.Stdout)
	defer cout.Flush()
	var n, a, b int
	result := 0
	cur := 0
	fmt.Fscan(cin, &n)
	for i := 0; i < n; i++ {
		fmt.Fscan(cin, &a, &b)
		cur -= a
		cur += b
		result = max(result, cur)
	}
	fmt.Fprintln(cout, result)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	var current = 0
	var max = 0
	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scan(&a, &b)
		current += b - a
		if current > max {
			max = current
		}
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
)

func main() {
	var n int
	var a, b int
	var maxPersons, currentPersons int
	maxPersons = 0
	fmt.Scanf("%d\n", &n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &a, &b)
		currentPersons -= a
		currentPersons += b
		if currentPersons > maxPersons {
			maxPersons = currentPersons
		}
	}
	fmt.Println(maxPersons)
}

package main

import "fmt"

func main(){
	var n int
	fmt.Scanf("%d", &n)
	prevStepPop := 0
	var maxCap int = 0
	for i:=0;i<n;i++{
		var a, b int
		fmt.Scanf("%d%d", &a, &b)
		b = b - a
		prevStepPop = prevStepPop + b
		if prevStepPop > maxCap{
			maxCap = prevStepPop
		}
	}
	fmt.Println(maxCap)
}

package main

import "fmt"

func main(){
	var n int
	fmt.Scanf("%d", &n)
	prevStepPop := 0
	var maxCap int = 0
	for i:=0;i<n;i++{
		var a, b int
		fmt.Scanf("%d%d", &a, &b)
		if ((prevStepPop - a ) + b) > maxCap {
			maxCap = (prevStepPop - a) + b
		}
		fmt.Println(prevStepPop)
		prevStepPop = (prevStepPop - a) + b
	}
	fmt.Println(maxCap)
}

package main

import "fmt"

func main(){
	var n int
	fmt.Scanf("%d", &n)
	prevStepPop := 0
	var maxCap int = 0
	for i:=0;i<n;i++{
		var a, b int
		fmt.Scanf("%d%d", &a, &b)
		if prevStepPop - a  + b > maxCap {
			maxCap = prevStepPop - a + b
		}
		prevStepPop = prevStepPop - a + b
	}
	fmt.Println(maxCap)
}

package main

import "fmt"

func main(){
	var n int
	fmt.Scanf("%d", &n)
	prevStepPop := 0
	var maxCap int = 0
	for i:=0;i<n;i++{
		var a, b int
		fmt.Scanf("%d%d", &a, &b)
		if ((prevStepPop - a ) + b) > maxCap {
			maxCap = (prevStepPop - a) + b
		}
		prevStepPop = (prevStepPop - a) + b
	}
	fmt.Println(maxCap)
}

package main

import "fmt"

func main() {
	var n, x, y, cap, currentPassenger int
	fmt.Scan(&n, &currentPassenger, &currentPassenger)
	cap = currentPassenger
	for i := 1; i < n; i++ {
		fmt.Scan(&x, &y)
		if currentPassenger+x-y > cap {
			cap = currentPassenger + x - y
		}
		currentPassenger = currentPassenger + x - y
	}
	fmt.Println(cap)
}

package main

import "fmt"

func main() {
	var n, x, y, cap, currentPassenger int
	fmt.Scan(&n, &currentPassenger, &currentPassenger)
	cap = currentPassenger
	for i := 1; i < n; i++ {
		fmt.Scan(&x, &y)
		if currentPassenger+x > cap {
			cap = currentPassenger + x
		}
		currentPassenger = currentPassenger + x - y
	}
	fmt.Println(cap)
}

package main

import (
	"fmt"
	"math"
)

func main() {
	ejemplo()
}

func ejemplo() {
	var n, m, a float64
	fmt.Scanf("%g %g %g", &n, &m, &a)
	fmt.Println(math.Ceil(n/a) * math.Ceil(m/a))
}

package main

import (
	"fmt"
	"math"
)

func main() {
	var n, m, a float64
	fmt.Scanf("%g%g%g", &n, &m, &a)
	fmt.Println(int64(math.Ceil(n/a) * math.Ceil(m/a)))
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n uint16
	var a, b uint16
	var cb uint32
	var max uint16
	fmt.Fscanf(in, "%d\n", &n)
	for i := 0; uint16(i) < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		cb -= uint32(a)
		cb += uint32(b)
		if cb > uint32(max) {
			max = uint16(cb)
		}
	}
	fmt.Println(max)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n uint16
	var a, b uint16
	var cb uint16
	var max uint16
	fmt.Fscanf(in, "%d\n", &n)
	for i := 0; uint16(i) < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		cb -= a
		cb += b
		if cb > max {
			max = cb
		}
	}
	fmt.Println(max)
}

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var n uint16
	var a, b uint16
	var cb uint16
	var max uint32
	fmt.Fscanf(in, "%d\n", &n)
	for i := 0; uint16(i) < n; i++ {
		fmt.Fscanf(in, "%d %d\n", &a, &b)
		cb -= a
		cb += b
		if uint32(cb) > max {
			max = uint32(cb)
		}
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
)

func main() {
	var t, a, b, ctr int
	fmt.Scan(&t)
	for t > 0 {
		fmt.Scan(&a, &b)
		if b-a > 0 {
			ctr += b - a
		}
		t--
	}
	fmt.Println(ctr)
}

package main

import "fmt"

func main() {
	var n int
	max := 0
	num := 0

	fmt.Scanf("%d", &n)

	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scanf("%d %d", &a, &b)

		num += -a + b
		if num > max {
			max = num
		}
	}
	// xD

	fmt.Printf("%d", max)
}

package main

import "fmt"

func main() {
	var n int
	max := 0
	num := 0

	fmt.Scanf("%d", &n)

	for i := 0; i < n; i++ {
		var a, b int
		fmt.Scanf("%d %d", &a, &b)

		num += -a + b
		if num > max {
			max = num
		}
	}

	fmt.Printf("%d", max)
}

package main

import "fmt"

func main() {
	var n int
	ans := 0
	sisa := 0
	fmt.Scan(&n)
	for ; n > 0; n-- {
		var x, y int
		fmt.Scan(&x, &y)
		sisa -= x
		sisa += y
		if sisa > ans {
			ans = sisa
		}
	}
	print(ans)
}

package main

import (
	"fmt"
)

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

	max := 0

	for i := 0; i < n; i++ {
		var out int
		var in int

		_, err := fmt.Scanf("%d %d", &out, &in)

		if err != nil {
			i--
			continue
		}

		diff := in - out

		if max+diff > max {
			max = max + diff
		}
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
)

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

	max := 0
	now := 0

	var out int
	var in int

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

		_, err := fmt.Scanf("%d %d", &out, &in)

		if err != nil {
			i--
			continue
		}

		now = now - out + in

		if now > max {
			max = now
		}

		fmt.Println(max)
	}
	fmt.Println(max)
}

package main

import (
	"fmt"
	"math"
)

// 4 stop
// 0 3 exit-enter
// 2 5
// 4 2
// 4 0
func main() {

	var stops int
	fmt.Scanln(&stops)
	var cap float64 = 0
	var x, y int
	for i := 1; i <= stops; i++ {
		fmt.Scanf("%d %d\n", &x, &y)
		cap = math.Max(cap, cap+float64(y)-float64(x))
	}
	fmt.Println(int(cap))
}

package main

import (
	"fmt"
	"math"
)

// 4 stop
// 0 3 exit-enter
// 2 5
// 4 2
// 4 0
func main() {

	var stops int
	fmt.Scanln(&stops)
	var ans, cap float64 = 0, 0
	var x, y int
	for i := 1; i <= stops; i++ {
		fmt.Scanf("%d %d\n", &x, &y)
		cap = cap + float64(y) - float64(x)
		ans = math.Max(cap, ans)
		fmt.Println(cap)
	}
	fmt.Println(int(ans))
}

package main

import (
        "fmt"
)

func main() {
        var num int
        fmt.Scanf("%d", &num)
        max := 0
        curr := 0
        var indoor, outdoor int
        for i := 0; i < num; i++ {
                fmt.Scanf("%d %d", &outdoor, &indoor)
                curr -= outdoor
                curr += indoor
                if curr > max {
                        max = curr
                }
        }
        fmt.Println(max)
}

package main

import (
	"fmt"
)

func main() {
	var t int
	fmt.Scan(&t)
	tot := 0
	max := 0
	for i := 0; i < t; i++ {
		var tm int
		fmt.Scan(&tm)
		tot -= tm

		fmt.Scan(&tm)
		tot += t

		if max < tot {
			max = tot
		}
	}
	fmt.Println(max)
}

package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	//n, _ := strconv.Atoi(scanner.Text())
	max := 0
	cap := 0
	for scanner.Scan() {
		scanner.Scan()
		in := strings.Split(scanner.Text(), " ")
		a, _ := strconv.Atoi(in[0])
		b, _ := strconv.Atoi(in[1])
		cap -= a
		cap += b
		if cap > max {
			max = cap
		}
	}
	fmt.Println(max)
}

package main

import "fmt"

func main() {
	var n uint16
	var cap int32
	fmt.Scan(&n)
	for ; n > uint16(0); n-- {
		var a, b int16
		fmt.Scan(&a, &b)
		if int32(b-a) > 0 {
			cap += int32(b - a)
		}
	}
	fmt.Println(cap)
}

package main

import (
	"fmt"
)

func main() {
	var numStops, total, final int
    _, err1 := fmt.Scanf("%d",&numStops)
    fmt.Println(numStops)
	for x := 0; x < numStops; x++ {
        var out, in int
        fmt.Scan(&out)
        fmt.Scan(&in)
        //fmt.Println(out,in)
    //temp = append(temp, out,in)

		total += in-out
		//.Println(total)
		if final < total{
			final = total
		}

	}
	if err1!= nil{
	    panic(err1)
	}
	fmt.Println(final)

	//fmt.Println(strings.Compare(numStops,B))

}

package main

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

func main() {
	minCap := 0
	curr := 0
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	stops,_ := strconv.Atoi(scanner.Text())
	for i := 0; i < stops; i++ {
		temp := curr
		scanner.Scan()
		stp := scanner.Text()
		inout := strings.Split(stp, " ")
		pOut,_ := strconv.Atoi(inout[0])
		pIn,_ := strconv.Atoi(inout[1])
		temp -= pOut
		temp += pIn
		if temp > minCap {
			minCap = temp
		}
	}
	fmt.Println(minCap)
}

package main

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

func main() {
	minCap := 0
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	stops,_ := strconv.Atoi(scanner.Text())
	for i := 0; i < stops; i++ {
		temp := minCap
		scanner.Scan()
		stp := scanner.Text()
		inout := strings.Split(stp, " ")
		pOut,_ := strconv.Atoi(inout[0])
		pIn,_ := strconv.Atoi(inout[1])
		temp -= pOut
		temp += pIn
		if temp > minCap {
			minCap = temp
		}
	}
	fmt.Println(minCap)
}

package main

import (
	"fmt"
)

func main() {
	var n, x, y, in int
	fmt.Scanln(&n)
	max := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &x, &y)
		if i == 0 {
			in = y
		} else {
			in = in - x + y
			if in > max {
				max = in
			}
		}
	}
	fmt.Println(max)
}

package main

import "fmt"

func main() {
	var n, in, out int
	fmt.Scanf("%d\n", &n)
	count := 0
	max := 0
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d\n", &out, &in)
		count := count - out + in
		if max < count {
			max = count
		}
	}
	fmt.Println(max)
}


The weight of a sequence is defined as the number of unordered pairs of indexes $$$(i,j)$$$ (here $$$i \lt j$$$) with same value ($$$a_{i} = a_{j}$$$). For example, the weight of sequence $$$a = [1, 1, 2, 2, 1]$$$ is $$$4$$$. The set of unordered pairs of indexes with same value are $$$(1, 2)$$$, $$$(1, 5)$$$, $$$(2, 5)$$$, and $$$(3, 4)$$$.You are given a sequence $$$a$$$ of $$$n$$$ integers. Print the sum of the weight of all subsegments of $$$a$$$. A sequence $$$b$$$ is a subsegment of a sequence $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

For each test case, print a single integer — the sum of the weight of all subsegments of $$$a$$$.

Note  In test case $$$1$$$, all possible subsegments of sequence $$$[1, 2, 1, 1]$$$ having size more than $$$1$$$ are:   $$$[1, 2]$$$ having $$$0$$$ valid unordered pairs;  $$$[2, 1]$$$ having $$$0$$$ valid unordered pairs;  $$$[1, 1]$$$ having $$$1$$$ valid unordered pair;  $$$[1, 2, 1]$$$ having $$$1$$$ valid unordered pairs;  $$$[2, 1, 1]$$$ having $$$1$$$ valid unordered pair;  $$$[1, 2, 1, 1]$$$ having $$$3$$$ valid unordered pairs.  Answer is $$$6$$$. In test case $$$2$$$, all elements of the sequence are distinct. So, there is no valid unordered pair with the same value for any subarray. Answer is $$$0$$$.

package main

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

var t int

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

	fmt.Fscan(in, &t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in, &n)
		mp := make(map[int][]int64)

		for i := 1; i <= n; i++ {
			var x int
			fmt.Fscan(in, &x)
			mp[x] = append(mp[x], int64(i))
		}
		var ans int64 = 0
		for _, value := range mp {
			if len(value) < 2 {
				continue
			}
			suf := make([]int64, len(value))
			suf[len(suf)-1] = value[len(value)-1]
			for i := len(suf) - 2; i >= 0; i-- {
				suf[i] = suf[i+1] + value[i]
			}
			for i := 0; i < len(value)-1; i++ {
				sm := suf[i+1]
				ans += int64(len(value)-i-1)*int64(int64(n)*value[i]+value[i]) - value[i]*sm
			}
		}
		fmt.Fprintln(out, ans)
	}

}

package main

import (
	"bufio"
	"math/big"
	"os"
	"strconv"
)

type node struct {
	n, h *node
	pars []*node
	end  bool
}

func start() {
	t := sint()
	for ; t > 0; t-- {
		n := sint()
		a := sints(n)
		res := big.NewInt(0)
		pLocSum := make(map[int]int64)
		for i, v := range a {
			pSum := pLocSum[v]
			res.Add(res, big.NewInt(pSum*int64(n-i)))
			pLocSum[v] += int64(i + 1)
		}
		wlstring(res.String())
	}
}

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

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

// io utils

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

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

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

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

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

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

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

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	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 int64
	scan(&n)
	var elems = make(map[int][]int64)
	for i := int64(0); i < n; i++ {
		x := 0
		scan(&x)
		elems[x] = append(elems[x], i)
	}
	var ans int64
	for _, v := range elems {
		sort.Slice(v, func(i, j int) bool { return v[i] < v[j] })
		var fullsum int64
		for _, i := range v {
			fullsum += n - i
		}
		for _, i := range v {
			fullsum -= n - i
			ans += (i + 1) * fullsum
		}
	}
	printf("%v\n", ans)
}

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	out := os.Stdout
	fmt.Fscan(in,&t)
	for u:=1;u<=t;u++{
		var n int
		fmt.Fscan(in,&n)
		a := make([]int,n+5)
		for i:=1;i<=n;i++{
			fmt.Fscan(in,&a[i])
		}
		mp := map[int][]int{}
		for i:=1;i<=n;i++{
			mp[a[i]] = append(mp[a[i]],i)
		}
		var ans int64
		for _,v := range mp{
			pre := 0
			for i:=0;i<len(v);i++{
				ans += int64(pre) * int64(n-v[i]+1)
				pre += v[i]
			}
		}
		fmt.Fprintln(out,ans)
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
)

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

	solve := func(Case int) {
		var n, v int
		Fscan(in, &n)
		ans := int64(0)
		psum := map[int]int64{}
		for i := 0; i < n; i++ {
			Fscan(in, &v)
			ans += int64(n-i) * psum[v]
			psum[v] += int64(i + 1)
		}
		Fprintln(out, ans)
	}

	T := 1
	Fscan(in, &T) //
	for Case := 1; Case <= T; Case++ {
		solve(Case)
	}

	_leftData, _ := ioutil.ReadAll(in)
	if _s := strings.TrimSpace(string(_leftData)); _s != "" {
		panic("有未读入的数据：\n" + _s)
	}
}

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

package main

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

var t int

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

	fmt.Fscan(in,&t)
	for t > 0 {
		t--
		var n int
		fmt.Fscan(in,&n)
		mp := make(map[int][]int)

		for i := 1; i <= n; i++ {
			var x int
			fmt.Fscan(in,&x)
			mp[x] = append(mp[x], i)
		}
		var ans = 0
		for _, value := range mp {
			if len(value) < 2 {
				continue
			}
			suf := make([]int, len(value))
			suf[len(suf)-1] = value[len(value)-1]
			for i := len(suf) - 2; i >= 0; i-- {
				suf[i] = suf[i+1] + value[i]
			}
			for i := 0; i < len(value)-1; i++ {
				sm := suf[i+1]
				ans += (len(value)-i-1)*(n*value[i]+value[i]) - value[i]*sm
			}
		}
		fmt.Fprintln(out,ans)
	}

}

package main

import (
	"bufio"
	"math/big"
	"os"
	"strconv"
)

type node struct {
	n, h *node
	pars []*node
	end  bool
}

func start() {
	t := sint()
	for ; t > 0; t-- {
		n := sint()
		a := sints(n)
		res := big.NewInt(0)
		pLocSum := make(map[int]int)
		for i, v := range a {
			pSum := pLocSum[v]
			res.Add(res, big.NewInt(int64(pSum*(n-i))))
			pLocSum[v] += i + 1
		}
		wlstring(res.String())
	}
}

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

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

// io utils

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

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

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

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

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

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

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

package main

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

type node struct {
	n, h *node
	pars []*node
	end  bool
}

func start() {
	t := sint()
	for ; t > 0; t-- {
		n := sint()
		a := sints(n)
		res := 0
		pLocSum := make(map[int]int)
		for i, v := range a {
			pSum := pLocSum[v]
			res += pSum * (n - i)
			pLocSum[v] += i + 1
		}
		wlint(res)
	}
}

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

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

// io utils

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

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

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

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

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

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

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


Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:In the above formula, 1 ≤ l < r ≤ n must hold, where n is the size of the Main Uzhlyandian Array a, and |x| means absolute value of x. But the heroes skipped their math lessons in school, so they asked you for help. Help them calculate the maximum value of f among all possible values of l and r for the given array a.

The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a. The second line contains n integers a1, a2, ..., an (-109 ≤ ai ≤ 109) — the array elements.

Print the only integer — the maximum value of f.

NoteIn the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].In the second case maximal value of f is reachable only on the whole array.

package main

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

func core(A []int64) int64 {
	imos := make([]int64, len(A))
	imos[0] = A[0]
	p := int64(-1)
	mx := imos[0]
	for i := 1; i < len(A); i++ {
		imos[i] = p*A[i] + imos[i-1]
		p *= -1
		if mx < imos[i] {
			mx = imos[i]
		}
		if imos[i]<0{
			imos[i]=0
		}

	}
	return mx
}
func main() {
	sc := NewScanner()
	N := sc.NextInt64()
	A := sc.NextInt64Array()

	diff := make([]int64, N-1)
	for i := int64(1); i < N; i++ {
		diff[i-1] = abs(A[i] - A[i-1])
	}

	if N == 2 {
		fmt.Println(core(diff))
		return
	}

	a, b := core(diff), core(diff[1:])
	if a < b {
		fmt.Println(b)
	} else {
		fmt.Println(a)
	}

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

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

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

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

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

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

	return mp
}

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

package main

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

func core(A []int64) int64 {
	imos := make([]int64, len(A))
	imos[0] = A[0]
	p := int64(-1)
	mx := imos[0]
	for i := 1; i < len(A); i++ {
		imos[i] = p*A[i] + imos[i-1]
		p *= -1
		if mx < imos[i] {
			mx = imos[i]
		}
	}
	return mx
}
func main() {
	sc := NewScanner()
	N := sc.NextInt64()
	A := sc.NextInt64Array()

	diff := make([]int64, N-1)
	for i := int64(1); i < N; i++ {
		diff[i-1] = abs(A[i] - A[i-1])
	}

	if N == 2 {
		fmt.Println(core(diff))
		return
	}

	a, b := core(diff), core(diff[1:])
	if a < b {
		fmt.Println(b)
	} else {
		fmt.Println(a)
	}

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

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

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

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

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

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

	return mp
}

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


One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first.  There are $$$n$$$ books standing in a row on the shelf, the $$$i$$$-th book has color $$$a_i$$$.You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other.In one operation you can take one book from any position on the shelf and move it to the right end of the shelf.What is the minimum number of operations you need to make the shelf beautiful?

The first line contains one integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10^5$$$) — the number of books. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the book colors.

Output the minimum number of operations to make the shelf beautiful.

NoteIn the first example, we have the bookshelf $$$[1, 2, 2, 1, 3]$$$ and can, for example:   take a book on position $$$4$$$ and move to the right end: we'll get $$$[1, 2, 2, 3, 1]$$$;  take a book on position $$$1$$$ and move to the right end: we'll get $$$[2, 2, 3, 1, 1]$$$. In the second example, we can move the first book to the end of the bookshelf and get $$$[2,2,1,1,1]$$$.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1481E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	type pair struct{ l, r int }

	var n int
	Fscan(in, &n)
	a := make([]int, n)
	pos := make([]pair, n+1)
	for i := range pos {
		pos[i].l = -1
	}
	for i := range a {
		Fscan(in, &a[i])
		v := a[i]
		if pos[v].l < 0 {
			pos[v].l = i
		}
		pos[v].r = i
	}
	dp := make([]int, n+1)
	c := make([]int, n+1)
	for i := n - 1; i >= 0; i-- {
		v := a[i]
		c[v]++
		dp[i] = dp[i+1]
		if p := pos[v]; i > p.l {
			dp[i] = max(dp[i], c[v])
		} else {
			dp[i] = max(dp[i], dp[p.r+1]+c[v])
		}
	}
	Fprint(out, n-dp[0])
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1481E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	type pair struct{ l, r int }

	var n int
	Fscan(in, &n)
	n++
	a := make([]int, n)
	ps := make([]pair, n)
	for i := 1; i < n; i++ {
		Fscan(in, &a[i])
		v := a[i]
		if ps[v].l == 0 {
			ps[v].l = i
		}
		ps[v].r = i
	}
	dp := make([]int, n+1)
	c := make([]int, n)
	for i := n - 1; i > 0; i-- {
		v := a[i]
		c[v]++
		dp[i] = dp[i+1]
		if i > ps[v].l {
			dp[i] = max(dp[i], c[v])
		} else {
			dp[i] = max(dp[i], dp[ps[v].r+1]+c[v])
		}
	}
	Fprint(out, n-1-dp[1])
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1481E(_r io.Reader, _w io.Writer) {
	in := bufio.NewReader(_r)
	out := bufio.NewWriter(_w)
	defer out.Flush()
	max := func(a, b int) int {
		if a > b {
			return a
		}
		return b
	}
	type pair struct{ l, r int }

	var n int
	Fscan(in, &n)
	n++
	a := make([]int, n)
	c := make([]int, n)
	ps := make([]pair, n)
	for i := 1; i < n; i++ {
		Fscan(in, &a[i])
		v := a[i]
		c[v]++
		if ps[v].l == 0 {
			ps[v].l = i
		}
		ps[v].r = i
	}
	dp := make([]int, n+1)
	sc := make([]int, n)
	for i := n - 1; i > 0; i-- {
		v := a[i]
		sc[v]++
		dp[i] = dp[i+1]
		if i > ps[v].l {
			dp[i] = max(dp[i], sc[v])
		} else {
			dp[i] = max(dp[i], dp[ps[v].r+1]+c[v])
		}
	}
	Fprint(out, n-1-dp[1])
}

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

package main

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

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

	var n int;
	Fscan(in, &n);
	a := make([]int, n+1);
	dp := make([]int, n+2);
	l := make([]int, n+1);
	r := make([]int, n+1);
	cnt := make([]int, n+1);
	for i:=1;i<=n;i++ {
		Fscan(in, &a[i]);
		if l[a[i]] == 0 {
			l[a[i]] = i;
		}
		r[a[i]] = i;
	}
	for i:=n;i>=1;i-- {
		cnt[a[i]]++;
		if dp[i+1] < cnt[a[i]] {
			dp[i] = cnt[a[i]];
		} else{
			dp[i] = dp[i+1];
		}

		if i == l[a[i]] {
			if dp[i] < cnt[a[i]] + dp[r[a[i]]+1] {
				dp[i] = cnt[a[i]] + dp[r[a[i]]+1];
			}
		}
	}
	Fprint(out, n-dp[1], "\n");
}

package main

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

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

	var buf bytes.Buffer

	tc := 1
	for tc > 0 {
		tc--
		n := readNum(reader)
		C := readNNums(reader, n)
		buf.WriteString(fmt.Sprintf("%d\n", solve(n, C)))
	}
	fmt.Print(buf.String())
}

func readInt(bytes []byte, from int, val *int) int {
	i := from
	sign := 1
	if bytes[i] == '-' {
		sign = -1
		i++
	}
	tmp := 0
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + int(bytes[i]-'0')
		i++
	}
	*val = tmp * sign
	return i
}

func readNum(reader *bufio.Reader) (a int) {
	bs, _ := reader.ReadBytes('\n')
	readInt(bs, 0, &a)
	return
}

func readTwoNums(reader *bufio.Reader) (a int, b int) {
	res := readNNums(reader, 2)
	a, b = res[0], res[1]
	return
}

func readThreeNums(reader *bufio.Reader) (a int, b int, c int) {
	res := readNNums(reader, 3)
	a, b, c = res[0], res[1], res[2]
	return
}

func readNNums(reader *bufio.Reader, n int) []int {
	res := make([]int, n)
	x := 0
	bs, _ := reader.ReadBytes('\n')
	for i := 0; i < n; i++ {
		for x < len(bs) && (bs[x] < '0' || bs[x] > '9') && bs[x] != '-' {
			x++
		}
		x = readInt(bs, x, &res[i])
	}
	return res
}

func readUint64(bytes []byte, from int, val *uint64) int {
	i := from

	var tmp uint64
	for i < len(bytes) && bytes[i] >= '0' && bytes[i] <= '9' {
		tmp = tmp*10 + uint64(bytes[i]-'0')
		i++
	}
	*val = tmp

	return i
}

func solve(n int, C []int) int {
	L, R := make([]int, n), make([]int, n)
	F := make([]int, n)

	for i := 0; i < n; i++ {
		L[i] = -1
	}

	for i := 0; i < n; i++ {
		x := C[i] - 1
		if L[x] < 0 {
			L[x] = i
		}
		R[x] = i
		F[x]++
	}

	CF := make([]int, n)
	dp := make([]int, n+1)

	for i := n - 1; i >= 0; i-- {
		dp[i] = dp[i+1]
		x := C[i] - 1
		CF[x]++
		if i == L[x] {
			// color x都留在原地，其他的都被移到到后端
			dp[i] = max(dp[i], dp[R[x]+1]+F[x])
		} else {
			dp[i] = max(dp[i], CF[x])
		}
	}

	return n - dp[0]
}

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

package main

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

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

	var n int;
	Fscan(in, &n);
	a := make([]int, n+1);
	dp := make([]int, n+1);
	cnt := make([]int, n+1);
	l := make([]int, n+1);
	r := make([]int, n+1);
	for i:=1;i<=n;i++ {
		Fscan(in, &a[i]);
		if l[a[i]] == 0 {
			l[a[i]] = i;
		}
		r[a[i]] = i;
	}
	ans := 0;
	for i:=1;i<=n;i++ {
		cnt[a[i]]++;
		if dp[i-1] < dp[l[a[i]]] + cnt[a[i]] {
			dp[i] = dp[l[a[i]]] + cnt[a[i]];
		} else{
			dp[i] = dp[i-1];
		}

		if ans < dp[i] {
			ans = dp[i];
		}
	}
	Fprint(out, n-ans, "\n");
}


One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it?

The first line contains one integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the number of bracket sequences. Each of the following $$$n$$$ lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")". The sum of lengths of all bracket sequences in the input is at most $$$5 \cdot 10^5$$$. Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter.

Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement.

NoteIn the first example, it's optimal to construct two pairs: "((     )())" and "(     )".

package main

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	n := 0
	fmt.Fscanf(in, "%d\n", &n)
	numF := make(map[int]int)
	numB := make(map[int]int)
	perfect := 0
	for i := 0; i < n; i++ {
		s := ""
		fmt.Fscanf(in, "%s\n", &s)
		f := 0
		b := 0
		valid := true
		for _, ch := range s {
			if ch == '(' {
				f++
			} else {
				b++
			}
			if b > f {
				valid = false
			}
		}
		if valid {
			if f == b {
				perfect++
			} else if f > b {
				numF[f-b]++
			}
		} else {
			valid = true
			f = 0
			b = 0
			for i := range s {
				if s[len(s)-1-i] == ')' {
					f++
				} else {
					b++
				}
				if b > f {
					valid = false
				}
			}
			if valid {
				if f == b {
					perfect++
				} else if f > b {
					numB[f-b]++
				}
			}
		}
	}
	ans := 0
	ans += perfect / 2
	for k, v := range numF {
		ans += min(v, numB[k])
	}
	fmt.Fprintf(out, "%d\n", ans)
	return
}

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

package main

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

func main() {
	var N int
	scan(&N)

	m := make(map[int]int, 128)
	for i, s := 0, ""; i < N; i++ {
		scan(&s)
		lx, cnt := 0, 0
		for _, val := range s {
			switch val {
			case '(':
				cnt++
			case ')':
				cnt--
			}

			if lx > cnt {
				lx = cnt
			}
		}

		if cnt < 0 && lx < cnt || cnt >= 0 && lx < 0 {
			continue
		}
		m[cnt]++
	}

	ans := 0
	for idx, val := range m {
		if idx > 0 {
			ans += min2(val, m[-idx])
		} else if idx == 0 {
			ans += val / 2
		}
	}

	fmt.Println(ans)
}

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

// buffered
var bufin = bufio.NewReader(os.Stdin)

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

// For Parenthesis like "))))(())(((("
// We can scan twice(backward, forward) to get value like 4, 4 (producing 4 extra ")" and 4 extra "(")
// (Be careful "((): and ")((" is very different.)
// After get the meta data, we can start working on pairs.
package main

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

var (
	open  = "("[0]
	close = ")"[0]
)

func calcSequence(s string) (int, int) {
	oCounter, cCounter, extraO, extraC := 0, 0, 0, 0

	for i := 0; i < len(s); i++ {
		if s[i] == close {
			cCounter++
		}
		if s[i] == open {
			if oCounter < cCounter {
				extraC, oCounter, cCounter = extraC+(cCounter-oCounter), 0, 0
			}
			oCounter++
		}
	}
	if cCounter > oCounter {
		extraC += cCounter - oCounter
	}

	oCounter, cCounter = 0, 0
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == open {
			oCounter++
		}
		if s[i] == close {
			if cCounter < oCounter {
				extraO, oCounter, cCounter = extraO+(oCounter-cCounter), 0, 0
			}
			cCounter++
		}
	}
	if oCounter > cCounter {
		extraO += oCounter - cCounter
	}

	return extraO, extraC
}

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

func main() {
	n := 0
	fmt.Scan(&n)
	reader := bufio.NewReaderSize(os.Stdin, 5*int(1e5))
	p := make([]string, n)
	for i := 0; i < n; i++ {
		s := ""
		fmt.Fscan(reader, &s)
		p[i] = s
	}

	pMap := map[[2]int]int{}
	for _, s := range p {
		o, c := calcSequence(s)
		if (o == 0) || (c == 0) {
			pMap[[2]int{o, c}]++
		}
	}

	sum := pMap[[2]int{0, 0}] / 2
	for k, v := range pMap {
		if k == [2]int{0, 0} {
			continue
		}
		negK := [2]int{k[1], k[0]}
		pairCount := min(v, pMap[negK])
		sum, pMap[k], pMap[negK] = sum+pairCount, pMap[k]-pairCount, pMap[negK]-pairCount
	}
	fmt.Println(sum)
}

package main

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

const (
	BIG = 1024 * 1024 * 10
)

func readLine(r *bufio.Reader) string {
	bytes, _, err := r.ReadLine()
	if err != nil {
		log.Fatalln("Failed to read line")
	}

	return string(bytes)
}

func readInts(r *bufio.Reader) []int {
	elems := strings.Fields(readLine(r))

	ret := make([]int, len(elems))
	for i, el := range elems {
		tmp, err := strconv.ParseInt(el, 10, 64)
		if err != nil {
			log.Fatalf("Failed to parse number %v", el)
		}
		ret[i] = int(tmp)
	}

	return ret
}

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, BIG)
	var opening rune
	open := "("
	for _, el := range open {
		opening = el
		break
	}

	n := readInts(r)[0]

	pos := make(map[int]int)
	neg := make(map[int]int)
	good := 0
	for i := 0; i < n; i++ {
		seq := readLine(r)

		curr := 0
		isNeg := false
		min := 0
		for _, el := range seq {
			if el == opening {
				curr += 1
			} else {
				curr -= 1
			}
			if curr < 0 {
				isNeg = true
			}
			if curr < min {
				min = curr
			}
		}
		if isNeg {
			if min != curr {
				continue
			}
			if _, ok := neg[-curr]; ok {
				neg[-curr] += 1
			} else {
				neg[-curr] = 1
			}
		} else if curr == 0 {
			good += 1
		} else {
			if _, ok := pos[curr]; ok {
				pos[curr] += 1
			} else {
				pos[curr] = 1
			}
		}
	}

	res := good / 2
	for key, val := range pos {
		if nval, ok := neg[key]; ok {
			res += GetMin(val, nval)
		}
	}
	fmt.Println(res)
}

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

package main

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

type SequenceMetadata struct {
	CanStart   bool
	CanEnd     bool
	IsBalanced bool
	LeftCount  int
	RightCount int
}

func canStart(sequence string) bool {
	leftCount := 0
	rightCount := 0
	for i := 0; i < len(sequence); i++ {
		if string(sequence[i]) == "(" {
			leftCount += 1
		} else if string(sequence[i]) == ")" {
			rightCount += 1
		}
		if rightCount > leftCount {
			return false
		}
	}
	return true
}

func canEnd(sequence string) bool {
	leftCount := 0
	rightCount := 0
	for i := len(sequence) - 1; i >= 0; i-- {
		if string(sequence[i]) == "(" {
			leftCount += 1
		} else if string(sequence[i]) == ")" {
			rightCount += 1
		}
		if leftCount > rightCount {
			return false
		}
	}
	return true
}

func analyzeSequence(sequence string) SequenceMetadata {
	canStart := canStart(sequence)
	canEnd := canEnd(sequence)
	s := SequenceMetadata{
		CanStart:   canStart,
		CanEnd:     canEnd,
		IsBalanced: canStart && canEnd,
		LeftCount:  0,
		RightCount: 0,
	}
	for i := len(sequence) - 1; i >= 0; i-- {
		if string(sequence[i]) == "(" {
			s.LeftCount += 1
		} else if string(sequence[i]) == ")" {
			s.RightCount += 1
		}
	}
	return s
}

func Solve(n int, sequences []string) int {
	sequenceMetadata := make([]SequenceMetadata, n)
	balancedCount := 0
	var startingDeltas []int
	var endingDeltas = make(map[int]int)
	for i := 0; i < n; i++ {
		sequenceMetadata[i] = analyzeSequence(sequences[i])
		if sequenceMetadata[i].IsBalanced {
			balancedCount += 1
		} else if sequenceMetadata[i].CanStart {
			startingDeltas = append(startingDeltas, sequenceMetadata[i].LeftCount-sequenceMetadata[i].RightCount)
		} else if sequenceMetadata[i].CanEnd {
			endingDeltas[sequenceMetadata[i].RightCount-sequenceMetadata[i].LeftCount] += 1
		}
	}
	matches := 0
	for i := 0; i < len(startingDeltas); i++ {
		if endingDeltas[startingDeltas[i]] > 0 {
			matches += 1
			endingDeltas[startingDeltas[i]] -= 1
		}
	}
	return matches + balancedCount/2
}

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

	var n int
	fmt.Fscanf(reader, "%d\n", &n)
	sequences := make([]string, n)
	for i := 0; i < n; i++ {
		fmt.Fscanf(reader, "%s\n", &sequences[i])
	}
	fmt.Fprintf(writer, "%d\n", Solve(n, sequences))
}

// For Parenthesis like "))))(())(((("
// We can scan twice(backward, forward) to get value like 4, 4 (producing 4 extra ")" and 4 extra "(")
// (Be careful "((): and ")((" is very different.)
// After get the meta data, we can start working on pairs.
package main

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

var (
	open  = "("[0]
	close = ")"[0]
)

func calcSequence(s string) (int, int) {
	oCounter, cCounter, extraO, extraC := 0, 0, 0, 0

	for i := 0; i < len(s); i++ {
		if s[i] == close {
			cCounter++
		}
		if s[i] == open {
			if oCounter < cCounter {
				extraC, oCounter, cCounter = extraC+(cCounter-oCounter), 0, 0
			}
			oCounter++
		}
	}
	if cCounter > oCounter {
		extraC += cCounter - oCounter
	}

	oCounter, cCounter = 0, 0
	for i := len(s) - 1; i >= 0; i-- {
		if s[i] == open {
			oCounter++
		}
		if s[i] == close {
			if cCounter < oCounter {
				extraO, oCounter, cCounter = extraO+(oCounter-cCounter), 0, 0
			}
			cCounter++
		}
	}
	if oCounter > cCounter {
		extraO += oCounter - cCounter
	}

	return extraO, extraC
}

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

func main() {
	n := 0
	fmt.Scan(&n)
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	p := make([]string, n)
	for i := 0; i < n; i++ {
		scanner.Scan()
		p[i] = scanner.Text()
	}

	pMap := map[[2]int]int{}
	for _, s := range p {
		o, c := calcSequence(s)
		if (o == 0) || (c == 0) {
			pMap[[2]int{o, c}]++
		}
	}

	sum := pMap[[2]int{0, 0}] / 2

	for k, v := range pMap {
		if k == [2]int{0, 0} {
			continue
		}
		negK := [2]int{k[1], k[0]}
		pairCount := min(v, pMap[negK])
		sum, pMap[k], pMap[negK] = sum+pairCount, pMap[k]-pairCount, pMap[negK]-pairCount
	}
	fmt.Println(sum)
}

package main

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

const (
	BIG = 1024 * 1024 * 10
)

func readLine(r *bufio.Reader) string {
	bytes, _, err := r.ReadLine()
	if err != nil {
		log.Fatalln("Failed to read line")
	}

	return string(bytes)
}

func readInts(r *bufio.Reader) []int {
	elems := strings.Fields(readLine(r))

	ret := make([]int, len(elems))
	for i, el := range elems {
		tmp, err := strconv.ParseInt(el, 10, 64)
		if err != nil {
			log.Fatalf("Failed to parse number %v", el)
		}
		ret[i] = int(tmp)
	}

	return ret
}

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, BIG)
	var opening rune
	open := "("
	for _, el := range open {
		opening = el
		break
	}

	n := readInts(r)[0]

	pos := make(map[int]int)
	neg := make(map[int]int)
	good := 0
	for i := 0; i < n; i++ {
		seq := readLine(r)

		curr := 0
		isNeg := false
		min := 0
		for _, el := range seq {
			if el == opening {
				curr += 1
			} else {
				curr -= 1
			}
			if curr < 0 {
				isNeg = true
			}
			if curr < min {
				min = curr
			}
		}
		if isNeg {
			if _, ok := neg[min]; ok {
				neg[-min] += 1
			} else {
				neg[-min] = 1
			}
		} else if curr == 0 {
			good += 1
		} else {
			if _, ok := pos[curr]; ok {
				pos[curr] += 1
			} else {
				pos[curr] = 1
			}
		}
	}

	res := good / 2
	for key, val := range pos {
		if nval, ok := neg[key]; ok {
			res += GetMin(val, nval)
		}
	}
	fmt.Println(res)
}


This is an interactive problem.After getting AC after 13 Time Limit Exceeded verdicts on a geometry problem, Kuroni went to an Italian restaurant to celebrate this holy achievement. Unfortunately, the excess sauce disoriented him, and he's now lost!The United States of America can be modeled as a tree (why though) with $$$n$$$ vertices. The tree is rooted at vertex $$$r$$$, wherein lies Kuroni's hotel.Kuroni has a phone app designed to help him in such emergency cases. To use the app, he has to input two vertices $$$u$$$ and $$$v$$$, and it'll return a vertex $$$w$$$, which is the lowest common ancestor of those two vertices.However, since the phone's battery has been almost drained out from live-streaming Kuroni's celebration party, he could only use the app at most $$$\lfloor \frac{n}{2} \rfloor$$$ times. After that, the phone would die and there will be nothing left to help our dear friend! :(As the night is cold and dark, Kuroni needs to get back, so that he can reunite with his comfy bed and pillow(s). Can you help him figure out his hotel's location?

NoteNote that the example interaction contains extra empty lines so that it's easier to read. The real interaction doesn't contain any empty lines and you shouldn't print any extra empty lines as well.The image below demonstrates the tree in the sample test:

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

type Graph struct {
	n     int
	m     int
	edges int
	fst   []int
	nxt   []int
	end   []int
}

func (g *Graph) Add(a, b int) {
	g.nxt[g.edges] = g.fst[a]
	g.fst[a] = g.edges
	g.end[g.edges] = b
	g.edges++
}

func (g *Graph) Adjacent(a int, fn func(b int)) {
	for u := g.fst[a]; u != -1; u = g.nxt[u] {
		fn(g.end[u])
	}
}

func NewGraph(n, m int) *Graph {
	g := &Graph{n, m, 0, make([]int, n), make([]int, m), make([]int, m)}
	for i := 0; i < n; i++ {
		g.fst[i] = -1
	}
	return g
}

var leaf []int
var parent []int

func (g *Graph) Dfs(v int, par int) {
	leaf[v] = v
	parent[v] = par
	g.Adjacent(v, func(to int) {
		if to == par {
			return
		}
		g.Dfs(to, v)
		if leaf[v] == v || rand.Intn(2) == 0 {
			leaf[v] = leaf[to]
		}
	})
}

func lca(out io.Writer, u, v int) int {
	fmt.Fprintln(out, "?", u+1, v+1)
	// out.Flush()
	return NextInt() - 1
}

func found(out io.Writer, root int) {
	fmt.Fprintln(out, "!", root+1)
	// out.Flush()
}

func main() {
	inf := os.Stdin
	out := os.Stdout
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()
	leaf = make([]int, n)
	parent = make([]int, n)

	g := NewGraph(n, 2*n)
	for i := 0; i < n-1; i++ {
		u, v := NextInt()-1, NextInt()-1
		g.Add(u, v)
		g.Add(v, u)
	}
	rand.Seed(time.Now().UnixNano())
	root := rand.Intn(n)

	// stderr("root", root)
	g.Dfs(root, -1)

	l1, l2 := -1, -1
	possible := make([]int, 0)
	for {
		possible = possible[:0]
		leaf[root] = root
		g.Adjacent(root, func(to int) {
			if to == parent[root] {
				return
			}
			if leaf[to] == l1 || leaf[to] == l2 {
				return
			}
			possible = append(possible, to)
		})
		found := false
		if len(possible)%2 == 1 {
			possible = append(possible, root)
		}
		// stderr("possible", possible)
		for i := 0; i < len(possible); i += 2 {
			// stderr(possible[i], leaf[possible[i]])
			// stderr(possible[i+1], leaf[possible[i+1]])
			result := lca(out, leaf[possible[i]], leaf[possible[i+1]])
			if result != root {
				l1 = leaf[possible[i]]
				l2 = leaf[possible[i+1]]
				root = result
				found = true
				break
			}
		}
		if found {
			continue
		}
		break
	}

	found(out, root)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func main() {
	in := bufio.NewReader(os.Stdin)
	var n, v, w, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		// 求直径
		var u, maxD int
		var f func(v, fa, d int)
		f = func(v, fa, d int) {
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if w != fa {
					f(w, v, d+1)
				}
			}
		}
		maxD = -1
		f(lca, -1, 0)
		dv := u
		maxD = -1
		f(dv, -1, 0)
		dw := u
		Println("?", dv+1, dw+1)
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		// 删除 LCA 到直径上的边
		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Println("!", lca+1)
}

package main

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

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

	var n, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}
		maxD = -1
		vis = make([]bool, n)
		f(lca, 0)
		dv := u
		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u
		Fprintln(out, "?", dv+1, dw+1)
		out.Flush()
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Fprintln(out, "!", lca+1)
}

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1305D() {
	in := bufio.NewReader(os.Stdin)
	var n, v, w, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		// 求直径
		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}
		maxD = -1
		vis = make([]bool, n)
		f(lca, 0)
		dv := u
		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u
		Println("?", dv+1, dw+1)
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		// 删除 LCA 到直径上的边
		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Println("!", lca+1)
}

func main() { CF1305D() }

package main

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

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

	var n int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}
	//
	//var fa []int
	//initFa := func(n int) { // n+1
	//	fa = make([]int, 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) }
	//same := func(x, y int) bool { return find(x) == find(y) }
	//countRoots := func() (cnt int) {
	//	for i := range fa {
	//		if find(i) == i {
	//			cnt++
	//		}
	//	}
	//	return
	//}

	//a := make([]int, n)
	//for i := range a {
	//	a[i] = i
	//}
	//rand.Shuffle(n, func(i, j int) {a[i], a[j] = a[j], a[i]})
	var lca int
	for len(edges) > 0 {
		//initFa(n)
		g := make([][]int, n)

		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
			//merge(v,w)
		}
		//if countRoots() != 1 {
		//	break
		//}
		if len(g[lca]) == 0 {
			break
		}

		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}

		maxD = -1
		vis = make([]bool, n)
		f(lca, 0)
		dv := u

		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u

		Fprintf(out, "? %d %d\n", dv+1, dw+1)
		out.Flush()
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		var tar int
		var f2 func(v, fa int) bool
		f2 = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if f2(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		f2(lca, -1)
		tar = dw
		f2(lca, -1)
		//println()
	}
	Fprintf(out, "! %d\n", lca+1)
}

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

package main

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

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

	var n, lca, tar int
	Fscan(in, &n)
	edges := map[[2]int]bool{}
	for i := 0; i < n-1; i++ {
		var v, w int
		Fscan(in, &v, &w)
		v--
		w--
		edges[[2]int{v, w}] = true
	}

	for {
		g := make([][]int, n)
		for e := range edges {
			v, w := e[0], e[1]
			g[v] = append(g[v], w)
			g[w] = append(g[w], v)
		}
		if len(g[lca]) == 0 {
			break
		}

		var u, maxD int
		var vis []bool
		var f func(int, int)
		f = func(v, d int) {
			vis[v] = true
			if d > maxD {
				maxD = d
				u = v
			}
			for _, w := range g[v] {
				if !vis[w] {
					f(w, d+1)
				}
			}
		}
		maxD = -1
		vis = make([]bool, n)
		f(0, 0)
		dv := u
		maxD = -1
		vis = make([]bool, n)
		f(dv, 0)
		dw := u
		Fprintln(out, "?", dv+1, dw+1)
		out.Flush()
		Fscan(in, &lca)
		lca--
		if lca == dv || lca == dw {
			break
		}

		var del func(v, fa int) bool
		del = func(v, fa int) bool {
			if v == tar {
				delete(edges, [2]int{v, fa})
				delete(edges, [2]int{fa, v})
				return true
			}
			for _, w := range g[v] {
				if w != fa {
					if del(w, v) {
						delete(edges, [2]int{v, w})
						delete(edges, [2]int{w, v})
						return true
					}
				}
			}
			return false
		}
		tar = dv
		del(lca, -1)
		tar = dw
		del(lca, -1)
	}
	Fprintln(out, "!", lca+1)
}

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

package main

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

var reader io.Reader
var scanner *bufio.Scanner

func Init(io io.Reader, max_token_size int) {
	reader = bufio.NewReader(io)
	scanner = bufio.NewScanner(reader)
	// max token size
	scanner.Split(bufio.ScanWords)
	scanner.Buffer([]byte{}, max_token_size)
}

func stderr(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
}

func die(v ...interface{}) {
	fmt.Fprintln(os.Stderr, v...)
	os.Exit(1)
}

func NextInt() int {
	bytes := NextBytes()
	i, err := Atoi(bytes)
	if err != nil {
		die(err)
	}
	return i
}

func NextBytes() []byte {
	if !scanner.Scan() {
		die(scanner.Err())
	}
	bytes := scanner.Bytes()
	copied := make([]byte, len(bytes))
	copy(copied, bytes)
	return copied
}

func Atoi(b []byte) (int, error) {
	neg := false
	if b[0] == '+' {
		b = b[1:]
	} else if b[0] == '-' {
		neg = true
		b = b[1:]
	}
	n := 0
	for _, v := range b {
		if v < '0' || v > '9' {
			return 0, strconv.ErrSyntax
		}
		n = n*10 + int(v-'0')
	}
	if neg {
		return -n, nil
	}
	return n, nil
}

func ExtendByteArray(a []byte, size int) []byte {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]byte, size-len(a))...)
	}
}

func ExtendIntArray(a []int, size int) []int {
	if size <= len(a) {
		return a
	} else {
		return append(a, make([]int, size-len(a))...)
	}
}

type Graph struct {
	n     int
	m     int
	edges int
	fst   []int
	nxt   []int
	end   []int
}

func (g *Graph) Add(a, b int) {
	g.nxt[g.edges] = g.fst[a]
	g.fst[a] = g.edges
	g.end[g.edges] = b
	g.edges++
}

func (g *Graph) Adjacent(a int, fn func(b int)) {
	for u := g.fst[a]; u != -1; u = g.nxt[u] {
		fn(g.end[u])
	}
}

func NewGraph(n, m int) *Graph {
	g := &Graph{n, m, 0, make([]int, n), make([]int, m), make([]int, m)}
	for i := 0; i < n; i++ {
		g.fst[i] = -1
	}
	return g
}

var leaf []int
var parent []int

func (g *Graph) Dfs(v int, par int) {
	leaf[v] = v
	parent[v] = par
	g.Adjacent(v, func(to int) {
		if to == par {
			return
		}
		g.Dfs(to, v)
		if leaf[v] == v || rand.Intn(2) == 0 {
			leaf[v] = leaf[to]
		}
	})
}

func lca(out *bufio.Writer, u, v int) int {
	fmt.Fprintln(out, u+1, v+1)
	out.Flush()
	return NextInt() - 1
}

func found(out *bufio.Writer, root int) {
	fmt.Fprintln(out, "!", root+1)
	out.Flush()
}

func main() {
	inf := os.Stdin
	out := bufio.NewWriter(os.Stdout)
	// inf, err := os.Open("a.in")
	// if err != nil {
	// 	die(err)
	// }
	// out, err := os.Create("a.out")
	// if err != nil {
	// 	die(err)
	// }
	// defer out.Close()
	Init(inf, 1000010)

	n := NextInt()
	leaf = make([]int, n)
	parent = make([]int, n)

	g := NewGraph(n, 2*n)
	for i := 0; i < n-1; i++ {
		u, v := NextInt()-1, NextInt()-1
		g.Add(u, v)
		g.Add(v, u)
	}
	rand.Seed(time.Now().UnixNano())
	root := rand.Intn(n)

	// stderr("root", root)
	g.Dfs(root, -1)

	l1, l2 := -1, -1
	possible := make([]int, 0)
	for {
		possible = possible[:0]
		leaf[root] = root
		g.Adjacent(root, func(to int) {
			if to == parent[root] {
				return
			}
			if leaf[to] == l1 || leaf[to] == l2 {
				return
			}
			possible = append(possible, to)
		})
		found := false
		if len(possible)%2 == 1 {
			possible = append(possible, root)
		}
		// stderr("possible", possible)
		for i := 0; i < len(possible); i += 2 {
			// stderr(possible[i], leaf[possible[i]])
			// stderr(possible[i+1], leaf[possible[i+1]])
			result := lca(out, leaf[possible[i]], leaf[possible[i+1]])
			if result != root {
				l1 = leaf[possible[i]]
				l2 = leaf[possible[i+1]]
				root = result
				found = true
				break
			}
		}
		if found {
			continue
		}
		break
	}

	found(out, root)
}