← Home
```go
package main

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

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

type Node struct {
	v [2][2]int64
}

func leaf(val int64) Node {
	return Node{
		v: [2][2]int64{
			{0, 0},
			{0, max(0, val)},
		},
	}
}

func merge(L, R Node) Node {
	var res Node
	res.v[0][0] = max(L.v[0][0]+R.v[1][0], L.v[0][1]+R.v[0][0])
	res.v[0][1] = max(L.v[0][0]+R.v[1][1], L.v[0][1]+R.v[0][1])
	res.v[1][0] = max(L.v[1][0]+R.v[1][0], L.v[1][1]+R.v[0][0])
	res.v[1][1] = max(L.v[1][0]+R.v[1][1], L.v[1][1]+R.v[0][1])
	return res
}

type SegTree struct {
	nodes []Node
}

func (st *SegTree) build(arr []int64, idx, l, r int) {
	if l == r {
		st.nodes[idx] = leaf(arr[l])
		return
	}
	mid := l + (r-l)/2
	st.build(arr, 2*idx, l, mid)
	st.build(arr, 2*idx+1, mid+1, r)
	st.nodes[idx] = merge(st.nodes[2*idx], st.nodes[2*idx+1])
}

func (st *SegTree) update(idx, l, r, pos int, val int64) {
	if l == r {
		st.nodes[idx] = leaf(val)
		return
	}
	mid := l + (r-l)/2
	if pos <= mid {
		st.update(2*idx, l, mid, pos, val)
	} else {
		st.update(2*idx+1, mid+1, r, pos, val)
	}
	st.nodes[idx] = merge(st.nodes[2*idx], st.nodes[2*idx+1])
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, 1024*1024)
	scanner.Buffer(buf, 10*1024*1024)
	scanner.Split(bufio.ScanWords)

	scanInt := func() int {
		scanner.Scan()
		v, _ := strconv.Atoi(scanner.Text())
		return v
	}

	scanInt64 := func() int64 {
		scanner.Scan()
		v, _ := strconv.ParseInt(scanner.Text(), 10, 64)
		return v
	}

	n := scanInt()
	a := make([]int64, n-1)
	var totalSum int64 = 0
	for i := 0; i < n-1; i++ {
		a[i] = scanInt64()
		totalSum += a[i]
	}

	q := scanInt()

	N := n - 3
	var st SegTree
	if N > 0 {
		st.nodes = make([]Node, 4*N+1)
		candidates := a[1 : n-2]
		st.build(candidates, 1, 0, N-1)
	}

	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	for i := 0; i < q; i++ {
		k := scanInt()
		x := scanInt64()
		k--

		totalSum += x - a[k]
		a[k] = x

		if k >= 1 && k <= n-3 {
			st.update(1, 0, N-1, k-1, x)
		}

		var maxIS int64 = 0
		if N > 0 {
			maxIS = st.nodes[1].v[1][1]
		}

		ans := 2 * (totalSum - maxIS)
		fmt.Fprintln(out, ans)
	}
}
```