package main

import "fmt"

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

	cnt := 0
	for {
		n = n - 2 * d - 1
		cnt++
		if n <= 0 {
			fmt.Println(cnt)
			return
		}
	}
}

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


package main

import "fmt"

func main() {
	// Code for B - Golden Apple
	fmt.Printf("Hello world")
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


package main

import (
	"fmt"
	"math"
)

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

	fmt.Println(math.Ceil(float64(n) / float64((2*d + 1))))
}


package main

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

type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func main() {
	sc := NewScanner()
	N, D := sc.nextInt(), sc.nextInt()
	count := 1
	for {
		if (D*2+1)*count >= N {
			break
		}
		count++
	}
	fmt.Println(count)
}


package main
import "fmt"

func main() {
	var n, d int
  fmt.Scan(&n)
  fmt.Scan(&d)
  fmt.Println(calc(n, d))
}

func calc(n, d int) int{
  cov := d * 2
  i := n - d
  ans := i / cov
  return ans
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

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

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n := nextInt()
	a := make([]int, n)

	for i := 0; i < n; i++ {
		a[i] = nextInt()
	}

	x := make([]int, n)
	y := make([]int, n)

	x[0] = a[0]
	y[n-1] = a[n-1]

	for i := 1; i < n; i++ {
		x[i] = max(x[i-1], a[i])
	}
	for i := n - 2; i >= 0; i-- {
		y[i] = max(y[i+1], a[i])
	}

	fmt.Println(y[1])
	for i := 1; i < n-1; i++ {
		fmt.Println(max(x[i]-1, y[i+1]))
	}
	fmt.Println(x[n-1])
}

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


package main

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

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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

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

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	N := getInt()
	a := make([]int, N)
	b := make([]int, N)
	for i := 0; i < N; i++ {
		a[i] = getInt()
		b[i] = a[i]
	}
	sort.Ints(b)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	for i := 0; i < N; i++ {
		if a[i] == b[len(b)-1] {
			fmt.Fprintln(w, b[len(b)-2])
		} else {
			fmt.Fprintln(w, b[len(b)-1])
		}
	}
}


package main
import (
  "fmt"
  "sort"
)

func main() {
	var n, d, ind int
  	a := make([]int, 0)
  	fmt.Scan(&n)
  for i := 0; i < n; i++ {
    fmt.Scan(&d)
   		a = append(a, d)
  	}
  b := make([]int, 0)
  for _, v := range a {
    b = append(b, v)
  }
  sort.Sort(sort.IntSlice(b))
  size := len(b)
  max := b[size-1]
  big := b[size-2]
  for i, v := range a {
    if ind == 0 {
      switch {
    	case v == max :
      		ind = i
      }
    }
  }
  switch {
    case max == big:
    for i := 0; i < n; i++{
      fmt.Println(max)
    }
    case max != big:
    	a[ind] = big
      for i, v := range a {
        if i == ind {
          fmt.Println(v)
        }else{
          fmt.Println(max)
        }
      }
    }
}


package main
import "fmt"

func main() {
  var n, a int
  maxVal := 0
  secondVal := 0
  maxIdx := 0

  fmt.Scanln(&n)

  for i := 0; i < n; i++ {
    fmt.Scanln(&a)

    if(a >= maxVal) {
      secondVal = maxVal
      maxVal = a
      maxIdx = i
    } else if( a >= secondVal) {
      secondVal = a
    }
  }

  for i := 0; i < n; i++ {
    if(i == maxIdx) {
      fmt.Printf("%d\n", secondVal)
    } else {
      fmt.Printf("%d\n", maxVal)
    }
  }
}


// Package main provides
//
// File:  main.go
// Author: ymiyamoto
//
// Created on Sat Jul 20 21:12:29 2019
//
package main

import (
	"fmt"
	"sort"
)

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

	arr := make([]int, n)
	m := map[int]int{}
	for i := range arr {
		fmt.Scan(&arr[i])
		m[arr[i]]++
	}

	var nums []int
	for k := range m {
		nums = append(nums, k)
	}
	sort.Ints(nums)
	max := nums[len(nums)-1]
	for i := range arr {
		if arr[i] != max {
			fmt.Println(max)
		} else {
			if m[arr[i]] > 1 {
				fmt.Println(max)
			} else {
				fmt.Println(nums[len(nums)-2])
			}
		}
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

const inf = 1 << 60
const mod int = 1e9 + 7

func ri() (n int) {
	sc.Scan()
	for _, v := range sc.Bytes() {
		n = n*10 + int(v-48)
	}
	return
}

func maxAndSec(a []int) (int, int) {
	max, sec := -1, -1
	for _, i := range a {
		if max <= i {
			max, sec = i, max
		} else if sec < i {
			sec = i
		}
	}
	return max, sec
}

func main() {
	sc.Split(bufio.ScanWords)
	n := ri()

	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = ri()
	}

	// fmt.Println(a)

	mx, sec := maxAndSec(a)

	ctMx := 0
	for _, aa := range a {
		if aa == mx {
			ctMx++
		}
	}

	// fmt.Println(mx, ctMx)

	if ctMx > 1 {
		for i := 0; i < n; i++ {
			fmt.Println(mx)
		}
		return
	} else { // ctMx == 1
		for _, aa := range a {
			if aa == mx {
				fmt.Println(sec)
			} else {
				fmt.Println(mx)
			}
		}
	}
}


package main

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

var DEBUG = false

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO functions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbufferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Setup nextToken without input buffer.
func SetUnbufferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func debugf(f string, args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintf(os.Stderr, f, args...)
}

func debug(args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

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

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

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

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

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

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

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

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	SetInteractive(os.Stdout, os.Stdin)
}

func main() {
	defer Flush()
	n := readi()
	_, a := readInts(n)
	solve(n, a)
}

func solve(n int, a []int) {
	box := make([]int, n)
	for i:= 0; i < n; i++ {
		box[i] = -1
	}
	multi := make([][]int, n)
	for i := 0; i < n; i++ {
		multi[i] = []int{}
		for j := i+1; j <= n; j += i+1 {
			multi[i] = append(multi[i], j-1)
		}
	}
	answer := []int{}
	for i := n -1; i >= 0; i-- {
		if len(multi[i]) == 1 {
			box[i] = a[i]
			if a[i] == 1 {
				answer = append(answer, i+1)
			}
		} else {
			counter := 0
			for _, v := range multi[i] {
				if v > i {
					counter += box[v]
				}
			}

			if counter %2 == 0 {
				if a[i] == 0 {
					box[i] = 0
				} else {
					box[i] = 1
					answer = append(answer, i+1)
				}
			} else {
				if a[i] == 0 {
					box[i] = 1
					answer = append(answer, i+1)
				} else {
					box[i] = 0
				}
			}

		}
	}

	debug(box)
	debug(multi)

	if len(answer) == 0 {
		println(0)
	} else {
		println(len(answer))
		for i:= 0; i < len(answer)-1; i++ {
			printf("%d ", answer[i])
		}
		println(answer[len(answer)-1])
	}
}

Score : 400 points

Problem Statement

There are N empty boxes arranged in a row from left to right.
The integer i is written on the i-th box from the left (1 \leq i \leq N).

For each of these boxes, Snuke can choose either to put a ball in it or to put nothing in it.

We say a set of choices to put a ball or not in the boxes is good when the following condition is satisfied:

For every integer i between 1 and N (inclusive), the total number of balls contained in the boxes with multiples of i written on them is congruent to a_i modulo 2.

Does there exist a good set of choices? If the answer is yes, find one good set of choices.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

a_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If a good set of choices does not exist, print -1.

If a good set of choices exists, print one such set of choices in the following format:

M
b_1 b_2 ... b_M

where M denotes the number of boxes that will contain a ball, and b_1,\ b_2,\ ...,\ b_M are the integers written on these boxes, in any order.

Sample Input 1

3
1 0 0

Sample Output 1

1
1

Consider putting a ball only in the box with 1 written on it.

There are three boxes with multiples of 1 written on them: the boxes with 1, 2, and 3. The total number of balls contained in these boxes is 1.

There is only one box with a multiple of 2 written on it: the box with 2. The total number of balls contained in these boxes is 0.

There is only one box with a multiple of 3 written on it: the box with 3. The total number of balls contained in these boxes is 0.

Thus, the condition is satisfied, so this set of choices is good.

Sample Input 2

5
0 0 0 0 0

Sample Output 2

0

Putting nothing in the boxes can be a good set of choices.


package main

import (
	"fmt"
)

const MOD = 1000000007

func ModAdd(a, b int64) int64 {
	return (a + b) % MOD
}

func ModMul(a, b int64) int64 {
	return a * b % MOD
}

func ModSub(a, b int64) int64 {
	r := (a%MOD - b%MOD) % MOD
	if r < 0 {
		r += MOD
	}
	return r
}

func ModDiv(a, b int64) int64 {
	return a % MOD * ModInv(b) % MOD
}

func ModPow(a, n int64) int64 {
	var r int64 = 1
	for n > 0 {
		if n&1 == 1 {
			r = r * a % MOD
		}
		a = a * a % MOD
		n >>= 1
	}
	return r
}

func ModInv(a int64) int64 {
	var b, u, v int64 = MOD, 1, 0
	for b > 0 {
		t := a / b
		a -= t * b
		a, b = b, a
		u -= t * v
		u, v = v, u
	}
	u %= MOD
	if u < 0 {
		u += MOD
	}
	return u
}

type Q struct {
	idx, value int
	a, b       int
	index      int
}

type QS []*Q

func (p QS) Len() int           { return len(p) }
func (p QS) Less(i, j int) bool { return p[i].value > p[j].value }
func (pq QS) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *QS) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Q)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *QS) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

func FactoredPrimeNumber(num int) map[int]int {
	m := map[int]int{}
	return factor(m, num, 2)
}

func factor(result map[int]int, num, pnum int) map[int]int {
	if pnum*pnum > num {
		if num != 1 {
			result[num] += 1
		}
		return result
	}

	if num%pnum == 0 {
		num /= pnum
		result[pnum]++
	} else {
		pnum++
	}
	return factor(result, num, pnum)
}

func main() {
	var N int
	fmt.Scanf("%d\n", &N)
	A := make([]int, N)
	for i := 0; i < N; i++ {
		var a int
		fmt.Scan(&a)
		A[i] = a
	}
	B := make([]int, N)
	max := 0
	for i := N; i > 0; i-- {
		a := A[i-1]
		if a == 0 {
			continue
		}
		p := 2
		sum := 0
		for i*p <= N {
			a := A[i*p-1]
			if a != 0 {
				sum++
			}
			p++
		}
		if sum%2 == 0 {
			B[i-1] = 1
			if i > max {
				max = i
			}
		}
	}
	if max == 0 {
		fmt.Println(0)
	} else {
		fmt.Println(max)
		for i := 0; i < max; i++ {
			fmt.Print(B[i])
			fmt.Print(" ")
		}
		fmt.Println()
	}
}

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


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func main() {
	var n, m int
	var a, b, c []int
	sc.Split(bufio.ScanWords)
	n = nextInt()
	for i := 0; i < n; i++ {
		h := nextInt()
		a = append(a, h)
	}
	b = make([]int, n+1)

	for i := len(a) - 1; i >= 0; i-- {
		var sum int
		for j := 2; (i+1)*j < len(a); {
			if b[(i+1)*j] == 1 {
				sum++
			}
			j++
		}
		switch a[i] {
		case 0:
			if sum%2 == 1 {
				b[i+1] = 1
				c = append([]int{i + 1}, c...)
				m++
			}
		case 1:
			if sum%2 == 0 {
				b[i+1] = 1
				c = append([]int{i + 1}, c...)
				m++
			}
		}

	}
	fmt.Println(m)
	for i, h := range c {
		if i == len(c)-1 {
			fmt.Printf("%v\n", h)
		} else {
			fmt.Printf("%v ", h)
		}
	}

}


package main

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

type Color struct {
	MaxNumber int
	Color int
}

func calc(arr []int, times int)  {
	colorArr := make([]Color,0)
	lastColor := new(Color)
	for i := 0; i < len(arr); i ++ {
		isCheck := true
		if i > 0 && arr[i] > arr[i-1] {
			lastColor.MaxNumber = arr[i]
			continue
		}
		for j := 0; j < len(colorArr); j++ {
			if colorArr[j].MaxNumber < arr[i]{
				colorArr[j].MaxNumber = arr[i]
				isCheck = false
				*lastColor = colorArr[j]
				break
			}
		}
		if isCheck {
			colorS := new(Color)
			colorS.MaxNumber = arr[i]
			colorArr = append(colorArr, *colorS)
			*lastColor = *colorS
		}
	}
	fmt.Println(len(colorArr))
}

func main() {
	arr := make([]int,0)
	reader := bufio.NewReaderSize(os.Stdin, 1024*1024)
	times, _:= strconv.Atoi(readLine(reader))
	for i:= 0; i< times; i++ {
		tmp, _ := strconv.Atoi(readLine(reader))
		arr = append(arr, tmp)
	}
	calc(arr, times)
}

func readLine(reader *bufio.Reader) string {
	str, _, err := reader.ReadLine()
	if err == io.EOF {
		return ""
	}

	return strings.TrimRight(string(str), "\r\n")
}

Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.


package main

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

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

func main() {
	sc.Split(bufio.ScanWords)
	defer out.Flush() // !!!!coution!!!! you must use Fprint(out, ) not Print()
	/* --- code ---*/
	N := nextInt()
	A := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = nextInt()
	}
	c := make([]int, N)
	ci := 1
	for i := N - 1; i >= 0; i-- {
		if c[i] != 0 {
			continue
		}
		c[i] = ci
		x := A[i]
		for j := i - 1; j >= 0; j-- {
			if A[j] < x {
				c[j] = ci
				x = c[j]
			}
		}
		ci++
	}
	fmt.Fprint(out, c)
	// 5 2 1 4 5 3
}

func solve(N int, a []int, used []int, s2, x1, x2, depth int) bool {
	// fmt.Fprint(out, used, depth, x1, x2, "\n")
	if depth == N {
		// fmt.Fprint(out, "atta\n")
		return x1^x2^a[0] == 0 && x2^a[0]^s2 == 0
	}
	for i := 0; i < N; i++ {
		if used[i] > 0 {
			continue
		}
		if depth >= 2 && x1^x2^a[i] != 0 {
			continue
		}
		used[i] = 1
		if depth == 1 {
			s2 = a[i]
		}
		if solve(N, a, used, s2, x2, a[i], depth+1) {
			return true
		}
		used[i] = 0
	}
	return false
}

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

func nextInt() int {
	a, _ := strconv.Atoi(next())
	return a
}

func nextFloat64() float64 {
	a, _ := strconv.ParseFloat(next(), 64)
	return a
}

func nextInts(n int) []int {
	ret := make([]int, n)
	for i := 0; i < n; i++ {
		ret[i] = nextInt()
	}
	return ret
}
func nextFloats(n int) []float64 {
	ret := make([]float64, n)
	for i := 0; i < n; i++ {
		ret[i] = nextFloat64()
	}
	return ret
}
func nextStrings(n int) []string {
	ret := make([]string, n)
	for i := 0; i < n; i++ {
		ret[i] = next()
	}
	return ret
}

// Genericsがないので筋肉で...
// interfaceでの取扱がようわからんし
func containStr(arr []string, target string) bool {
	for _, v := range arr {
		if v == target {
			return true
		}
	}
	return false
}

func PrintOut(src interface{}, joinner string) {
	switch reflect.TypeOf(src).Kind() {
	case reflect.Slice:
		s := reflect.ValueOf(src)
		for idx := 0; idx < s.Len(); idx++ {
			fmt.Fprintf(out, "%v", s.Index(idx))
			if idx < s.Len()-1 {
				fmt.Fprintf(out, "%s", joinner)
			} else {
				fmt.Fprintln(out)
			}
		}
	default:
		fmt.Fprintln(out, "fuck")
	}
}

// mapからkeysとvaluesを返す感じのやつ
// なんかしらんけどmap[interface{}]interface{}しか受け付けない。なんのためのinterface{}やねん
// なので呼ぶときのmapはmap[int]stringとかじゃダメ
func splitKeyValue(m map[interface{}]interface{}) (keys, values []interface{}) {
	switch reflect.TypeOf(m).Kind() {
	case reflect.Map:
		s := reflect.ValueOf(m)
		for _, v := range s.MapKeys() {
			keys = append(keys, v)
			values = append(values, s.MapIndex(v))
		}
	default:
		fmt.Fprintln(out, "fuck")
	}
	return
}


package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n := scanInt()
	k := scanInt()

	dp := [60][60][10000]int{}
	dp[0][0][0] = 1
	for i := 0; i < n; i++ {
		for j := 0; j < i+1; j++ {
			for x := 0; x < k+1; x++ {
				if j > 0 {
					dp[i+1][j-1][x+(j-1)*2] += dp[i][j][x]*j*j%MOD
					dp[i+1][j-1][x+(j-1)*2] %= MOD
				}
				dp[i+1][j][x+j*2] += dp[i][j][x] + dp[i][j][x]*j*2%MOD
				dp[i+1][j][x+j*2] %= MOD
				dp[i+1][j+1][x+(j+1)*2] += dp[i][j][x]
				dp[i+1][j+1][x+(j+1)*2] %= MOD
			}
		}
	}

	fmt.Fprintln(wr, dp[n][0][k])
}

// MOD constant
const MOD = 1000000007

Score : 600 points

Problem Statement

Let us define the oddness of a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n} as \sum_{i = 1}^n |i - p_i|.

Find the number of permutations of {1,\ 2,\ ...,\ n} of oddness k, modulo 10^9+7.

Constraints

All values in input are integers.

1 \leq n \leq 50

0 \leq k \leq n^2

Input

Input is given from Standard Input in the following format:

n k

Output

Print the number of permutations of {1,\ 2,\ ...,\ n} of oddness k, modulo 10^9+7.

Sample Input 1

3 2

Sample Output 1

2

There are six permutations of {1,\ 2,\ 3}. Among them, two have oddness of 2: {2,\ 1,\ 3} and {1,\ 3,\ 2}.

Sample Input 2

39 14

Sample Output 2

74764168


package main

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

var rdr = bufio.NewReaderSize(os.Stdin, 10000)

func readLine() string {
	buf := make([]byte, 0, 10000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

func atoi(s string) int {
	i, _ := strconv.Atoi(s)
	return i
}

func readInt() int {
	return atoi(readLine())
}

func readInts() []int {
	in := readLine()
	ins := strings.Split(in, " ")
	out := make([]int, len(ins))
	for i, v := range ins {
		out[i] = atoi(v)
	}
	return out
}

func judge(a []int) bool {
	for i := 0; i < len(a); i++ {
		if i == 0 {
			if a[0] != a[len(a)-1]^a[1] {
				return false
			}
		} else if i == len(a)-1 {
			if a[len(a)-1] != a[i-1]^a[0] {
				return false
			}
		} else {
			if a[i] != a[i-1]^a[i+1] {
				return false
			}
		}
	}
	return true
}

func main() {
	n := readInt()
	a := readInts()

	var count int
	for i := 0; i < n; i++ {
		if a[i] == 0 {
			count++
		}
	}
	if count == n {
		fmt.Println("Yes")
		return
	}
	if n%3 != 0 {
		fmt.Println("No")
		return
	}

	x := a[0]
	y := a[1]
	z := x ^ y
	var xc, yc, zc int
	for j := 0; j < n; j++ {
		if x == a[j] {
			xc++
		}
		if y == a[j] {
			yc++
		}
		if z == a[j] {
			zc++
		}
	}

	if xc == yc && yc == zc && zc == xc {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
}

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


package main

import "fmt"

func check(cntr map[int]int, n int) bool {
	for _, v := range cntr {
		if v*3 != n {
			return false
		}
	}
	return true
}
func check2(cntr map[int]int, n int) bool {
	for _, v := range cntr {
		if v*2 != n {
			return false
		}
	}
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	xs := make([]int, n)
	for i := range xs {
		fmt.Scan(&xs[i])
	}

	counter := make(map[int]int)
	for _, x := range xs {
		counter[x]++
	}

	if len(counter) == 3 {
		if n%3 != 0 || !check(counter, n) {
			fmt.Println("No")
		} else {
			vs := make([]int, 0)
			for k := range counter {
				vs = append(vs, k)
			}
			if vs[0]^vs[1] == vs[2] {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	} else if len(counter) == 2 {
		vs := make([]int, 0)
		for k := range counter {
			vs = append(vs, k)
		}
		if vs[0] == 0 || vs[1] == 0 {
			nzero := 0
			if vs[0] == 0 {
				nzero = vs[1]
			} else {
				nzero = vs[0]
			}
			if (counter[nzero] >= counter[0] && counter[nzero] <= 2*counter[0]) || counter[0] >= counter[nzero] {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		} else {
			fmt.Println("No")
		}
	} else if len(counter) == 1 {
		if counter[0] > 0 {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	} else {
		fmt.Println("No")
	}
}


package main

import "fmt"

func check(cntr map[int]int, n int) bool {
	for _, v := range cntr {
		if v*3 != n {
			return false
		}
	}
	return true
}
func check2(cntr map[int]int, n int) bool {
	for _, v := range cntr {
		if v*2 != n {
			return false
		}
	}
	return true
}

func main() {
	var n int
	fmt.Scan(&n)
	xs := make([]int, n)
	for i := range xs {
		fmt.Scan(&xs[i])
	}

	counter := make(map[int]int)
	for _, x := range xs {
		counter[x]++
	}

	if len(counter) == 3 {
		if n%3 != 0 || !check(counter, n) {
			fmt.Println("No")
		} else {
			vs := make([]int, 0)
			for k := range counter {
				vs = append(vs, k)
			}
			if vs[0]^vs[1] == vs[2] {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		}
	} else if len(counter) == 2 {
		vs := make([]int, 0)
		for k := range counter {
			vs = append(vs, k)
		}
		if vs[0] == 0 || vs[1] == 0 {
			nzero := 0
			if vs[0] == 0 {
				nzero = vs[1]
			} else {
				nzero = vs[0]
			}
			if (counter[nzero] >= counter[0] && counter[nzero] <= 2*counter[0]) || (counter[0] >= counter[nzero] && counter[0] <= 2*counter[nzero]) {
				fmt.Println("Yes")
			} else {
				fmt.Println("No")
			}
		} else {
			fmt.Println("No")
		}
	} else if len(counter) == 1 {
		if counter[0] > 0 {
			fmt.Println("Yes")
		} else {
			fmt.Println("No")
		}
	} else {
		fmt.Println("No")
	}
}


package main

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

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  var line, buffer []byte
  var isPrefix bool = true
  var err error
  for isPrefix {
    line, isPrefix, err = reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
  }
  return string(buffer)
}
func NextInt() int {
  var n int
  n, _ = strconv.Atoi(NextLine())
  return n
}
func NextIntVec() []int {
  L := strings.Split(NextLine(), " ")
  N := make([]int, len(L))
  for i := range N {
    N[i], _ = strconv.Atoi(L[i])
  }
  return N
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func WriteIntVec(A *[]int) {
  S := make([]string, len((*A)))
  for i, a := range (*A) {
    S[i] = strconv.Itoa(a)
  }
  fmt.Fprintln(writer, strings.Join(S, " "))
}
func Output() {
  _ = writer.Flush()
}

type Node struct {
  Index int
  Edge []int
  Degree int
  Undefined int
  Dist int
}
func main() {
  NM := NextIntVec()
  N, M := NM[0], NM[1]
  G := make([]Node, N)
  for i := range G {
    G[i] = Node{i, []int{}, 0, -1, -1}
  }
  E := make([][]int, M)
  for i := range E {
    AB := NextIntVec()
    A, B := AB[0] - 1, AB[1] - 1
    E[i] = []int{A, B}
    G[A].Edge = append(G[A].Edge, i)
    G[B].Edge = append(G[B].Edge, i)
  }
  G[0].Dist = 0
  Q := make([]int, N)
  pos := 1
  for i, _ := range Q {
    s := Q[i]
    for _, e := range G[s].Edge {
      if G[s].Undefined == e { continue }
      t := E[e][0]
      if t == s { t = E[e][1] }
      if G[t].Dist < 0 {
        Q[pos] = t
        pos++
        G[t].Undefined = e
        G[t].Dist = G[s].Dist + 1
      } else if E[e][0] == s {
        G[s].Degree++
      }
    }
  }
  for i := N - 1; 0 < i; i-- {
    s := Q[i]
    u := G[s].Undefined
    t := E[u][0]
    if t == s { t = E[u][1] }
    if G[s].Degree % 2 > 0 {
      E[u] = []int{s, t}
    } else {
      E[u] = []int{t, s}
      G[t].Degree++
    }
  }
  if M % 2 > 0 {
    Write(-1)
  } else {
    for i, e := range E {
      E[i] = []int{e[0] + 1, e[1] + 1}
      WriteIntVec(&E[i])
    }
  }
  Output()
}

Score : 700 points

Problem Statement

You are given a simple connected undirected graph with N vertices and M edges. The vertices are numbered 1 to N, and the i-th edge connects Vertex A_i and Vertex B_i.
Takahashi will assign one of the two possible directions to each of the edges in the graph to make a directed graph.
Determine if it is possible to make a directed graph with an even number of edges going out from every vertex. If the answer is yes, construct one such graph.

Notes

An undirected graph is said to be simple when it contains no self-loops or multiple edges.

Constraints

2 \leq N \leq 10^5

N-1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N (1\leq i\leq M)

The given graph is simple and connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If it is impossible to assign directions to satisfy the requirement, print -1.
Otherwise, print an assignment of directions that satisfies the requirement, in the following format:

C_1 D_1
:
C_M D_M

Here each pair (C_i, D_i) means that there is an edge directed from Vertex C_i to Vertex D_i. The edges may be printed in any order.

Sample Input 1

4 4
1 2
2 3
3 4
4 1

Sample Output 1

1 2
1 4
3 2
3 4

After this assignment of directions, Vertex 1 and 3 will each have two outgoing edges, and Vertex 2 and 4 will each have zero outgoing edges.

Sample Input 2

5 5
1 2
2 3
3 4
2 5
4 5

Sample Output 2

-1


//go:generate echo "https://atcoder.jp/contests/abc133/tasks/abc133_a"
package main

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

var scan = newScanner(os.Stdin)

func solve() {
}

func main() {
	N, A, B := scan.Int(), scan.Int(), scan.Int()
	fmt.Println(imin(N*A, B))
}

type scanner struct {
	*bufio.Scanner
}

func newScanner(r io.Reader) *scanner {
	s := bufio.NewScanner(r)
	s.Split(bufio.ScanWords)
	s.Buffer(nil, 100000000)
	return &scanner{s}
}

func (s *scanner) String() string {
	s.Scan()
	return s.Text()
}

func (s *scanner) Strings(l int) []string {
	if l == 0 {
		return []string{}
	}
	sl := make([]string, l, l)
	for i := range sl {
		sl[i] = s.String()
	}
	return sl
}

func (s *scanner) Int() int {
	n, _ := strconv.Atoi(s.String())
	return n
}

func (s *scanner) Ints(l int) []int {
	if l == 0 {
		return []int{}
	}
	sl := make([]int, l, l)
	for i := range sl {
		sl[i] = s.Int()
	}
	return sl
}

func (s *scanner) Float64() float64 {
	f, _ := strconv.ParseFloat(s.String(), 64)
	return f
}

func (s *scanner) Float64s(l int) []float64 {
	if l == 0 {
		return []float64{}
	}
	sl := make([]float64, l, l)
	for i := range sl {
		sl[i] = s.Float64()
	}
	return sl
}

func (s *scanner) BigInt() *big.Int {
	n, _ := strconv.ParseInt(s.String(), 10, 64)
	return big.NewInt(n)
}

func (s *scanner) BigInts(l int) []*big.Int {
	if l == 0 {
		return []*big.Int{}
	}
	sl := make([]*big.Int, l, l)
	for i := range sl {
		sl[i] = s.BigInt()
	}
	return sl
}

func iabs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

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

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

func ipow(x, n int) int {
	if n == 0 {
		return 1
	}
	a := ipow(x, n>>1)
	a *= a
	if n&1 != 0 {
		a *= x
	}
	return a
}

func isum(X []int) int {
	s := 0
	for _, x := range X {
		s += x
	}
	return s
}

func chmax(x *int, v int) {
	if *x < v {
		*x = v
	}
}

func chmin(x *int, v int) {
	if *x > v {
		*x = v
	}
}

func factor(n int) map[int]int {
	m := map[int]int{}
	for i := 2; i*i <= n; i++ {
		for n%i == 0 {
			n /= i
			m[i]++
		}
	}
	if n != 1 {
		m[n]++
	}
	return m
}

var mod = 1000000007

func setMintMod(x int) {
	mod = x
}

type mint int

func newMint(v int) mint {
	return mint((v%mod + mod) % mod)
}

func (x mint) Add(y mint) mint {
	v := x + y
	if v >= mint(mod) {
		return v - mint(mod)
	}
	return v
}

func (x mint) Sub(y mint) mint {
	v := x + mint(mod) - y
	if v >= mint(mod) {
		return v - mint(mod)
	}
	return v
}

func (x mint) Mul(y mint) mint {
	return (x * y) % mint(mod)
}

func (x mint) pow(n int) mint {
	if n == 0 {
		return mint(1)
	}
	a := x.pow(n >> 1)
	a = a.Mul(a)
	if n&1 != 0 {
		a = a.Mul(x)
	}
	return a
}

func (x mint) Inverse() mint {
	return x.pow(mod - 2)
}

func (x mint) Div(y mint) mint {
	return x * y.Inverse()
}

func (x mint) Neg() mint {
	return newMint(int(-x))
}

type factorial struct {
	fact        []mint
	factInverse []mint
}

func newFactorial(n int) *factorial {
	f := new(factorial)
	f.init(n)
	return f
}

func (f *factorial) init(n int) {
	fact := make([]mint, n+1, n+1)
	fact[0] = newMint(1)
	for i := 1; i <= n; i++ {
		fact[i] = fact[i-1].Mul(newMint(i))
	}

	inv := make([]mint, n+1, n+1)
	inv[n] = fact[n].Inverse()
	for i := n; i >= 1; i-- {
		inv[i-1] = inv[i].Mul(newMint(i))
	}

	f.fact = fact
	f.factInverse = inv
}

func (f *factorial) Get(n int) mint {
	return f.fact[n]
}

func (f *factorial) GetInverse(n int) mint {
	return f.factInverse[n]
}

func (f *factorial) Permutation(n, k int) mint {
	if k < 0 || n < k {
		return 0
	}
	return f.fact[n].Mul(f.factInverse[n-k])
}

func (f *factorial) Combination(n, k int) mint {
	if k < 0 || n < k {
		return 0
	}
	return f.fact[n].Mul(f.factInverse[k]).Mul(f.factInverse[n-k])
}

type sieve struct {
	n          int
	primes     []int
	maxFactors []int
}

func newSieve(n int) *sieve {
	s := &sieve{
		n:          n,
		primes:     []int{},
		maxFactors: make([]int, n+1, n+1),
	}
	s.init()
	return s
}

func (s *sieve) init() {
	s.maxFactors[0] = -1
	s.maxFactors[1] = -1
	for i := 2; i <= s.n; i++ {
		if s.maxFactors[i] != 0 {
			continue
		}
		s.primes = append(s.primes, i)
		s.maxFactors[i] = i
		for j := i * i; j <= s.n; j += i {
			if s.maxFactors[j] == 0 {
				s.maxFactors[j] = i
			}
		}
	}
}

func (s *sieve) IsPrime(x int) bool {
	return s.maxFactors[x] == x
}

func (s *sieve) Factor(x int) map[int]int {
	m := map[int]int{}
	for x != 1 {
		m[s.maxFactors[x]]++
		x /= s.maxFactors[x]
	}
	return m
}

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


package main

import "fmt"

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

	train := a * n
	taxi := b

	if train > taxi {
		fmt.Println(taxi)
	} else {
		fmt.Println(train)
	}
}


package main

import (
	"fmt"
	"math"
)

func main() {
	var N, A, B float64
	fmt.Scan(&N, &A, &B)
	fmt.Println(math.Min(A*N, B*N))
}


package main

import (
	"fmt"
	"math"
)

func main() {
	var N, D, X, r int
	fmt.Scan(&N, &D)
	var x [][]int
	for i := 0; i < N; i++ {
		w := make([]int, D)
		for j := 0; j < D; j++ {
			fmt.Scan(&X)
			w[j] = X
		}
		x = append(x, w)
	}
	for i := 0; i < N-1; i++ {
		for j := i + 1; j < N; j++ {
			p := 0
			for k := 0; k < D; k++ {
				q := x[i][k] - x[j][k]
				p += q * q
			}
			n := math.Sqrt(float64(p))
			if n == float64(int64(n)) {
				r++
			}

		}
	}
	fmt.Println(r)
}

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


package main

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

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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

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

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	N := getInt()
	a := make([]int, N)
	for i := 0; i < N; i++ {
		a[i] = getInt()
	}
	b := 0
	for i := 0; i < N; i++ {
		if i%2 == 0 {
			b += a[i]
		} else {
			b -= a[i]
		}
	}

	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	fmt.Fprint(w, b, " ")
	for i := 0; i < N-1; i++ {
		b = a[i] - b/2
		b *= 2
		fmt.Fprint(w, b, " ")
	}
	fmt.Fprintln(w)
}

Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0


package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.ParseInt(sc.Text(), 10, 64)
	if e != nil {
		panic(e)
	}
	return int(i)
}

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

func main() {
	n := nextInt()
	lis := make([]int, 0, n)

	for i := 0; i < n; i++ {
		lis = append(lis, nextInt())
	}

	var x2 int
	for i, item := range lis {
		if i%2 == 0 {
			x2 += item
		} else {
			x2 -= item
		}
	}

	ans := make([]int, len(lis), len(lis))

	ans[0] = x2 / 2
	for i := 1; i < n; i++ {
		ans[i] = lis[i-1] - ans[i-1]
	}

	for _, item := range ans {
		fmt.Printf("%#v ", item*2)
	}
	fmt.Println()
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	a := make([]int, n)
	s := 0
	for i := 0; i < n; i++ {
		a[i] = nextInt()
		s += a[i]
	}

	x := make([]int, n)
	for i := 1; i < n; i += 2 {
		s -= 2 * a[i]
	}
	x[0] = s

	for i := 0; i < n-1; i++ {
		x[i+1] = 2*a[i] - x[i]
	}

	fmt.Println(x)

}


package main

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

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

	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)

	nodes := make([][][]int, n)
	for i := 0; i < n; i++ {
		nodes[i] = [][]int{}
	}

	for i := 0; i < n-1; i++ {
		scanner.Scan()
		a, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		c, _ := strconv.Atoi(scanner.Text())
		scanner.Scan()
		d, _ := strconv.Atoi(scanner.Text())
		a--
		b--
		c--
		nodes[a] = append(nodes[a], []int{b, c, d})
		nodes[b] = append(nodes[b], []int{a, c, d})
	}

	log2n := 17
	parents := make([][]int, n)
	depths := make([]int, n)
	for i := 0; i < n; i++ {
		parents[i] = make([]int, log2n+1)
	}

	queues := [][]int{
		{0, 0, 0},
	}
	for len(queues) > 0 {
		node := queues[0][0]
		parent := queues[0][1]
		depth := queues[0][2]

		parents[node][0] = parent
		depths[node] = depth

		for i := 0; i < len(nodes[node]); i++ {
			if nodes[node][i][0] == parent {
				continue
			}
			queues = append(queues, []int{nodes[node][i][0], node, depth + 1})
		}

		queues = queues[1:]
	}

	for i := 1; i <= log2n; i++ {
		for j := 0; j < n; j++ {
			parents[j][i] = parents[parents[j][i-1]][i-1]
		}
	}

	x := make([]int, q)
	y := make([]int, q)
	u := make([]int, q)
	v := make([]int, q)
	lca := make([]int, q)
	need := make([]map[int]bool, n)
	for i := 0; i < n; i++ {
		need[i] = make(map[int]bool)
	}

	for i := 0; i < q; i++ {
		scanner.Scan()
		x[i], _ = strconv.Atoi(scanner.Text())
		x[i]--
		scanner.Scan()
		y[i], _ = strconv.Atoi(scanner.Text())
		scanner.Scan()
		u[i], _ = strconv.Atoi(scanner.Text())
		u[i]--
		scanner.Scan()
		v[i], _ = strconv.Atoi(scanner.Text())
		v[i]--

		uu := u[i]
		vv := v[i]
		xx := x[i]
		if depths[uu] > depths[vv] {
			uu, vv = vv, uu
		}

		a := uu
		b := vv
		diff := depths[vv] - depths[uu]
		for j := uint(0); j <= uint(log2n); j++ {
			if (diff>>j)&1 == 1 {
				b = parents[b][j]
			}
		}
		for j := log2n; j >= 0; j-- {
			if parents[a][j] != parents[b][j] {
				a = parents[a][j]
				b = parents[b][j]
			}
		}
		lca[i] = parents[a][0]

		need[uu][xx] = true
		need[vv][xx] = true
		need[lca[i]][xx] = true
 	}

	prefixSum := make([]int64, n)
	totalColorCount := make([]int, n-1)
	totalColorDist := make([]int64, n-1)
	prefixColorCount := make([][]int, n)
	prefixColorDist := make([][]int64, n)
	for i := 0; i < n; i++ {
		prefixColorCount[i] = make([]int, n-1)
		prefixColorDist[i] = make([]int64, n-1)
	}

	var dfs func(node int, parent int)
	dfs = func(node int, parent int) {
		for c := range need[node] {
			prefixColorCount[node][c] = totalColorCount[c]
			prefixColorDist[node][c] = totalColorDist[c]
		}

		for i := 0; i < len(nodes[node]); i++ {
			child := nodes[node][i][0]
			if child == parent {
				continue
			}

			c := nodes[node][i][1]
			d := nodes[node][i][2]

			prefixSum[child] = prefixSum[node] + int64(d)

			totalColorCount[c]++
			totalColorDist[c] += int64(d)
			dfs(child, node)
			totalColorCount[c]--
			totalColorDist[c] -= int64(d)
		}
	}
	dfs(0, 0)

	/*
	queues = [][]int{{0, 0, 0, -1, 0}}
	for len(queues) > 0 {
		op := queues[0][0]
		node := queues[0][1]
		parent := queues[0][2]
		color := queues[0][3]
		dist := queues[0][4]

		queues = queues[1:]

		if op == 0 {
			if color != -1 {
				totalColorCount[color]++
				totalColorDist[color] += int64(dist)
			}

			for c := range need[node] {
				prefixColorCount[node][c] = totalColorCount[c]
				prefixColorDist[node][c] = totalColorDist[c]
			}

			for i := 0; i < len(nodes[node]); i++ {
				child := nodes[node][i][0]
				if child == parent {
					continue
				}

				c := nodes[node][i][1]
				d := nodes[node][i][2]

				prefixSum[child] = prefixSum[node] + int64(d)

				queues = append([][]int{{0, child, node, c, d}, {1, child, node, c, d}}, queues...)
			}
		} else {
			totalColorCount[color]--
			totalColorDist[color] -= int64(dist)
		}
	}
	 */

	for i := 0; i < q; i++ {
		uu := u[i]
		vv := v[i]
		xx := x[i]
		yy := y[i]
		ll := lca[i]

		du := prefixSum[uu] - prefixColorDist[uu][xx] + int64(prefixColorCount[uu][xx]*yy)
		dv := prefixSum[vv] - prefixColorDist[vv][xx] + int64(prefixColorCount[vv][xx]*yy)
		dlca := prefixSum[ll] - prefixColorDist[ll][xx] + int64(prefixColorCount[ll][xx]*yy)
		d := du + dv - 2*dlca
		fmt.Println(d)
	}
}

Score : 600 points

Problem Statement

There is a tree with N vertices numbered 1 to N.
The i-th edge in this tree connects Vertex a_i and Vertex b_i, and the color and length of that edge are c_i and d_i, respectively.
Here the color of each edge is represented by an integer between 1 and N-1 (inclusive). The same integer corresponds to the same color, and different integers correspond to different colors.

Answer the following Q queries:

Query j (1 \leq j \leq Q): assuming that the length of every edge whose color is x_j is changed to y_j, find the distance between Vertex u_j and Vertex v_j. (The changes of the lengths of edges do not affect the subsequent queries.)

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

1 \leq a_i, b_i \leq N

1 \leq c_i \leq N-1

1 \leq d_i \leq 10^4

1 \leq x_j \leq N-1

1 \leq y_j \leq 10^4

1 \leq u_j < v_j \leq N

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1 c_1 d_1
:
a_{N-1} b_{N-1} c_{N-1} d_{N-1}
x_1 y_1 u_1 v_1
:
x_Q y_Q u_Q v_Q

Output

Print Q lines. The j-th line (1 \leq j \leq Q) should contain the answer to Query j.

Sample Input 1

5 3
1 2 1 10
1 3 2 20
2 4 4 30
5 2 1 40
1 100 1 4
1 100 1 5
3 1000 3 4

Sample Output 1

130
200
60

The graph in this input is as follows:

Here the edges of Color 1 are shown as solid red lines, the edge of Color 2 is shown as a bold green line, and the edge of Color 4 is shown as a blue dashed line.

Query 1: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 4 is 100 + 30 = 130.

Query 2: Assuming that the length of every edge whose color is 1 is changed to 100, the distance between Vertex 1 and Vertex 5 is 100 + 100 = 200.

Query 3: Assuming that the length of every edge whose color is 3 is changed to 1000 (there is no such edge), the distance between Vertex 3 and Vertex 4 is 20 + 10 + 30 = 60. Note that the edges of Color 1 now have their original lengths.


package main

import (
	"fmt"
	"strings"
)

func main() {
	var s string
	fmt.Scan(&s)

	sliceS := strings.Split(s, "")
	m := make(map[string]int)
	for _, val := range sliceS {
		m[val]++
	}

	flag := 0
	if len(m) == 2 {
		for _, val := range m {
			if val != 2 {
				flag = 1
			}
		}

		if flag == 0 {
			fmt.Println("Yes")
		}
	} else {
		fmt.Println("No")
	}
}

Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


package main

import "fmt"

func main()  {
	var s string
	fmt.Scan(&s)

	m := make(map[uint8]int)
	for j:=0;j<4;j++ {
		if _, ok := m[s[j]]; ok {
			m[s[j]]++
		} else {
			m[s[j]] = 1
		}
	}
	for _, v := range m {
		if v != 2 {
			fmt.Println("No")
			return
		}
	}
	fmt.Println("Yes")
}


package main

import (
	"fmt"
	"sort"
)

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

	p := make([]int,n)
	for i,_ := range p {
		fmt.Scan(&p[i])
	}

	cnt := 0
	for i := 1; i < len(p)-1; i++ {
		target := []int{p[i-1],p[i],p[i+1]}
		sort.Ints(target)
		if p[i] == target[1] {
			cnt++
		}
	}
	fmt.Println(cnt)
}

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


package main

import "fmt"

func main() {
	var n int
	fmt.Scanf("%d", &n)
	P := make([]int, n)

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

	ans := 0
	for i := 1; i < n-1; i++ {
		if P[i] > P[i-1] && P[i] < P[i+1] {
			ans++
		} else if P[i] < P[i-1] && P[i] > P[i+1] {
			ans++
		}
	}
	fmt.Println(ans)
}


package main

import (
	"fmt"
	"sort"
)

func main() {
	var n int
	fmt.Scan(&n)
	if n%2 != 0 {
		fmt.Println(0)
		return
	}
	ds := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&ds[i])
	}
	sort.Sort(sort.IntSlice(ds))
	if ds[n/2] == ds[n/2-1] {
		fmt.Println(0)
		return
	} else {
		fmt.Println(ds[n/2] - ds[n/2-1])
	}

}

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


package main

import (
	"fmt"
	"sort"
	"unicode/utf8"
)

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

	inputs := scanNums(N)

	sort.Sort(sort.IntSlice(inputs))
	// fmt.Println(inputs)
	a := inputs[N/2-1]
	b := inputs[N/2]
	// fmt.Println(a, b)

	if b == a {
		fmt.Println(0)
	} else {

		fmt.Println(b - a)
	}

}

func scanStrings(len int) (strings []string) {
	var str string
	for i := 0; i < len; i++ {
		fmt.Scanf("%s", &str)
		strings = append(strings, str)
	}
	return
}

func scanNums(len int) (nums []int) {
	var num int
	for i := 0; i < len; i++ {
		fmt.Scan(&num)
		nums = append(nums, num)
	}
	return
}

func spritString(input string) (res []string) {
	for i := 0; i < utf8.RuneCountInString(input); i++ {
		res = append(res, input[i:i+1])
	}
	return
}


package main
import(
  "fmt"
)
func main(){
  var n, i, j, temp int
  fmt.Scan(&n)
  a:=make([]int, n)
  for i=0;i<n;i++{
    for j=n-1;j>i;j--{
      if a[j]<a[j-1]{
        temp=a[j]
        a[j]=a[j-1]
        a[j-1]=temp
      }
    }
  }
  fmt.Println(a[n/2]-a[n/2-1])
}


// パスカルの三角形
package main

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

const (
	m = 1000000007
)

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

func main() {
	defer flush()

	N := readInt()
	K := readInt()

	n := max(K, N-K+1)
	c := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		c[i] = make([]int, n+1)
	}
	c[0][0] = 1
	for i := 0; i < n+1; i++ {
		c[i][0] = 1
		for j := 1; j < i+1; j++ {
			c[i][j] = (c[i-1][j-1] + c[i-1][j]) % m
		}
	}

	for i := 1; i <= K; i++ {
		println(c[K-1][i-1] * c[N-K+1][i] % m)
	}
}

const (
	ioBufferSize = 1 * 1024 * 1024 // 1 MB
)

var stdinScanner = func() *bufio.Scanner {
	result := bufio.NewScanner(os.Stdin)
	result.Buffer(make([]byte, ioBufferSize), ioBufferSize)
	result.Split(bufio.ScanWords)
	return result
}()

func readString() string {
	stdinScanner.Scan()
	return stdinScanner.Text()
}

func readInt() int {
	result, err := strconv.Atoi(readString())
	if err != nil {
		panic(err)
	}
	return result
}

var stdoutWriter = bufio.NewWriter(os.Stdout)

func flush() {
	stdoutWriter.Flush()
}

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdoutWriter, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdoutWriter, args...)
}

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


package main

import "fmt"

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

	ans := 0
	min := 1000
	for i := 1 ; i  <= n ;i++ {
		ans += l + i - 1
		if  min > abs(l + i - 1 ) {
			min =  abs(l + i - 1 )
		}
	}
	fmt.Println(ans - min)
}
func abs(n int) int {
	y := n >> 63
	return (n ^ y) - y
}

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


package main
import "fmt"
func main(){
    var n, l int
    fmt.Scan(&n, &l)
    sum := l * n + n * (n - 1) / 2
    if l <= 0 && (n + l - 1) >= 0 {
        fmt.Println(sum)
    }else if l > 0 {
        fmt.Println(sum - l)
    }else{
        fmt.Println(sum - (n + l - 1))
    }
}


package main

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

type Job struct {
	A int
	B int
}

type Jobs []Job

func (j Jobs) Len() int {
	return len(j)
}

func (j Jobs) Swap(x, y int) {
	j[x], j[y] = j[y], j[x]
}

func (j Jobs) Less(x, y int) bool {
	return j[x].B < j[y].B
}

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

	var jobs Jobs = []Job{}
	for i := 0; i < n; i++ {
		fmt.Scan(&a, &b)
		jobs = append(jobs, Job{a, b})
	}

	sort.Sort(jobs)

	time := 0
	for i := 0; i < n; i++ {
		time += jobs[i].A
		if time > jobs[i].B {
			fmt.Println("No")
			os.Exit(0)
		}
	}
	fmt.Println("Yes")
}

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


package main

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

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

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

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

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

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

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	N, K := getInt(), getInt()

	maxk := (N - 1) * (N - 2) / 2
	maxm := N * (N - 1) / 2

	if maxk < K {
		out(-1)
		return
	}

	m := maxm - K
	fmt.Println(m)
	cnt := 0
	for i := 1; i <= N; i++ {
		for j := i + 1; j <= N; j++ {
			if cnt < K && j != N {
				cnt++
				continue
			}
			fmt.Println(i, j)
		}

	}

}

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


package main

import (
	"fmt"
)

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

	L := make([]int, N+1)
	var D []int

	for i:=0; i < N; i++ {
		fmt.Scan(&L[i])
	}
	D = append(D, 0)
	qtd := 0
	for i:=0; D[i] <= X; i++ {
		D = append(D, D[i]+L[i])
		qtd++
		//fmt.Println(D[i], qtd)
	}
	fmt.Println(qtd)
}

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


package main

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

var sc *bufio.Scanner = func() *bufio.Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	return sc
}()

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func nextInts(n int) []int {
	ints := make([]int, n)
	for i := 0; i < n; i++ {
		ints[i] = nextInt()
	}
	return ints
}

func countBounding(x int, ls []int) (boundNum int) {
	boundNum = 1
	n := len(ls)
	d := make([]int, n+1)
	d[0] = 0
	for i, l := range ls {
		next := d[i] + l
		if x < next {
			break
		}
		boundNum++
		d[i+1] = next
	}
	return
}

func main() {
	n, x := nextInt(), nextInt()
	ints := nextInts(n)
	r := countBounding(x, ints)
	fmt.Println(r)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func main() {
	sc.Split(bufio.ScanWords)
	w := nextInt()
	h := nextInt()
	x := nextInt()
	y := nextInt()

	area := float64(w) * float64(h) / 2.0
	fmt.Printf("%v ", area)
	if x == w/2 && y == h/2 {
		fmt.Println(1)
	} else {
		fmt.Println(0)
	}
}

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


package main

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

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n := scanInt()
	m := scanInt()
	s := scanInts(n)
	t := scanInts(m)

	dp := make([][]int, n+1)
	for i := 0; i < n+1; i++ {
		dp[i] = make([]int, m+1)
	}
	for i := 0; i < m+1; i++ {
		dp[0][i] = 1
	}

	for i := 0; i < n; i++ {
		dp[i+1][0] = 1
		for j := 0; j < m; j++ {
			dp[i+1][j+1] = dp[i][j+1] + dp[i+1][j]
			if s[i] != t[j] {
				dp[i+1][j+1] -= dp[i][j]
			}

			dp[i+1][j+1] = (MOD+dp[i+1][j+1])%MOD
		}
		// debug(dp[i+1])
	}

	fmt.Fprintln(wr, dp[n][m])
}

// MOD constant
const MOD = 1000000007

Score : 500 points

Problem Statement

You are given two integer sequences S and T of length N and M, respectively, both consisting of integers between 1 and 10^5 (inclusive).

In how many pairs of a subsequence of S and a subsequence of T do the two subsequences are the same in content?

Here the subsequence of A is a sequence obtained by removing zero or more elements from A and concatenating the remaining elements without changing the order.

For both S and T, we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Since the answer can be tremendous, print the number modulo 10^9+7.

Constraints

1 \leq N, M \leq 2 \times 10^3

The length of S is N.

The length of T is M.

1 \leq S_i, T_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
S_1 S_2 ... S_{N-1} S_{N}
T_1 T_2 ... T_{M-1} T_{M}

Output

Print the number of pairs of a subsequence of S and a subsequence of T such that the subsequences are the same in content, modulo 10^9+7.

Sample Input 1

2 2
1 3
3 1

Sample Output 1

3

S has four subsequences: (), (1), (3), (1, 3).

T has four subsequences: (), (3), (1), (3, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 1 \times 1 pair of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (3), for a total of three pairs.

Sample Input 2

2 2
1 1
1 1

Sample Output 2

6

S has four subsequences: (), (1), (1), (1, 1).

T has four subsequences: (), (1), (1), (1, 1).

There are 1 \times 1 pair of subsequences in which the subsequences are both (), 2 \times 2 pairs of subsequences in which the subsequences are both (1), and 1 \times 1 pair of subsequences in which the subsequences are both (1,1), for a total of six pairs.
Note again that we distinguish two subsequences if the sets of the indices of the removed elements are different, even if the subsequences are the same in content.

Sample Input 3

4 4
3 4 5 6
3 4 5 6

Sample Output 3

16

Sample Input 4

10 9
9 6 5 7 5 9 8 5 6 7
8 6 8 5 5 7 9 9 7

Sample Output 4

191

Sample Input 5

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample Output 5

846527861

Be sure to print the number modulo 10^9+7.


package main

import "fmt"

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

	if k == 1 {
		fmt.Println(0)
	} else {
		fmt.Println(n - k)
	}
}

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


package main

import (
	"fmt"
)

type Point struct{ X, Y int }
type Key struct{ Dx, Dy int }

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

	if n == 1 {
		fmt.Println(1)
		return
	}

	ps := make([]Point, n)
	for i := range ps {
		fmt.Scan(&ps[i].X, &ps[i].Y)
	}

	xm := make(map[Key]int)
	for _, v1 := range ps {
		for _, v2 := range ps {
			if v1 == v2 {
				continue
			}
			xm[Key{v1.X - v2.X, v1.Y - v2.Y}]++
		}
	}

	max := 0
	for _, v := range xm {
		if v > max {
			max = v
		}
	}
	fmt.Println(n - max)
}

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


package main

import (
	"fmt"
	"sort"
)

type area struct {
	x int
	y int
}

type areas []area

func (d areas) Len() int {
	return len(d)
}

func (d areas) Swap(i, j int) {
	d[i], d[j] = d[j], d[i]
}

func (d areas) Less(i, j int) bool {
	return d[i].x < d[j].x
}

func main() {
	var n int
	fmt.Scan(&n)
	ar := make(areas, n)
	for i := range ar {
		fmt.Scan(&ar[i].x, &ar[i].y)
	}
	if n == 1 {
		fmt.Println(1)
		return
	}
	sort.Sort(ar)
	mp := map[area]int{}
	for i := 0; i < n-1; i++ {
		buf := map[area]bool{}
		for j := i + 1; j < n; j++ {
			v := area{}
			v.x = ar[i].x - ar[j].x
			v.y = ar[i].y - ar[j].y
			if buf[v] {
				continue
			}
			mp[v]++
			buf[v] = true
		}
	}
	max := 0
	for k, v := range mp {
		if k.x == 0 && k.y == 0 {
			continue
		}
		if max < v {
			max = v
		}
	}
	fmt.Println(n - max)
}


package main

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

func main() {
	io := NewIo()
	defer io.Flush()

	n := io.NextInt()

	nums := []int{}

	positives := []int{}
	negatives := []int{}
	zeros := []int{}
	for i := 0; i < n; i++ {
		n := io.NextInt()
		nums = append(nums, n)

		if n > 0 {
			positives = append(positives, n)
		} else if n <= 0 {
			negatives = append(negatives, n)
		} else {
			zeros = append(zeros, n)
		}
	}

	lateShow := []string{}

	if len(negatives) == 0 {
		if len(zeros) == 0 {
			if positives[0] > positives[1] {
				lateShow = append(lateShow, fmt.Sprintln(positives[1], positives[0]))
			} else {
				lateShow = append(lateShow, fmt.Sprintln(positives[0], positives[1]))
			}
			negatives = append(negatives, -abs(positives[0]-positives[1]))
			positives = positives[2:]
			n--
		} else {
			negatives = append(negatives, zeros...)
		}
	} else if len(positives) == 0 {
		if len(zeros) == 0 {
			if negatives[0] > negatives[1] {
				lateShow = append(lateShow, fmt.Sprintln(negatives[0], negatives[1]))
			} else {
				lateShow = append(lateShow, fmt.Sprintln(negatives[1], negatives[0]))
			}
			positives = append(positives, abs(negatives[0]-negatives[1]))
			negatives = negatives[2:]
			n--

		} else {
			positives = append(positives, zeros...)

		}
	}

	ans := 0
	for _, n := range positives {
		ans += abs(n)
	}

	for _, n := range negatives {
		ans += abs(n)
	}

	fmt.Println(ans)

	for _, s := range lateShow {
		if s != "" {
			fmt.Print(s)
		}

	}

	for i := 0; i < n-1; i++ {
		plus := positives[0]
		minus := negatives[0]
		//fmt.Println("hogheo", plus, minus)
		//fmt.Println("hogheo", plus, minus)
		if len(positives) > len(negatives) {
			fmt.Println(minus, plus)
			negatives = append(negatives, minus-plus)
		} else {
			fmt.Println(plus, minus)
			positives = append(positives, plus-minus)
		}

		positives = positives[1:]
		negatives = negatives[1:]
	}
}

type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buffer []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buffer = append(buffer, line...)
		if !isPrefix {
			break
		}
	}
	return string(buffer)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	r := io.tokens[io.nextToken]
	io.nextToken++
	return r
}

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

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) PrintLn(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntLn(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.PrintLn(b...)
}

func (io *Io) PrintStringLn(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.PrintLn(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func abs(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func pow(p, q int) int {
	return int(math.Pow(float64(p), float64(q)))
}

func min(nums ...int) int {
	if len(nums) == 0 {
		panic("funciton min() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Min(float64(res), float64(nums[i])))
	}
	return res
}

func max(nums ...int) int {
	if len(nums) == 0 {
		panic("funciton max() requires at least one argument.")
	}
	res := nums[0]
	for i := 0; i < len(nums); i++ {
		res = int(math.Max(float64(res), float64(nums[i])))
	}
	return res
}

func strSearch(a []string, b string) bool {
	for i := 0; i < len(a); i++ {
		if a[i] == b {
			return true
		}
	}
	return false
}

func sortAsc(a []int) []int {
	sort.Ints(a)
	return a
}

func sortDesc(a []int) []int {
	sort.Sort(sort.Reverse(sort.IntSlice(a)))
	return a
}

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

func lcm(a, b int) int {
	if a == 0 && b == 0 {
		return 0
	}
	return a * b / gcd(a, b)
}

func cumSum(nums []int) []int {
	sums := []int{0}

	for i := 0; i < len(nums); i++ {
		sums = append(sums, sums[i]+nums[i])
	}
	return sums
}

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


package main

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

func main() {
  sc := bufio.NewScanner(os.Stdin)
  sc.Split(bufio.ScanWords)

  sc.Scan()
  p, _ := strconv.Atoi(sc.Text())

  sc.Scan()
  q, _ := strconv.Atoi(sc.Text())

  sc.Scan()
  r, _ := strconv.Atoi(sc.Text())

  if p + q < q + r {
    if p + q < r + p {
      fmt.Println(p + q)
    } else {
      fmt.Println(r + p)
    }
  } else {
    if q + r < r + p {
      fmt.Println(q + r)
    } else {
      fmt.Println(r + p)
    }
  }

}

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


package main

import (
	"fmt"
	"math"
)

func main() {
	var N int
	fmt.Scan(&N)
	W := make([]int, N)
	sum := 0
	for i := 0; i < N; i++ {
		fmt.Scan(&W[i])
		sum += W[i]
	}

	ans := sum
	p := 0
	for i := 0; i < N; i++ {
		p += W[i]
		ans = int(math.Min(
			float64(ans),
			math.Abs(float64(p-(sum-p))),
		))
	}
	fmt.Println(ans)
}

Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

2 \leq N \leq 100

1 \leq W_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.

Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2


package main

import (
	"fmt"
)

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

	sum := 0
	ws := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scan(&ws[i])
		sum += ws[i]
	}

	ret := 0
	tmp := 0
	for i := 0; i < n; i++ {
		tmp += ws[i]
		if tmp >= (sum / 2) {
			calc1 := getAbs(tmp, sum-tmp)
			calc2 := getAbs(tmp-ws[i], sum-tmp-ws[i])
			if calc1 > calc2 {
				ret = calc2
			} else {
				ret = calc1
			}
			break
		}
	}

	fmt.Println(ret)
}

func getAbs(a, b int) int {
	tmp := a - b
	if tmp > 0 {
		return tmp
	} else {
		return -tmp
	}
}


package main

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

var (
	readString func() string
	readBytes  func() []byte
	stdout     *bufio.Writer
)

func init() {
	readString, readBytes = newReadString(os.Stdin)
	stdout = bufio.NewWriter(os.Stdout)
}

func newReadString(ior io.Reader) (func() string, func() []byte) {
	r := bufio.NewScanner(ior)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)

	f1 := func() string {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Text()
	}
	f2 := func() []byte {
		if !r.Scan() {
			panic("Scan failed")
		}
		return r.Bytes()
	}
	return f1, f2
}

func readInt() int {
	return int(readInt64())
}

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

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

func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(stdout, f, args...)
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdout, args...)
}

func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

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

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

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

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

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

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

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

func gcd64(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return gcd64(b, a%b)
}

// readString() string
// readInt() int
// readInt64() int64
// readFloat64() float64

// -----------------------------------------------------------------------------

func main() {
	defer stdout.Flush()
	N := readInt()
	W := make([]int, N)
	var ans int
	for i := range W {
		W[i] = readInt()
		ans += W[i]
	}

	for i := 0; i < N-1; i++ {
		var s1, s2, j int
		for ; j <= i; j++ {
			s1 += W[j]
		}
		for ; j < N; j++ {
			s2 += W[j]
		}
		ans = min(ans, abs(s1-s2))
	}
	println(ans)
}

// -----------------------------------------------------------------------------


package main

import (
	"fmt"
)

func COM(n, k int64) int64 {
	r := int64(1)
	for i := int64(0); i < k; i++ {
		r = ModMul(r, n-i)
		r = ModDiv(r, i+1)
	}

	return r
}

const (
	MOD = 1000000000 + 7
)

func ModAdd(a, b int64) int64 {
	return (a + b) % MOD
}

func ModMul(a, b int64) int64 {
	return a * b % MOD
}

func ModSub(a, b int64) int64 {
	r := (a%MOD - b%MOD) % MOD
	if r < 0 {
		r += MOD
	}
	return r
}

func ModDiv(a, b int64) int64 {
	return a % MOD * ModInv(b) % MOD
}

func ModPow(a, n int64) int64 {
	var r int64 = 1
	for n > 0 {
		if n&1 == 1 {
			r = r * a % MOD
		}
		a = a * a % MOD
		n >>= 1
	}
	return r
}

func ModInv(a int64) int64 {
	var b, u, v int64 = MOD, 1, 0
	for b > 0 {
		t := a / b
		a -= t * b
		a, b = b, a
		u -= t * v
		u, v = v, u
	}
	u %= MOD
	if u < 0 {
		u += MOD
	}
	return u
}

type Q struct {
	value int
	index int
	A, B  int
}

type QS []*Q

func (p QS) Len() int           { return len(p) }
func (p QS) Less(i, j int) bool { return p[i].value > p[j].value }
func (pq QS) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	pq[i].index = i
	pq[j].index = j
}

func (pq *QS) Push(x interface{}) {
	n := len(*pq)
	item := x.(*Q)
	item.index = n
	*pq = append(*pq, item)
}

func (pq *QS) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	old[n-1] = nil  // avoid memory leak
	item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

func FactoredPrimeNumber(num int) map[int]int {
	m := map[int]int{}
	return factor(m, num, 2)
}

func factor(result map[int]int, num, pnum int) map[int]int {
	if pnum*pnum > num {
		if num != 1 {
			result[num] += 1
		}
		return result
	}

	if num%pnum == 0 {
		num /= pnum
		result[pnum]++
	} else {
		pnum++
	}
	return factor(result, num, pnum)
}

func GCD(a, b int64) int64 {
	if b == 0 {
		return a
	}
	return GCD(b, a%b)
}

func LCM(a, b int64) int64 {
	return a * b / GCD(a, b)
}

func main() {
	var N, M int
	fmt.Scanf("%d %d\n", &N, &M)
	m := map[int]bool{}
	for i := 0; i < M; i++ {
		var w int
		fmt.Scanf("%d\n", &w)
		m[w] = true
	}
	dp := [1000000]int{}
	dp[0] = 1
	for i := 0; i <= N; i++ {
		for j := i + 1; j <= N && j <= i+2; j++ {
			if m[j] {
				continue
			}
			dp[j] += dp[i]
			dp[j] %= MOD
		}
	}
	fmt.Println(dp[N])
}

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

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.