package main

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

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

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

    var n, t, k int
    Fscan(r, &n, &t, &k)
    d, a := make([]int, n), make([]int, n)
    for i := 0; i < n; i++ {
        Fscan(r, &d[i])
    }
    for i := 0; i < n; i++ {
        Fscan(r, &a[i])
    }
    dp := make([][]int64, n+1)
    for i := range dp {
        dp[i] = make([]int64, n+1)
        for j := range dp[i] {
            dp[i][j] = math.MaxInt32
        }
    }
    d = append(d, t)
    a = append(a, 0)

    dp[0][0] = 0 // 一开始在第一个点，没有移掉一个，最小时间是0
    // 注意第一个点不能remove, 不然第一个点到第二个点之间的速度就不知道是多少了
    for i := 1; i <= n; i++ {
        for j := 0; j <= k; j++ {
            for p := 0; p <= i-1; p++ {
                oj := j - (i - p - 1)
                if oj >= 0 {
                    dp[i][j] = min(dp[i][j], dp[p][oj]+int64(a[p]*(d[i]-d[p])))
                }
            }
        }
    }
    res := int64(math.MaxInt64)
    for j := 0; j <= n; j++ {
        res = min(res, dp[n][j])
    }
    Fprintln(w, res)
}

package main

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

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

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

    var n, t, k int
    Fscan(r, &n, &t, &k)
    d, a := make([]int, n), make([]int, n)
    for i := 0; i < n; i++ {
        Fscan(r, &d[i])
    }
    for i := 0; i < n; i++ {
        Fscan(r, &a[i])
    }
    dp := make([][]int64, n+1)
    for i := range dp {
        dp[i] = make([]int64, n+1)
        for j := range dp[i] {
            dp[i][j] = math.MaxInt32
        }
    }
    d = append(d, t)
    a = append(a, 0)

    dp[0][0] = 0

    for i := 1; i <= n; i++ {
        for j := 0; j <= k; j++ {
            for p := 0; p <= i-1; p++ {
                oj := j - (i - p - 1)
                if oj >= 0 {
                    dp[i][j] = min(dp[i][j], dp[p][oj]+int64(a[p]*(d[i]-d[p])))
                }
            }
        }
    }
    res := int64(math.MaxInt64)
    for j := 0; j <= n; j++ {
        res = min(res, dp[n][j])
    }
    Fprintln(w, res)
}

// LUOGU_RID: 92444052
package main

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

	var n, l, K int
	Fscan(in, &n, &l, &K)
	pos := make([]int, n)
	for i := range pos {
		Fscan(in, &pos[i])
	}
	pos = append(pos, l)
	lim := make([]int, n)
	for i := range lim {
		Fscan(in, &lim[i])
	}

	if K == 0 {
		s := 0
		for i := 1; i < n+1; i++ {
			s += (pos[i] - pos[i-1]) * lim[i-1]
		}
		Fprintln(out, s)
		return
	}

	pre := make([][]int, K+2)
	for i := range pre {
		pre[i] = make([]int, n+2)
		for _i := range pre[i] {
			pre[i][_i] = 1e9
		}
	}

	pre[0][0] = (pos[1] - pos[0]) * lim[0]

	dp := make([][]int, K+2)
	for i := range dp {
		dp[i] = make([]int, n+2)
		for _i := range dp[i] {
			dp[i][_i] = 1e9
		}
	}

	ans := int(2e9)
	for i := 1; i < n; i++ {
		for del := 0; del <= min(i, K); del++ {

			if del < i {
				dp[del][i] = int(2e9)
				for pi := i - (del + 1); pi < i; pi++ {
					dp[del][i] = min(dp[del][i], pre[del][pi])
				}
				dp[del][i] += (pos[i+1] - pos[i]) * lim[i] // save
			}

			if del > 0 {
				for pi := 0; pi < i; pi++ {
					dp[del][pi] = pre[del-1][pi] + (pos[i+1]-pos[i])*lim[pi]
				}
			}
		}
		if i == n-1 {
			for del, r := range dp {
				for pi := max(i-del,0); pi <= i; pi++ {
					ans = min(ans, r[pi])
				}
			}
		}
		dp, pre = pre, dp
	}
	Fprintln(out, ans)
}

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

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

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

/**
https://codeforces.com/contest/1625/problem/C

Trying the unconventional (for me) dp provided in the discussion
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j := 1; j <= n; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1]*(amark[j]-amark[j-1]))
	}

	// Fill up the rest with infinity
	for i := 1; i <= k; i++ {
		dp_arr[i] = make([]int64, n+1)
		dp_arr[i][0] = 0
		for j := 1; j <= n; j++ {
			dp_arr[i][j] = int64(1<<63 - 1)
		}
	}

	for j := 0; j < n; j++ {
		for i := 0; i <= k; i++ {
			for nr := 0; nr <= k; nr++ {
				// nr == num removed
				jpos := j + nr + 1
				ipos := i + nr
				if jpos > n || ipos > k {
					continue
				}
				v := dp_arr[ipos][jpos]
				vnew := dp_arr[i][j] + int64(asign[j]*(amark[jpos]-amark[j]))
				dp_arr[ipos][jpos] = int64(math.Min(float64(v), float64(vnew)))
			}
		}
	}

	// fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i := 0; i <= k; i++ {
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

package main

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

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

	N, L, K := readThreeNums(reader)

	X := readNNums(reader, N)
	A := readNNums(reader, N)

	res := solve(L, N, K, X, A)

	fmt.Println(res)
}

func readString(reader *bufio.Reader) string {
	s, _ := reader.ReadString('\n')
	for i := 0; i < len(s); i++ {
		if s[i] == '\n' {
			return s[:i]
		}
	}
	return s
}

func normalize(s string) string {

	for i := len(s); i > 0; i-- {
		if s[i-1] >= 'a' && s[i-1] <= 'z' {
			return s[:i]
		}
	}
	return ""
}

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
}

const INF = 1 << 60

func solve(L int, N int, K int, X []int, signs []int) int64 {
	// dp[i][x] = 到达位置i，且删除x个路牌时所花的最小时间
	dp := make([][]int64, N)
	for i := 0; i < N; i++ {
		dp[i] = make([]int64, K+1)
		for j := 0; j <= K; j++ {
			dp[i][j] = INF
		}
	}

	dp[0][0] = 0

	for i := 1; i < N; i++ {
		for j := i - 1; j >= 0; j-- {
			for x := j; x >= 0; x-- {
				// dp[j][x] = 到达位置j，且删除x个路牌的情况
				if x+(i-j-1) <= K {
					dp[i][x+(i-j-1)] = min(dp[i][x+(i-j-1)], dp[j][x]+int64(X[i]-X[j])*int64(signs[j]))
				}
			}
		}
	}
	var best int64 = INF

	for i := 0; i < N; i++ {
		for x := i; x >= 0; x-- {
			// dp[i][x]
			y := x + (N - (i + 1))
			if y <= K {
				// remove at most K
				tmp := dp[i][x] + int64(L-X[i])*int64(signs[i])
				best = min(best, tmp)
			}
		}
	}
	return best
}

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

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

package main

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

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

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

	var n, t, k int
	Fscan(r, &n, &t, &k)
	d, a := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(r, &d[i])
	}
	for i := 0; i < n; i++ {
		Fscan(r, &a[i])
	}
	dp := make([][]int64, n+1)
	for i := range dp {
		dp[i] = make([]int64, n+1)
		for j := range dp[i] {
			dp[i][j] = math.MaxInt32
		}
	}
	d = append(d, t)
	a = append(a, 0)
	// dp[i][j] 表示 当前在考虑第i个限速牌，且已经拆掉了j个限速牌的最小值
	// 答案是 max(dp[n][v]) (v=[1~k]), 也就是当前在最后一个限速牌(在终点多加一个
	// dp[i][j] is the minimum time it will take to reach position d[i], when we exactly remove j boards
	// transitions: dp[i][j] = min(dp[i][j], dp[p][j - (i - p - 1)]
	// 遍历在这之前的P点(当前状态的上一个状态，前一个点)，从那个点转移到这个点
	// 假设这个点是p，且p~i之间的所有点，都remove, 那这个点之前remove了多少个? 是j - (i - p - 1)个 (j是当前点移除的点的数量)

	dp[0][0] = 0 // 一开始在第一个点，没有移掉一个，最小时间是0
	// 注意第一个点不能remove, 不然第一个点到第二个点之间的速度就不知道是多少了
	for i := 1; i <= n; i++ {
		for j := 0; j <= k; j++ {
			for p := 0; p <= i-1; p++ {
				oj := j - (i - p - 1)
				if oj >= 0 {
					dp[i][j] = min(dp[i][j], dp[p][oj]+int64(a[p]*(d[i]-d[p])))
				}
			}
		}
	}
	res := int64(math.MaxInt64)
	for j := 0; j <= n; j++ {
		res = min(res, dp[n][j])
	}
	Fprintln(w, res)
}

/**
https://codeforces.com/contest/1625/problem/C

Trying the unconventional (for me) dp provided in the discussion
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j := 1; j <= n; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1]*(amark[j]-amark[j-1]))
	}

	// Fill up the rest with infinity
	for i := 1; i <= k; i++ {
		dp_arr[i] = make([]int64, n+1)
		dp_arr[i][0] = 0
		for j := 1; j <= n; j++ {
			dp_arr[i][j] = int64(1<<63 - 1)
		}
	}

	for j := 0; j < n; j++ {
		for i := 0; i <= k; i++ {
			for nr := 0; nr <= k; nr++ {
				// nr == num removed
				jpos := j + nr + 1
				ipos := i + nr
				if jpos > n || ipos > k {
					continue
				}
				v := dp_arr[ipos][jpos]
				vnew := dp_arr[i][j] + int64(asign[j]*(amark[jpos]-amark[j]))
				dp_arr[ipos][jpos] = int64(math.Min(float64(v), float64(vnew)))
			}
		}
	}

	fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i := 0; i <= k; i++ {
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C

Trying the unconventional (for me) dp provided in the discussion
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j := 1; j <= n; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1]*(amark[j]-amark[j-1]))
	}

	// Fill up the rest with infinity
	for i := 1; i <= k; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j <= n; j++ {
			dp_arr[i][j] = int64(1<<63 - 1)
		}
	}

	for j := 0; j < n; j++ {
		for i := 0; i <= k; i++ {
			for nr := 1; nr <= k; nr++ {
				// nr == num removed
				jpos := j + nr + 1
				ipos := i + nr
				if jpos > n || ipos > k {
					continue
				}
				v := dp_arr[ipos][jpos]
				vnew := dp_arr[i][j] + int64(asign[j]*(amark[jpos]-amark[j]))
				dp_arr[ipos][jpos] = int64(math.Min(float64(v), float64(vnew)))
			}
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i := 0; i <= k; i++ {
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j := 1; j <= n; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1]*(amark[j]-amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i := 1; i <= k; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j <= i; j++ {
			dp_arr[i][j] = dp_arr[i-1][j]
		}
		for j := i + 1; j <= n; j++ {
			// we really need to loop for all consecutive values of misses say if k is 3
			// then pick the best of dp_arr[0][j], dp_arr[1][j-1]
			min_val := dp_arr[0][j]
			for s := 1; s <= i; s++ {
				v := dp_arr[i-s][j-s-1] + int64(asign[j-s-1]*(amark[j]-amark[j-s-1]))

				if min_val > v {
					min_val = v
				}
			}

			dp_arr[i][j] = min_val
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i := 0; i <= k; i++ {
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j := 1; j <= n; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1]*(amark[j]-amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i := 1; i <= k; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j <= i; j++ {
			dp_arr[i][j] = dp_arr[0][j]
		}
		for j := i + 1; j <= n; j++ {
			// we really need to loop for all consecutive values of misses say if k is 3
			// then pick the best of dp_arr[0][j], dp_arr[1][j-1]
			min_val := dp_arr[0][j]
			for s := 1; s <= i; s++ {
				v := dp_arr[i-s][j-s-1] + int64(asign[j-s-1]*(amark[j]-amark[j-s-1]))

				if min_val > v {
					min_val = v
				}
			}

			dp_arr[i][j] = min_val
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i := 0; i <= k; i++ {
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j := 1; j < n+1; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1]*(amark[j]-amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i := 1; i <= k; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j <= i; j++ {
			dp_arr[i][j] = dp_arr[0][j]
		}
		for j := i + 1; j <= n; j++ {
			// we really need to loop for all consecutive values of misses say if k is 3
			// then pick the best of dp_arr[0][j], dp_arr[1][j-1]
			min_val := dp_arr[0][j]
			for s := 1; s <= i; s++ {
				v := dp_arr[s][j-s-1] + int64(asign[j-s-1]*(amark[j]-amark[j-s-1]))

				if min_val > v {
					min_val = v
				}
			}

			dp_arr[i][j] = min_val
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i := 0; i < k+1; i++ {
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j := 1; j < n+1; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1]*(amark[j]-amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i := 1; i <= k; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j <= i; j++ {
			dp_arr[i][j] = dp_arr[0][j]
		}
		for j := i + 1; j <= n; j++ {
			// we really need to loop for all consecutive values of misses say if k is 3
			// then pick the best of dp_arr[0][j], dp_arr[1][j-1]
			min_val := dp_arr[0][j]
			for s := 1; s <= i; s++ {
				v := dp_arr[s][j-s-1] + int64(asign[j-s-1]*(amark[j]-amark[j-s-1]))

				if min_val > v {
					min_val = v
				}
			}

			dp_arr[i][j] = min_val
		}
	}

	fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i := 0; i < k+1; i++ {
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j:= 1; j<n+1; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1] * (amark[j] - amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i:=1; i<k+1 ; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j<=i; j++ {
			dp_arr[i][j] = dp_arr[i-1][j]
		}
		for j:= i+1; j<n+1; j++ {
			// we really need to loop for all consecutive values of misses say if k is 3
			// then pick the best of dp_arr[0][j], dp_arr[1][j-1]
			min_val := int64(1<<63 - 1)
			for s:= 0; s<=i; s++ {
				var v int64
				if s == 0 {
					v = dp_arr[s][j]

				} else {
					v = dp_arr[s][j-s-1] + int64(asign[j-s-1] * (amark[j] - amark[j-s-1]))
				}
				if min_val > v {
					min_val = v
				}
			}

			dp_arr[i][j] = min_val
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i:=0; i<k+1; i++{
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C

dp approach didn't work (too complicated using a simpler one)
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	min_val := 0

	for i := 0; i < k; i++ {
		removed := false
		amark, asign, removed = compute_area_diff_and_resize(amark, asign)

		if !removed {
			break
		}
	}

	new_length := len(asign)

	for i := 0; i < new_length; i++ {
		min_val += asign[i] * (amark[i+1] - amark[i])
	}

	fmt.Println(min_val)
}

/*
Deletes the best sign post and recreates the array
Deletion is based on area difference
*/
func compute_area_diff_and_resize(amark, asign []int) ([]int, []int, bool) {
	n := len(asign)
	delta_max := 0
	delta_pos := -1
	for i := 0; i < n-1; i++ {
		delta := (asign[i+1] - asign[i]) * (amark[i+2] - amark[i+1])
		//fmt.Printf("i: %v, mark_diff: %v\n", i, (amark[i+2] - amark[i+1]))
		//fmt.Printf("i: %v, sign_diff: %v, left: %v, right: %v\n", i, (asign[1+1] - asign[i]), asign[1+1], asign[i])
		//fmt.Printf("i: %v, delta: %v\n", i, delta)
		if delta > delta_max {
			delta_max = delta
			delta_pos = i + 1
		}
	}

	if delta_pos <= 0 {
		return amark, asign, false
	}

	//fmt.Printf("delta_pos:%v\n", delta_pos)
	//fmt.Printf("delta_max:%v\n", delta_max)

	// SO remove that delta_pos and compact the signs and marks
	ret_sign := make([]int, n-1)
	ret_mark := make([]int, n)
	ret_sign[0] = asign[0]
	ret_mark[0] = ret_mark[0]
	for i := 1; i < n; i++ {
		if i < delta_pos {
			ret_sign[i] = asign[i]
			ret_mark[i] = amark[i]
		} else if i == delta_pos {
			continue
		} else {
			ret_sign[i-1] = asign[i]
			ret_mark[i-1] = amark[i]
		}
	}
	ret_mark[n-1] = amark[n]
	//fmt.Println("inside -- ret_mark:", ret_mark)
	//fmt.Println("inside -- ret_sign:", ret_sign)
	return ret_mark, ret_sign, true

}

/**
https://codeforces.com/contest/1625/problem/C

dp approach didn't work (too complicated using a simpler one)
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	min_val := 0

	for i:= 0; i< k; i++ {
		removed := false
		amark, asign, removed = compute_area_diff_and_resize(amark, asign)

		if !removed {
			break
		}
	}

	for i:=0; i<n-k; i++ {
		min_val += asign[i] * (amark[i+1] - amark[i])
	}

	fmt.Println(min_val)
}

/*
Deletes the best sign post and recreates the array
Deletion is based on area difference
*/
func compute_area_diff_and_resize(amark, asign []int) ([]int, []int, bool) {
	n := len(asign)
	delta_max := 0
	delta_pos := -1
	for i:=0; i<n-1; i++ {
		delta := (asign[i+1] - asign[i]) * (amark[i+2] - amark[i+1])
		//fmt.Printf("i: %v, mark_diff: %v\n", i, (amark[i+2] - amark[i+1]))
		//fmt.Printf("i: %v, sign_diff: %v, left: %v, right: %v\n", i, (asign[1+1] - asign[i]), asign[1+1], asign[i])
		//fmt.Printf("i: %v, delta: %v\n", i, delta)
		if delta > delta_max {
			delta_max = delta
			delta_pos = i+1
		}
	}

	if delta_pos <=0 {
		return amark, asign, false
	}

	//fmt.Printf("delta_pos:%v\n", delta_pos)
	//fmt.Printf("delta_max:%v\n", delta_max)

	// SO remove that delta_pos and compact the signs and marks
	ret_sign := make([]int, n-1)
	ret_mark := make([]int, n)
	ret_sign[0] = asign[0]
	ret_mark[0] = ret_mark[0]
	for i:=1; i<n; i++ {
		if i < delta_pos {
			ret_sign[i] = asign[i]
			ret_mark[i] = amark[i]
		} else if i == delta_pos{
			continue
		} else {
			ret_sign[i-1] = asign[i]
			ret_mark[i-1] = amark[i]
		}
	}
	ret_mark[n-1] = amark[n]
	//fmt.Println("inside -- ret_mark:", ret_mark)
	//fmt.Println("inside -- ret_sign:", ret_sign)
	return ret_mark, ret_sign, true

}

/**
https://codeforces.com/contest/1625/problem/C

dp approach didn't work (too complicated using a simpler one)
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	min_val := 0
	if k == n -1 {
		min_val = asign[0] * l
		fmt.Println(min_val)
		os.Exit(0)
	}

	for i:= 0; i< k; i++ {
		amark, asign = compute_area_diff_and_resize(amark, asign)
	}

	for i:=0; i<n-k; i++ {
		min_val += asign[i] * (amark[i+1] - amark[i])
	}

	fmt.Println(min_val)
}

/*
Deletes the best sign post and recreates the array
Deletion is based on area difference
*/
func compute_area_diff_and_resize(amark, asign []int) ([]int, []int) {
	n := len(asign)
	delta_max := 0
	delta_pos := -1
	for i:=0; i<n-1; i++ {
		delta := (asign[i+1] - asign[i]) * (amark[i+2] - amark[i+1])
		//fmt.Printf("i: %v, mark_diff: %v\n", i, (amark[i+2] - amark[i+1]))
		//fmt.Printf("i: %v, sign_diff: %v, left: %v, right: %v\n", i, (asign[1+1] - asign[i]), asign[1+1], asign[i])
		//fmt.Printf("i: %v, delta: %v\n", i, delta)
		if delta > delta_max {
			delta_max = delta
			delta_pos = i+1
		}
	}

	//fmt.Printf("delta_pos:%v\n", delta_pos)
	//fmt.Printf("delta_max:%v\n", delta_max)

	// SO remove that delta_pos and compact the signs and marks
	ret_sign := make([]int, n-1)
	ret_mark := make([]int, n)
	ret_sign[0] = asign[0]
	ret_mark[0] = ret_mark[0]
	for i:=1; i<n; i++ {
		if i < delta_pos {
			ret_sign[i] = asign[i]
			ret_mark[i] = amark[i]
		} else if i == delta_pos{
			continue
		} else {
			ret_sign[i-1] = asign[i]
			ret_mark[i-1] = amark[i]
		}
	}
	ret_mark[n-1] = amark[n]
	//fmt.Println("inside -- ret_mark:", ret_mark)
	//fmt.Println("inside -- ret_sign:", ret_sign)
	return ret_mark, ret_sign

}

/**
https://codeforces.com/contest/1625/problem/C

dp approach didn't work (too complicated using a simpler one)
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	for i:= 0; i< k; i++ {
		amark, asign = compute_area_diff_and_resize(amark, asign)
	}

	min_val := 0
	for i:=0; i<n-k; i++ {
		min_val += asign[i] * (amark[i+1] - amark[i])
	}

	fmt.Println(min_val)
}

/*
Deletes the best sign post and recreates the array
Deletion is based on area difference
*/
func compute_area_diff_and_resize(amark, asign []int) ([]int, []int) {
	n := len(asign)
	delta_max := 0
	delta_pos := -1
	for i:=0; i<n-1; i++ {
		delta := (asign[i+1] - asign[i]) * (amark[i+2] - amark[i+1])
		//fmt.Printf("i: %v, mark_diff: %v\n", i, (amark[i+2] - amark[i+1]))
		//fmt.Printf("i: %v, sign_diff: %v, left: %v, right: %v\n", i, (asign[1+1] - asign[i]), asign[1+1], asign[i])
		//fmt.Printf("i: %v, delta: %v\n", i, delta)
		if delta > delta_max {
			delta_max = delta
			delta_pos = i+1
		}
	}

	//fmt.Printf("delta_pos:%v\n", delta_pos)
	//fmt.Printf("delta_max:%v\n", delta_max)

	// SO remove that delta_pos and compact the signs and marks
	ret_sign := make([]int, n-1)
	ret_mark := make([]int, n)
	ret_sign[0] = asign[0]
	ret_mark[0] = ret_mark[0]
	for i:=1; i<n; i++ {
		if i < delta_pos {
			ret_sign[i] = asign[i]
			ret_mark[i] = amark[i]
		} else if i == delta_pos{
			continue
		} else {
			ret_sign[i-1] = asign[i]
			ret_mark[i-1] = amark[i]
		}
	}
	ret_mark[n-1] = amark[n]
	//fmt.Println("inside -- ret_mark:", ret_mark)
	//fmt.Println("inside -- ret_sign:", ret_sign)
	return ret_mark, ret_sign

}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j:= 1; j<n+1; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1] * (amark[j] - amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i:=1; i<k+1 ; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j<=i; j++ {
			dp_arr[i][j] = dp_arr[i-1][j]
		}
		for j:= i+1; j<n+1; j++ {
			miss_board := dp_arr[i-1][j-2] + int64(asign[j-2] * (amark[j] - amark[j-2]))
			dont_miss_board := dp_arr[i-1][j-1] + int64(asign[j-1] * (amark[j] - amark[j-1]))
			dp_arr[i][j] = int64(math.Min(float64(miss_board), float64(dont_miss_board)))
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i:=0; i<k+1; i++{
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j:= 1; j<n+1; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1] * (amark[j] - amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i:=1; i<k+1 ; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j<=i; j++ {
			dp_arr[i][j] = dp_arr[i-1][j]
		}
		for j:= i+1; j<n+1; j++ {
			miss_board := dp_arr[i-1][j-2] + int64(asign[j-2] * (amark[j] - amark[j-2]))
			//dont_miss_board := dp_arr[i][j-1] + int64(asign[j-1] * (amark[j] - amark[j-1]))
			//dp_arr[i][j] = int64(math.Min(float64(miss_board), float64(dont_miss_board)))
			dp_arr[i][j] = miss_board
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i:=0; i<k+1; i++{
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int64, k+1)
	dp_arr[0] = make([]int64, n+1)
	dp_arr[0][0] = 0
	for j:= 1; j<n+1; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + int64(asign[j-1] * (amark[j] - amark[j-1]))
	}

	// now construct the remaing k vals of dp arr
	for i:=1; i<k+1 ; i++ {
		dp_arr[i] = make([]int64, n+1)
		for j := 0; j<=i; j++ {
			dp_arr[i][j] = dp_arr[i-1][j]
		}
		for j:= i+1; j<n+1; j++ {
			miss_board := dp_arr[i-1][j-2] + int64(asign[j-2] * (amark[j] - amark[j-2]))
			dont_miss_board := dp_arr[i][j-1] + int64(asign[j-1] * (amark[j] - amark[j-1]))
			dp_arr[i][j] = int64(math.Min(float64(miss_board), float64(dont_miss_board)))
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	// we don't necessarily have to miss all boards
	min_val := int64(1<<63 - 1)
	for i:=0; i<k+1; i++{
		if min_val > dp_arr[i][n] {
			min_val = dp_arr[i][n]
		}
	}

	fmt.Println(min_val)
}

/**
https://codeforces.com/contest/1625/problem/C
**/

package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	line1, _ := reader.ReadString('\n')
	line1_arr := strings.Fields(line1)
	//fmt.Println(line1_arr)
	n, _ := strconv.Atoi(line1_arr[0])
	l, _ := strconv.Atoi(line1_arr[1])
	k, _ := strconv.Atoi(line1_arr[2])
	//fmt.Printf("n: %v, l: %v, k: %v\n", n, l, k)

	line2, _ := reader.ReadString('\n')
	line2_arr := strings.Fields(line2)

	amark := make([]int, n+1)
	for i := 0; i < n; i++ {
		amark[i], _ = strconv.Atoi(line2_arr[i])
	}
	amark[n] = l
	//fmt.Println("amark:", amark)

	line3, _ := reader.ReadString('\n')
	asign := make([]int, n)
	line3_arr := strings.Fields(line3)
	for i := 0; i < n; i++ {
		asign[i], _ = strconv.Atoi(line3_arr[i])
	}
	//fmt.Println("asign:", asign)

	// intitalize k=0 of dp i.e. no removal of signs
	dp_arr := make([][]int, k+1)
	dp_arr[0] = make([]int, n+1)
	dp_arr[0][0] = 0
	for j:= 1; j<n+1; j++ {
		dp_arr[0][j] = dp_arr[0][j-1] + asign[j-1] * (amark[j] - amark[j-1])
	}

	// now construct the remaing k vals of dp arr
	for i:=1; i<k+1 ; i++ {
		dp_arr[i] = make([]int, n+1)
		for j := 0; j<=i; j++ {
			dp_arr[i][j] = dp_arr[i-1][j]
		}
		for j:= i+1; j<n+1; j++ {
			miss_board := dp_arr[i-1][j-2] + asign[j-2] * (amark[j] - amark[j-2])
			dont_miss_board := dp_arr[i][j-1] + asign[j-1] * (amark[j] - amark[j-1])
			dp_arr[i][j] = int(math.Min(float64(miss_board), float64(dont_miss_board)))
		}
	}

	//fmt.Println("dp_arr:", dp_arr)
	fmt.Println(dp_arr[k][n])
}

package main

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

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

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

	var n, t, k int
	Fscan(r, &n, &t, &k)
	d, a := make([]int, n), make([]int, n)
	for i := 0; i < n; i++ {
		Fscan(r, &d[i])
	}
	for i := 0; i < n; i++ {
		Fscan(r, &a[i])
	}
	dp := make([][]int, n+1)
	for i := range dp {
		dp[i] = make([]int, n+1)
		for j := range dp[i] {
			dp[i][j] = math.MaxInt32
		}
	}
	d = append(d, t)
	a = append(a, 0)

	dp[0][0] = 0 // 一开始在第一个点，没有移掉一个，最小时间是0
	// 注意第一个点不能remove, 不然第一个点到第二个点之间的速度就不知道是多少了
	for i := 1; i <= n; i++ {
		for j := 0; j <= k; j++ {
			for p := 0; p <= i-1; p++ {
				oj := j - (i - p - 1)
				if oj >= 0 {
					dp[i][j] = min(dp[i][j], dp[p][oj]+(a[p]*(d[i]-d[p])))
				}
			}
		}
	}
	res := math.MaxInt32
	for j := 0; j <= n; j++ {
		res = min(res, dp[n][j])
	}
	Fprintln(w, res)
}

package main

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

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

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

    var n, t, k int
    Fscan(r, &n, &t, &k)
    d, a := make([]int, n), make([]int, n)
    for i := 0; i < n; i++ {
        Fscan(r, &d[i])
    }
    for i := 0; i < n; i++ {
        Fscan(r, &a[i])
    }
    dp := make([][]int, n+1)
    for i := range dp {
        dp[i] = make([]int, n+1)
        for j := range dp[i] {
            dp[i][j] = math.MaxInt32
        }
    }
    d = append(d, t)
    a = append(a, 0)

    dp[0][0] = 0 // 一开始在第一个点，没有移掉一个，最小时间是0
    // 注意第一个点不能remove, 不然第一个点到第二个点之间的速度就不知道是多少了
    for i := 1; i <= n; i++ {
        for j := 0; j <= k; j++ {
            for p := 0; p <= i-1; p++ {
                oj := j - (i - p - 1)
                if oj >= 0 {
                    dp[i][j] = min(dp[i][j], dp[p][oj]+a[p]*(d[i]-d[p]))
                }
            }
        }
    }
    res := math.MaxInt32
    for j := 0; j <= n; j++ {
        res = min(res, dp[n][j])
    }
    Fprintln(w, res)
}

package main

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

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

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

    var n, t, k int
    Fscan(r, &n, &t, &k)
    d, a := make([]int, n), make([]int, n)
    for i := 0; i < n; i++ {
        Fscan(r, &d[i])
    }
    for i := 0; i < n; i++ {
        Fscan(r, &a[i])
    }
    dp := make([][]int, n+1)
    for i := range dp {
        dp[i] = make([]int, n+1)
        for j := range dp[i] {
            dp[i][j] = math.MaxInt32
        }
    }
    d = append(d, t)
    a = append(a, 0)

    dp[0][0] = 0 // 一开始在第一个点，没有移掉一个，最小时间是0
    for i := 1; i <= n; i++ {
        for j := 0; j <= k; j++ {
            for p := 0; p <= i-1; p++ {
                oj := j - (i - p - 1)
                if oj >= 0 {
                    dp[i][j] = min(dp[i][j], dp[p][oj]+a[p]*(d[i]-d[p]))
                }
            }
        }
    }
    res := math.MaxInt32
    for j := 0; j <= n; j++ {
        res = min(res, dp[n][j])
    }
    Fprintln(w, res)
}

package main

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

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

	var n, l, K int
	Fscan(in, &n, &l, &K)
	pos := make([]int, n)
	for i := range pos {
		Fscan(in, &pos[i])
	}
	pos = append(pos, l)
	lim := make([]int, n)
	for i := range lim {
		Fscan(in, &lim[i])
	}

	if K == 0 {
		s := 0
		for i := 1; i < n+1; i++ {
			s += (pos[i] - pos[i-1]) * lim[i-1]
		}
		Fprintln(out, s)
		return
	}

	pre := make([][]int, K+2)
	for i := range pre {
		pre[i] = make([]int, n+2)
		for _i := range pre[i] {
			pre[i][_i] = 1e9
		}
	}

	pre[0][0] = (pos[1] - pos[0]) * lim[0]

	dp := make([][]int, K+2)
	for i := range dp {
		dp[i] = make([]int, n+2)
		for _i := range dp[i] {
			dp[i][_i] = 1e9
		}
	}

	ans := int(2e9)
	for i := 1; i < n; i++ {
		for del := 0; del <= min(i, K); del++ {

			if del < min(i, K) {
				dp[del][i] = int(2e9)
				for pi := i - (del + 1); pi < i; pi++ {
					dp[del][i] = min(dp[del][i], pre[del][pi])
				}
				dp[del][i] += (pos[i+1] - pos[i]) * lim[i] // save
			}

			if del > 0 {
				for pi := 0; pi < i; pi++ {
					dp[del][pi] = pre[del-1][pi] + (pos[i+1]-pos[i])*lim[pi]
				}
			}
		}
		if i == n-1 {
			for del, r := range dp {
				for pi := max(i-del,0); pi <= i; pi++ {
					ans = min(ans, r[pi])
				}
			}
		}
		dp, pre = pre, dp
	}
	Fprintln(out, ans)
}

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

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

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


package main

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

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

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

	return result
}

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

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func main() {
	sc := NewScanner()
	N := sc.NextInt()
	A := sc.NextIntArray()
	l := 0
	for i := 0; i < N; i++ {
		g := l + A[i]
		if l == 1 && A[i] == 0 {
			fmt.Println("NO")
			return
		}

		if g % 2 == 0 {
			l = 2
		} else {
			l = 1
		}
	}
	if l == 1 {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}

}

package main

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

type bufReader struct {
	r   *bufio.Reader
	buf []byte
	i   int
}

var reader = &bufReader{
	bufio.NewReader(os.Stdin),
	make([]byte, 0),
	0,
}

func (r *bufReader) readLine() {
	if r.i < len(r.buf) {
		return
	}
	r.buf = make([]byte, 0)
	r.i = 0
	for {
		line, isPrefix, err := r.r.ReadLine()
		if err != nil {
			panic(err)
		}
		r.buf = append(r.buf, line...)
		if !isPrefix {
			break
		}
	}
}

func (r *bufReader) next() string {
	r.readLine()
	from := r.i
	for ; r.i < len(r.buf); r.i++ {
		if r.buf[r.i] == ' ' {
			break
		}
	}
	s := string(r.buf[from:r.i])
	r.i++
	return s
}

func (r *bufReader) nextLine() string {
	r.readLine()
	s := string(r.buf[r.i:])
	r.i = len(r.buf)
	return s
}

var writer = bufio.NewWriter(os.Stdout)

func next() string {
	return reader.next()
}

func nextInt64() int64 {
	i, err := strconv.ParseInt(reader.next(), 10, 64)
	if err != nil {
		panic(err)
	}
	return i
}

func nextInt() int {
	return int(nextInt64())
}

func nextLine() string {
	return reader.nextLine()
}

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

func max64(x, y int64) int64 {
	if x > y {
		return x
	}
	return y
}

func min64(x, y int64) int64 {
	if x < y {
		return x
	}
	return y
}

func max(x, y int) int {
	return int(max64(int64(x), int64(y)))
}

func min(x, y int) int {
	return int(min64(int64(x), int64(y)))
}

func joinInt64s(a []int64, sep string) string {
	b := make([]string, len(a))
	for i, v := range a {
		b[i] = strconv.FormatInt(v, 10)
	}
	return strings.Join(b, sep)
}

func joinInts(a []int, sep string) string {
	b := make([]string, len(a))
	for i, v := range a {
		b[i] = strconv.Itoa(v)
	}
	return strings.Join(b, sep)
}

func divUp64(x, y int64) int64 {
	return (x + y - 1) / y
}

func divUp(x, y int) int {
	return int(divUp64(int64(x), int64(y)))
}

func main() {
	solve()
	writer.Flush()
}

func solve() {
	n := nextInt()
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = nextInt64()
	}
	var cur int64
	for _, v := range a {
		if v == 0 {
			if cur&1 == 1 {
				out("NO")
				return
			}
			cur = 0
		} else {
			cur += v
		}
	}
	if cur&1 == 1 {
		out("NO")
	} else {
		out("YES")
	}
}

package main

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

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

var n int
var a []int

func yes() bool {
	for i := 0; i < n; i++ {
		if a[i] > 0 {
			if a[i]%2 == 0 {
				a[i] = 0
			} else {
				a[i] = 1
				if (i+1 < n) && (a[i+1] > 0) {
					a[i]--
					a[i+1]--
				}
			}
		}
	}
	for i := 0; i < n; i++ {
		if a[i] != 0 {
			return false
		}
	}
	return true
}

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

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

	if yes() {
		fmt.Fprintf(bufout, "YES\n")
	} else {
		fmt.Fprintf(bufout, "NO\n")
	}

}

package main

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

func main() {
	var n int
	if _, err := fmt.Scanf("%d\n", &n); err != nil {
		return
	}

	var have_tdc, failed bool = false, false

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	for i := 0; i < n && scanner.Scan(); i++ {
		a, err := strconv.Atoi(scanner.Text())
		if err != nil {
			return
		}
		if a > 0 {
			a %= 2
			if a > 0 {
				have_tdc = !have_tdc
			}
		} else if have_tdc {
			failed = true
		}
	}

	if failed || have_tdc {
		fmt.Println("NO")
	} else {
		fmt.Println("YES")
	}
}

package main

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

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

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

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

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

func main() {
    scanner.Split(bufio.ScanWords)

    n := ReadInt()
    hasTicket := false

    for ; n > 0; n-- {
        a := ReadInt()

        if hasTicket {
            a--
            hasTicket = false
        }

        if a < 0 {
            fmt.Print("NO")
            return
        }

        hasTicket = a % 2 == 1
    }

    if !hasTicket {
        fmt.Print("YES")
    } else {
        fmt.Print("NO")
    }
}

package main

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

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

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r:rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}

func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}

func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

	return result
}

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

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}
func main() {
	sc := NewScanner()
	N := sc.NextInt()
	A := sc.NextIntArray()
	l := 0
	for i := 0; i < N; i++ {
		if l == 1 && A[i] == 0 {
			fmt.Println("NO")
			return
		}
		g := l + A[i]
		if g % 2 == 0 {
			l = 2
		} else {
			l = 1
		}
	}
	fmt.Println("YES")

}


/*
URL:
https://codeforces.com/contest/1373/problem/C
*/

package main

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

/********** FAU standard libraries **********/

//fmt.Sprintf("%b\n", 255) 	// binary expression

/********** I/O usage **********/

//str := ReadString()
//i := ReadInt()
//X := ReadIntSlice(n)
//S := ReadRuneSlice()
//a := ReadFloat64()
//A := ReadFloat64Slice(n)

//str := ZeroPaddingRuneSlice(num, 32)
//str := PrintIntsLine(X...)

/*******************************************************************/

const (
	// General purpose
	MOD          = 1000000000 + 7
	ALPHABET_NUM = 26
	INF_INT64    = math.MaxInt64
	INF_BIT60    = 1 << 60
	INF_INT32    = math.MaxInt32
	INF_BIT30    = 1 << 30
	NIL          = -1

	// for dijkstra, prim, and so on
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

func init() {
	// bufio.ScanWords <---> bufio.ScanLines
	ReadString = newReadString(os.Stdin, bufio.ScanWords)
	stdout = bufio.NewWriter(os.Stdout)
}

var (
	t int

	S []rune
)

func main() {
	t = ReadInt()

	for i := 0; i < t; i++ {
		S = ReadRuneSlice()

		solve()
	}
}

func solve() {
	n := len(S)
	prefix := make([]int, n)
	memo := make([]int, n)

	for i := 0; i < n; i++ {
		if S[i] == '+' {
			if i == 0 {
				prefix[i]++
			} else {
				prefix[i] = prefix[i-1] + 1
			}
		} else {
			if i == 0 {
				prefix[i]--
			} else {
				prefix[i] = prefix[i-1] - 1
			}
		}
	}
	// PrintfDebug("prefix: %v\n", prefix)

	mini := 0
	for i := 0; i < n; i++ {
		if ChMin(&mini, prefix[i]) {
			memo[i] = 1
		}
	}

	ans := int64(0)
	for i := 0; i < n; i++ {
		if memo[i] == 1 {
			ans += int64(i + 1)
		}
	}
	ans += int64(n)

	fmt.Println(ans)
}

// ChMin accepts a pointer of integer and a target value.
// If target value is SMALLER than the first argument,
//	then the first argument will be updated by the second argument.
func ChMin(updatedValue *int, target int) bool {
	if *updatedValue > target {
		*updatedValue = target
		return true
	}
	return false
}

/*******************************************************************/

/*********** I/O ***********/

var (
	// ReadString returns a WORD string.
	ReadString func() string
	stdout     *bufio.Writer
)

func newReadString(ior io.Reader, sf bufio.SplitFunc) func() string {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+9)) // for Codeforces
	r.Split(sf)

	return func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
}

// ReadInt returns an integer.
func ReadInt() int {
	return int(readInt64())
}
func ReadInt2() (int, int) {
	return int(readInt64()), int(readInt64())
}
func ReadInt3() (int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64())
}
func ReadInt4() (int, int, int, int) {
	return int(readInt64()), int(readInt64()), int(readInt64()), int(readInt64())
}

// ReadInt64 returns as integer as int64.
func ReadInt64() int64 {
	return readInt64()
}
func ReadInt64_2() (int64, int64) {
	return readInt64(), readInt64()
}
func ReadInt64_3() (int64, int64, int64) {
	return readInt64(), readInt64(), readInt64()
}
func ReadInt64_4() (int64, int64, int64, int64) {
	return readInt64(), readInt64(), readInt64(), readInt64()
}

func readInt64() int64 {
	i, err := strconv.ParseInt(ReadString(), 0, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// ReadIntSlice returns an integer slice that has n integers.
func ReadIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt()
	}
	return b
}

// ReadInt64Slice returns as int64 slice that has n integers.
func ReadInt64Slice(n int) []int64 {
	b := make([]int64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadInt64()
	}
	return b
}

// ReadFloat64 returns an float64.
func ReadFloat64() float64 {
	return float64(readFloat64())
}

func readFloat64() float64 {
	f, err := strconv.ParseFloat(ReadString(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// ReadFloatSlice returns an float64 slice that has n float64.
func ReadFloat64Slice(n int) []float64 {
	b := make([]float64, n)
	for i := 0; i < n; i++ {
		b[i] = ReadFloat64()
	}
	return b
}

// ReadRuneSlice returns a rune slice.
func ReadRuneSlice() []rune {
	return []rune(ReadString())
}

/*********** Debugging ***********/

// ZeroPaddingRuneSlice returns binary expressions of integer n with zero padding.
// For debugging use.
func ZeroPaddingRuneSlice(n, digitsNum int) []rune {
	sn := fmt.Sprintf("%b", n)

	residualLength := digitsNum - len(sn)
	if residualLength <= 0 {
		return []rune(sn)
	}

	zeros := make([]rune, residualLength)
	for i := 0; i < len(zeros); i++ {
		zeros[i] = '0'
	}

	res := []rune{}
	res = append(res, zeros...)
	res = append(res, []rune(sn)...)

	return res
}

// Strtoi is a wrapper of strconv.Atoi().
// If strconv.Atoi() returns an error, Strtoi calls panic.
func Strtoi(s string) int {
	if i, err := strconv.Atoi(s); err != nil {
		panic(errors.New("[argument error]: Strtoi only accepts integer string"))
	} else {
		return i
	}
}

// PrintIntsLine returns integers string delimited by a space.
func PrintIntsLine(A ...int) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.Itoa(A[i])
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintIntsLine returns integers string delimited by a space.
func PrintInts64Line(A ...int64) string {
	res := []rune{}

	for i := 0; i < len(A); i++ {
		str := strconv.FormatInt(A[i], 10) // 64bit int version
		res = append(res, []rune(str)...)

		if i != len(A)-1 {
			res = append(res, ' ')
		}
	}

	return string(res)
}

// PrintfDebug is wrapper of fmt.Fprintf(os.Stderr, format, a...)
func PrintfDebug(format string, a ...interface{}) {
	fmt.Fprintf(os.Stderr, format, a...)
}

// PrintfBufStdout is function for output strings to buffered os.Stdout.
// You may have to call stdout.Flush() finally.
func PrintfBufStdout(format string, a ...interface{}) {
	fmt.Fprintf(stdout, format, a...)
}

package main

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

var reader *bufio.Reader

func parseInt(s string) int {
	return int(parseInt64(s))
}

func parseInt64(s string) int64 {
	x, _ := strconv.ParseInt(s, 10, 64)
	return x
}

func readLine() string {
	if reader == nil {
		reader = bufio.NewReader(os.Stdin)
	}
	s, _ := reader.ReadString('\n')
	return strings.TrimSuffix(s, "\n")
}

func readFields() []string {
	return strings.Fields(readLine())
}

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

var t int

func main() {
	t := parseInt(readFields()[0])
	for tc := 0; tc < t; tc++ {
      f := readLine()
      var min,cur,ans int64 = 0,0,0
      for i := 0; i < len(f)-1; i++ {
        if f[i]=='-' {
            cur--
            if cur < min {
                min = cur
                ans += int64(i+1)
            }
        } else {
            cur++
        }
      }
      ans += int64(len(f)-1)
      Println(ans)
    }
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		sum := int64(0)
		cindex := int64(0)
		for {
			current := 0
			sum += cindex
			flag := true
			for ;cindex<int64(len(line));cindex++ {
				sum++
				if line[cindex] == '+' {
					current++
				} else {
						current--
				}
				if current < 0 {
					flag = false
					cindex++
					break
				}
			}
			if flag {
				break
			}
		}
		fmt.Println(sum)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	var cur int64
	var highest int64
	result := int64(len(line))
	for i, c := range line {
		cc := conv(c)
		if i == 0 {
			cur = cc
		} else {
			cur += cc
		}
		if cur < highest {
			result += int64(i) + 1
			highest = cur
		}
	}

	return result
}

func main() {
	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	input := bufio.NewScanner(os.Stdin)
	input.Buffer(buf, maxCapacity)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	results := []int64{}
	for i := 0; i < n; i++ {
		input.Scan()
		results = append(results, evaluate(input.Text()))
	}

	for _, result := range results {
		fmt.Println(result)
	}

}

package main

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

func main() {
	t := NextInt()
	for t > 0 {
		t--
		s := Next()
		cur := 0
		min := len(s)
		minPos := make([]int, len(s)+1)
		for i := range minPos {
			minPos[i] = -1
		}
		for i := range s {
			if s[i] == '+' {
				cur++
			} else {
				cur--
			}
			if cur < 0 && minPos[-cur] == -1 {
				minPos[-cur] = i + 1
			}
			if cur < min {
				min = cur
			}
		}
		ans := int64(len(s))
		if min < 0 {
			for i := 0; i < -min; i++ {
				ans += int64(minPos[i+1])
			}
		}
		fmt.Println(ans)
	}
}

var sc = bufio.NewScanner(os.Stdin)

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

func Next() string {
	sc.Scan()
	return sc.Text()
}

func NextInt() int {
	i, err := strconv.Atoi(Next())
	if err != nil {
		panic(err)
	}
	return i
}

func NextDouble() float64 {
	f, err := strconv.ParseFloat(Next(), 64)
	if err != nil {
		panic(err)
	}
	return f
}

package main

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

var reader *bufio.Reader

func parseInt(s string) int {
	return int(parseInt64(s))
}

func parseInt64(s string) int64 {
	x, _ := strconv.ParseInt(s, 10, 64)
	return x
}

func readLine() string {
	if reader == nil {
		reader = bufio.NewReader(os.Stdin)
	}
	s, _ := reader.ReadString('\n')
	return strings.TrimSuffix(s, "\n")
}

func readFields() []string {
	return strings.Fields(readLine())
}

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

var t int

func main() {
	t := parseInt(readFields()[0])
	for tc := 0; tc < t; tc++ {
      f := readLine()
      min := 0
      cur := 0
      ans := 0
      for i := 0; i < len(f)-1; i++ {
        if f[i]=='-' {
            cur--
            if cur < min {
                min = cur
                ans += i+1
            }
        } else {
            cur++
        }
      }
      ans += len(f)-1
      Println(ans)
    }
}

package main
import "fmt"

func main(){
	var t int;
	fmt.Scanln(&t);
	for ;t>0;t--{
		var s string;
		fmt.Scanln(&s);
		var cnt, mn int;
		var ans int64 = int64(len(s));
		for i:= 0; i < len(s); i ++ {
			if( s[i] == '+' ){
				cnt++;
			}else{
				cnt--;
			}
			if( cnt < 0 ){
				if( cnt < mn ){
					mn = cnt;
					ans += int64(-mn);
				}
			}
		}
		fmt.Println(ans);
	}
}

// --- In the name of Allah ---

package main
import(
	"fmt"
	"bufio"
	"os"
)
const _MAXCAPACITY = 1048576;
var _BYTES []byte;
var _IDX int;
var _ISPREFIX bool;
var _ERR error;
var _INPUT *bufio.Reader;

func InputInt()int{
	if _IDX < len(_BYTES){
		var ret int = 0;
		var flag bool = false;
		var sign int = 1;
		for ;_IDX < len(_BYTES);_IDX++{
			if _BYTES[_IDX] == ' '{
				if flag{
					break;
				}
			}else{
				if !flag && _BYTES[_IDX] == '-'{
					sign = -1;
				}else{
					ret = (ret<<3) + (ret<<1) + int(_BYTES[_IDX]-48);
					flag = true;
				}
			}
		}
		return ret*sign;
	}else{
		_BYTES, _ISPREFIX, _ERR = _INPUT.ReadLine();
		_IDX = 0;
		return InputInt();
	}
}
func InputLine()string{
	var ret string;
	if _IDX < len(_BYTES){
		for ;_IDX < len(_BYTES); _IDX++{
			ret += string(_BYTES[_IDX]);
		}
		return ret;
	}else{
		_BYTES, _ISPREFIX, _ERR = _INPUT.ReadLine();
		_IDX = 0;
		return InputLine();
	}
}
func main(){
	_INPUT = bufio.NewReader(os.Stdin);
	var t int = InputInt();
	for ;t>0;t--{
		var s string = InputLine();
		var cnt, mn int;
		var ans int64 = int64(len(s));
		for i:= 0; i < len(s); i ++ {
			if( s[i] == '+' ){
				cnt++;
			}else{
				cnt--;
			}
			if( cnt < 0 ){
				if( cnt < mn ){
					mn = cnt;
					ans += int64(i+1);
				}
			}
		}
		fmt.Println(ans);
	}
}

// --- In the name of Allah ---

package main
import(
	"fmt"
	"bufio"
	"os"
)
const _MAXCAPACITY = 1000001;
var n int;
var _BYTES []byte;
var _IDX int;
var _ISPREFIX bool;
var _ERR error;
var _INPUT *bufio.Reader;

func InputInt()int{
	if _IDX < len(_BYTES){
		var ret int = 0;
		var flag bool = false;
		var sign int = 1;
		for ;_IDX < len(_BYTES);_IDX++{
			if _BYTES[_IDX] == ' '{
				if flag{
					break;
				}
			}else{
				if !flag && _BYTES[_IDX] == '-'{
					sign = -1;
				}else{
					ret = (ret<<3) + (ret<<1) + int(_BYTES[_IDX]-48);
					flag = true;
				}
			}
		}
		return ret*sign;
	}else{
		_BYTES, _ISPREFIX, _ERR = _INPUT.ReadLine();
		_IDX = 0;
		return InputInt();
	}
}
func InputLine()string{
	var ret string;
	_BYTES, _ISPREFIX, _ERR = _INPUT.ReadLine();
	for i:=0; i < len(_BYTES); i ++{
		ret += string(_BYTES[i]);
	}
	return ret;
}
func main(){
	_INPUT = bufio.NewReaderSize(os.Stdin, _MAXCAPACITY);
	var t int;// = InputInt();
	fmt.Scanf("%d", &t);
	n = t;
	for ;t>0;t--{
		var s string;// = InputLine();
		fmt.Scanf("%s", &s);
		var cnt, mn int;
		var ans int64 = int64(len(s));
		for i:= 0; i < len(s); i ++ {
			if( s[i] == '+' ){
				cnt++;
			}else{
				cnt--;
			}
			if( cnt < mn ){
				mn = cnt;
				ans += int64(i+1);
			}
		}
		fmt.Println(ans);
	}
}

// --- In the name of Allah ---

package main
import(
	"fmt"
	"bufio"
	"os"
)
const _MAXCAPACITY = 1048576;
var _BYTES []byte;
var _IDX int;
var _ISPREFIX bool;
var _ERR error;
var _INPUT *bufio.Reader;

func InputInt()int{
	if _IDX < len(_BYTES){
		var ret int = 0;
		var flag bool = false;
		var sign int = 1;
		for ;_IDX < len(_BYTES);_IDX++{
			if _BYTES[_IDX] == ' '{
				if flag{
					break;
				}
			}else{
				if !flag && _BYTES[_IDX] == '-'{
					sign = -1;
				}else{
					ret = (ret<<3) + (ret<<1) + int(_BYTES[_IDX]-48);
					flag = true;
				}
			}
		}
		return ret*sign;
	}else{
		fmt.Println("InputInt")
		_BYTES, _ISPREFIX, _ERR = _INPUT.ReadLine();
		_IDX = 0;
		return InputInt();
	}
}
func InputLine()string{
	var ret string;
	if _IDX < len(_BYTES){
		for ;_IDX < len(_BYTES); _IDX++{
			ret += string(_BYTES[_IDX]);
		}
		return ret;
	}else{
		_BYTES, _ISPREFIX, _ERR = _INPUT.ReadLine();
		_IDX = 0;
		fmt.Println("InputLine", len(_BYTES), _BYTES, _IDX);
		return InputLine();
	}
}
func main(){
	_INPUT = bufio.NewReader(os.Stdin);
	var t int = InputInt();
	fmt.Println(t);
	for ;t>0;t--{
		var s string = InputLine();
		fmt.Println(s);
		var cnt, mn int;
		var ans int64 = int64(len(s));
		for i:= 0; i < len(s); i ++ {
			if( s[i] == '+' ){
				cnt++;
			}else{
				cnt--;
			}
			if( cnt < 0 ){
				if( cnt < mn ){
					mn = cnt;
					ans += int64(i+1);
				}
			}
		}
		fmt.Println(ans);
	}
}

package main

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

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	reader := bufio.NewReader(os.Stdin)
	for i := 0; i < cases; i++ {
		line := readLine(reader)
		sum := 0
		cindex := 0
		for {
			current := 0
			sum += cindex
			flag := true
			for ;cindex<len(line);cindex++ {
				sum++
				if line[cindex] == '+' {
					current++
				} else {
						current--
				}
				if current < 0 {
					flag = false
					cindex++
					break
				}
			}
			if flag {
				break
			}
		}
		fmt.Println(sum)
	}
}

func readLine(reader *bufio.Reader) []byte {
	result := make([]byte, 0)
	for {
		s, isPrefix, err := reader.ReadLine()
		if err != nil {
			fmt.Printf("error=%+v\n", err)
			break
		}
		result = append(result, s...)
		if !isPrefix {
			break
		}
	}
	return result
}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	var cur int64
	var highest int64
	result := int64(len(line))
	for i, c := range line {
		cc := conv(c)
		i_64 := int64(i)
		if i_64 == 0 {
			cur = cc
		} else {
			cur += cc
		}
		if cur < 0 {
			if cur < highest {
				result += int64(i + 1)
				highest = cur
			}
		}
	}

	return result
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	results := []int64{}
	for i := 0; i < n; i++ {
		input.Scan()
		results = append(results, evaluate(input.Text()))
	}

	for _, result := range results {
		fmt.Println(result)
	}

}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	var cur int64
	var highest int64
	result := int64(len(line))
	for i, c := range line {
		cc := conv(c)
		if i == 0 {
			cur = cc
		} else {
			cur += cc
		}
		if cur < highest {
			result += int64(i) + 1
			highest = cur
		}
	}

	return result
}

func main() {
	const maxCapacity = 512 * 1024
	buf := make([]byte, maxCapacity)
	input := bufio.NewScanner(os.Stdin)
	input.Buffer(buf, maxCapacity)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	results := []int64{}
	for i := 0; i < n; i++ {
		input.Scan()
		results = append(results, evaluate(input.Text()))
	}

	for _, result := range results {
		fmt.Println(result)
	}

}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	temp := make(map[int64]int64)
	var cur int64
	for i, c := range line {
		cc := conv(c)
		i_64 := int64(i)
		if i_64 == 0 {
			cur = cc
		} else {
			cur += cc
		}
		if cur >= 0 {
			continue
		}
		if _, ok := temp[cur]; !ok {
			//	fmt.Println(i_64)
			//	fmt.Println(counter[i_64])
			temp[cur] = i_64
		}
	}

	result := int64(len(line))
	//fmt.Println(temp)

	for _, v := range temp {
		result += v + 1
	}

	return result
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	var lines = []string{}
	for i := 0; i < n; i++ {
		input.Scan()
		lines = append(lines, input.Text())
	}

	for _, line := range lines {
		fmt.Println(evaluate(line))
	}

}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	temp := make(map[int64]int64)
	var cur int64
	for i, c := range line {
		cc := conv(c)
		i_64 := int64(i)
		if i_64 == 0 {
			cur = cc
		} else {
			cur += cc
		}
		if cur >= 0 {
			continue
		}
		if _, ok := temp[cur]; !ok {
			//	fmt.Println(i_64)
			//	fmt.Println(counter[i_64])
			temp[cur] = i_64
		}
	}

	result := int64(len(line))
	// fmt.Println(len(temp))

	for _, v := range temp {
		result += v + 1
	}

	return result
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	results := []int64{}
	for i := 0; i < n; i++ {
		input.Scan()
		results = append(results, evaluate(input.Text()))
	}

	for _, result := range results {
		fmt.Println(result)
	}

}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	temp := make(map[int64]int64)
	counter := make([]int64, len(line))
	for i, c := range line {
		cur := conv(c)
		i_64 := int64(i)
		if i_64 == 0 {
			counter[i_64] = cur
		} else {
			counter[i_64] = counter[i_64-1] + cur
		}
		if counter[i_64] >= 0 {
			continue
		}
		if _, ok := temp[counter[i_64]]; !ok {
			//	fmt.Println(i_64)
			//	fmt.Println(counter[i_64])
			temp[counter[i_64]] = i_64
		}
	}

	result := int64(len(line))
	//fmt.Println(temp)

	for _, v := range temp {
		result += v + 1
	}

	return result
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	var lines = []string{}
	for i := 0; i < n; i++ {
		input.Scan()
		lines = append(lines, input.Text())
	}

	for _, line := range lines {
		fmt.Println(evaluate(line))
	}

}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	var cur int64
	var highest int64
	result := int64(len(line))
	for i, c := range line {
		cc := conv(c)
		if i == 0 {
			cur = cc
		} else {
			cur += cc
		}
		if cur < highest {
			result += int64(i) + 1
			highest = cur
		}
	}

	return result
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	results := []int64{}
	for i := 0; i < n; i++ {
		input.Scan()
		results = append(results, evaluate(input.Text()))
	}

	for _, result := range results {
		fmt.Println(result)
	}

}

package main

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

func conv(c rune) int64 {
	if c == '+' {
		return 1
	}

	return -1
}

func evaluate(line string) int64 {
	temp := make(map[int64]int64)
	var cur int64
	for i, c := range line {
		cc := conv(c)
		i_64 := int64(i)
		if i_64 == 0 {
			cur = cc
		} else {
			cur += cc
		}
		if cur >= 0 {
			continue
		}
		if _, ok := temp[cur]; !ok {
			//	fmt.Println(i_64)
			//	fmt.Println(counter[i_64])
			temp[cur] = i_64
		}
	}

	result := int64(len(line))
	// fmt.Println(len(temp))

	for _, v := range temp {
		result += v + 1
	}

	return result
}

func main() {
	input := bufio.NewScanner(os.Stdin)
	input.Scan()
	n, _ := strconv.Atoi(input.Text())
	results := []int64{}
	for i := 0; i < n; i++ {
		input.Scan()
		str := input.Text()
		results = append(results, evaluate(str))
	}

	for _, result := range results {
		fmt.Println(result)
	}

}


package main

import (
	"fmt"
)

func main() {
	var n, m, k int
	fmt.Scan(&n, &m, &k)
	firstM := int64(0)
	p := make([]int, n)
	for i := range p {
		fmt.Scan(&p[i])
		if i < m {
			firstM += int64(p[i])
		}
	}
	before := make([]int64, n+1)
	after := make([]int64, n+1)
	for i := 0; i < k; i++ {
		sum := firstM
		best := int64(0)
		for i := m; i <= n; i++ {
			if best < before[i-m] {
				best = before[i-m]
			}
			after[i] = best + sum
			if i+1 <= n {
				sum += int64(p[i] - p[i-m])
			}
		}
		before, after = after, before
	}
	ans := int64(0)
	for _, b := range before {
		if ans < b {
			ans = b
		}
	}
	fmt.Println(ans)
}

// http://codeforces.com/problemset/problem/467/C
package main

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

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

const (
	MOD = 1e9 + 7
)

type (

)

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

	return b
}

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

	//f, _ := os.Open("input.txt")
	//input := bufio.NewReader(f)
	input := bufio.NewReader(os.Stdin)

	fmt.Fscan(input, &n, &m, &k)

	p := make([]int64, n)
	sum := make([]int64, n + 1)
	dp := make([][]int64, n)
	for i := 0; i < n; i++ {
		dp[i] = make([]int64, k)
		fmt.Fscan(input, &p[i])

		sum[i + 1] = sum[i] + p[i]
	}

	// Solve
	curs := make([]int64, n)
	for i := m - 1; i < n; i++ {
		curs[i] = sum[i + 1] - sum[i - m + 1]

		if i > 0 {
			dp[i][0] = max(sum[i + 1] - sum[i - m + 1], dp[i - 1][0])
		}  else {
			dp[i][0] = sum[i + 1] - sum[i - m + 1]
		}
	}

	for i := m; i < n; i++ {
		for j := 1; j < k; j++ {
			dp[i][j] = max(dp[i - 1][j], curs[i] + dp[i - m][j - 1])
		}
	}

	fmt.Println(dp[n - 1][k - 1])
}

package main

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

// github.com/EndlessCheng/codeforces-go
func Sol467C(reader io.Reader, writer io.Writer) {
	max := func(a, b int64) int64 {
		if a > b {
			return a
		}
		return b
	}
	in := bufio.NewReader(reader)
	out := bufio.NewWriter(writer)
	defer out.Flush()

	var n, sz, k int
	Fscan(in, &n, &sz, &k)
	p := make([]int64, n)
	for i := range p {
		Fscan(in, &p[i])
	}

	sum := make([]int64, n-sz+1)
	for _, v := range p[:sz] {
		sum[0] += v
	}
	for i, v := range p[sz:] {
		sum[i+1] = sum[i] + v - p[i]
	}

	dp := make([]int64, n+1)
	for i := 1; i <= k; i++ {
		for end := n; end >= sz; end-- {
			dp[end] = max(dp[end], dp[end-sz]+sum[end-sz])
		}
		maxVal := int64(0)
		for i, dpi := range dp {
			maxVal = max(maxVal, dpi)
			dp[i] = maxVal
		}
	}
	Fprint(out, dp[n])
}

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

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

var n, m, K int

func max(nums ...int64) int64 {
	var m int64
	for i, v := range nums{
		if i == 0 || v > m {
			m = v
		}
	}
	return m
}

func main() {
	scanf("%d %d %d\n", &n, &m, &K)

	p := make([]int64, n)
	for i := range p {
		scanf("%d", &p[i])
	}
	sum := make(map[int]int64)
	for i, v := range p {
		sum[i] = sum[i-1] + v
	}

	prev, curr := make([]int64, n), make([]int64, n)

	// prev init k = 1
	prev[m-1] = sum[m-1]
	for i := m; i < n; i++ {
		prev[i] = max(prev[i-1], sum[i] - sum[i-m])
	}

	for k := 2; k <= K; k++{
		curr[m*k-1] = sum[m*k-1]
		for i:= m*k; i<n; i++{
			curr[i] = max(curr[i-1], prev[i-m] + sum[i] - sum[i-m])
		}
		prev, curr = curr, prev
	}

	fmt.Println(prev[n-1])

}

package main

import (
    "fmt"
)

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

func main() {
    var n, m, k int
    fmt.Scanf("%d %d %d\n", &n, &m, &k)
    sum := make([]uint64, n+1)
    dp := make([][]uint64, 2)
    dp[0] = make([]uint64, n+1)
    dp[1] = make([]uint64, n+1)

    for i := 1; i <= n; i++ {
        fmt.Scan(&sum[i])
        sum[i] += sum[i-1]
    }

    q := 1
    for i := 1; i <= k; i++ {
        q = 1 - q
        for j := i * m; j <= n; j++ {
            dp[q][j] = max(dp[q][j-1], dp[1-q][j-m]+sum[j]-sum[j-m])
        }
    }
    fmt.Println(dp[q][n])
}

package main

import (
	"fmt"
)

func main() {
	var n, m, k int
	fmt.Scan(&n, &m, &k)
	firstM := 0
	p := make([]int, n)
	for i := range p {
		fmt.Scan(&p[i])
		if i < m {
			firstM += p[i]
		}
	}
	before := make([]int, n+1)
	after := make([]int, n+1)
	for i := 0; i < k; i++ {
		sum := firstM
		best := 0
		for i := m; i <= n; i++ {
			if best < before[i-m] {
				best = before[i-m]
			}
			after[i] = best + sum
			if i+1 <= n {
				sum += p[i] - p[i-m]
			}
		}
		before, after = after, before
	}
	ans := 0
	for _, b := range before {
		if ans < b {
			ans = b
		}
	}
	fmt.Println(ans)
}

package main

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

var reader *bufio.Reader = bufio.NewReader(os.Stdin)
var writer *bufio.Writer = bufio.NewWriter(os.Stdout)
func printf(f string, a ...interface{}) { fmt.Fprintf(writer, f, a...) }
func scanf(f string, a ...interface{}) { fmt.Fscanf(reader, f, a...) }

var n, m, K int

func max(nums ...int) int {
	var m int
	for i, v := range nums{
		if i == 0 || v > m {
			m = v
		}
	}
	return m
}

func main() {
	scanf("%d %d %d\n", &n, &m, &K)

	p := make([]int, n)
	for i := range p {
		scanf("%d", &p[i])
	}
	sum := make(map[int]int)
	for i, v := range p {
		sum[i] = sum[i-1] + v
	}

	prev, curr := make([]int, n), make([]int, n)

	// prev init k = 1
	prev[m-1] = sum[m-1]
	for i := m; i < n; i++ {
		prev[i] = max(prev[i-1], sum[i] - sum[i-m])
	}

	for k := 2; k <= K; k++{
		curr[m*k-1] = sum[m*k-1]
		for i:= m*k; i<n; i++{
			curr[i] = max(curr[i-1], prev[i-m] + sum[i] - sum[i-m])
		}
		prev, curr = curr, prev
	}

	fmt.Println(prev[n-1])

}

package main

import (
	"container/list"
	"fmt"
)

type el struct {
	index int
	value uint64
}

func main() {
	var n, m, k int
	var x uint64
	fmt.Scanf("%d %d %d\n", &n, &m, &k)
	sum := make([]uint64, n)
	var e *list.Element
	l := list.New()
	fmt.Scanf("%d", &sum[0])

	for i := 1; i < n; i++ {
		fmt.Scanf("%d", &x)
		sum[i] = sum[i-1] + x
	}

	l.PushBack(el{0, sum[m-1]})

	for i := 1; i < n-(m-1); i++ {
		s := sum[i+m-1] - sum[i-1]
		if l.Len() < k {
			for e = l.Front(); (e != nil) && e.Value.(el).value < s; e = e.Next() {
			}

			if e != nil {
				l.InsertBefore(el{i, s}, e)
			} else {
				l.PushBack(el{i, s})
			}
		} else {
			for e = l.Front(); (e != nil) && e.Value.(el).value < s; e = e.Next() {
			}

			if e != nil {
				if e.Value.(el).value < s {
					l.InsertBefore(el{i, s}, e)
					l.Remove(l.Front())
				}
			} else {
				l.PushBack(el{i, s})
				l.Remove(l.Front())
			}
		}
	}

	x = 0
	if m > 1 {
		for e := l.Front(); e != nil; e = e.Next() {
			x += e.Value.(el).value
		}
	} else {
		for e := l.Front(); e != nil; e = e.Next() {
			x += e.Value.(el).value
		}
	}
	fmt.Println(x)
}

package main

import (
	"container/list"
	"fmt"
)

type el struct {
	index int
	value uint64
}

func main() {
	var n, m, k int
	var x, s uint64
	fmt.Scanf("%d %d %d\n", &n, &m, &k)
	sum := make([]uint64, n)
	var e *list.Element
	l := list.New()
	fmt.Scanf("%d", &sum[0])

	for i := 1; i < n; i++ {
		fmt.Scanf("%d", &x)
		sum[i] = sum[i-1] + x
	}

	l.PushBack(el{0, sum[m-1]})

	for i := 1; i < n-(m-1); i++ {
		s = sum[i+m-1] - sum[i-1]
		if l.Len() < k {
			for e = l.Front(); (e != nil) && e.Value.(el).value < s; e = e.Next() {
			}

			if e != nil {
				l.InsertBefore(el{i, s}, e)
			} else {
				l.PushBack(el{i, s})
			}
		} else {
			for e = l.Front(); (e != nil) && e.Value.(el).value < s; e = e.Next() {
			}

			if e != nil {
				if (l.Front()).Value.(el).value < s {
					l.InsertBefore(el{i, s}, e)
					l.Remove(l.Front())
				}
			} else {
				l.PushBack(el{i, s})
				l.Remove(l.Front())
			}
		}
	}

	x = 0
	for e := l.Front(); e != nil; e = e.Next() {
		x += e.Value.(el).value
	}
	fmt.Println(x)
}

package main

import (
	"container/list"
	"fmt"
)

func main() {
	var n, m, k int
	var x, s, max uint64
	fmt.Scanf("%d %d %d\n", &n, &m, &k)
	sum := make([]uint64, n)
	var e *list.Element
	fmt.Scanf("%d", &sum[0])

	for i := 1; i < n; i++ {
		fmt.Scanf("%d", &x)
		sum[i] = sum[i-1] + x
	}

	max = 0
	for offset := 0; offset <= m-1; offset++ {
		l := list.New()
		if offset == 0 {
			x = sum[offset]
		} else {
			x = sum[offset] - sum[offset-1]
		}
		l.PushBack(x)

		for i := offset + m; i < n-(m-1); i += m {
			s = sum[i+m-1] - sum[i-1]
			if l.Len() < k {
				for e = l.Front(); (e != nil) && e.Value.(uint64) < s; e = e.Next() {
				}

				if e != nil {
					l.InsertBefore(s, e)
				} else {
					l.PushBack(s)
				}
				x += s
			} else {
				for e = l.Front(); (e != nil) && e.Value.(uint64) < s; e = e.Next() {
				}

				if e != nil {
					if (l.Front()).Value.(uint64) < s {
						l.InsertBefore(s, e)
						l.Remove(l.Front())
						x += s - l.Front().Value.(uint64)
					}
				} else {
					l.PushBack(s)
					x += s - l.Front().Value.(uint64)
					l.Remove(l.Front())
				}
			}
		}
		if x > max {
			max = x
		}
	}

	fmt.Println(max)
}

package main

import (
	"container/list"
	"fmt"
)

func main() {
	var n, m, k int
	var x, s, max uint64
	fmt.Scanf("%d %d %d\n", &n, &m, &k)
	sum := make([]uint64, n)
	var e *list.Element
	fmt.Scanf("%d", &sum[0])

	for i := 1; i < n; i++ {
		fmt.Scanf("%d", &x)
		sum[i] = sum[i-1] + x
	}

	max = 0
	for offset := 0; offset <= m-1; offset++ {
		//fmt.Println()
		if m-1+offset >= n {
			break
		}
		l := list.New()
		if offset == 0 {
			x = sum[m-1+offset]
		} else {
			x = sum[m-1+offset] - sum[offset-1]
		}
		l.PushBack(x)
		//		fmt.Printf("%d ", x)

		for i := offset + m; i < n-(m-1); i += m {
			s = sum[i+m-1] - sum[i-1]
			if l.Len() < k {
				for e = l.Front(); (e != nil) && e.Value.(uint64) < s; e = e.Next() {
				}

				if e != nil {
					l.InsertBefore(s, e)
				} else {
					l.PushBack(s)
				}
				x += s
			} else {
				for e = l.Front(); (e != nil) && e.Value.(uint64) < s; e = e.Next() {
				}

				if e != nil {
					if (l.Front()).Value.(uint64) < s {
						l.InsertBefore(s, e)
						l.Remove(l.Front())
						x += s - l.Front().Value.(uint64)
					}
				} else {
					l.PushBack(s)
					x += s - l.Front().Value.(uint64)
					l.Remove(l.Front())
				}
			}
		}
		if x > max {
			max = x
		}
	}

	fmt.Println(max)
}

package main

import (
	"container/list"
	"fmt"
)

type el struct {
	index int
	value int
}

func main() {
	var n, m, k int
	fmt.Scanf("%d %d %d\n", &n, &m, &k)
	a := make([]int, n)
	l := list.New()
	var e *list.Element

	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}

	for i := 0; i < n-(m-1); i++ {
		s := a[i] + a[i+m-1]
		if l.Len() < k {
			for e = l.Front(); (e != nil) && e.Value.(el).value < s; e = e.Next() {
			}

			if e != nil {
				l.InsertBefore(el{i, s}, e)
			} else {
				l.PushBack(el{i, s})
			}
		} else {
			for e = l.Front(); (e != nil) && e.Value.(el).value < s; e = e.Next() {
			}

			if e != nil {
				if e.Value.(el).value < s {
					l.InsertBefore(el{i, s}, e)
					l.Remove(l.Front())
				}
			} else {
				l.PushBack(el{i, s})
				l.Remove(l.Front())
			}
		}
	}

	sum := 0
	if m > 1 {
		for e := l.Front(); e != nil; e = e.Next() {
			sum += a[e.Value.(el).index] + a[e.Value.(el).index+m-1]
		}
	} else {
		for e := l.Front(); e != nil; e = e.Next() {
			sum += a[e.Value.(el).index]
		}
	}
	fmt.Println(sum)
}


package main

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

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

	type result struct{ v, l, r int }
	set := []result{}
	var n, v, ans int
	Fscan(in, &n)
o:
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if v == 1 {
			set = set[:0]
			ans++
			Fprint(out, ans, " ")
			continue
		}
		for j, p := range set {
			g := gcd(p.v, v)
			if l := i + 1 - g; p.l <= l && l <= p.r {
				set = set[:0]
				ans++
				Fprint(out, ans, " ")
				continue o
			}
			set[j].v = g
		}
		set = append(set, result{v, i, i})
		k := 0
		for _, q := range set[1:] {
			if set[k].v != q.v {
				k++
				set[k] = q
			} else {
				set[k].r = q.r
			}
		}
		set = set[:k+1]
		Fprint(out, ans, " ")
	}
}

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

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

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1632D(_r io.Reader, _w io.Writer) {
	out := bufio.NewWriter(_w)
	defer out.Flush()
	buf := make([]byte, 1<<12)
	_i := len(buf)
	rc := func() byte {
		if _i >= len(buf) {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}

	type result struct{ v, l, r int }
	set := []result{}
	n := r()
	ans := 0
o:
	for i := 0; i < n; i++ {
		v := r()
		if v == 1 {
			set = set[:0]
			ans++
			Fprint(out, ans, " ")
			continue
		}
		for j, p := range set {
			g := gcd(p.v, v)
			if l := i + 1 - g; p.l <= l && l <= p.r {
				set = set[:0]
				ans++
				Fprint(out, ans, " ")
				continue o
			}
			set[j].v = g
		}
		set = append(set, result{v, i, i})
		k := 0
		for _, q := range set[1:] {
			if set[k].v != q.v {
				k++
				set[k] = q
			} else {
				set[k].r = q.r
			}
		}
		set = set[:k+1]
		Fprint(out, ans, " ")
	}
}

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

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

package main

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

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

	type result struct{ v, l, r int }
	set := []result{}
	var n, v, ans int
	Fscan(in, &n)
o:
	for i := 0; i < n; i++ {
		Fscan(in, &v)
		if v == 1 {
			set = nil
			ans++
			Fprint(out, ans, " ")
			continue
		}
		for j, p := range set {
			g := gcd(p.v, v)
			if l := i + 1 - g; p.l <= l && l < p.r {
				set = nil
				ans++
				Fprint(out, ans, " ")
				continue o
			}
			set[j].v = g
		}
		set = append(set, result{v, i, i + 1})
		k := 0
		for _, q := range set[1:] {
			if set[k].v != q.v {
				k++
				set[k] = q
			} else {
				set[k].r = q.r
			}
		}
		set = set[:k+1]
		Fprint(out, ans, " ")
	}
}

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

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

package main

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

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

func flush()                            { out.Flush() }
func scan(a ...interface{})             { fmt.Fscan(in, a...) }
func printf(f string, a ...interface{}) { fmt.Fprintf(out, f, a...) }
func assert(f bool) {
	if !f {
		panic("gg")
	}
}

// ----------------------------- TEMPLATE END ----------------------------------

func gcd(x, y int) int {
	if y == 0 {
		return x
	}
	return gcd(y, x%y)
}

var bit []int

func updateBit(i, v int) {
	for i++; i < len(bit); i += (i & -i) {
		bit[i] = gcd(bit[i], v)
	}
}

func queryBit(i int) int {
	r := 0
	for i++; i > 0; i -= (i & -i) {
		r = gcd(r, bit[i])
	}
	return r
}

var n int
var dp []int

func gcdFrom(i int) int {
	return queryBit(n - i + 1)
}

func main() {
	defer flush()
	scan(&n)

	bit = make([]int, n+10)
	dp = make([]int, n+1)
	var a []int = make([]int, n+1)
	for i := 1; i <= n; i++ {
		scan(&a[i])
		updateBit(n-i+1, a[i])

		lo := 1
		hi := i
		mi := i
		var segGcd, segLen int
		for lo < hi {
			mi = (lo + hi) / 2
			segGcd = gcdFrom(mi)
			segLen = i - mi + 1
			if segGcd < segLen {
				lo = mi + 1
			} else {
				hi = mi
			}
		}
		segGcd = gcdFrom(lo)
		segLen = i - lo + 1
		// for j := 1; j <= i; j++ {
		// 	printf("gcd[%d..%d]=%d\n", j, i, gcdFrom(j))
		// }
		// printf("lo = %d\n", lo)
		if segGcd == segLen {
			// printf("at %d found length of %d. gcd=%d\n", i, segLen, segGcd)
			dp[i] = dp[i-segLen] + 1
		} else {
			dp[i] = dp[i-1]
		}
	}
	for i := 1; i <= n; i++ {
		if i > 1 {
			printf(" ")
		}
		printf("%d", dp[i])
	}
	printf("\n")
}


package main

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

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

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

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

func sint64() int64 {
	s := sstring()
	i := int64(0)
	if s[0] == '-' {
		for _, r := range s[1:] {
			i *= 10
			i -= int64(r - '0')
		}
		return i
	}
	for _, r := range s {
		i *= 10
		i += int64(r - '0')
	}
	return i
}

func sint64s(n int64) []int64 {
	a := make([]int64, n)
	for i := range a {
		a[i] = sint64()
	}
	return a
}

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

func main() {
	defer writer.Flush()
	for t := sint64(); t > 0; t-- {
		n := sint64()
		k := sint64()
		a := sint64s(k)
		t := sint64s(k)
		res := sol(n, k, a, t)
		for _, r := range res {
			printf("%d ", r)
		}
		printf("\n")
	}
}

func sol(n, k int64, a, t []int64) []int64 {
	ret := make([]int64, n)
	p := prep(k, a, t)
	// if k != int64(len(p)) {
	// 	printf("check: %v %v %v\n", n, k, p)
	// }
	nxt := int64(0)
	for i := range ret {
		idx1 := int64(i) + 1
		if nxt == int64(len(p)) {
			ret[i] = p[nxt-1][1] + (idx1 - p[nxt-1][0])
			continue
		}
		if idx1 == p[nxt][0] {
			ret[i] = p[nxt][1]
			nxt++
			continue
		}
		if nxt == 0 {
			ret[i] = p[nxt][1] + (p[nxt][0] - idx1)
			continue
		}
		tmpPrev := p[nxt-1][1] + (idx1 - p[nxt-1][0])
		tmpNxt := p[nxt][1] + (p[nxt][0] - idx1)
		ret[i] = min(tmpPrev, tmpNxt)
	}
	return ret
}

func prep(k int64, a, t []int64) [][2]int64 {
	tmp := make([][2]int64, k) // idx, temp
	for i := range tmp {
		tmp[i] = [2]int64{a[i], t[i]}
	}
	sort.Slice(tmp, func(il, ir int) bool {
		return tmp[il][0] < tmp[ir][0]
	})
	ret := [][2]int64{}
	for i := range tmp {
		for len(ret) > 0 {
			last := ret[len(ret)-1]
			if last[1]-tmp[i][1] > tmp[i][0]-last[0] {
				ret = ret[:len(ret)-1]
			} else {
				break
			}
		}
		if len(ret) > 0 {
			last := ret[len(ret)-1]
			if tmp[i][1]-last[1] > tmp[i][0]-last[0] {
				continue
			}
		}
		ret = append(ret, tmp[i])
	}
	return ret
}

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

package main

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

const MAX_INT int = 2_000_000_000

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var test int
	fmt.Fscan(in, &test)
	for ; test > 0; test-- {
		var n, k int
		fmt.Fscan(in, &n, &k)

		a := make([]int, k)
		t := make([]int, k)
		ar := make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range t {
			fmt.Fscan(in, &t[i])
		}
		for i := range a {
			ar[a[i]-1] = t[i]
		}
		for i := range ar {
			if ar[i] == 0 {
				ar[i] = MAX_INT
			}
		}

		L := make([]int, n)
		R := make([]int, n)
		p := MAX_INT - 1
		for i := 0; i < n; i++ {
			p = Min(p+1, ar[i])
			L[i] = p
		}
		p = MAX_INT - 1
		for i := n - 1; i >= 0; i-- {
			p = Min(p+1, ar[i])
			R[i] = p
		}

		for i := range L {
			if i != 0 {
				fmt.Fprint(out, " ")
			}
			fmt.Fprint(out, Min(L[i], R[i]))
		}
		fmt.Fprintln(out)
	}
}

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

package main

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

var in *bufio.Reader

type airCondi struct {
	idx, t int
}

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

func solve() {
	var n, k int
	fmt.Fscan(in, &n)
	fmt.Fscan(in, &k)

	airs := make([]airCondi, k)
	for i := 0; i < k; i++ {
		fmt.Fscan(in, &airs[i].idx)
		airs[i].idx--
	}
	for i := 0; i < k; i++ {
		fmt.Fscan(in, &airs[i].t)
	}

	sort.Slice(airs, func(i, j int) bool {
		return airs[i].idx < airs[j].idx
	})

	stack := []airCondi{airs[0]}
	for i := 1; i < k; i++ {
		last := stack[len(stack)-1]
		if airs[i].t-airs[i].idx >= last.t-last.idx {
			continue
		}
		for len(stack) > 0 {
			last := stack[len(stack)-1]
			if last.t+last.idx >= airs[i].t+airs[i].idx {
				stack = stack[:len(stack)-1]
			} else {
				break
			}
		}
		stack = append(stack, airs[i])
	}

	i := 0
	for si := 0; si < len(stack); si++ {
		air := stack[si]
		for ; i <= air.idx; i++ {
			curT := air.t + air.idx - i
			if si > 0 {
				last := stack[si-1]
				curT = min(curT, last.t+i-last.idx)
			}
			fmt.Printf("%d ", curT)
		}
	}
	air := stack[len(stack)-1]
	for ; i < n; i++ {
		curT := air.t + i - air.idx
		fmt.Printf("%d ", curT)
	}
	fmt.Println()
}

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

	var q int
	fmt.Fscan(in, &q)

	for i := 0; i < q; i++ {
		solve()
	}
}

package main

import (
	"bufio"
	. "fmt"
	"io"
	"io/ioutil"
	"os"
	"sort"
	"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()
	type pair struct{ p, t, y, z int }

	solve := func(Case int) {
		var n, k int
		Fscan(in, &n, &k)
		a := make([]pair, k)
		for i := range a {
			Fscan(in, &a[i].p)
		}
		for i := range a {
			Fscan(in, &a[i].t)
			a[i].y = a[i].t + a[i].p - 1
			a[i].z = a[i].t + n - a[i].p
		}

		ans := make([]int, n+1)
		for _i := range ans {
			ans[_i] = 2e9
		}
		sort.Slice(a, func(i, j int) bool { return a[i].y < a[j].y })
		cur := 1
		for _, p := range a {
			for cur <= p.p {
				ans[cur] = min(ans[cur], p.t + p.p - cur)
				cur++
			}
		}
		sort.Slice(a, func(i, j int) bool { return a[i].z < a[j].z })
		cur = n
		for _, p := range a {
			for cur >= p.p {
				ans[cur] = min(ans[cur], p.t +cur- p.p )
				cur--
			}
		}

		for _, v := range ans[1:] {
			Fprint(out, v, " ")
		}
		Fprintln(out)
	}

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

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

package main

import (
	"bufio"
	"fmt"
	"os"
)
func min(x,y int)int{
	if x<y{
		return x
	}
	return y
}
func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for c:=1;c<=t;c++{
		var n int
		var k int
		fmt.Fscan(in,&n,&k)
		p := make([]int,n+5)
		l := make([]int,n+5)
		v := make([]int,n+5)
		f := make([]int,n+5)
		b := make([]int,n+5)
		for i:=1;i<=k;i++{
			fmt.Fscan(in,&p[i])
		}
		for i:=1;i<=k;i++{
			fmt.Fscan(in,&v[i])
		}
		for i:=1;i<=k;i++{
			l[p[i]]=v[i]
		}
		for i:=1;i<=n;i++{
			if l[i]!=0&&f[i-1]==0{
				f[i]=l[i]
				continue
			}
			if l[i]!=0&&f[i-1]!=0{
				f[i] = min(f[i-1]+1,l[i])
				continue
			}
			if f[i-1]!=0{
				f[i] = f[i-1]+1
			}
		}
		for i:=n;i>=1;i--{
			if l[i]!=0 && b[i+1]==0{
				b[i] = l[i]
				continue
			}
			if l[i]!=0&&b[i+1]!=0{
				b[i] = min(b[i+1]+1,l[i])
				continue
			}
			if b[i+1]!=0{
				b[i]=b[i+1]+1
			}
		}
		for i:=1;i<=n;i++{
			var ans int
			if f[i]==0{
				ans = b[i]
			}
			if b[i]==0{
				ans = f[i]
			}
			if f[i]!=0&&b[i]!=0{
				ans = min(f[i],b[i])
			}
			fmt.Fprintf(out,"%d ", ans)
		}
		fmt.Fprintf(out,"\n")
	}
}

package main

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

const MAX_INT int = 1_000_000_100

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var test int
	fmt.Fscan(in, &test)
	for ; test > 0; test-- {
		var n, k int
		fmt.Fscan(in, &n, &k)

		a := make([]int, k)
		t := make([]int, k)
		ar := make([]int, n)
		for i := range a {
			fmt.Fscan(in, &a[i])
		}
		for i := range t {
			fmt.Fscan(in, &t[i])
		}
		for i := range a {
			ar[a[i]-1] = t[i]
		}
		for i := range ar {
			if ar[i] == 0 {
				ar[i] = MAX_INT
			}
		}

		L := make([]int, n)
		R := make([]int, n)
		p := MAX_INT - 1
		for i := 0; i < n; i++ {
			p = Min(p+1, ar[i])
			L[i] = p
		}
		p = MAX_INT - 1
		for i := n - 1; i >= 0; i-- {
			p = Min(p+1, ar[i])
			R[i] = p
		}

		for i := range L {
			if i != 0 {
				fmt.Fprint(out, " ")
			}
			fmt.Fprint(out, Min(L[i], R[i]))
		}
		fmt.Fprintln(out)
	}
}

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