Screen resolution of Polycarp's monitor is $$$a \times b$$$ pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates $$$(x, y)$$$ ($$$0 \le x < a, 0 \le y < b$$$). You can consider columns of pixels to be numbered from $$$0$$$ to $$$a-1$$$, and rows — from $$$0$$$ to $$$b-1$$$.Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.

In the first line you are given an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. In the next lines you are given descriptions of $$$t$$$ test cases. Each test case contains a single line which consists of $$$4$$$ integers $$$a, b, x$$$ and $$$y$$$ ($$$1 \le a, b \le 10^4$$$; $$$0 \le x < a$$$; $$$0 \le y < b$$$) — the resolution of the screen and the coordinates of a dead pixel. It is guaranteed that $$$a+b>2$$$ (e.g. $$$a=b=1$$$ is impossible).

Print $$$t$$$ integers — the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.

NoteIn the first test case, the screen resolution is $$$8 \times 8$$$, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window.

// A. Dead Pixel
/*
Screen resolution of Polycarp's monitor is 𝑎×𝑏 pixels. Unfortunately, there is one dead pixel at his screen. It has coordinates (𝑥,𝑦) (0≤𝑥<𝑎,0≤𝑦<𝑏). You can consider columns of pixels to be numbered from 0 to 𝑎−1, and rows — from 0 to 𝑏−1.

Polycarp wants to open a rectangular window of maximal size, which doesn't contain the dead pixel. The boundaries of the window should be parallel to the sides of the screen.

Print the maximal area (in pixels) of a window that doesn't contain the dead pixel inside itself.

Input

In the first line you are given an integer 𝑡 (1≤𝑡≤104) — the number of test cases in the test. In the next lines you are given descriptions of 𝑡 test cases.

Each test case contains a single line which consists of 4 integers 𝑎,𝑏,𝑥 and 𝑦 (1≤𝑎,𝑏≤104; 0≤𝑥<𝑎; 0≤𝑦<𝑏) — the resolution of the screen and the coordinates of a dead pixel. It is guaranteed that 𝑎+𝑏>2 (e.g. 𝑎=𝑏=1 is impossible).

Output
Print 𝑡 integers — the answers for each test case. Each answer should contain an integer equal to the maximal possible area (in pixels) of a rectangular window, that doesn't contain the dead pixel.

Example
Input
6
8 8 0 0
1 10 0 3
17 31 10 4
2 1 0 0
5 10 3 9
10 10 4 8

Output
56
6
442
1
45
80

Note
In the first test case, the screen resolution is 8×8, and the upper left pixel is a dead pixel. Here you can see one of two possible layouts of the maximal window.
*/
package main

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

func main() {
	reader := bufio.NewReader(os.Stdin)
	input1, _ := reader.ReadString('\n')
	t, _ := strconv.Atoi(strings.TrimSpace(input1))

	for ; t > 0; t-- {
		input2, _ := reader.ReadString('\n')
		arr := strings.Split(strings.TrimSpace(input2), " ")

		a, _ := strconv.Atoi(arr[0])
		b, _ := strconv.Atoi(arr[1])
		x, _ := strconv.Atoi(arr[2])
		y, _ := strconv.Atoi(arr[3])

		fmt.Println(max(max(x, a-1-x)*b, max(y, b-y-1)*a))
	}
}

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

package main

import "fmt"

func main() {
	var cases int
	fmt.Scanf("%d\n", &cases)
	for i := 0; i < cases; i++ {
		var a, b, x, y int
		fmt.Scanf("%d %d %d %d\n", &a, &b, &x, &y)
		s1 := b * (x)
		s2 := a * (y)
		s3 := (a - x - 1) * b
		s4 := (b - y - 1) * a
		max := s1
		if max < s2 {
			max = s2
		}
		if max < s3 {
			max = s3
		}
		if max < s4 {
			max = s4
		}
		fmt.Println(max)
	}
}

package main

import "fmt"
import "os"
import "bufio"
import "strings"
import "strconv"

func main() {
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    line := scanner.Text()
    t, _ := strconv.Atoi(line)
    for i := 0; i < t; i++ {
        scanner.Scan()
        line = scanner.Text()
        parts := strings.Split(line, " ")
        a, _ := strconv.Atoi(parts[0])
        b, _ := strconv.Atoi(parts[1])
        x, _ := strconv.Atoi(parts[2])
        y, _ := strconv.Atoi(parts[3])
        //~ fmt.Println(a, b, x, y)

        area_l := x * b
        area_r := (a - 1 - x) * b
        area_t := y * a
        area_b := (b - 1 - y) * a

        max := area_l
        if area_r > max {
            max = area_r
        }
        if area_t > max {
            max = area_t
        }
        if area_b > max {
            max = area_b
        }

        fmt.Println(max)
    }
}

package main

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

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

	var tc int
	fmt.Fscanf(r, "%d\n", &tc)

	for t := 0; t < tc; t++ {
		var a, b, x, y int
		fmt.Fscanf(r, "%d %d %d %d\n", &a, &b, &x, &y)
		fmt.Fprintf(w, "%d\n", findBestRect(a, b, x, y))
	}
}

func findBestRect(a, b, x, y int) int {
	r := 0
	if y-1 >= 0 {
		r = max(r, y*a)
	}
	if y+1 < b {
		r = max(r, (b-y-1)*a)
	}
	if x+1 < a {
		r = max(r, (a-x-1)*b)
	}
	if x-1 >= 0 {
		r = max(r, x*b)
	}

	return r
}

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

package main

import "fmt"

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

func main() {
	var t int
	fmt.Scan(&t)
	for i := 0; i < t; i++ {
		var a, b, x, y int
		fmt.Scan(&a, &b, &x, &y)
		rls := 0
		rls = max(rls, a*y)
		rls = max(rls, a*(b-y-1))
		rls = max(rls, b*x)
		rls = max(rls, b*(a-x-1))
		fmt.Println(rls)
	}
}

package main

import (
	"fmt"
)

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

	return a
}

func main() {
	var t, a, b, x, y int
	_, _ = fmt.Scanf("%v\n", &t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Scanf("%v %v %v %v\n", &a, &b, &x, &y)
		_, _ = fmt.Printf("%v\n", Max(b*Max(x, a-x-1), a*Max(y, b-y-1)))
	}
}

package main

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

func Max(a, b int32) int32 {
	if b > a {
		return b
	}

	return a
}

func main() {
	var t, a, b, x, y int32
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	_, _ = fmt.Fscanf(in, "%d\n", &t)
	for i := int32(0); i < t; i++ {
		_, _ = fmt.Fscanf(in, "%v %v %v %v\n", &a, &b, &x, &y)
		_, _ = fmt.Fprintf(out, "%v\n", Max(b*Max(x, a-x-1), a*Max(y, b-y-1)))
	}
	_ = out.Flush()
}

package main

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

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

	return a
}

func main() {
	var t, a, b, x, y int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	_, _ = fmt.Fscanf(in, "%v\n", &t)
	for i := 0; i < t; i++ {
		_, _ = fmt.Fscanf(in, "%v %v %v %v\n", &a, &b, &x, &y)
		_, _ = fmt.Fprintf(out, "%v\n", Max(b*Max(x, a-x-1), a*Max(y, b-y-1)))
	}
	_ = out.Flush()
}


Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed!Vasya knows that the matrix consists of n rows and m columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence a1, a2, ..., an denotes the xor of elements in rows with indices 1, 2, ..., n, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence b1, b2, ..., bm denotes the xor of elements in columns with indices 1, 2, ..., m, respectively.Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix.

The first line contains two numbers n and m (2 ≤ n, m ≤ 100) — the dimensions of the matrix. The second line contains n numbers a1, a2, ..., an (0 ≤ ai ≤ 109), where ai is the xor of all elements in row i. The third line contains m numbers b1, b2, ..., bm (0 ≤ bi ≤ 109), where bi is the xor of all elements in column i.

If there is no matrix satisfying the given constraints in the first line, output "NO". Otherwise, on the first line output "YES", and then n rows of m numbers in each ci1, ci2, ... , cim (0 ≤ cij ≤ 2·109) — the description of the matrix. If there are several suitable matrices, it is allowed to print any of them.

package main

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

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

	var n, m, s, s0 int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		s ^= a[i]
		if i > 0 {
			s0 ^= a[i]
		}
	}
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
		s ^= b[i]
	}

	if s > 0 {
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	Fprint(out, s0^b[0])
	for j := 1; j < m; j++ {
		Fprint(out, " ", b[j])
	}
	Fprintln(out)
	for i := 1; i < n; i++ {
		Fprint(out, a[i])
		for j := 1; j < m; j++ {
			Fprint(out, " 0")
		}
		Fprintln(out)
	}
}

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

package main

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

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

	var n, m, s, s0 int
	Fscan(in, &n, &m)
	a := make([]int, n)
	for i := range a {
		Fscan(in, &a[i])
		s ^= a[i]
		if i > 0 {
			s0 ^= a[i]
		}
	}
	b := make([]int, m)
	for i := range b {
		Fscan(in, &b[i])
		s ^= b[i]
	}

	if s > 0 {
		Fprint(out, "NO")
		return
	}
	Fprintln(out, "YES")
	Fprint(out, s0^b[0])
	for j := 1; j < m; j++ {
		Fprint(out, " ", b[j])
	}
	Fprintln(out)
	for i := 1; i < n; i++ {
		Fprintln(out, a[i], strings.Repeat(" 0", m-1))
	}
}

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


The Government of Mars is not only interested in optimizing space flights, but also wants to improve the road system of the planet.One of the most important highways of Mars connects Olymp City and Kstolop, the capital of Cydonia. In this problem, we only consider the way from Kstolop to Olymp City, but not the reverse path (i. e. the path from Olymp City to Kstolop).The road from Kstolop to Olymp City is $$$\ell$$$ kilometers long. Each point of the road has a coordinate $$$x$$$ ($$$0 \le x \le \ell$$$), which is equal to the distance from Kstolop in kilometers. So, Kstolop is located in the point with coordinate $$$0$$$, and Olymp City is located in the point with coordinate $$$\ell$$$.There are $$$n$$$ signs along the road, $$$i$$$-th of which sets a speed limit $$$a_i$$$. This limit means that the next kilometer must be passed in $$$a_i$$$ minutes and is active until you encounter the next along the road. There is a road sign at the start of the road (i. e. in the point with coordinate $$$0$$$), which sets the initial speed limit.If you know the location of all the signs, it's not hard to calculate how much time it takes to drive from Kstolop to Olymp City. Consider an example:  Here, you need to drive the first three kilometers in five minutes each, then one kilometer in eight minutes, then four kilometers in three minutes each, and finally the last two kilometers must be passed in six minutes each. Total time is $$$3\cdot 5 + 1\cdot 8 + 4\cdot 3 + 2\cdot 6 = 47$$$ minutes.To optimize the road traffic, the Government of Mars decided to remove no more than $$$k$$$ road signs. It cannot remove the sign at the start of the road, otherwise, there will be no limit at the start. By removing these signs, the Government also wants to make the time needed to drive from Kstolop to Olymp City as small as possible.The largest industrial enterprises are located in Cydonia, so it's the priority task to optimize the road traffic from Olymp City. So, the Government of Mars wants you to remove the signs in the way described above.

The first line contains three integers $$$n$$$, $$$\ell$$$, $$$k$$$ ($$$1 \le n \le 500$$$, $$$1 \le \ell \le 10^5$$$, $$$0 \le k \le n-1$$$), the amount of signs on the road, the distance between the cities and the maximal number of signs you may remove. The second line contains $$$n$$$ integers $$$d_i$$$ ($$$d_1 = 0$$$, $$$d_i < d_{i+1}$$$, $$$0 \le d_i \le \ell - 1$$$) — coordinates of all signs. The third line contains $$$n$$$ integers $$$a_i$$$ ($$$1 \le a_i \le 10^4$$$) — speed limits.

Print a single integer — minimal possible time to drive from Kstolop to Olymp City in minutes, if you remove no more than $$$k$$$ road signs.

NoteIn the first example, you cannot remove the signs. So the answer is $$$47$$$, as it's said in the statements above.In the second example, you may remove the second and the fourth sign. In this case, you need to drive four kilometers in $$$4\cdot5 = 20$$$ minutes, and then six kilometers in $$$6\cdot3 = 18$$$, so the total time is $$$4\cdot5 + 6\cdot3 = 38$$$ minutes.

/**
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"
    "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
}

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
}


The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day.There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.

The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.

If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).

NoteIn the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.

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++ {
		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"
	"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")

}


While Vasya finished eating his piece of pizza, the lesson has already started. For being late for the lesson, the teacher suggested Vasya to solve one interesting problem. Vasya has an array a and integer x. He should find the number of different ordered pairs of indexes (i, j) such that ai ≤ aj and there are exactly k integers y such that ai ≤ y ≤ aj and y is divisible by x.In this problem it is meant that pair (i, j) is equal to (j, i) only if i is equal to j. For example pair (1, 2) is not the same as (2, 1).

The first line contains 3 integers n, x, k (1 ≤ n ≤ 105, 1 ≤ x ≤ 109, 0 ≤ k ≤ 109), where n is the size of the array a and x and k are numbers from the statement. The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.

Print one integer — the answer to the problem.

NoteIn first sample there are only three suitable pairs of indexes — (1, 2), (2, 3), (3, 4).In second sample there are four suitable pairs of indexes(1, 1), (2, 2), (3, 3), (4, 4).In third sample every pair (i, j) is suitable, so the answer is 5 * 5 = 25.

package main

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

const N = 100010
var n, x, k int
var a[] int

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

	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &k)

	a = make([]int, n)

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

	sort.Ints(a)

	var ret = int64(0)

	for i := 0; i < n; i++ {
		var L, R int64

		var bound = a[i] - (a[i] % x)

		if k == 0 {
			L = int64(bound) + 1
			R = int64(a[i])
		} else {
			R = int64(bound) - int64(k - 1) * int64(x)
			L = R - int64(x) + 1
		}

		if L > R {
			continue
		}

		var low_pos = -1
		var up_pos = -1

		var l = 0
		var r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if int64(a[mid]) >= L {
				low_pos = mid
				r = mid - 1
			} else {
				l = mid + 1
			}
		}

		l = 0
		r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if int64(a[mid]) <= R {
				up_pos = mid
				l = mid + 1
			} else {
				r = mid - 1
			}
		}

		if low_pos != -1 && up_pos != -1 && low_pos <= up_pos {
			ret += int64(up_pos - low_pos + 1)
		}
	}
	fmt.Fprintf(writer, "%d", ret)
}

package main

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

const N = 100010
var n, x, k int
var a[] int

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

	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &k)

	a = make([]int, n)

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

	sort.Ints(a)

	var ret = int64(0)

	for i := 0; i < n; i++ {
		var bound = a[i] - a[i] % x

		//fmt.Println(bound, x * k)
		//fmt.Println(bound, x * (k - 1))

		var R = bound - x * (k - 1)
		var L = bound - x * k + 1

		if k == 0 {
			L = bound + 1
			R = a[i]
			if a[i] % x == 0 {
				R--
			}
		}

		//fmt.Println(L, R)

		var low_pos = -1
		var up_pos = -1

		var l = 0
		var r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if a[mid] >= L {
				low_pos = mid
				r = mid - 1
			} else {
				l = mid + 1
			}
		}

		l = 0
		r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if a[mid] <= R {
				up_pos = mid
				l = mid + 1
			} else {
				r = mid - 1
			}
		}

		if low_pos != -1 && up_pos != -1 && low_pos <= up_pos {
			ret += int64(up_pos - low_pos + 1)
		}
	}
	fmt.Fprintf(writer, "%d", ret)
}

package main

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

const N = 100010
var n, x, k int
var a[] int

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

	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &k)

	a = make([]int, n)

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

	sort.Ints(a)

	var ret = int64(0)

	for i := 0; i < n; i++ {
		var bound = a[i] - a[i] % x

		var R = bound - x * (k - 1)
		var L = bound - x * k + 1

		if k == 0 {
			L = bound + 1
			R = a[i]
			if a[i] % x == 0 {
				R--
			}
		}

		var low_pos = -1
		var up_pos = -1

		var l = 0
		var r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if a[mid] >= L {
				low_pos = mid
				r = mid - 1
			} else {
				l = mid + 1
			}
		}

		l = 0
		r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if a[mid] <= R {
				up_pos = mid
				l = mid + 1
			} else {
				r = mid - 1
			}
		}

		if low_pos != -1 && up_pos != -1 {
			ret += int64(up_pos - low_pos + 1)
		}
	}
	fmt.Fprintf(writer, "%d", ret)
}

package main

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

const N = 100010
var n, x, k int
var a[] int

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

	fmt.Fscanf(reader, "%d %d %d\n", &n, &x, &k)

	a = make([]int, n)

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

	sort.Ints(a)

	var ret = int64(0)

	for i := 0; i < n; i++ {
		var bound = a[i] - a[i] % x
		var R = bound - x * (k - 1)
		var L = bound - x * k + 1

		var low_pos = -1
		var up_pos = -1

		var l = 0
		var r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if a[mid] >= L {
				low_pos = mid
				r = mid - 1
			} else {
				l = mid + 1
			}
		}

		l = 0
		r = n - 1
		for l <= r {
			var mid = (l + r) >> 1
			if a[mid] <= R {
				up_pos = mid
				l = mid + 1
			} else {
				r = mid - 1
			}
		}

		if low_pos != -1 && up_pos != -1 {
			ret += int64(up_pos - low_pos + 1)
		}
	}
	fmt.Fprintf(writer, "%d", ret)
}


You are given a string $$$s$$$ consisting only of characters + and -. You perform some process with this string. This process can be described by the following pseudocode: res = 0for init = 0 to inf    cur = init    ok = true    for i = 1 to |s|        res = res + 1        if s[i] == '+'            cur = cur + 1        else            cur = cur - 1        if cur < 0            ok = false            break    if ok        breakNote that the $$$inf$$$ denotes infinity, and the characters of the string are numbered from $$$1$$$ to $$$|s|$$$.You have to calculate the value of the $$$res$$$ after the process ends.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The only lines of each test case contains string $$$s$$$ ($$$1 \le |s| \le 10^6$$$) consisting only of characters + and -. It's guaranteed that sum of $$$|s|$$$ over all test cases doesn't exceed $$$10^6$$$.

For each test case print one integer — the value of the $$$res$$$ after the process ends.

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
}

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

}