You are an upcoming movie director, and you have just released your first movie. You have also launched a simple review site with two buttons to press — upvote and downvote.However, the site is not so simple on the inside. There are two servers, each with its separate counts for the upvotes and the downvotes.$$$n$$$ reviewers enter the site one by one. Each reviewer is one of the following types:   type $$$1$$$: a reviewer has watched the movie, and they like it — they press the upvote button;  type $$$2$$$: a reviewer has watched the movie, and they dislike it — they press the downvote button;  type $$$3$$$: a reviewer hasn't watched the movie — they look at the current number of upvotes and downvotes of the movie on the server they are in and decide what button to press. If there are more downvotes than upvotes, then a reviewer downvotes the movie. Otherwise, they upvote the movie. Each reviewer votes on the movie exactly once.Since you have two servers, you can actually manipulate the votes so that your movie gets as many upvotes as possible. When a reviewer enters a site, you know their type, and you can send them either to the first server or to the second one.What is the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of reviewers. The second line of each testcase contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$1 \le r_i \le 3$$$) — the types of the reviewers in the same order they enter the site.

For each testcase print a single integer — the maximum total number of upvotes you can gather over both servers if you decide which server to send each reviewer to.

NoteIn the first testcase of the example you can send the only reviewer to either of the servers — they'll downvote anyway. The movie won't receive any upvotes.In the second testcase of the example you can send all reviewers to the first server:   the first reviewer upvotes;  the second reviewer downvotes;  the last reviewer sees that the number of downvotes is not greater than the number of upvotes — upvote themselves. There are two upvotes in total. Alternatevely, you can send the first and the second reviewers to the first server and the last reviewer — to the second server:   the first reviewer upvotes on the first server;  the second reviewer downvotes on the first server;  the last reviewer sees no upvotes or downvotes on the second server — upvote themselves.

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	fmt.Fscanf(in, "%d\n", &t)
	for case_idx := 0; case_idx < t; case_idx++ {
		var n int
		fmt.Fscanf(in, "%d\n", &n)
		upvote := 0
		var rType int
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &rType)
			if rType == 1 || rType == 3 {
				upvote++
			}
		}
		fmt.Fscanf(in, "\n", &rType)
		fmt.Fprintf(out, "%d\n", upvote)
	}
	out.Flush()

}

package main

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

func solve(r *bufio.Reader) {
	var n int
	fmt.Fscan(r, &n)
	total := 0
	var tmp int
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &tmp)
		if tmp != 2 {
			total++
		}
	}
	fmt.Println(total)
}
func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	for i := 0; i < n; i++ {
		solve(r)
	}
}

 package main

 import "fmt"
 import "bufio"
 import "os"

 func main() {
     var in *bufio.Reader = bufio.NewReader(os.Stdin)
     var out *bufio.Writer = bufio.NewWriter(os.Stdout)

     defer out.Flush()

     var T, n, a int
     fmt.Fscan(in, &T)
     for ; T > 0; T-- {
         fmt.Fscan(in, &n)
         var ans int
         for i := 0; i < n; i++ {
             fmt.Fscan(in, &a)
             if (a & 1) == 1 {
                 ans++
             }
         }
         fmt.Fprintln(out, ans)
     }
     return
 }

package main

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

type CF1511A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1511A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1511A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1511A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1511A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1511A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1511A
Date: 21/11/21
User: epradere
URL: https://codeforces.com/problemset/problem/1511/A
Problem: CF1511A
**/
func (in *CF1511A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt()
		s1 := 0
		s2 := 0
		for i := 0; i < n; i++ {
			tp := in.NextInt()
			if tp == 1 {
				s1++
			} else if tp == 2 {
				s2--
			} else {
				if s1 >= 0 {
					s1++
				} else {
					s2--
				}
			}
		}

		ans := 0
		if s1 > 0 {
			ans += s1
		}
		if s2 > 0 {
			ans += s2
		}
		fmt.Println(ans)

	}
}

func NewCF1511A(r *bufio.Reader) *CF1511A {
	return &CF1511A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1511A(bufio.NewReader(os.Stdin)).Run()
}

package main
import (
    "os"
    "fmt"
    "bufio"
)
var rd = bufio.NewReader(os.Stdin)
func main() {
    q := 0
    fmt.Fscan(rd, &q)
    for t := 0; t < q; t++ {
        n := 0
        fmt.Fscan(rd, &n)
        a := make([]int, n)
        for i := range a {
            fmt.Fscan(rd, &a[i])
        }
        res := 0
        for _, v := range a {
            if v != 2 {
                res++
            }
        }
        fmt.Println(res)
    }
}

package main
import(
    "fmt"
    "bufio"
    "os"
)
func main() {
	in:= bufio.NewReader(os.Stdin)
	out:= bufio.NewWriter(os.Stdout)
	defer out.Flush()
	var t int
	fmt.Fscan(in,&t)
	for i:=0;i<t;i++{
		var n int
		fmt.Fscan(in,&n)
		counter:=0
		for j:=0;j<n;j++{
			var x int
			fmt.Fscan(in,&x)
			if x == 1 || x==3 {
				counter++
			}
		}
		fmt.Fprintln(out,counter)
	}
}

package main

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

var (
	reader = bufio.NewReader(os.Stdin)
	t, n   int
)

func solve(n int, r []int) int {
	var c int
	for _, x := range r {
		if x != 2 {
			c++
		}
	}
	return c
}

func main() {
	fmt.Fscan(reader, &t)
	for ; t > 0; t-- {
		fmt.Fscan(reader, &n)
		r := make([]int, n)
		for i := 0; i < n; i++ {
			fmt.Fscan(reader, &r[i])
		}
		fmt.Println(solve(n, r))
	}
}

package main

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

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

	solve := func() {
		var n int
		ans := 0
		fmt.Fscan(in, &n)
		for i := 0; i < n; i++ {
			var x int
			fmt.Fscan(in, &x)
			if x != 2 {
				ans++
			}
		}
		fmt.Fprintln(out, ans)
	}

	T := 1
	fmt.Fscan(in, &T)
	for o := 0; o < T; o++ {
		solve()
	}

}

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

package main

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

var bytes []byte

func nextByte() byte {
	b := bytes[0]
	bytes = bytes[1:]
	return b
}

func ScanInt64() int64 {
	var negative bool
	var result int64

	b := nextByte()

	for ; b < '0' || b > '9'; b = nextByte() {
		if b == '-' {
			negative = true
		}
	}

	for ; '0' <= b && b <= '9'; b = nextByte() {
		result = (result << 3) + (result << 1) + int64(b-'0')
	}

	if negative {
		return -result
	} else {
		return result
	}
}

func ScanInt() int {
	return int(ScanInt64())
}

func ScanString() string {
	builder := strings.Builder{}

	for b := nextByte(); b != '\r'; b = nextByte() {
		if b != '\n' {
			builder.WriteByte(b)
		}
	}

	return builder.String()
}

func init() {
	f, err := os.Open("in.txt")

	if err == nil {
		defer f.Close()
	} else {
		f = os.Stdin
	}

	bytes, _ = ioutil.ReadAll(bufio.NewReader(f))
}

func main() {
	for t := ScanInt(); t > 0; t-- {
		n := ScanInt()
		upvotes := 0

		for i := 0; i < n; i++ {
			r := ScanInt()

			if r != 2 {
				upvotes++
			}
		}

		fmt.Println(upvotes)
	}
}

package main

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

type Scanner struct {
	bScanner *bufio.Scanner
}

func NewScanner() *Scanner {
	bScanner := bufio.NewScanner(os.Stdin)
	bScanner.Split(bufio.ScanWords)
	bScanner.Buffer([]byte{}, math.MaxInt32)
	return &Scanner{
		bScanner,
	}
}
func (s *Scanner) Next() string {
	s.bScanner.Scan()
	return s.bScanner.Text()
}
func (s *Scanner) NextInt() int {
	s.bScanner.Scan()
	res, err := strconv.Atoi(s.bScanner.Text())
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInt64() int64 {
	s.bScanner.Scan()
	res, err := strconv.ParseInt(s.bScanner.Text(), 10, 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextFloat() float64 {
	s.bScanner.Scan()
	res, err := strconv.ParseFloat(s.bScanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return res
}
func (s *Scanner) NextInts(N int) []int {
	res := make([]int, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt()
	}
	return res
}
func (s *Scanner) NextInt64s(N int) []int64 {
	res := make([]int64, N)
	for i := 0; i < N; i++ {
		res[i] = s.NextInt64()
	}
	return res
}
func Min(nums ...int) int {
	ans := math.MaxInt32
	for _, num := range nums {
		if num < ans {
			ans = num
		}
	}
	return ans
}
func Max(nums ...int) int {
	ans := math.MinInt32
	for _, num := range nums {
		if num > ans {
			ans = num
		}
	}
	return ans
}

func main() {
	sc := NewScanner()
	wr := bufio.NewWriter(os.Stdout)
	defer wr.Flush()

	TC := sc.NextInt()
	for tc := 0; tc < TC; tc++ {
		N := sc.NextInt()
		up := 0
		for i := 0; i < N; i++ {
			reviewerType := sc.NextInt()
			if reviewerType != 2 {
				up++
			}
		}
		fmt.Fprintln(wr, up)
	}

}

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

func readIntFromLine(s string) int64 {
        if v, err := strconv.ParseInt(s, 10, 64); err == nil {
                return v
        }
        return 0
}

func readIntsFromLine(s string) []int64 {
        nums := strings.Split(s, " ")
        var o []int64
        for _, n := range nums {
                o = append(o, readIntFromLine(n))
        }
        return o
}

func main() {
        s := bufio.NewScanner(os.Stdin)
        for s.Scan() {
                numTc := readIntFromLine(s.Text())
                for i := int64(0); i < numTc; i++ {
                        s.Scan()
                        //viewers := readIntFromLine(s.Text())
                        s.Scan()
                        reviews := readIntsFromLine(s.Text())
                        up, down := 0, 0
                        for _, r := range reviews {
                                if r == 2 {
                                        down++
                                } else {
                                        up++
                                }

                        }
                        fmt.Println(up)
                }
        }

/*
        var numTc int
        fmt.Scanf("%d", &numTc)
        for i  := 0; i < numTc; i++ {
                up, down, viewers := 0, 0, 0
                fmt.Scanf("%d", &viewers)
                for j := 0; j < viewers; j++ {
                        vType := 0
                        fmt.Scanf("%d", &vType)
                        switch  vType {
                                case 1:
                                        up++
                                case 2:
                                        down++
                                default:
                                        up++
                        }
                }
                fmt.Sprintf("%d", up)

        }
        */
}

package main

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

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

func main() {
	defer out.Flush()

	var t int
	Fscan(in, &t)

	for cases := 0; cases < t; cases++ {
		var n, answer int
		Fscan(in, &n)
		for i := 0; i < n; i++ {
			var temp int
			Fscan(in, &temp)
			if temp == 1 || temp == 3 {
				answer++
			}
		}

		Fprintln(out, answer)
	}
}

package main

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

// var m []int
// err = json.Unmarshal([]byte(r.FormValue("example")), &m)

// for i := 0; i < len(m); i++ {
// 	fmt.Println(m[i])
// }

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

func MKO(x, y int64) int64 {
	if x < y {
		return 0
	} else {
		return x - y + 1
	}
}

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

func LCM(x, y int) int {
	if x >= y {
		initial := x
		for i := 1; i < y+2; i++ {
			if initial%y == 0 {
				break
			} else {
				initial = initial + x
			}

		}
		return initial
	} else {
		initial := y
		for i := 1; i < x+2; i++ {
			if initial%x == 0 {
				break
			} else {
				initial = initial + y
			}

		}
		return initial
	}
}

func GCD(x, y int64) int64 {
	var l, q int64
	l = MAX(x, y)
	q = MIN(x, y)
	var m int64
	for l*q > 0 {
		a := l % q
		b := l - a
		cc := int64(b / q)
		remain := l - cc*q
		l = MAX(remain, q)
		q = MIN(remain, q)
		if l*q == 0 {
			m = l + q
			break
		}

	}
	return m

}

func IsItPrime(x int) bool {
	if x < 10 {
		if x == 2 || x == 3 || x == 5 || x == 7 {
			return true
		} else {
			return false
		}
	} else {
		j := 0
		k := math.Sqrt(float64(x))
		for m := 3; m <= int(k); {
			if x%m == 0 {
				j = 1
				break
			}
			m = m + 2
		}
		if j == 1 {
			return false
		} else {
			return true
		}
	}

}

// sort.Slice(arr, func(i, j int) bool {
// 	return arr[i] > arr[j]
// })
var t int

func main() {
	var input io.Reader = bufio.NewReader(os.Stdin)
	fmt.Fscan(input, &t)

	for i := 0; i < t; i++ {
		var a int
		fmt.Fscan(input, &a)
		ans := 0
		for i := 0; i < a; i++ {
			var q int
			fmt.Fscan(input, &q)
			if q == 2 {
				ans++
			}

		}
		fmt.Println(a - ans)
	}

}

package main

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

var io *IO

type IO struct {
	*bufio.ReadWriter
}

func init() {
	io = &IO{irw()}
}

func irw(size ...int) *bufio.ReadWriter {
	if len(size) == 0 {
		return bufio.NewReadWriter(ir(), iw())
	}
	return bufio.NewReadWriter(ir(size[0]), iw(size[1]))
}

func ir(size ...int) *bufio.Reader {
	if len(size) == 0 {
		return bufio.NewReader(os.Stdin)
	}
	return bufio.NewReaderSize(os.Stdin, size[0])
}

func iw(size ...int) *bufio.Writer {
	if len(size) == 0 {
		return bufio.NewWriter(os.Stdout)
	}
	return bufio.NewWriterSize(os.Stdin, size[0])
}

func main() {
	T := rI()
	for t := 0; t < T; t++ {
		n := rI()
		ns := ris()
		cnt := 0
		for i := 0; i < n; i++ {
			if ns[i] == 2 {
				cnt++
			}
		}
		fmt.Println(n - cnt)
	}
}

func rl() string {
	l, _, _ := io.ReadLine()
	return string(l)
}

func ri() int64 {
	s := rl()
	i64, _ := strconv.ParseInt(s, 10, 64)
	return i64
}

func rI() int {
	return int(ri())
}

func ris(sep ...string) []int64 {
	s := rl()
	var splits []string
	if sep == nil {
		splits = strings.Split(s, " ")
	} else {
		splits = strings.Split(s, sep[0])
	}
	ns := make([]int64, len(splits))
	for i, split := range splits {
		i64, _ := strconv.ParseInt(split, 10, 64)
		ns[i] = i64
	}
	return ns
}

package main

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

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

	solve := func(Case int) {
		var n,c int
		Fscan(in, &n)
		a := make([]int, n)
		for i := range a {
			Fscan(in, &a[i])
			if a[i] != 2 {
				c++
			}
		}
		Fprintln(out, c)
	}

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

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

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

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	for case_idx := 0; case_idx < t; case_idx++ {
		var n int
		fmt.Fscanf(in, "%d\n", &n)
		upvote := 0
		var rType int
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &rType)
			if rType == 1 || rType == 3 {
				upvote++
			}
		}
		fmt.Fprintf(out, "%d\n", upvote)
	}

}

package main

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

func main() {
	var t int
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)

	for case_idx := 0; case_idx < t; case_idx++ {
		var n int
		fmt.Fscanf(in, "%d\n", &n)
		upvote := 0
		var rType int
		for i := 0; i < n; i++ {
			fmt.Fscanf(in, "%d", &rType)
			if rType == 1 || rType == 3 {
				upvote++
			}
		}
		fmt.Fscanf(in, "\n")
		fmt.Fprintf(out, "%d\n", upvote)
	}

}

package main

import "fmt"

func main() {
	var t int
	fmt.Scanf("%d\n", &t)
	for case_idx := 0; case_idx < t; case_idx++ {
		var n int
		fmt.Scanf("%d\n", &n)
		upvote := 0
		var rType int
		for i := 0; i < n; i++ {
			fmt.Scanf("%d", &rType)
			if rType == 1 || rType == 3 {
				upvote++
			}
		}
		fmt.Printf("%d\n", upvote)
	}

}

package main

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

func solve(r *bufio.Reader) {
	var n int
	fmt.Fscan(r, &n)
	total := 0
	var tmp int
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &tmp)
		if tmp == 1 || ((total != 0 || i == 0) && tmp == 3) {
			total++
		}
	}
	fmt.Println(total)
}
func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	for i := 0; i < n; i++ {
		solve(r)
	}
}

package main

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

func solve(r *bufio.Reader) {
	var n int
	fmt.Fscan(r, &n)
	total := 0
	var tmp int
	for i := 0; i < n; i++ {
		fmt.Fscan(r, &tmp)
		if tmp != 2 && total != 0 {
			total++
		}
	}
	fmt.Println(total)
}
func main() {
	r := bufio.NewReader(os.Stdin)
	var n int
	fmt.Fscan(r, &n)
	for i := 0; i < n; i++ {
		solve(r)
	}
}

 package main

 import "fmt"

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

 package main

 import "fmt"

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

package main

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

type CF1511A struct {
	sc        *bufio.Reader
	split     []string
	index     int
	separator string
}

func (in *CF1511A) GetLine() string {
	line, err := in.sc.ReadString('\n')
	if err != nil {
		fmt.Println("error line:", line, " err: ", err)
	}
	in.split = []string{}
	in.index = 0
	return line
}
func (in *CF1511A) load() {
	if len(in.split) <= in.index {
		in.split = strings.Split(in.GetLine(), in.separator)
		in.index = 0
	}
}

func (in *CF1511A) NextInt() int {
	in.load()
	val, _ := strconv.Atoi(strings.TrimSpace(in.split[in.index]))
	in.index++
	return val
}

func (in *CF1511A) NextInt64() int64 {
	in.load()
	val, _ := strconv.ParseInt(strings.TrimSpace(in.split[in.index]), 10, 64)
	in.index++
	return val
}

func (in *CF1511A) NextString() string {
	in.load()
	val := strings.TrimSpace(in.split[in.index])
	in.index++
	return val
}

/**
Run solve the problem CF1511A
Date: 21/11/21
User: epradere
URL: https://codeforces.com/problemset/problem/1511/A
Problem: CF1511A
**/
func (in *CF1511A) Run() {
	t := in.NextInt()
	for ; t > 0; t-- {
		n := in.NextInt()
		s1 := 0
		s2 := 0
		for i := 0; i < n; i++ {
			tp := in.NextInt()
			if tp == 1 {
				if s1 < s2 {
					s1++
				} else {
					s2++
				}
			} else if tp == 2 {
				if s1 < s2 {
					s1--
				} else {
					s2--
				}
			} else {
				if s1 >= 0 {
					s1++
				} else if s2 >= 0 {
					s2++
				} else if s1 > s2 {
					s1--
				} else {
					s2--
				}
			}
		}

		ans := 0
		if s1 > 0 {
			ans += s1
		}
		if s2 > 0 {
			ans += s2
		}
		fmt.Println(ans)

	}
}

func NewCF1511A(r *bufio.Reader) *CF1511A {
	return &CF1511A{
		sc:        r,
		split:     []string{},
		index:     0,
		separator: " ",
	}
}

func main() {
	NewCF1511A(bufio.NewReader(os.Stdin)).Run()
}


$$$n$$$ robots have escaped from your laboratory! You have to find them as soon as possible, because these robots are experimental, and their behavior is not tested yet, so they may be really dangerous!Fortunately, even though your robots have escaped, you still have some control over them. First of all, you know the location of each robot: the world you live in can be modeled as an infinite coordinate plane, and the $$$i$$$-th robot is currently located at the point having coordinates ($$$x_i$$$, $$$y_i$$$). Furthermore, you may send exactly one command to all of the robots. The command should contain two integer numbers $$$X$$$ and $$$Y$$$, and when each robot receives this command, it starts moving towards the point having coordinates ($$$X$$$, $$$Y$$$). The robot stops its movement in two cases:  either it reaches ($$$X$$$, $$$Y$$$);  or it cannot get any closer to ($$$X$$$, $$$Y$$$). Normally, all robots should be able to get from any point of the coordinate plane to any other point. Each robot usually can perform four actions to move. Let's denote the current coordinates of the robot as ($$$x_c$$$, $$$y_c$$$). Then the movement system allows it to move to any of the four adjacent points:  the first action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c - 1$$$, $$$y_c$$$);  the second action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c + 1$$$);  the third action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c + 1$$$, $$$y_c$$$);  the fourth action allows it to move from ($$$x_c$$$, $$$y_c$$$) to ($$$x_c$$$, $$$y_c - 1$$$). Unfortunately, it seems that some movement systems of some robots are malfunctioning. For each robot you know which actions it can perform, and which it cannot perform.You want to send a command so all robots gather at the same point. To do so, you have to choose a pair of integer numbers $$$X$$$ and $$$Y$$$ so that each robot can reach the point ($$$X$$$, $$$Y$$$). Is it possible to find such a point?

The first line contains one integer $$$q$$$ ($$$1 \le q \le 10^5$$$) — the number of queries. Then $$$q$$$ queries follow. Each query begins with one line containing one integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the number of robots in the query. Then $$$n$$$ lines follow, the $$$i$$$-th of these lines describes the $$$i$$$-th robot in the current query: it contains six integer numbers $$$x_i$$$, $$$y_i$$$, $$$f_{i, 1}$$$, $$$f_{i, 2}$$$, $$$f_{i, 3}$$$ and $$$f_{i, 4}$$$ ($$$-10^5 \le x_i, y_i \le 10^5$$$, $$$0 \le f_{i, j} \le 1$$$). The first two numbers describe the initial location of the $$$i$$$-th robot, and the following four numbers describe which actions the $$$i$$$-th robot can use to move ($$$f_{i, j} = 1$$$ if the $$$i$$$-th robot can use the $$$j$$$-th action, and $$$f_{i, j} = 0$$$ if it cannot use the $$$j$$$-th action). It is guaranteed that the total number of robots over all queries does not exceed $$$10^5$$$.

You should answer each query independently, in the order these queries appear in the input. To answer a query, you should do one of the following:   if it is impossible to find a point that is reachable by all $$$n$$$ robots, print one number $$$0$$$ on a separate line;  if it is possible to find a point that is reachable by all $$$n$$$ robots, print three space-separated integers on the same line: $$$1$$$ $$$X$$$ $$$Y$$$, where $$$X$$$ and $$$Y$$$ are the coordinates of the point reachable by all $$$n$$$ robots. Both $$$X$$$ and $$$Y$$$ should not exceed $$$10^5$$$ by absolute value; it is guaranteed that if there exists at least one point reachable by all robots, then at least one of such points has both coordinates not exceeding $$$10^5$$$ by absolute value.

package main

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

/* solution */

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

func solveOne() {

	n := readInt()

	l, r, t, b := -100000, 100000, 100000, -100000
	for i := 0; i < n; i++ {
		x, y, ml, mt, mr, mb := readInt(), readInt(), readInt(), readInt(), readInt(), readInt()
		if ml == 0 && l < x {
			l = x
		}
		if mr == 0 && x < r {
			r = x
		}
		if mt == 0 && y < t {
			t = y
		}
		if mb == 0 && b < y {
			b = y
		}
	}
	if l <= r && b <= t {
		printInt(1)
		printInt(l)
		printInt(t)
		println()
	} else {
		printInt(0)
		println()
	}

}

/* stubs */

func main() {

	var f *os.File
	if runtime.GOROOT() == "C:\\Soft\\Go" {
		f, _ = os.Open("input.txt")
		defer f.Close()
	} else {
		f = os.Stdin
	}

	writer = bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	reader = bufio.NewScanner(f)

	const maxCapacity = 1024 * 1024
	buf := make([]byte, maxCapacity)
	reader.Buffer(buf, maxCapacity)

	reader.Split(bufio.ScanWords)

	solve()

}

var reader *bufio.Scanner
var writer *bufio.Writer

func read() string {
	reader.Scan()
	return reader.Text()
}

func readInt() int {
	reader.Scan()
	ret, _ := strconv.Atoi(reader.Text())
	return ret
}

func print(s string) {
	writer.WriteString(s)
	writer.WriteString(" ")
}

func println() {
	writer.WriteString("\n")
}

func printInt(i int) {
	print(strconv.Itoa(i))
}

func readInt64() int64 {
	reader.Scan()
	ret, _ := strconv.ParseInt(reader.Text(), 10, 64)
	return ret
}

package main

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

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

	var q int
	fmt.Fscanf(input, "%d\n", &q)
	for i := 0; i < q; i++ {
		var n int
		fmt.Fscanf(input, "%d\n", &n)

		xMax := 100000
		xMin := -100000
		yMax := 100000
		yMin := -100000
		for j := 0; j < n; j++ {
			var x, y int
			f := make([]int, 4)
			fmt.Fscanf(input, "%d %d %d %d %d %d\n", &x, &y, &f[0], &f[1], &f[2], &f[3])

			if f[0] == 0 && x > xMin {
				xMin = x
			}
			if f[1] == 0 && y < yMax {
				yMax = y
			}
			if f[2] == 0 && x < xMax {
				xMax = x
			}
			if f[3] == 0 && y > yMin {
				yMin = y
			}
		}
		if xMax < xMin || yMax < yMin {
			fmt.Printf("0\n")
		} else {
			fmt.Printf("1 %d %d\n", xMin, yMin)
		}
	}
}

package main

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

func main() {

	//stdin, err := os.Open("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdin.Close()
	stdin := os.Stdin
	reader := bufio.NewReaderSize(stdin, 1024*1024)

	//stdout, err := os.Create("test.txt")
	//if err != nil {
	//	panic(err)
	//}
	//defer stdout.Close()
	stdout := os.Stdout
	writer := bufio.NewWriterSize(stdout, 1024*1024)

	var q int
	fmt.Fscanf(reader, "%d\n", &q)

	for i := 0; i < q; i++ {
		var n, minx, miny, maxx, maxy int = 0, -1e5, -1e5, 1e5, 1e5
		fmt.Fscanf(reader, "%d\n", &n)
		for j := 0; j < n; j++ {
			var x, y, f1, f2, f3, f4 int
			fmt.Fscanf(reader, "%d %d %d %d %d %d\n", &x, &y, &f1, &f2, &f3, &f4)
			// !left
			if f1 == 0 {
				minx = int(math.Max(float64(minx), float64(x)))
			}
			// !top
			if f2 == 0 {
				maxy = int(math.Min(float64(maxy), float64(y)))
			}
			// !right
			if f3 == 0 {
				maxx = int(math.Min(float64(maxx), float64(x)))
			}
			// !down
			if f4 == 0 {
				miny = int(math.Max(float64(miny), float64(y)))
			}

		}
		if minx > maxx || miny > maxy {
			fmt.Fprintf(writer, "0\n")
		} else {
			fmt.Fprintf(writer, "1 %d %d\n", minx, miny)
		}
	}

	writer.Flush()
}

// Author: sighduck
// URL: https://codeforces.com/contest/1196/problem/C

package main

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

const (
	MinValue = -100000
	MaxValue = 100000
)

type Robot struct {
	X int
	Y int
	L int
	U int
	R int
	D int
}

type Solution struct {
	Exists bool
	X      int
	Y      int
}

func min(x int, y int) int {
	if x < y {
		return x
	}
	return y
}

func max(x int, y int) int {
	if x > y {
		return x
	}
	return y
}

func Solve(n int, robots []Robot) Solution {
	xmin, xmax, ymin, ymax := MinValue, MaxValue, MinValue, MaxValue
	for _, robot := range robots {
		if robot.R == 0 {
			xmax = min(xmax, robot.X)
		}
		if robot.L == 0 {
			xmin = max(xmin, robot.X)
		}
		if robot.U == 0 {
			ymax = min(ymax, robot.Y)
		}
		if robot.D == 0 {
			ymin = max(ymin, robot.Y)
		}
	}
	if xmin > xmax || ymin > ymax {
		return Solution{Exists: false}
	}
	return Solution{Exists: true, X: xmin, Y: ymin}
}

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

	var q int
	fmt.Fscanf(reader, "%d\n", &q)
	for i := 0; i < q; i++ {
		var n int
		fmt.Fscanf(reader, "%d\n", &n)
		robots := make([]Robot, n)
		for i := 0; i < n; i++ {
			fmt.Fscanf(
				reader,
				"%d %d %d %d %d %d\n",
				&robots[i].X, &robots[i].Y, &robots[i].L, &robots[i].U, &robots[i].R, &robots[i].D,
			)
		}
		solution := Solve(n, robots)
		if solution.Exists {
			fmt.Fprintf(writer, "1 %d %d\n", solution.X, solution.Y)
		} else {
			fmt.Fprintf(writer, "0\n")
		}
	}
}

package main

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

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

	var q int
	fmt.Fscanf(input, "%d\n", &q)
	for i := 0; i < q; i++ {
		var n int
		fmt.Fscanf(input, "%d\n", &n)

		xMax := math.MinInt32
		xMin := math.MaxInt32
		yMax := math.MinInt32
		yMin := math.MaxInt32
		for j := 0; j < n; j++ {
			var x, y int
			f := make([]int, 4)
			fmt.Fscanf(input, "%d %d %d %d %d %d\n", &x, &y, &f[0], &f[1], &f[2], &f[3])

			if f[0] == 0 && xMin > x {
				xMin = x
			}
			if f[1] == 0 && yMax < y {
				yMax = y
			}
			if f[2] == 0 && xMax < x {
				xMax = x
			}
			if f[3] == 0 && yMin > y {
				yMin = y
			}
		}
		if xMax == math.MinInt32 {
			xMax = 100000
		}
		if xMin == math.MaxInt32 {
			xMin = -100000
		}
		if yMax == math.MinInt32 {
			yMax = 100000
		}
		if yMin == math.MaxInt32 {
			yMin = -100000
		}
		if xMax < xMin || yMax < yMin {
			fmt.Printf("0\n")
		} else {
			fmt.Printf("1 %d %d\n", xMin, yMin)
		}
	}
}


You are given an array $$$a$$$ of $$$n$$$ positive integers numbered from $$$1$$$ to $$$n$$$. Let's call an array integral if for any two, not necessarily different, numbers $$$x$$$ and $$$y$$$ from this array, $$$x \ge y$$$, the number $$$\left \lfloor \frac{x}{y} \right \rfloor$$$ ($$$x$$$ divided by $$$y$$$ with rounding down) is also in this array.You are guaranteed that all numbers in $$$a$$$ do not exceed $$$c$$$. Your task is to check whether this array is integral.

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 \le n \le 10^6$$$, $$$1 \le c \le 10^6$$$) — the size of $$$a$$$ and the limit for the numbers in the array. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 \le a_i \le c$$$) — the array $$$a$$$. Let $$$N$$$ be the sum of $$$n$$$ over all test cases and $$$C$$$ be the sum of $$$c$$$ over all test cases. It is guaranteed that $$$N \le 10^6$$$ and $$$C \le 10^6$$$.

For each test case print Yes if the array is integral and No otherwise.

NoteIn the first test case it is easy to see that the array is integral:   $$$\left \lfloor \frac{1}{1} \right \rfloor = 1$$$, $$$a_1 = 1$$$, this number occurs in the arry  $$$\left \lfloor \frac{2}{2} \right \rfloor = 1$$$  $$$\left \lfloor \frac{5}{5} \right \rfloor = 1$$$  $$$\left \lfloor \frac{2}{1} \right \rfloor = 2$$$, $$$a_2 = 2$$$, this number occurs in the array  $$$\left \lfloor \frac{5}{1} \right \rfloor = 5$$$, $$$a_3 = 5$$$, this number occurs in the array  $$$\left \lfloor \frac{5}{2} \right \rfloor = 2$$$, $$$a_2 = 2$$$, this number occurs in the array Thus, the condition is met and the array is integral.In the second test case it is enough to see that$$$\left \lfloor \frac{7}{3} \right \rfloor = \left \lfloor 2\frac{1}{3} \right \rfloor = 2$$$, this number is not in $$$a$$$, that's why it is not integral.In the third test case $$$\left \lfloor \frac{2}{2} \right \rfloor = 1$$$, but there is only $$$2$$$ in the array, that's why it is not integral.

package main

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

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

const mod = 998244353

// 1 2 3 4
func main() {
	in, out := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
	defer out.Flush()
	t := 0
	fmt.Fscan(in, &t)

	for ii := 0; ii < t; ii++ {
		var n, c int
		fmt.Fscan(in, &n, &c)
		nums := make([]int, c+1)

		for i := 0; i < n; i++ {
			num := 0
			fmt.Fscan(in, &num)
			nums[num] = 1
		}
		for i := 1; i < len(nums); i++ {
			nums[i] = nums[i] + nums[i-1]
		}
		ok := true
	out:
		for i := 2; i <= c; i++ {
			if nums[i]-nums[i-1] == 0 {
				continue
			}
			for j := i; j <= c; j += i {
				max := j + i - 1
				if max > c {
					max = c
				}
				if nums[max]-nums[j-1] > 0 && nums[j/i]-nums[j/i-1] == 0 {
					ok = false
					break out
				}
			}
		}
		if ok {
			fmt.Fprintln(out, "YES")
		} else {
			fmt.Fprintln(out, "NO")
		}
	}
}

package main

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

var T int

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

	Fscan(in, &T)
	for t := 0; t < T; t++ {
		var n, c int
		Fscan(in, &n, &c)
		a := make([]int, n+1)
		cnt := make([]int, 2*c+1)
		pre := make([]int, 2*c+1)
		for j := 1; j <= n; j++ {
			Fscan(in, &a[j])
			cnt[a[j]]++
		}

		for i := 1; i <= 2*c; i++ {
			pre[i] += pre[i-1] + cnt[i]
		}

		ok := true
		for i := 1; i <= c; i++ {
			if cnt[i] > 0 {
				for j := 1; i*j <= c; j++ {
					if cnt[j] == 0 && pre[i*j-1] != pre[i*(j+1)-1] {
						ok = false
						break
					}
				}
				if !ok {
					break
				}
			}
		}
		if ok {
			Fprintln(out, "Yes")
		} else {
			Fprintln(out, "No")
		}
	}
}


This is the harder version of the problem. In this version, $$$1 \le n \le 10^6$$$ and $$$0 \leq a_i \leq 10^6$$$. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problemsChristmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $$$n$$$ boxes of chocolate, numbered from $$$1$$$ to $$$n$$$. Initially, the $$$i$$$-th box contains $$$a_i$$$ chocolate pieces.Since Bob is a typical nice guy, he will not send Alice $$$n$$$ empty boxes. In other words, at least one of $$$a_1, a_2, \ldots, a_n$$$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $$$k > 1$$$ such that the number of pieces in each box is divisible by $$$k$$$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $$$i$$$ and put it into either box $$$i-1$$$ or box $$$i+1$$$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy.

The first line contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the number of chocolate boxes. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 10^6$$$) — the number of chocolate pieces in the $$$i$$$-th box. It is guaranteed that at least one of $$$a_1, a_2, \ldots, a_n$$$ is positive.

If there is no way for Charlie to make Alice happy, print $$$-1$$$. Otherwise, print a single integer $$$x$$$ — the minimum number of seconds for Charlie to help Bob make Alice happy.

NoteIn the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by $$$17$$$.In the second example, Charlie can move a piece from box $$$2$$$ to box $$$3$$$ and a piece from box $$$4$$$ to box $$$5$$$. Each box will be divisible by $$$3$$$.In the third example, each box is already divisible by $$$5$$$.In the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy.

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1254B2(_r io.Reader, out io.Writer) {
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}
	n := r()
	sum := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		sum[i] = sum[i-1] + int64(r())
	}
	x := sum[n]
	if x < 2 {
		Fprint(out, -1)
		return
	}
	a := []int64{}
	for i := int64(2); i*i <= x; i++ {
		k := 0
		for ; x%i == 0; x /= i {
			k++
		}
		if k > 0 {
			a = append(a, i)
		}
	}
	if x > 1 {
		a = append(a, x)
	}
	ans := int64(1e18)
	for _, p := range a {
		s := int64(0)
		for _, v := range sum {
			s += min(v%p, p-v%p)
		}
		ans = min(ans, s)
	}
	Fprint(out, ans)
}

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

package main

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

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

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

func main() {
	defer out.Flush()
	inn := bufio.NewScanner(os.Stdin)
	inn.Split(bufio.ScanWords)
	r := func() (x int) {
		inn.Scan()
		data := inn.Bytes()
		if data[0] == '-' {
			for _, b := range data[1:] {
				x = x*10 + int(b&15)
			}
			return -x
		}
		for _, b := range data {
			x = x*10 + int(b&15)
		}
		return
	}
	var n int
	n = r()
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = int64(r())
	}
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	if a[n-1] == 1 {
		Fprint(out, -1)
		return
	}
	var ans = int64(1e18)
	tmp := a[n-1]
	cal := func(x int64) int64 {
		var res int64 = 0
		for i := 0; i < n; i++ {
			res += min(a[i]%x, x-a[i]%x)
		}
		return res
	}
	for f := int64(2); f <= int64(math.Sqrt(float64(tmp)))+1; f++ {
		if tmp%f == 0 {
			ans = min(ans, cal(int64(f)))
			for tmp%f == 0 {
				tmp /= f
			}
		}
	}
	if tmp > 1 {
		ans = min(ans, cal(tmp))
	}
	Fprint(out, ans)
}

package main

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

var (
	inn = bufio.NewScanner(os.Stdin)
	out = bufio.NewWriter(os.Stdout)
)

func main() {
	defer out.Flush()
	inn.Buffer([]byte{}, 1000000)
	inn.Split(bufio.ScanWords)
	n := rd()
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = rdll()
	}
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	if a[n-1] == 1 {
		Fprint(out, -1)
		return
	}
	var ans = int64(1e18)
	tmp := a[n-1]
	cal := func(x int64) int64 {
		var res int64 = 0
		for i := 0; i < n; i++ {
			res += min(a[i]%x, x-a[i]%x)
		}
		return res
	}
	for f := int64(2); f*f <= tmp; f++ {
		if tmp%f == 0 {
			ans = min(ans, cal(int64(f)))
			for tmp%f == 0 {
				tmp /= f
			}
		}
	}
	if tmp > 1 {
		ans = min(ans, cal(tmp))
	}
	Fprint(out, ans)
}

func max(a, b int64) int64 {
	if a > b {
		return a
	}
	return b
}
func min(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}
func rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.Atoi(string(data))
	return x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	x, _ = strconv.ParseInt(string(data), 10, 0)
	return x
}
func rds() string {
	inn.Scan()
	return string(inn.Bytes())
}

package main

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

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

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

func main() {
	defer out.Flush()
	inn.Split(bufio.ScanWords)
	n := rd()
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = rdll()
	}
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	if a[n-1] == 1 {
		Fprint(out, -1)
		return
	}
	var ans = int64(1e18)
	tmp := a[n-1]
	cal := func(x int64) int64 {
		var res int64 = 0
		for i := 0; i < n; i++ {
			res += min(a[i]%x, x-a[i]%x)
		}
		return res
	}
	for f := int64(2); f <= int64(math.Sqrt(float64(tmp)))+1; f++ {
		if tmp%f == 0 {
			ans = min(ans, cal(int64(f)))
			for tmp%f == 0 {
				tmp /= f
			}
		}
	}
	if tmp > 1 {
		ans = min(ans, cal(tmp))
	}
	Fprint(out, ans)
}

func rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	f := 1
	if data[0] == '-' {
		f = -1
		data = data[1:]
	}
	for _, b := range data {
		x = x*10 + int(b&15)
	}
	return f * x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	f := int64(1)
	if data[0] == '-' {
		f = -1
		data = data[1:]
	}
	for _, b := range data {
		x = x*10 + int64(b&15)
	}
	return f * x
}

package main

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

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

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

func main() {
	defer out.Flush()
	inn := bufio.NewScanner(os.Stdin)
	inn.Split(bufio.ScanWords)
	r := func() (x int) {
		inn.Scan()
		data := inn.Bytes()
		f := 1
		if data[0] == '-' {
			f = -1
			data = data[1:]
		}
		for _, b := range data {
			x = x*10 + int(b&15)
		}
		return f * x
	}
	var n int
	n = r()
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = int64(r())
	}
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	if a[n-1] == 1 {
		Fprint(out, -1)
		return
	}
	var ans = int64(1e18)
	tmp := a[n-1]
	cal := func(x int64) int64 {
		var res int64 = 0
		for i := 0; i < n; i++ {
			res += min(a[i]%x, x-a[i]%x)
		}
		return res
	}
	for f := int64(2); f <= int64(math.Sqrt(float64(tmp)))+1; f++ {
		if tmp%f == 0 {
			ans = min(ans, cal(int64(f)))
			for tmp%f == 0 {
				tmp /= f
			}
		}
	}
	if tmp > 1 {
		ans = min(ans, cal(tmp))
	}
	Fprint(out, ans)
}

package main

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

// github.com/EndlessCheng/codeforces-go
func CF1254B2(_r io.Reader, out io.Writer) {
	_i, buf := 1<<12, make([]byte, 1<<12)
	rc := func() byte {
		if _i == 1<<12 {
			_r.Read(buf)
			_i = 0
		}
		b := buf[_i]
		_i++
		return b
	}
	r := func() (x int) {
		b := rc()
		for ; '0' > b; b = rc() {
		}
		for ; '0' <= b; b = rc() {
			x = x*10 + int(b&15)
		}
		return
	}
	min := func(a, b int64) int64 {
		if a < b {
			return a
		}
		return b
	}
	n := r()
	sum := make([]int64, n+1)
	for i := 1; i <= n; i++ {
		sum[i] = sum[i-1] + int64(r())
	}
	x := sum[n]
	if x < 2 {
		Fprint(out, -1)
		return
	}
	a := []int64{}
	for i := int64(2); i*i <= x; i++ {
		k := 0
		for ; x%i == 0; x /= i {
			k++
		}
		if k > 0 {
			a = append(a, i)
		}
	}
	if n > 1 {
		a = append(a, x)
	}
	ans := int64(1e18)
	for _, p := range a {
		s := int64(0)
		for _, v := range sum {
			s += min(v%p, p-v%p)
		}
		ans = min(ans, s)
	}
	Fprint(out, ans)
}

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

package main

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

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

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

func main() {
	defer out.Flush()
	inn.Split(bufio.ScanWords)
	n := rd()
	a := make([]int64, n)
	for i := 0; i < n; i++ {
		a[i] = rdll()
	}
	for i := 1; i < n; i++ {
		a[i] += a[i-1]
	}
	if a[n-1] == 1 {
		Fprint(out, -1)
		return
	}
	var ans = int64(1e18)
	tmp := a[n-1]
	cal := func(x int64) int64 {
		var res int64 = 0
		for i := 0; i < n; i++ {
			res += min(a[i]%x, x-a[i]%x)
		}
		return res
	}
	for f := int64(2); f <= int64(math.Sqrt(float64(tmp)))+1; f++ {
		if tmp%f == 0 {
			ans = min(ans, cal(int64(f)))
			for tmp%f == 0 {
				tmp /= f
			}
		}
	}
	if tmp > 1 {
		ans = min(ans, cal(tmp))
	}
	Fprint(out, ans)
}

func rd() (x int) {
	inn.Scan()
	data := inn.Bytes()
	f := 1
	if data[0] == '-' {
		f = -1
		data = data[1:]
	}
	for _, b := range data {
		x = x*10 + int(b&15)
	}
	return f * x
}
func rdll() (x int64) {
	inn.Scan()
	data := inn.Bytes()
	f := int64(-1)
	if data[0] == '-' {
		f = -1
		data = data[1:]
	}
	for _, b := range data {
		x = x*10 + int64(b&15)
	}
	return f * x
}