package main

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

)

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

type ReaderEx struct {
	baseScanner *bufio.Scanner
	words       []string
	nextWordIdx func() int
	initWordIdx func()
}

func NewScannerEx(stdin io.Reader) *ReaderEx {
	var this ReaderEx
	var idx int
	this.baseScanner = bufio.NewScanner(stdin)
	this.baseScanner.Buffer(make([]byte, 0), 1E+11)
	this.nextWordIdx = func() int {
		cur := idx
		idx++
		return cur
	}
	this.initWordIdx = func() {
		idx = 0
	}
	return &this
}

func (me *ReaderEx) ScanAndSplit() (isScanned bool) {
	isScanned = me.baseScanner.Scan()

	me.words = strings.Fields(me.baseScanner.Text())
	me.initWordIdx()
	return
}

func (me *ReaderEx) Scan() (ok bool) {
	ok = me.baseScanner.Scan()
	return
}
func (me *ReaderEx) getString() string {
	return me.baseScanner.Text()
}

func (me *ReaderEx) getInt() int {
	n, err := strconv.Atoi(me.baseScanner.Text())
	defer func() {
		if err != nil {
			panic(err)
		}
	}()

	return n
}

func (me *ReaderEx) nextBytes() []byte {
	return []byte(me.words[me.nextWordIdx()])
}
func (me *ReaderEx) nextString() string {
	return me.words[me.nextWordIdx()]
}

type StringArray []string

func (me *ReaderEx) nextStringArray() StringArray {
	return me.words[me.nextWordIdx():]
}

func (me StringArray) toStrings() []string {
	return me
}

func (me StringArray) toInts() []int {
	strValues := me.toStrings()
	values := make([]int, len(strValues))
	for i, v := range strValues {
		n, err := strconv.Atoi(v)
		if err != nil {
			panic(err)
		}
		values[i] = n
	}
	return values
}
func (me *ReaderEx) nextInt() int {
	n, err := strconv.Atoi(me.words[me.nextWordIdx()])

	defer func() {
		if err != nil {
			panic(fmt.Sprintf("getInt(%d)", n))
		}
	}()
	return n
}

func pow(x, y, mod int) (ans int) {

	ans = 1
	for bit := 1 << uint(math.Floor(math.Log2(float64(y)))); ; bit >>= 1 {
		if y&bit != 0 {
			ans *= x
			if mod > 0 {
				ans %= mod
			}
		}
		if bit <= 1 {
			return
		}
		ans *= ans
		if mod > 0 {
			ans %= mod
		}
	}
}

type factType struct {
	prime int
	count int
}

func fact(n int) (facts []factType) {

	for i := 2; i*i <= n; i++ {
		if n%i == 0 {
			fa := factType{prime: i}
			for n%fa.prime == 0 {
				n /= fa.prime
				fa.count++
			}
			facts = append(facts, fa)
		}
	}
	if n > 1 {
		facts = append(facts, factType{prime: n, count: 1})
	}
	return facts
}
func dec2bin(n int) (ret []bool) {
	for bit := 1 << uint(math.Floor(math.Log2(float64(n)))); ; bit >>= 1 {
		ret = append(ret, n&bit != 0)

		if bit <= 1 {
			return
		}
	}

}

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

func solve(stdin io.Reader, stdout io.Writer) {
	r := NewScannerEx(stdin)
	r.ScanAndSplit()
	n, y := r.nextInt(), r.nextInt()

	satu := []int{10000, 5000, 1000}
	yBy10000 := y / 10000

	var ans = []int{-1, -1, -1}
loop:
	for i := 0; i < yBy10000; i++ {
		rest := y - i*satu[0]

		yBy5000 := rest / satu[1]
		for j := 0; j < yBy5000; j++ {
			rest -= j * satu[1]

			if rest%satu[2] == 0 {
				k := rest / satu[2]

				if i+j+k == n {
					ans = []int{i, j, k}
					break loop
				}
			}
		}
	}
	fmt.Fprintln(stdout, func(x string) string { return x[1 : len(x)-1] }(fmt.Sprint(ans)))

}


package main

import (
	"fmt"
)

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

	var slice [3]int
	var judge bool
	for i := 0; i <= n; i++ {
		for j := 0; j <= n; j++ {
			for k := 0; k <= n; k++ {
				if i+j+k > n {
					break
				}
				if 10000*i+5000*j+1000*k == yen {
					slice[0] = i
					slice[1] = j
					slice[2] = k
					judge = true
				}
			}
			if judge || i+j > n {
				break
			}
		}
		if judge {
			break
		}
	}
	if !judge {
		slice[0] = -1
		slice[1] = -1
		slice[2] = -1
	}
	fmt.Println(slice)

}

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

Score : 300 points

Problem Statement

The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.

According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Constraints

1 ≤ N ≤ 2000

1000 ≤ Y ≤ 2 × 10^7

N is an integer.

Y is a multiple of 1000.

Input

Input is given from Standard Input in the following format:

N Y

Output

If the total value of N bills cannot be Y yen, print -1 -1 -1.

If the total value of N bills can be Y yen, let one such set of bills be "x 10000-yen bills, y 5000-yen bills and z 1000-yen bills", and print x, y, z with spaces in between. If there are multiple possibilities, any of them may be printed.

Sample Input 1

9 45000

Sample Output 1

4 0 5

If the envelope contained 4 10000-yen bills and 5 1000-yen bills, he had 9 bills and 45000 yen in total. It is also possible that the envelope contained 9 5000-yen bills, so the output 0 9 0 is also correct.

Sample Input 2

20 196000

Sample Output 2

-1 -1 -1

When the envelope contained 20 bills in total, the total value would be 200000 yen if all the bills were 10000-yen bills, and would be at most 195000 yen otherwise, so it would never be 196000 yen.

Sample Input 3

1000 1234000

Sample Output 3

14 27 959

There are also many other possibilities.

Sample Input 4

2000 20000000

Sample Output 4

2000 0 0

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 (
	"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
}

type xyz struct {
	x int
	y int
	z int
}

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

	N := nextInt()
	Y := nextInt()
	//N := 9
	//Y := 45000
	slice := []xyz{}

	L:
	for i := 0; i < N+1; i++ {
		x := N - i
		for j := 0; j < x+1; j++ {
			z := N - i - j
			if 10000*i+5000*j+1000*z == Y {
				slice = append(slice, xyz{
					x: i,
					y: j,
					z: z,
				})
				break L
			}

		}
	}

	if len(slice) == 0 {
		fmt.Printf("-1 -1 -1")
	} else {
		x := slice[0].x
		y := slice[0].y
		z := slice[0].z

		fmt.Printf("%d %d %d", x, y, z)
	}
}


package main

import "fmt"

func main() {
	var N, Y int
	fmt.Scan(&N, &Y)
	for i := 0; i <= N; i++ {
		for j := 0; j <= N; j++ {
			k := Y/1000 - 10*i - 5*j
			if 0 <= k && (i+j+k) == N {
				fmt.Println(i, j, k)
				return
			}
		}
	}
	fmt.Println(-1, -1, -1)
}


package main

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

var sc *bufio.Scanner

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}
func answer(reader io.Reader, writer io.Writer) {
	sc = bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)
	a := nextInt()
	b := nextInt()
	x := -1
	y := -1
	z := -1
	c := b - a*1000
	for i := 0; i <= a && c-i*9000 >= 0; i++ {
		t := float64(c - i*9000)
		if (t/4000) == float64(int(t/4000)) && a-i-int(t/4000) >= 0 {
			x = i
			y = int(t / 4000)
			z = a - x - y
			break
		}
	}
	/*
		    x+y+z = a
			x*10000 + y*5000 + z*1000 = b
		    z = a - x - y
		    x*10000 + y*5000 + (a-x-y)*1000 = b
		    x*9000 + y*4000 = b - a*1000
		    y = (b-a*1000-x*9000)/4000
	*/

	fmt.Fprintf(writer, "%d %d %d", x, y, z)
}

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


package main

import "fmt"

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

	for i := 0; i <= n; i++ {
		var osatu10000Yen = i
		for j := 0; j <= n-i; j++ {
			var osatu5000Yen = j
			for k := 0; k <= n-i-j; k++ {
				var osatu1000Yen = k
				if y == (osatu10000Yen*10000 + osatu5000Yen*5000 + osatu1000Yen*1000) {
					fmt.Println(osatu10000Yen, osatu5000Yen, osatu1000Yen)
					return
				}
			}
		}
	}

	fmt.Println(-1, -1, -1)
}


package main

import "fmt"

func main() {
	var N, Y int
	fmt.Scan(&N, &Y)
	a := -1; b := -1; c := -1

	for i := 0; i <= N; i++ {
		for j := 0; j <= (N - i); j++ {
			k := N - i - j
			if i * 10000 + j * 5000 + 1000 * k == Y{
				a = i
				b = j
				c = k
			}
		}
	}
	fmt.Println(a, b, c)
	}


package main

import (
    "fmt"
)

func main() {
    var num, sum int
    fmt.Scan(&num, &sum)
    sum /= 1000

    //fmt.Println(num, sum)

    var done bool
    maxM := int(sum / 10)
    for m := 0; m <= maxM; m++ {
        sum2 := sum - m*10
        num2 := num - m
        maxG := int(sum2 / 5)
        if maxG > num2 {
            continue
        }
        for g := 0; g <= maxG; g++ {
            sum3 := sum2 - g*5
            num3 := num2 - g
            if sum3 == num3 {
                if !done {
                    done = true
                    fmt.Println(m, g, num3)
                }
            }
        }
    }
    if !done {
        fmt.Println(-1, -1, -1)
    }
}


package main

import (
	"fmt"
)

func main() {
	var N, Y int
	ax, ay, az := -1, -1, -1
	fmt.Scan(&N, &Y)
	for ix := 0; ix <= N; ix++ {
		for iy := 0; iy <= N; iy++ {
			iz := N - ix - iy
			sum := 10000*ix + 5000*iy + 1000*iz
			if Y == sum {
				ax = ix
				ay = iy
				az = iz
			}
		}
	}
	fmt.Println(ax, ay, az)
}


package main

import (
	"fmt"
)

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

	max5000 := min(n, y/5000)
	max10000 := min(n, y/10000)

	var i, j int
	for i = 0; i <= max10000; i++ {
		for j = 0; j <= max5000-i; j++ {
			if y-10000*i-5000*j == 1000*(n-i-j) {
				fmt.Println(i, j, n-i-j)
				return
			}
		}
	}

	fmt.Println(-1, -1, -1)
	return
}

func min(i, j int) int {
	if i <= j {
		return i
	}
	return j
}


package main

import (
	"fmt"
)

func main() {
	var n, y int
	fmt.Scanf("%d %d", &n, &y)

	// a, b, c := findPair(0, 0, 0, n, y)
	a, b, c := findPair2(n, y)
	fmt.Println(a, b, c)
}

func findPair2(n, y int) (int, int, int) {
	for i := 0; i <= n; i++ {
		for j := 0; j <= n-i; j++ {
			k := n - i - j
			if k >= 0 && i*10000+j*5000+k*1000 == y {
				return i, j, k
			}
		}
	}
	return -1, -1, -1
}

func findPair(a, b, c, n, y int) (int, int, int) {
	// fmt.Printf("a=%d, b=%d, c=%d, n=%d, y=%d\n", a, b, c, n, y)
	if y == 0 {
		if n == 0 {
			return a, b, c
		} else {
			return -1, -1, -1
		}
	} else if y < 0 {
		return -1, -1, -1
	}

	if n > 0 {
		if 10000*n < y {
			return -1, -1, -1
		}
		if r1, r2, r3 := findPair(a+1, b, c, n-1, y-10000); r1 != -1 {
			return r1, r2, r3
		}
		if 5000*n < y {
			return -1, -1, -1
		}
		if r1, r2, r3 := findPair(a, b+1, c, n-1, y-5000); r1 != -1 {
			return r1, r2, r3
		}
		if 1000*n < y {
			return -1, -1, -1
		}
		if r1, r2, r3 := findPair(a, b, c+1, n-1, y-1000); r1 != -1 {
			return r1, r2, r3
		}
	}
	return -1, -1, -1
}


package main

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

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)

	maiInt := nextInt()
	yenInt := nextInt()
	yenIntRaw := yenInt

	if yenInt%1000 != 0 {
		fmt.Println("-1 -1 -1")
		return
	}

	yenInt /= 1000

	yen10 := 0
	yen5 := 0
	yen1 := 0

	if yenInt > 10 {
		yen10 = (yenInt - yenInt%10) / 10
		yenInt -= yen10 * 10
	}

	if yenInt > 5 {
		yen5 = (yenInt - yenInt%5) / 5
		yenInt -= yen5 * 5
	}

	if yenInt >= 1 {
		yen1 = yenInt
	}

	is10 := false
	is10N := false
	is5 := false
reCheck:
	fmt.Printf("%d %d %d\n", yen10, yen5, yen1)
	if maiInt != yen1+yen5+yen10 {
		if is5 {
			if yen1 < 10 {
				fmt.Println("-1 -1 -1")
				return
			} else {
				yen1 -= 10
				yen10 += 1
			}
		} else if is10 || is10N || yen10 == 0 {
			yen5 -= 1
			yen1 += 5
			if !is10 {
				is10N = false
			}
			is10 = false
			if yen5 == 0 && (is10 && is10N || is5) {
				is5 = true
			}
		} else {
			yen10 -= 1
			yen5 += 2
			is10 = true
			is10N = true
		}

		if yen1 < 0 || yen5 < 0 || yen10 < 0 {
			fmt.Println("-1 -1 -1")
			return
		}
		goto reCheck
	}

	if yen10*10000+yen5*5000+yen1*1000 != yenIntRaw {
		fmt.Println("-1 -1 -1")
	} else {
		fmt.Printf("%d %d %d", yen10, yen5, yen1)
	}
}


package main

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

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)

	maiInt := nextInt()
	yenInt := nextInt()
	yenIntRaw := yenInt

	if yenInt%1000 != 0 {
		fmt.Println("-1 -1 -1")
		return
	}

	yenInt /= 1000

	yen10 := 0
	yen5 := 0
	yen1 := 0

	if yenInt > 10 {
		yen10 = (yenInt - yenInt%10) / 10
		yenInt -= yen10 * 10
	}

	if yenInt > 5 {
		yen5 = (yenInt - yenInt%5) / 5
		yenInt -= yen5 * 5
	}

	if yenInt > 1 {
		yen1 = yenInt
	}

	is10 := false
	is5 := false
reCheck:
	if maiInt > yen1+yen5+yen10 {
		if is5 {
			if yen1 < 10{
				fmt.Println("-1 -1 -1")
				return
			}else {
				yen1 -= 10
				yen10 += 1
			}
		} else if is10 || yen10 == 0 {
			yen5 -= 1
			yen1 += 5
			is10 = false
			if yen5 == 0 {
				is5 = true
			}
		} else {
			yen10 -= 1
			yen5 += 2
			is10 = true
		}
		goto reCheck
	}

	if yen10*10000+yen5*5000+yen1*1000 != yenIntRaw {
		fmt.Println("-1 -1 -1")
	} else {
		fmt.Printf("%d %d %d", yen10, yen5, yen1)
	}
}


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

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 main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)

	N, H := getInt(), getInt()
	a := make([]int, N)
	b := make([]int, N)
	for i := 0; i < N; i++ {
		a[i], b[i] = getInt(), getInt()
	}
	sort.Ints(a)
	sort.Ints(b)
	maxA := a[len(a)-1]
	ans := 0
	pos := len(b) - 1
	for b[pos] > maxA {
		H -= b[pos]
		ans++
		pos--
		if pos < 0 {
			break
		}
	}

	if H > 0 {
		n := (H + maxA/2 - 1) / maxA
		ans += n
	}

	out(ans)
}

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9


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

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 main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)

	N, H := getInt(), getInt()
	a := make([]int, N)
	b := make([]int, N)
	maxA := 0
	minBA := 0
	posB := 0
	for i := 0; i < N; i++ {
		a[i], b[i] = getInt(), getInt()
		if a[i] > maxA {
			maxA = a[i]
			minBA = b[i]
			posB = i
		} else if a[i] == maxA {
			if minBA > b[i] {
				minBA = b[i]
				posB = i
			}
		}
	}
	sort.Ints(a)
	b = append(b[:posB], b[posB+1:]...)
	sort.Ints(b)

	ans := 0
	posB = len(b) - 1
	for posB >= 0 && b[posB] > maxA {
		if H <= minBA {
			H -= minBA
			break
		}
		H -= b[posB]
		posB--
		ans++
		if H <= 0 {
			break
		}
	}
	//	out(b, posB, maxA)
	//	out(H)
	if H <= minBA {
		ans++
	} else {
		if minBA > maxA {
			n := (H - minBA) / maxA
			if (H-minBA)%maxA != 0 {
				n++
			}
			ans += n + 1
		} else {
			n := H / maxA
			if H%maxA != 0 {
				n++
			}
			ans += n
		}
	}
	out(ans)
}


package main

import (
	"sort"
	"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()
	h := scanInt()

	a := make([]int, n)
	maxa := 0
	b := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
		maxa = max(maxa, a[i])
		b[i] = scanInt()
	}

	sort.Ints(b)

	c := 0
	for i := 0; i < n; i++ {
		if maxa > b[n-1-i] {
			break
		}

		h -= b[n-1-i]
		c++

		if h <= 0 {
			h = 0
			break
		}
	}

	fmt.Fprintln(wr, c+(h+maxa-1)/maxa)

}


package main

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

func main() {
	var n, h int
	fmt.Scan(&n, &h)
	a := make([]int, n)
	b := make([]int, n)
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	for i := 0; i < n; i++ {
		sc.Scan()
		a[i], _ = strconv.Atoi(sc.Text())
		sc.Scan()
		b[i], _ = strconv.Atoi(sc.Text())
	}
	amax := a[0]
	for _, e := range a {
		if amax < e {
			amax = e
		}
	}
	sort.Sort(sort.Reverse(sort.IntSlice(b)))
	ans := 0
	for _, e := range b {
		if amax < e {
			h -= e
			ans++
			if h <= 0 {
				break
			}
		}
	}
	if h > 0 {
		ans += (h + amax - 1) / amax
	}
	fmt.Println(ans)
}


package main

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

func main() {
	sc.Split(bufio.ScanWords)
	N, H := nextInt(), nextInt()
	a := make([]int, N)
	b := make([]int, N)
	maxA := 0
	for i := 0; i < N; i++ {
		a[i] = nextInt()
		b[i] = nextInt()
		if a[i] > maxA {
			 maxA = a[i]
		}
	}
	sort.Ints(b)

	res := 0
	for i := len(b) - 1; i >= 0 && H > 0; i-- {
		if maxA > b[i] {
			break
		} else {
			res++
			H -= b[i]
		}
	}

	if H > 0 {
		res += (H + maxA - 1) / maxA
	}
	fmt.Println(res)
}

// -------- Library --------
var sc = bufio.NewScanner(os.Stdin)

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

func nextInt() int {
	v, err := strconv.Atoi(next())
	if err != nil {
		fmt.Println("Failed to read int", err)
		os.Exit(1)
	}
	return v
}

func nextIntArray(size int) []int {
	res := make([]int, size)
	for i := 0; i < size; i++ {
		res[i] = nextInt()
	}
	return res
}


package main

import (
	"fmt"
	"sort"
)

type Sord struct {
	a int64
	b int64
}

type Sords []Sord

func (a Sords) Len() int {
	return len(a)
}

func (a Sords) Swap(i, j int) {
	a[i], a[j] = a[j], a[i]
}

func (a Sords) Less(i, j int) bool {
	return a[i].b > a[j].b
}

func main() {
	var N int
	var H int64

	fmt.Scanf("%d %d", &N, &H)

	sords := make(Sords, N)

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

	sort.Sort(sords)

	max := sords[0].a
	for i := 0; i < N; i++ {
		if sords[i].a > max {
			max = sords[i].a
		}
	}

	attack := 0
	damage := int64(0)
	for i := 0; i < N; i++ {
		if damage >= H || sords[i].b < sords[max].a {
			break
		}

		damage += sords[i].b
		attack++
	}

	if damage < H {
		more := int((H - damage) / sords[max].a)
		if (H-damage)%sords[max].a != 0 {
			more++
		}
		attack += more
		damage += sords[max].a * int64(more)
	}

	if attack <= 0 {
		fmt.Println(1)
	} else {
		fmt.Println(attack)
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	scanInit()

	n := nextInt()
	a := nextInt()
	b := nextInt()

	sum := 0
	for i := 1; i <= n; i++ {
		tmp := sumKeta(i)
		if a <= tmp && tmp <= b {
			sum += i
		}
	}

	fmt.Println(sum)

}

// 各桁の和
func sumKeta(n int) int {
	keta := 0
	for n > 0 {
		keta += n % 10
		n /= 10
	}
	return keta
}

// ---- readfunc
func scanInit() {
	const cap = 200 * 1024 // Default=64
	var buf = make([]byte, cap)
	sc.Buffer(buf, cap)
	sc.Split(bufio.ScanWords)
	return
}
func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


package main

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

func main() {
	sc = initScanner(os.Stdin)
	n, a, b := scanInt(sc), scanInt(sc), scanInt(sc)
	fmt.Println(resolve(n, a, b))
}

func resolve(n, a, b int) int {
	var tmp, sum int
	for i := 1; i <= n; i++ {
		tmp = 0
		is := strings.Split(strconv.Itoa(i), "")
		for _, v:= range is {
			vi, _ := strconv.Atoi(v)
			tmp += vi
		}
		if a <= tmp && tmp <= b {
			sum += i
		}
	}

	return sum
}

// --------------- BASE DEFINITIONS ---------------

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords)
	return sc
}

func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func scanIntSlice(sc *bufio.Scanner, n int) []int {
	is := make([]int, n)
	for i := 0; i < n; i++ {
		is[i] = scanInt(sc)
	}

	return is
}

func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func scanStringSlice(sc *bufio.Scanner, n int) []string {
	ss := make([]string, n)
	for i := 0; i < n; i++ {
		sc.Scan()
		ss[i] = sc.Text()
	}

	return ss
}


package main
import (
   "fmt"
   "os"
   "bufio"
   "strconv"
)
var sc = bufio.NewScanner(os.Stdin)

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

  a := nextInt()
  b := nextInt()

  sum := 0
  for i := 1; i <= n; i++ {
	  if a <= i && i <= b && i < 10{
		sum = sum + i
	  } else {
		partSum := 0
        for _, v := range strconv.Itoa(i) {
         v, _ := strconv.Atoi(string(v))
         partSum = partSum + v
        }
	    if a <= partSum && partSum <= b {
		  sum = sum + i
		}
	  }
  }
  fmt.Println(sum)

}

func nextString() (string) {
	sc.Scan()
	v := sc.Text()
	return v
}

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

func nextFloat() (float64) {
	sc.Scan()
	v, e := strconv.ParseFloat(sc.Text(), 64)
    if e != nil { panic(e) }
    return v
}


package main

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

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

func solve(in io.Reader, out io.Writer) {
	bs := NewBufScanner(in)
	bw := NewBufWriter(out)
	defer bw.w.Flush()
	n, a, b := bs.IntScan(), bs.IntScan(), bs.IntScan()

	sum := 0
	for i := 1; i <= n; i++ {
		r4 := i / 1000
		r3 := (i - r4 * 1000) / 100
		r2 := (i - r4 * 1000 - r3 * 100) / 10
		r1 := i  - r4 * 1000 - r3 * 100 - r2 * 10
		s := r1 + r2 + r3 + r4
		if a <= s && s <= b {
			sum += i
		}
	}

	bw.Printf("%v\n", sum)
}

func sumDigits(n int) int {
	sum := 0
	for n > 0 {
		sum += n % 10
		n /= 10
	}
	return sum
}

// BufScanner original scanner
type BufScanner struct {
	s *bufio.Scanner
}

// NewBufScanner constructer
func NewBufScanner(in io.Reader) *BufScanner {
	s := bufio.NewScanner(in)
	s.Buffer(make([]byte, 1024), 1e+9)
	s.Split(bufio.ScanWords)
	return &BufScanner{
		s: s,
	}
}

// Scan Scan Data
func (b *BufScanner) Scan() string {
	b.s.Scan()
	return b.s.Text()
}

// IntScan Scan Data
func (b *BufScanner) IntScan() int {
	v, err := strconv.Atoi(b.Scan())
	if err != nil {
		panic(err)
	}
	return v
}

// BufWriter original writer
type BufWriter struct {
	w *bufio.Writer
}

// NewBufWriter constructer
func NewBufWriter(out io.Writer) *BufWriter {
	w := bufio.NewWriter(out)
	return &BufWriter{
		w: w,
	}
}

// Printf Output file
func (b *BufWriter) Printf(format string, a ...interface{}) {
	fmt.Fprintf(b.w, format, a...)
}


package main

import "fmt"

func main() {
	var n, a, b int

	fmt.Scan(&n, &a, &b)

	var result int
	for i := 0; i <= n; i++ {
		digitSum := digitSum(i)
		if a <= digitSum && digitSum <= b {
			result += i
		}
	}
	fmt.Println(result)
}

func digitSum(n int) int {
	var sum int

	for {
		digit := n % 10
		sum += digit
		if n/10 == 0 {
			break
		}
		n = n / 10
	}

	return sum
}


package main

import (
	"fmt"
)

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

	res := 0
	for x := 1; x <= n; x++ {
		v := x
		sum := 0
		for v > 0 {
			sum += v % 10
			v /= 10
		}
		if a <= sum && sum <= b {
			res += x
		}
	}
	fmt.Println(res)
}


package main

import (
    "fmt"
)

func sumOfDegit(n int) int {
    var sum int
    for n > 0 {
        sum += n % 10
        n /= 10
    }
    return sum
}

func main() {
    var n, a, b, sum int
    fmt.Scan(&n, &a, &b)
    for i := 1; i < n+1; i++ {
        if v := sumOfDegit(i); a <= v && v <= b {
            sum += i
        }
    }
    fmt.Println(sum)
}


package main

import (
	"fmt"
)

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

	total := 0
	for i := 1; i <= n; i++ {
		sum := calc(i)
		if a <= sum && sum <= b {
			total += i
		}
	}
	fmt.Println(total)
}

func calc(n int) int{
	sum := 0
	for {
		if n == 0 {
			break
		}
		sum += n % 10
		n = n / 10
	}
	return sum
}


package main

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

var stdin = bufio.NewScanner(os.Stdin)

func main() {
	var o int
	arr := scanArrayInt()
	n, a, b := arr[0], arr[1], arr[2]
	for i := 1; i <= n; i++ {
		t, d := 0, i
		for {
			t, d = t+d%10, d/10
			if d == 0 {
				break
			}
		}
		if a <= t && t <= b {
			o += i
		}
	}
	fmt.Println(o)
}

func scanArrayInt() []int {
	var ret = []int{}
	stdin.Scan()
	for _, s := range strings.Split(stdin.Text(), " ") {
		i, _ := strconv.Atoi(s)
		ret = append(ret, i)
	}
	return ret
}


package main

import (
	"fmt"
)

func sum_of_digit(n int) int {
	var sum int
	for n > 0 {
		sum += n % 10
		n /= 10
	}
	return sum
}
func main() {
	var n, a, b, sum int
	fmt.Scan(&n, &a, &b)
	for i := 1; i < n+1; i++ {
		if v := sum_of_digit(i); a <= v && v <= b {
			sum += i
		}
	}
	fmt.Println(sum)
}


package main

import "fmt"

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

	var inRanges []int

	for i := n; i > 0; i-- {
		sum := 0
		num := i
		for num > 0 {
			sum += num%10
			num /= 10
		}
		if a <= sum && sum <= b {
			inRanges = append(inRanges, i)
		}
	}

	ans := 0
	for _, value := range inRanges {
		ans += value
	}

	fmt.Println(ans)
}


// Package main provides
//
// File:  b.go
// Author: ymiyamoto
//
// Created on Thu Jan  3 17:24:32 2019
//
package main

import (
	"fmt"
	"strconv"
)

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

	ans := 0
	for i := 1; i <= N; i++ {
		s := strconv.Itoa(i)

		sum := 0
		for i := range s {
			sum += int(s[i] - '0')
		}
		if A <= sum && sum <= B {
			ans += i
		}
	}
	fmt.Println(ans)
}


package main

import (
	"fmt"
	"strconv"
)

func solve() {
	var N, A, B int
	fmt.Scan(&N, &A, &B)

	ans := 0
	for i := 1; i <= N; i++ {
		si := strconv.FormatInt(int64(i), 10)
		c := 0
		for _, s := range si {
			po, _ := strconv.ParseInt(string(s), 10, 64)
			c += int(po)
		}
		if A <= c && c <= B {
			ans += i
		}

	}
	fmt.Println(ans)
}

func main() {
	solve()
}


package main

import (
	"bufio"
	"os"
	"strings"
)

func main() {
	sc := bufio.NewScanner(os.Stdin)
	A(sc)
}

func A(sc *bufio.Scanner) (cnt int) {
	sc.Scan()
	s := sc.Text()
	for _, t := range strings.Split(s, "") {
		if t == "1" {
			cnt++
		}
	}
	return
}

Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.


package main
import "fmt"

func main(){
	var s string
	ans := 0
	fmt.Scan(&s)
	for i:=0;i<=2;i++{
		if s[i] == '1'{
			ans ++
		}
	}
	fmt.Println(ans)
}


package main

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

var (
	reader io.Reader = os.Stdin
	writer io.Writer = os.Stdout
)

func main() {
	run()
}

func run() {
	var str string
	fmt.Fscan(reader, &str)
	var count int

	for _, r := range str {
		if r == '1' {
			count++
		}
	}

	fmt.Fprintln(writer, count)
}


package main

import (
	"fmt"
	"strconv"
)

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

	var s1 int
	var s2 int
	var s3 int

	s1, _ = strconv.Atoi(string(s[0]))
	s2, _ = strconv.Atoi(string(s[1]))
	s3, _ = strconv.Atoi(string(s[2]))
	fmt.Println(s1 + s2 + s3)

}


package main

import (
	"fmt"
	"strings"
)

func main() {
	var a string
	fmt.Scan(&a)
	fmt.Printf("%d\n", strings.Count(a, "1"))
}


package main

import (
	"fmt"
)

func main() {
	var flds string

	fmt.Scan(&flds)
	grid := []rune(flds)
	count := 0
	for _, v := range grid {
		if v == '1' {
			count++
		}
	}

	fmt.Printf("%d\n", count)
}


package main

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

type Pair struct {
	p1, p2 interface{}
}

const (
	initialBufSize = 1e4
	maxBufSize     = 1e8
)

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

func main() {
	buf := make([]byte, initialBufSize)
	scanner.Buffer(buf, maxBufSize)
	scanner.Split(bufio.ScanWords)

	a := readString()
	fmt.Println(strings.Count(a, "1"))
}

/*==========================================
 *             Library
 *==========================================*/
func write(s string) {
	writer.WriteString(s)
}

func print() {
	writer.Flush()
}

// scanner.Split(bufio.ScanWords) をコメントアウトしないと使用不可
func readLine() (s string) {
	if scanner.Scan() {
		s = scanner.Text()
	}
	return s
}

func readInt() (read int) {
	scanner.Scan()
	read, err := strconv.Atoi(scanner.Text())
	if err != nil {
		panic(err)
	}
	return
}

func readFloat() (read float64) {
	scanner.Scan()
	read, err := strconv.ParseFloat(scanner.Text(), 64)
	if err != nil {
		panic(err)
	}
	return
}

func readRunes() (read []rune) {
	scanner.Scan()
	for _, v := range scanner.Text() {
		read = append(read, v)
	}
	return
}

func readString() (read string) {
	scanner.Scan()
	read = scanner.Text()
	return
}

func readStrings() (read []string) {
	scanner.Scan()
	for _, v := range scanner.Text() {
		read = append(read, string(v))
	}
	return
}

func s2i(s string) int {
	var intVal, e = strconv.Atoi(s)
	if e != nil {
		panic(e)
	}
	return intVal
}

func i2s(i int) string {
	var strVal = strconv.Itoa(i)
	return strVal
}

func s2f(s string) float64 {
	var floatVal, e = strconv.ParseFloat(s, 64)
	if e != nil {
		panic(e)
	}
	return floatVal
}

func sum(i []int) int {
	sum := 0
	for _, val := range i {
		sum += val
	}
	return sum
}

func split(s string) []string {
	return strings.Fields(s)
}

func strAry2intAry(strs []string) []int {
	var ret = make([]int, 0, len(strs))
	for _, str := range strs {
		var intVal, e = strconv.Atoi(string(str))
		if e != nil {
			panic(e)
		}
		ret = append(ret, intVal)
	}
	return ret
}

func intAry2strAry(nums []int) []string {
	var ret = make([]string, 0, len(nums))
	for _, num := range nums {
		var strVal = strconv.Itoa(num)
		ret = append(ret, strVal)
	}
	return ret
}

func ary2str(strs []string) string {
	return strings.Join(strs, " ")
}

func reverse(res []int) []int {
	for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
		res[i], res[j] = res[j], res[i]
	}
	return res
}

func initalize(res []int, init int) {
	if len(res) == 0 {
		return
	}
	res[0] = init
	for i := 0; i < len(res); i++ {
		copy(res[i:], res[:i])
	}
}

//
// func regexpExample() {
//     // Your code here!
//     var str = "13:20"
//     r := regexp.MustCompile(`(\d+):(\d+)`)
//     fmt.Println(r.FindStringSubmatch(str))
// }

// type Country struct {
//     gold int
//     silver int
//     blonze int
// }

// // 複数ソートのサンプル
// func stableSortExample() []Country{
//     var country = []Country {
//         {gold:1, silver:2, blonze:3},
//         {gold:1, silver:2, blonze:3},
//         {gold:1, silver:3, blonze:2},
//         {gold:1, silver:3, blonze:3},
//     }
//     sort.SliceStable(country, func(i, j int) bool { return country[i].blonze > country[j].blonze })
//     sort.SliceStable(country, func(i, j int) bool { return country[i].silver > country[j].silver })
//     sort.SliceStable(country, func(i, j int) bool { return country[i].gold > country[j].gold })
//     return country
// }


package main

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

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

func main() {
	//sc.Split(bufio.ScanWords)
	var a string
	ans := 0
	a = next()
	for i := 0; i < 3; i++ {
		if string(a[i]) == "1" {
			ans++
		}
	}
	out.Flush()
	fmt.Println(ans)
}
func next() string {
	sc.Scan()
	return sc.Text()
}
func nextInt() int {
	a, e := strconv.Atoi(next())
	if e != nil {
		panic(e)
	}
	return a
}


package main

import (
	"fmt"
)

func main() {
	var a string
	fmt.Scan(&a)
	counter := 0
	for i := 0; i < len(a); i++ {
		if a[i] == '1' {
			counter++
		}
	}
	fmt.Println(counter)
}


package main

import "fmt"

func main() {
	var a int

	fmt.Scan(&a)

	var count int = 0

	if a/100 == 1 {
		count++
		a -= 100
	}
	if a/10 == 1 {
		count++
		a -= 10
	}
	if a == 1 {
		count++
	}

	fmt.Printf("%d\n", count)
}


package main

import "fmt"

func main() {
	var (
		a   string
		ans = 0
	)
	fmt.Scan(&a)
	for i := 0; i < len(a); i++ {
		if a[i] == '1' {
			ans++
		}
	}
	fmt.Println(ans)
}


package main

import (
	"fmt"
)

func main() {
	var s string
	fmt.Scan(&s)
    count := 0
	if s[0:1] == "1" {
		count++
	}
	if s[1:2] == "1" {
		count++
	}
	if s[2:3] == "1" {
		count++
	}
	fmt.Println(count)
}


package main

import (
	"fmt"
	"strings"
)

func main() {
	var s string
	fmt.Scanf("%s", &s)
	fmt.Println(mass(s))
}

func mass(s string) int {
	return strings.Count(s, "1")
}


package main

import "fmt"

func main() {
	var s string

	fmt.Scan(&s)

	res := 0
	for i := 0; i < len(s); i++ {
		if s[i:i+1] == "1" {
			res += 1
		}
	}
	fmt.Println(res)
}


package main

import "fmt"

func stringToRunes(s string) (runes []rune) {
	runes = make([]rune, len(s))
	for i, r := range s {
		runes[i] = r
	}
	return
}

func main() {
	var s string
	fmt.Scan(&s)
	runes := stringToRunes(s)
	cnt := 0
	for i := 0; i < len(runes); i++ {
		if runes[i] == '1' {
			cnt++
		}
	}
	fmt.Println(cnt)
}


package main

import "fmt"

func main() {
	var s string
	count := 0
	fmt.Scan(&s)
	for i := 0; i < len(s); i++ {
		if s[i] == '1' {
			count++
		}
	}
	fmt.Println(count)
}


package main

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

func main() {
	s := nextString()
	var sum int
	for i := 0; i < 3; i++ {
		if s[i:i+1] == "1" {
			sum++
		}
	}
	fmt.Println(sum)
}

// 以下がテンプレート

var nextReader func() string

func init() {
	nextReader = NewScanner()
}

func NewScanner() func() string {
	r := bufio.NewScanner(os.Stdin)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)
	return func() string {
		r.Scan()
		return r.Text()
	}
}
func nextString() string {
	return nextReader()
}
func nextInt64() int64 {
	v, _ := strconv.ParseInt(nextReader(), 10, 64)
	return v
}
func nextInt() int {
	v, _ := strconv.Atoi(nextReader())
	return v
}
func nextInts(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}
func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(nextReader(), 64)
	return f
}


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 ---*/
	s := next()
	c := 0
	for _, v := range s {
		if v == '1' {
			c++
		}
	}
	fmt.Println(c)
}

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 (
	"fmt"
)

func main() {
	var s string
	fmt.Scan(&s)
	ans := 0
	for _, c := range s {
		if c == '0' {
			ans++
		}
	}
	fmt.Println(ans)
}


package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	s := getNextString(scanner)
	t := getNextString(scanner)
	n := len(s)
	m := len(t)
	for i := 0; i < n-m+1; i++ {
		if ok(s[i:], t) {
			for j := 0; j < i; j++ {
				if s[j] == '?' {
					fmt.Fprint(writer, "a")
					continue
				}
				fmt.Fprintf(writer, "%c", s[j])
			}
			fmt.Fprint(writer, t)
			for j := i + m; j < n; j++ {
				if s[j] == '?' {
					fmt.Fprint(writer, "a")
					continue
				}
				fmt.Fprintf(writer, "%c", s[j])
			}
			fmt.Fprintln(writer, "")
			return
		}
	}
	fmt.Fprintln(writer, "UNRESTORABLE")
}
func ok(s, t string) bool {
	n := len(t)
	for i := 0; i < n; i++ {
		if s[i] == '?' {
			continue
		}
		if s[i] != t[i] {
			return false
		}
	}
	return true
}

Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

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

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

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

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

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var S, T []string

func main() {
	sc.Split(bufio.ScanWords)
	tmpS, tmpT := nextStr(), nextStr()

	S = strings.Split(tmpS, "")
	T = strings.Split(tmpT, "")

	var indxs []int
	next := 0
	for i := 0; i < len(S); i++ {
		if S[i] == T[next] || string(S[i]) == "?" {
			//fmt.Println(string(S[i]), S[i], T[next], next, i)
			if next == len(T)-1 {
				indxs = append(indxs, i)
				next = 0
				continue
			}
			next++
			continue
		}
		next = 0
	}

	if len(indxs) == 0 {
		fmt.Println("UNRESTORABLE")
		return
	}

	var cad []string
	for _, indx := range indxs {
		j := 0
		tmpStr := ""
		for i := 0; i < len(S); i++ {
			if i >= indx-len(T)+1 && i <= indx {
				tmps := string(T[j])
				tmpStr += tmps
				j++
			} else if string(S[i]) == "?" {
				tmpStr += "a"
			} else {
				tmpStr += S[i]
			}
		}
		cad = append(cad, tmpStr)
	}
	//fmt.Println(cad)
	sort.Strings(cad)
	fmt.Println(cad[0])
}


package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
)

const (
	initialBufSize = 10000
	maxBufSize     = 1000000
	mod            = 1e9 + 7
)

var (
	sc *bufio.Scanner = func() *bufio.Scanner {
		sc := bufio.NewScanner(os.Stdin)
		buf := make([]byte, initialBufSize)
		sc.Buffer(buf, maxBufSize)
		sc.Split(bufio.ScanWords)
		return sc
	}()
)

func main() {
	Sd := getString()
	T := getString()

	var candidates []string
	for i := 0; i <= len(Sd) - len(T); i++ {
		flag := true
		for j := 0; j < len(T); j++ {
			fmt.Println(string(Sd[i+j]), ", ", string(T[j]))
			if Sd[i+j] != T[j] && Sd[i+j] != '?' {
				flag = false
				break
			}
		}
		fmt.Println()
		if flag {
			candidate := strings.Replace(Sd, Sd[i:i+len(T)], T, 1)
			candidate = strings.Replace(candidate, "?", "a", -1)
			candidates = append(candidates, candidate)
		}
	}

	if len(candidates) == 0 {
		fmt.Println("UNRESTORABLE")
	} else {
		sort.Sort(sort.StringSlice(candidates))

		for _, candidate := range candidates {
			fmt.Println(candidate)
		}

		fmt.Println(candidates[0])
	}
}

type Graph struct {
	n     int
	edges [][]int
}

func NewGraph(n int) *Graph {
	g := &Graph{
		n:     n,
		edges: make([][]int, n),
	}
	return g
}

func (g *Graph) AddEdge(u, v int) {
	g.edges[v] = append(g.edges[v], u)
	g.edges[u] = append(g.edges[u], v)
}

func dfs(c int, edges [][]int, visited map[int]struct{}) {
	visited[c] = struct{}{}

	for _, v := range edges[c] {
		_, flag := visited[v]
		if flag {
			continue
		}
		dfs(v, edges, visited)
	}
}

func bfs(c int, graph *Graph) {
	next := make([]int, 0)
	next = append(next, c)
	visited := make(map[int]struct{})

	for ; len(next) != 0; {
		u := next[0]
		next = next[1:]
		visited[u] = struct{}{}

		for _, v := range graph.edges[u] {
			_, flag := visited[v]
			if flag {
				continue
			}

			// なにか処理

			next = append(next, v)
		}
	}
}

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

func getIntArray(n int) []int {
	array := make([]int, n)
	for i := 0; i < n; i++ {
		array[i] = getInt()
	}
	return array
}

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

func getStringArray(n int) []string {
	array := make([]string, n)
	for i := 0; i < n; i++ {
		array[i] = getString()
	}
	return array
}

func abs(a int) int {
	return int(math.Abs(float64(a)))
}

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

func powMod(n, p int) int {
	if p == 0 {
		return 1
	} else if p%2 == 0 {
		t := powMod(n, p/2)
		return calcMod(t * t)
	} else {
		return calcMod(n * powMod(n, p-1))
	}
}

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 printIntArray(array []int) {
	strArray := fmt.Sprint(array)
	fmt.Println(strArray[1 : len(strArray)-1])
}

func calcMod(x int) int {
	return x % mod
}

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

func isPrime(n int) bool {
	if n < 2 {
		return false
	} else if n == 2 {
		return true
	} else if n % 2 == 0 {
		return false
	}

	sqrtN := int(math.Sqrt(float64(n)))
	for i := 3; i <= sqrtN; i += 2 {
		if n % i == 0 {
			return false
		}
	}

	return true
}

func contains(s []int, e int) bool {
	for _, v := range s {
		if e == v {
			return true
		}
	}
	return false
}

func makeRange(min, max int) []int {
	a := make([]int, max-min+1)
	for i := range a {
		a[i] = min + i
	}
	return a
}

func powerset2(nums []int) [][]int {
	length := int(math.Pow(2, float64(len(nums))))
	result := make([][]int, length)

	index := 0
	result[index] = []int{}
	index++

	for _, n := range nums {
		max := index
		for i := 0; i < max; i++ {
			result[index] = copyAndAppend(result[i], n)
			index++
		}
	}

	return result
}

func copyAndAppend(nums []int, n int) []int {
	dst := make([]int, len(nums)+1)
	copy(dst, nums)
	dst[len(nums)] = n
	return dst
}

func calcGcd(x, y int) int {
	if y == 0 {
		return x
	} else if x >= y {
		return calcGcd(y, x % y)
	} else {
		return calcGcd(x, y % x)
	}
}

func getDivisor(n int) []int {
	var divisor []int
	divisor = append(divisor, 1)
	if n != 1 {
		divisor = append(divisor, n)
	}

	sqrt := int(math.Sqrt(float64(n)))
	for i := 2; i <= sqrt; i++ {
		if n % i == 0 {
			divisor = append(divisor, i)
			divisor = append(divisor, n/i)
		}
	}
	return divisor
}

type intHeap []int

func (h intHeap) Len() int {
	return len(h)
}

func (h intHeap) Less(i, j int) bool {
	return h[i] > h[j]
}

func (h intHeap) Swap(i, j int) {
	h[i], h[j] = h[j], h[i]
}

func (h *intHeap) Push(x interface{}) {
	*h = append(*h, x.(int))
}

func (h *intHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

func initHeap() *intHeap {
	ih := &intHeap{}
	heap.Init(ih)
	return ih
}

func (h *intHeap) pushHeap(n int) {
	heap.Push(h, n)
}

func (h *intHeap) popHeap() int {
	return heap.Pop(h).(int)
}

func factMod(n int) int {
	value := 1
	for ; n > 1; n-- {
		value = calcMod(value * n)
	}
	return value
}

func combinationMod(n, k int) int {
	factN := factMod(n)
	factK := factMod(k)
	factNK := factMod(n - k)
	factKR := powMod(factK, mod - 2)
	factNKR := powMod(factNK, mod - 2)
	return calcMod(factN * calcMod(factKR * factNKR))
}


package main

import "fmt"

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

	ans := make([]rune, len(s))
	j := 0
	for i := range s {
		if j == len(t) {
			if s[i] == '?' {
				ans[i] = 'a'
			} else {
				ans[i] = rune(s[i])
			}

			continue
		}

		if s[i] == '?' {
			ans[i] = 'a'
		} else if s[i] == t[j] {
			ans[i] = rune(s[i])
		} else {
			ans[i] = rune(s[i])
			if s[i] == t[0] {
				j = 1
			} else {
				j = 0
			}

			continue
		}

		j++
		if j == len(t) {
			for k := 0; k < j; k++ {
				ans[i-j+1+k] = rune(t[k])
			}
		}
	}

	if j == len(t) {
		fmt.Println(string(ans))
	} else {
		fmt.Println("UNRESTORABLE")
	}
}


package main

import "fmt"

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

	s, t = reverse(s), reverse(t)

	ti := 0
	ng := true
	for i := 0; i < len(s); i++ {
		if s[i] == '?' || (i < len(t)-1 && s[i] == t[i]) {
			ok := true
			for j := 0; j < len(t); j++ {
				if len(s)-1 <= i+j {
					ok = false
					break
				}
				if s[i+j] == '?' {
					continue
				}

				if s[i+j] != t[j] {
					ok = false
				}
			}
			if ok {
				ti = i
				ng = false
				break
			}
		}
	}

	if ng {
		fmt.Println("UNRESTORABLE")
		return
	}

	ans := ""
	for i := 0; i < len(s); i++ {
		if i == ti {
			for j := 0; j < len(t); j++ {
				ans += string(t[j])
			}
			i += len(t) - 1
		} else {
			if s[i] == '?' {
				ans += "a"
			} else {
				ans += string(s[i])
			}
		}
	}

	fmt.Println(reverse(ans))
}

func reverse(s string) string {
	t := ""
	for _, c := range s {
		t = string(c) + t
	}
	return t
}


package main

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

var (
	sDash string
	t     string
	s     string
)

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	sDash = scanner.Text()
	scanner.Scan()
	t = scanner.Text()

	//check if t can apply to sDash
	for i := len(sDash) - len(t); 0 <= i; i-- {
		k := 0
		match := false
		for j := i; j < i+len(t); j++ {
			fmt.Printf("i %d, j %d, k %d\n", i, j, k)
			if sDash[j] != '?' && sDash[j] != t[k] {
				match = false
				break
			}

			k++
			match = true
		}
		if match {
			s = sDash[:i] + t + sDash[(i+len(t)):]
			s = strings.Replace(s, "?", "a", -1)
			fmt.Println(s)
			return
		}

	}
	fmt.Println("UNRESTORABLE")
}


package main

import "fmt"

func main() {
	var s, t string
	fmt.Scan(&s, &t)
	s = reverse(s)
	t = reverse(t)

	for i := 0; i < len(s); i++ {
		ok := true
		for j := 0; j < len(t); j++ {
			if len(s) <= i+j {
				ok = false
				break
			}
			tt := t[j]
			ss := s[i+j]
			if !(ss == tt || ss == '?') {
				ok = false
				break
			}
		}
		if ok {
			ans := ""
			for j := 0; j < len(s); j++ {
				c := s[j]
				if i <= j && j < i+len(t) {
					c = t[j-i]
				} else if c == '?' {
					c = 'a'
				}
				ans += string(c)
			}
			fmt.Println(reverse(ans))
			return
		}
	}
	fmt.Println("UNRESTORABLE")
}

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


package main

import (
	"fmt"
	"os"
)

func main() {
	var S, T string
	fmt.Scan(&S, &T)
	start := len(S) - len(T)
	if start < 0 {
		fmt.Println("UNRESTORABLE")
	}
	for s := start; s >= 0; s-- {
		i := 0
		for i = 0; i < len(T); i++ {
			if string(S[s+i]) != "?" && S[s+i] != T[i] {
				break
			}
		}
		if i == len(T) {
			result := ""
			for j := 0; j < start; j++ {
				if string(S[j]) == "?" {
					result += "a"
				} else {
					result += string(S[j])
				}
			}
			result += T
			fmt.Println(result)
			os.Exit(0)
		}
	}
	fmt.Println("UNRESTORABLE")
}


package main

import "fmt"

func main() {
	var sp, t string
	fmt.Scan(&sp, &t)

	begin := make([]int, 0)
	for i := 0; i < len(sp); i++ {
		flag := false
		for j := 0; j < len(t) && i+j < len(sp); j++ {
			if !(sp[i+j] == t[j] || sp[i+j] == '?') {
				break
			}
			if j == len(t)-1 {
				flag = true
			}
		}
		if flag {
			begin = append(begin, i)
		}
	}

	if len(begin) == 0 {
		fmt.Println("UNRESTORABLE")
		return
	}

	candinates := make([]string, len(begin))
	for j, k := range begin {
		s := ""
		for i := range sp {
			if i >= k && i < k+len(t) {
				s += string(t[i-k])
			} else if sp[i] == '?' {
				s += string('a')
			} else {
				s += string(sp[i])
			}
		}
		candinates[j] = s
	}

	var min string
	for i, str := range candinates {
		if i == 0 || str < min {
			min = str
		}
	}
	fmt.Println(min)
}


// ProblemURL : https://atcoder.jp/contests/abc076/tasks/abc076_c
// ---------------------------------------------
package main

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

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
func diff(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}
func larger(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func smaller(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func largest(a, b, c int) (lgst int) {
	if a > b {
		lgst = a
	} else {
		lgst = b
	}
	if c > lgst {
		lgst = c
	}
	return
}
func smallerst(a, b, c int) (slst int) {
	if a < b {
		slst = a
	} else {
		slst = b
	}
	if c < slst {
		slst = c
	}
	return
}
func intsMax(a []int) (idx, val int) {
	if len(a) == 0 {
		panic("func max: argument slice length must not be zero")
	}
	val = a[0]
	for i, aa := range a {
		if aa > val {
			idx, val = i, aa
		}
	}
	return
}
func intsMin(a []int) (idx, val int) {
	if len(a) == 0 {
		panic("func min: argument slice length must not be zero")
	}
	val = a[0]
	for i, aa := range a {
		if aa < val {
			idx, val = i, aa
		}
	}
	return
}
func intsSum(a []int) int {
	res := 0
	for _, v := range a {
		res += v
	}
	return res
}
func intsSigma(x int) int          { return x * (x + 1) / 2 }
func isEven(n int) bool            { return n&1 == 0 }
func isOdd(n int) bool             { return n&1 == 1 }
func swap(a int, b int) (int, int) { return b, a }
func calcMod(n int) int            { return n % mod }
func ceil(x float64) int           { return int(math.Ceil(x)) }
func pow(x, y int) int {
	if y < 0 {
		panic("Exponent must be a natural number")
	}
	z := 1
	for i := y; i != 0; i >>= 1 {
		if i&1 == 1 {
			z *= x
		}
		x *= x
	}
	return z
}
func powMod(x, y int) int {
	if y < 0 {
		panic("Exponent must be a natural number")
	}
	z := 1
	for i := y; i != 0; i >>= 1 {
		if i&1 == 1 {
			z = (z * x) % mod
		}
		x = (x * x) % mod
	}
	return z
}
func intsJoin(a, b []int) []int { return append(a, b...) }
func intsClear(a []int) []int   { return a[:0] }
func intsCopy(a []int) []int    { return append([]int(nil), a...) }
func intsSorted(a []int) []int {
	s := intsCopy(a)
	sort.Ints(s)
	return s
}
func intsReverse(a []int) {
	for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
		a[i], a[j] = a[j], a[i]
	}
	return
}
func intsFill(a []int, val int) {
	for i := 0; i < len(a); i++ {
		a[i] = val
	}
	return
}
func intsPeekBack(a []int) int {
	if len(a) == 0 {
		panic("func peekBack: zero length slice")
	}
	return a[len(a)-1]
}
func intsPeekFront(a []int) int {
	if len(a) == 0 {
		panic("func peekFront: zero length slice")
	}
	return a[0]
}
func intsPopBack(a []int) (int, []int) {
	if len(a) == 0 {
		panic("func popBack: zero length slice")
	}
	return a[len(a)-1], a[:len(a)-1]
}
func intsPopFront(a []int) (int, []int) {
	if len(a) == 0 {
		panic("func popFront: zero length slice")
	}
	return a[0], a[1:]
}
func intsPushBack(a []int, x int) []int  { return append(a, x) }
func intsPushFront(a []int, x int) []int { return append([]int{x}, a...) }

var (
	sc = bufio.NewScanner(os.Stdin)
	bw = bufio.NewWriterSize(os.Stdout, maxBufSize)
)

func init() {}
func ru() (n int) {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	for _, v := range sc.Bytes() {
		n = n*10 + int(v-48)
	}
	return
}
func ri() (n int) {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	b := sc.Bytes()
	neg := false
	if b[0] == 45 {
		neg = true
		b = b[1:]
	}
	for _, v := range b {
		n = n*10 + int(v-48)
	}
	if neg {
		n = -n
	}
	return
}
func ri64() (n int64) {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	b := sc.Bytes()
	neg := false
	if b[0] == 45 {
		neg = true
		b = b[1:]
	}
	for _, v := range b {
		n = n*10 + int64(v-48)
	}
	if neg {
		n = -n
	}
	return
}
func rf() float64 {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	f, err := strconv.ParseFloat(sc.Text(), 64)
	if err != nil {
		panic(err)
	}
	return f
}
func rs() string {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	return sc.Text()
}
func rb() []byte {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	return sc.Bytes()
}
func rr() []rune {
	sc.Scan()
	if err := sc.Err(); err != nil {
		panic(err)
	}
	return []rune(sc.Text())
}
func ris(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ri()
	}
	return s
}
func risZeroBased(n int) []int {
	s := make([]int, n)
	for i := range s {
		s[i] = ri() - 1
	}
	return s
}
func ri64s(n int) []int64 {
	s := make([]int64, n)
	for i := range s {
		s[i] = ri64()
	}
	return s
}
func rss(n int) []string {
	s := make([]string, n)
	for i := range s {
		s[i] = rs()
	}
	return s
}
func pf(format string, a ...interface{}) {
	if _, err := fmt.Fprintf(bw, format, a...); err != nil {
		panic(err)
	}
}
func pln(a ...interface{}) {
	if _, err := fmt.Fprintln(bw, a...); err != nil {
		panic(err)
	}
}
func pall(a []int) {
	for _, v := range a {
		fmt.Fprintln(bw, v)
	}
}
func pallol(a []int) {
	s := fmt.Sprint(a)
	if _, err := fmt.Fprintln(bw, s[1:len(s)-1]); err != nil {
		panic(err)
	}
}
func dbg(a ...interface{}) {
	if _, err := fmt.Fprintln(os.Stderr, a...); err != nil {
		panic(err)
	}
}
func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1e7), 1e7)
	solve()
	bw.Flush()
}

const (
	maxBufSize = 1e6

	// maxInt64  = 9223372036854775807  // 9e18
	// maxInt32  = 2147483647           // 2e9
	// maxUint64 = 18446744073709551615 // 1e19
	inf = 1 << 60
	mod = 1e9 + 7
)

type pair struct{ a, b int }
type point struct{ x, y int }

func solve() {
	s, t := rs(), rs()

	if len(t) > len(s) {
		pln("UNRESTORABLE")
		return
	}

	check := func(i int) bool {
		for j := range t {
			if s[i+j] != '?' && s[i+j] != t[j] {
				return false
			}
		}
		return true
	}
	ks := make([]int, 0, 50)
	for i := 0; i < len(s)-len(t)+1; i++ {
		if check(i) {
			ks = append(ks, i)
		}
	}
	if len(ks) == 0 {
		pln("UNRESTORABLE")
		return
	}

	anss := make([]string, 0, 50)
	for _, k := range ks {
		tmp := []byte(s)
		for j := 0; j < len(t); j++ {
			tmp[k+j] = t[j]
		}
		for i := range tmp {
			if tmp[i] == '?' {
				tmp[i] = 'a'
			}
		}
		anss = append(anss, string(tmp))
	}
	sort.Strings(anss)
	pln(anss[0])
}


package main

import "fmt"

func main() {
	var s, t string
	fmt.Scan(&s, &t)
	for i := len(s) - len(t); i >= 0; i-- {
		com := true
		for j := 0; j < len(t); j++ {
			if s[i+j] == '?' {
				continue
			}
			if t[j] != s[i+j] {
				com = false
				break
			}
		}
		if com {
			v := s[:i] + t + s[i+len(t):]
			ans := ""
			for _, c := range v {
				if c == '?' {
					ans += "a"
				} else {
					ans += string(c)
				}
			}
			fmt.Println(ans)
			return
		}
	}
	fmt.Println("UNRESTORABLE")
}


package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w]
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w]
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func main(){
	var S, T string
	fmt.Scan(&S)
	fmt.Scan(&T)
	lenS, lenT := len(S), len(T)
	ans_arr := make([]string, lenS)
	first := -1
	for i:=0; i<=lenS-lenT; i++ {
		cnt := 0
		for j:=0; j<lenT; j++ {
			if S[i+j] == T[j]  || S[i+j] == '?' {
				cnt++
			} else {
				break
			}
		}
		if cnt == lenT {
			first = i
		}
	}
	if first == -1 {
		fmt.Println("UNRESTORABLE")
	} else {
		for i:=0; i<lenS; i++ {
			if i<first {
				if S[i] == '?' {
					ans_arr[i] = "a"
				} else {
					ans_arr[i] = string(S[i])
				}
			} else {
				ans_arr[i] = string(T[i-first])
			}

		}
		fmt.Println(strings.Join(ans_arr, ""))
	}
}


package main

import (
  "fmt"
)
var str, t string
var quest []int

func check(idx int) bool {
  if len(str) - idx < len(t) {
    return false
  }
  for i := idx; i < idx + len(t); i++{
    if str[i] != t[i-idx] && str[i] != '?' {
      return false
    }
  }
  return true
}

func main() {
  fmt.Scanf("%s\n%s", &str, &t)
  if str == t {
    fmt.Println(str)
    return
  }

  quest = make([]int, len(str))
  cnt := 0
  for i := len(str) - 1; i >= 0; i-- {
    if str[i] != '?' {
      cnt = 0
    } else {
      cnt += 1
      quest[i] = cnt
    }
  }
  idx := -1
  for i := 0; i < len(str); i++ {
    if(check(i)) {
      idx = i
    }
  }
  if idx == -1 {
    fmt.Println("UNRESTORABLE")
  } else {
    for i := 0; i < len(str); i++ {
      if i == idx {
        fmt.Print(t)
        i += len(t)
      } else if str[i] == '?' {
        fmt.Print("a")
      } else {
        fmt.Print(string(str[i]))
      }
    }
    fmt.Println()
  }

}


package main

import (
	"fmt"
)

func main() {
	var S, T string
	fmt.Scan(&S, &T)

	index := -1
	for i := range S {
		var j int
		for ; j < len(T); j++ {
			if i+j < len(S) && (S[i+j] == '?' || S[i+j] == T[j]) {
			} else {
				break
			}
		}

		if j == len(T) {
			index = i
		}
	}

	if index == -1 {
		fmt.Println("UNRESTORABLE")
	} else {
		s := S[:index] + T + S[index+len(T):]
		for i := range s {
			if s[i] == '?' {
				fmt.Print("a")
			} else {
				fmt.Print(string(s[i]))
			}
		}
		fmt.Println()
	}

}


package main

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

func main() {
	sc := NewScanner()
	S, T := sc.Next(), sc.Next()

	for i := 0; i <= len(S)-len(T); i++ {
		count := 0
		for j := 0; j < len(T); j++ {
			if S[i+j] == T[j] || S[i+j] == '?' {
				count++
			}
		}
		if count == len(T) {
			ans := make([]byte, len(S))
			for p := 0; p < len(S); p++ {
				if i <= p && p < i+len(T) {
					ans[p] = T[p-i]
				} else {
					if S[p] == '?' {
						ans[p] = 'a'
					} else {
						ans[p] = S[p]
					}
				}
			}
			fmt.Println(string(ans))
			return
		}
	}

	fmt.Println("UNRESTORABLE")

}

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

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

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

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

// H =height/witdh
var H int

// W =height/witdh
var W int

func main() {
	scanInit()

	H = nextInt()
	W = nextInt()
	s := nextGrid(H, W)

	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			if s[i][j] == "." {
				s[i][j] = getBombnum(i, j, s)
			}
		}
	}

	DBGprintGrid(s)

}

// DBGprintGrid print the grid
//! DBG
func DBGprintGrid(g [][]string) {
	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			fmt.Printf("%s", g[i][j])
		}
		fmt.Printf("\n")
	}
}
func getBombnum(i, j int, s [][]string) string {
	cnt := 0

	// 上段
	if i > 0 && j > 0 {
		if s[i-1][j-1] == "#" {
			cnt++
		}
	}
	if i > 0 {
		if s[i-1][j] == "#" {
			cnt++
		}
	}
	if i > 0 && j < W-1 {
		if s[i-1][j+1] == "#" {
			cnt++
		}
	}
	// 中段
	if j > 0 {
		if s[i][j-1] == "#" {
			cnt++
		}
	}
	if j < W-1 {
		if s[i][j+1] == "#" {
			cnt++
		}
	}
	// 下段
	if i < H-1 && j > 0 {
		if s[i+1][j-1] == "#" {
			cnt++
		}
	}
	if i < H-1 {
		if s[i+1][j] == "#" {
			cnt++
		}
	}
	if i < H-1 && j < W-1 {
		if s[i+1][j+1] == "#" {
			cnt++
		}
	}

	return fmt.Sprintf("%d", cnt)
}

// ---- readfunc
func scanInit() {
	const cap = 200 * 1024 // default=64*1024
	var buf = make([]byte, cap)
	sc.Buffer(buf, cap)
	sc.Split(bufio.ScanWords)
	return
}
func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}
func nextStr() string {
	sc.Scan()
	return sc.Text()
}
func nextGrid(a int, b int) [][]string {
	mat := make([][]string, a)
	for i := 0; i < a; i++ {
		mat[i] = make([]string, b)
		tmp := nextStr()
		mat[i] = strings.Split(tmp, "")
	}
	return mat
}

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


package main

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

func main() {
	H, W := ReadInt(), ReadInt()
	S := make([]string, H)
	for i := 0; i < H; i++ {
		S[i] = ReadString()
	}
	dx := []int{1, 0, -1, 1, -1, 1, 0, -1}
	dy := []int{1, 1, 1, 0, 0, -1, -1, -1}
	for y := 0; y < H; y++ {
		l := make([]byte, W)
		for x := 0; x < W; x++ {
			if S[y][x] == '#' {
				l[x] = '#'
				continue
			}
			c := 0
			for i := 0; i < 8; i++ {
				nx, ny := x+dx[i], y+dy[i]
				if 0 <= nx && nx < W && 0 <= ny && ny < H {
					if S[ny][nx] == '#' {
						c++
					}
				}
			}
			l[x] = byte(c + '0')
		}
		fmt.Println(string(l))
	}
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}


package main

import "fmt"

func main() {
	var h, w int
	fmt.Scan(&h, &w)
	s := make([]string, h)
	for i := 0; i < h; i++ {
		fmt.Scan(&s[i])
	}

	a := make([]string, h)
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			if s[i][j] == '#' {
				a[i] += "#"
				continue
			}

			c := 0
			for y := -1; y <= 1; y++ {
				if i+y < 0 || i+y > h-1 {
					continue
				}
				for x := -1; x <= 1; x++ {
					if j+x < 0 || j+x > w-1 {
						continue
					}
					if s[i+y][j+x] == '#' {
						c++
					}
				}
			}

			a[i] += fmt.Sprintf("%d", c)
		}
	}

	for _, r := range a {
		fmt.Println(r)
	}
}


package main

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

func main() {
	var h, w int
	fmt.Scan(&h, &w)
	grid := make([]string, h+2)
	wall := createWall(w)
	grid[0] = wall
	grid[h+1] = wall
	sc := bufio.NewScanner(os.Stdin)
	for i := 1; i <= h; i++ {
		sc.Scan()
		grid[i] = "." + sc.Text() + "."
	}
	for i := 1; i <= h; i++ {
		buf := bytes.NewBuffer([]byte{})
		for j := 1; j <= w; j++ {
			if grid[i][j] == '#' {
				buf.Write([]byte{'#'})
				continue
			}
			n := bombNum(grid, i, j)
			buf.WriteString(fmt.Sprintf("%d", n))
		}
		fmt.Println(buf.String())
	}
}

func bombNum(grid []string, i, j int) (num int) {
	type pos struct{ y, x int }
	for _, p := range []pos{
		{i - 1, j - 1}, {i - 1, j}, {i - 1, j + 1},
		{i, j - 1}, {i, j + 1},
		{i + 1, j - 1}, {i + 1, j}, {i + 1, j + 1},
	} {
		if grid[p.y][p.x] == '#' {
			num++
		}
	}
	return
}

func createWall(w int) string {
	buf := bytes.NewBuffer([]byte{})
	for i := 0; i < w+2; i++ {
		buf.Write([]byte{'.'})
	}
	return buf.String()
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

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

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

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

func checkBomb(h int, w int, arr [][]string) string {
	if arr[h][w] == "#" {
		return "#"
	}

	res := 0
	for i := h-1; i <= h+1; i++ {
		for j := w-1; j <= w+1; j++ {
			if arr[i][j] == "#" {
				res++
			}
		}
	}
	return strconv.Itoa(res)
}

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

	h, w := nextInt(), nextInt()

	grid := make([][]string, h+2)
	grid[0] = make([]string, w+2)
	grid[h+1] = make([]string, w+2)

	for i := 0; i < h; i++ {
		row := strings.Split(nextLine(), "")
		tmp := make([]string, w+2)
		for j := 0; j < w; j++ {
			tmp[j+1] = row[j]
		}
		grid[i+1] = tmp
	}

	ans := ""
	for i := 1; i < h+1; i++ {
		tmp := ""
		for j := 1; j < w+1; j++ {
			tmp += checkBomb(i, j, grid)
		}
		ans += tmp + "\n"
	}
	fmt.Println(strings.TrimSuffix(ans, "\n"))
}


package main

import (
	"fmt"
	"strconv"
)

func mine(f, w int, maps []string) []string {
	dx := []int{-1, 0, 1, -1, 1, -1, 0, 1}
	dy := []int{1, 1, 1, 0, 0, -1, -1, -1}

	minemap := make([]string, f)
	for i := 0; i < f; i++ {
		for j := 0; j < w; j++ {
			if string(maps[i][j]) == "#" {
				minemap[i] += "#"
				continue
			}
			var mine int
			for d := 0; d < 8; d++ {
				posx := i + dx[d]
				posy := j + dy[d]
				if posx < 0 || f <= posx {
					continue
				}
				if posy < 0 || w <= posy {
					continue
				}
				if string(maps[posx][posy]) == "#" {
					mine++
				}
			}
			minemap[i] += strconv.Itoa(mine)
		}
	}
	return minemap
}

func main() {
	var f, w int
	fmt.Scan(&f, &w)
	maps := make([]string, f)
	for i := range maps {
		fmt.Scan(&maps[i])
	}
	for _, out := range mine(f, w, maps) {
		fmt.Println(out)
	}
}


package main

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

/*
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)
// readLine can read long line string (at least 10^5)
func readLine() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}
// NextLine reads a line text from stdin, and then returns its string.
func NextLine() string {
	return readLine()
}
*/

var sc = bufio.NewScanner(os.Stdin)

// NextLine reads a line text from stdin, and then returns its string.
func NextLine() string {
	sc.Scan()
	return sc.Text()
}

// NextIntsLine reads a line text, that consists of **ONLY INTEGERS DELIMITED BY SPACES**, from stdin.
// And then returns intergers slice.
func NextIntsLine() []int {
	ints := []int{}
	intsStr := NextLine()
	tmp := strings.Split(intsStr, " ")
	for _, s := range tmp {
		integer, _ := strconv.Atoi(s)
		ints = append(ints, integer)
	}
	return ints
}

// NextStringsLine reads a line text, that consists of **STRINGS DELIMITED BY SPACES**, from stdin.
// And then returns strings slice.
func NextStringsLine() []string {
	str := NextLine()
	return strings.Split(str, " ")
}

// NextRunesLine reads a line text, that consists of **ONLY CHARACTERS ARRANGED CONTINUOUSLY**, from stdin.
// Ant then returns runes slice.
func NextRunesLine() []rune {
	return []rune(NextLine())
}

// Max returns the max integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Max(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m < integer {
			m = integer
		}
	}
	return m
}

// Min returns the min integer among input set.
// This function needs at least 1 argument (no argument causes panic).
func Min(integers ...int) int {
	m := integers[0]
	for i, integer := range integers {
		if i == 0 {
			continue
		}
		if m > integer {
			m = integer
		}
	}
	return m
}

// PowInt is integer version of math.Pow
func PowInt(a, e int) int {
	if a < 0 || e < 0 {
		panic(errors.New("[argument error]: PowInt does not accept negative integers"))
	}
	fa := float64(a)
	fe := float64(e)
	fanswer := math.Pow(fa, fe)
	return int(fanswer)
}

// AbsInt is integer version of math.Abs
func AbsInt(a int) int {
	fa := float64(a)
	fanswer := math.Abs(fa)
	return int(fanswer)
}

// DeleteElement returns a *NEW* slice, that have the same and minimum length and capacity.
// DeleteElement makes a new slice by using easy slice literal.
func DeleteElement(s []int, i int) []int {
	if i < 0 || len(s) <= i {
		panic(errors.New("[index error]"))
	}
	// appendのみの実装
	n := make([]int, 0, len(s)-1)
	n = append(n, s[:i]...)
	n = append(n, s[i+1:]...)
	return n
}

// Concat returns a *NEW* slice, that have the same and minimum length and capacity.
func Concat(s, t []rune) []rune {
	n := make([]rune, 0, len(s)+len(t))
	n = append(n, s...)
	n = append(n, t...)
	return n
}

// UpperRune is rune version of `strings.ToUpper()`.
func UpperRune(r rune) rune {
	str := strings.ToUpper(string(r))
	return []rune(str)[0]
}

// LowerRune is rune version of `strings.ToLower()`.
func LowerRune(r rune) rune {
	str := strings.ToLower(string(r))
	return []rune(str)[0]
}

// ToggleRune returns a upper case if an input is a lower case, v.v.
func ToggleRune(r rune) rune {
	var str string
	if 'a' <= r && r <= 'z' {
		str = strings.ToUpper(string(r))
	} else if 'A' <= r && r <= 'Z' {
		str = strings.ToLower(string(r))
	} else {
		str = string(r)
	}
	return []rune(str)[0]
}

// ToggleString iteratively calls ToggleRune, and returns the toggled string.
func ToggleString(s string) string {
	inputRunes := []rune(s)
	outputRunes := make([]rune, 0, len(inputRunes))
	for _, r := range inputRunes {
		outputRunes = append(outputRunes, ToggleRune(r))
	}
	return string(outputRunes)
}

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

// sort package (snippets)
//sort.Sort(sort.IntSlice(s))
//sort.Sort(sort.Reverse(sort.IntSlice(s)))
//sort.Sort(sort.Float64Slice(s))
//sort.Sort(sort.StringSlice(s))

// copy function
//a = []int{0, 1, 2}
//b = make([]int, len(a))
//copy(b, a)

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

var h, w int
var S [][]rune

func main() {
	tmp := NextIntsLine()
	h, w = tmp[0], tmp[1]
	for i := 0; i < h; i++ {
		line := NextRunesLine()
		S = append(S, line)
	}
	step := [][]int{
		{-1, -1}, {0, -1}, {1, -1},
		{-1, 0}, {1, 0},
		{-1, 1}, {0, 1}, {1, 1},
	}
	for i := 0; i < w; i++ {
		for j := 0; j < h; j++ {
			if S[j][i] == '#' {
				continue
			}

			num := 0
			for _, s := range step {
				x := i + s[0]
				y := j + s[1]
				if 0 <= x && x < w && 0 <= y && y < h && S[y][x] == '#' {
					num++
				}
			}
			numStr := strconv.Itoa(num)
			for _, r := range numStr {
				S[j][i] = r
			}
		}
	}
	for i := 0; i < h; i++ {
		fmt.Println(string(S[i]))
	}
}


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)
	N := nextInt()
	sum := 0
	for i := 0; i < N; i++ {
		l, r := nextInt(), nextInt()
		sum += r - l + 1
	}
	fmt.Println(sum)
}

Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


package main

import (
	"fmt"
)

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

	ans := 0
	for i := 0; i < n; i++ {
		var l, r int
		fmt.Scan(&l, &r)
		ans += (r - l) + 1
	}

	fmt.Println(ans)
}


package main

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

func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	l := make([]int, n)
	r := make([]int, n)
	for i := 0; i < n; i++ {
		l[i] = nextInt()
		r[i] = nextInt()
	}
	fmt.Println(n, l, r)
	res := 0
	for i := 0; i < n; i++ {
		res += r[i] - l[i] + 1
	}
	fmt.Println(res)
}

// -------- Library --------
var sc = bufio.NewScanner(os.Stdin)

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

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

func nextIntArray(size int) []int {
	res := make([]int, size)
	for i := 0; i < size; i++ {
		res[i] = nextInt()
	}
	return res
}


package main

import "fmt"

func main() {
    var N int
    fmt.Scan(&N)
    ans := 0
    for i := 0; i < N; i++ {
        var l, r int
        fmt.Scan(&l)
        fmt.Scan(&r)
        ans += r - l + 1
    }
    fmt.Println(ans)
}


package main

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

func main() {
	sc := NewScanner()
	N := sc.NextInt()
	ans := 0
	for i := 0; i < N; i++ {
		A, B := sc.NextInt(), sc.NextInt()
		ans += B - A + 1
	}
	fmt.Println(ans)

}

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

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

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

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


package main

import (
	"fmt"
)

func main() {
	// Code for B - Not Found
	var S string
	fmt.Scanf("%s", &S)

	ans := make(map[string]int, len(S))
	for i := 0; i < len(S); i++ {
		ans[string(S[i])]++
	}

	for i := 0; i < 26; i++ {
		char := string('a' + i)
		if ans[char] == 0 {
			fmt.Println(char)
			break
		}
		if i == 25 {
			fmt.Println("None")
		}
	}

}

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


package main

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

func main() {
	S := ReadString()
	m := make(map[byte]bool)
	for _, c := range []byte(S) {
		m[c] = true
	}
	for c := byte('a'); c <= 'z'; c++ {
		if !m[c] {
			fmt.Println(string(c))
			return
		}
	}
	fmt.Println("None")
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}


package main

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

func main() {
	S := ReadString()
	m := make(map[byte]bool)
	for _, c := range []byte(S) {
		m[c] = true
	}
	for c := byte('a'); c <= 'z'; c++ {
		if !m[c] {
			fmt.Printf("%c\n", c)
			return
		}
	}
	fmt.Println("None")
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}


package main

import (
  "fmt"
)

func main() {
  var s string
  fmt.Scan(&s)
  res := make(map[rune]bool)
  for _, c := range s {res[c] = true}
  ans := "None"
  for i := 'a'; i < 'z' + 1; i++ {
    if _, ok := res[i]; !ok {
      ans = string(i)
      break
    }
  }
  fmt.Println(ans)
}


package main

import (
	"bufio"
	"fmt"
	"os"
	"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()
}

func main() {
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)

	S := getString()
	m := make([]int, 26)
	for _, v := range S {
		m[v-'a'] = 1
	}
	ans := "None"
	for i := 0; i < 26; i++ {
		if m[i] == 0 {
			ans = string('a' + i)
			break
		}
	}
	out(ans)
}


package main

import "fmt"

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

	m := make(map[rune]int, 26)
	for _, r := range s {
		m[r]++
	}

	if len(m) == 26 {
		fmt.Println("None")
	} else {
		az := "abcdefghijklmnopqrstuvwxyz"
		for _, r := range az {
			if _, ok := m[r]; !ok {
				fmt.Println(string(r))
				return
			}
		}
	}
}


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() {
	var str string
	fmt.Scan(&str)

	slice := make([]int, 26)
	for _, v := range str {
		slice[int(v)-97] = 1
	}

	for i, v := range slice {
		if v == 0 {
			fmt.Println(string(i + 97))
			return
		}
	}

	fmt.Println("None")

}


package main

import "fmt"

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

	bucket := make([]int, 26)
	for _, i := range s {
		bucket[i-97]++
	}

	for i, v := range bucket {
		if v == 0 {
			fmt.Printf(string(i + 97))
			return
		}
	}
	fmt.Println("None")
}


package main

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

func main() {
	sc := NewScanner()
	S := sc.NextLine()

	mp := make([]int, 26)
	for i := 0; i < len(S); i++ {
		mp[S[i]-'a']++
	}
	for i := 0; i < 26; i++ {
		if mp[i] == 0 {
			fmt.Println(string('a' + i))
			return
		}
	}
	fmt.Println("None")

}

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

func NewScanner() *Scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 1000)
	return &Scanner{r: rdr}
}
func (s *Scanner) Next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *Scanner) NextLine() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *Scanner) NextInt() int {
	v, _ := strconv.Atoi(s.Next())
	return v
}
func (s *Scanner) NextInt64() int64 {
	v, _ := strconv.ParseInt(s.Next(), 10, 64)
	return v
}

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

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

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

	return mp
}

func (s *Scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *Scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


package main

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

const (
	initialBufSize = 1e4
	maxBufSize     = 1e6 + 7
)

var buf []byte = make([]byte, maxBufSize)
var sc = bufio.NewScanner(os.Stdin)

func init() {
	sc.Split(bufio.ScanWords)
	sc.Buffer(buf, maxBufSize)
}

func main() {
	N := nextInt()
	cnt := map[int]int{
		1: 0,
		2: 0,
		4: 0,
	}
	for i := 0; i < N; i++ {
		a := nextInt()
		if a%2 == 1 {
			cnt[1]++
		} else if a%4 == 0 {
			cnt[4]++
		} else {
			cnt[2]++
		}
	}
	// fmt.Println(cnt)
	if cnt[2] == 0 {
		cnt[4]++
	}
	if cnt[4] >= cnt[1] {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}
	// if cnt[2] == 0 {
	// 	if cnt[4]+1 >= cnt[1] {
	// 		fmt.Println("Yes")
	// 	} else {
	// 		fmt.Println("No")
	// 	}
	// } else {
	// 	if cnt[4] >= cnt[1] {
	// 		fmt.Println("Yes")
	// 	} else {
	// 		fmt.Println("No")
	// 	}
	// }
	// if (cnt[4]*2 >= cnt[1] && cnt[2]%2 == 0) || cnt[4]*2 >= cnt[1]+cnt[2] {
	// 	fmt.Println("Yes")
	// } else {
	// 	fmt.Println("No")
	// }
}

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

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

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

func pow(p, q int) int {
	res := p
	if q == 0 {
		return 1
	}
	for i := 0; i < q-1; i++ {
		res *= p
	}
	return res
}

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

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

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

func lcmof2numbers(a int, b int) int {
	return a / gcdof2numbers(a, b) * b
}

// マイナスの場合は考慮していない
func fuctorial(a int) int {
	if a == 1 || a == 0 {
		return 1
	} else {
		return fuctorial(a-1) * a
	}
}

func NextPermutation(x sort.Interface) bool {
	n := x.Len() - 1
	if n < 1 {
		return false
	}
	j := n - 1
	for ; !x.Less(j, j+1); j-- {
		if j == 0 {
			return false
		}
	}
	l := n
	for !x.Less(j, l) {
		l--
	}
	x.Swap(j, l)
	for k, l := j+1, n; k < l; {
		x.Swap(k, l)
		k++
		l--
	}
	return true
}

// 素因数分解したmapを返す
func primeFuctorize(n int) map[int]int {
	pf := map[int]int{}
	for i := 2; i*i <= n; i++ {
		for n%i == 0 {
			pf[i]++
			n /= i
		}
	}
	if n != 1 {
		pf[n]++
	}
	return pf
}

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


package main

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

// const abcd = "abcdefghijklnmopqrstuvwxyz"
// const ABCD = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

// var dy = [...]int{1, 1, 0, -1, -1, -1, 0, 1}
// var dx = [...]int{0, 1, 1, 1, 0, -1, -1, -1}

// var dx = [...]int{0, 1, 0, -1}
// var dy = [...]int{1, 0, -1, 0}

// var inf int = 1e9
// var mod = 1000000007

// ---------------------------------------------------------
func main() {
	log.SetFlags(log.Lshortfile)
	next := newScanner()
	n := next.Int()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = next.Int()
	}
	var cnt [3]int
	for i := 0; i < n; i++ {
		log.Println(a[i], a[i]%4, a[i]/2)
		if a[i]%2 != 0 {
			a[i] = 1
			cnt[0]++
		} else if a[i]/2 == 1 {
			a[i] = 2
			cnt[1]++
		} else if a[i]/2 >= 2 {
			a[i] = 4
			cnt[2]++
		}
	}
	log.Println(cnt)
	ans := ""
	if cnt[1] == 0 {
		if cnt[0] <= cnt[2]+1 {
			ans = "Yes"
		} else {
			ans = "No"
		}
	} else {
		if cnt[0] <= cnt[2] {
			ans = "Yes"
		} else {
			ans = "No"
		}
	}

	fmt.Println(ans)
}

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

// Pair is liked C++ pair
type Pair struct {
	a, b int
}

// Pairs is sorted by []Pair struct
type Pairs []Pair

func (p Pairs) Len() int {
	return len(p)
}
func (p Pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p Pairs) Less(i, j int) bool {
	if p[i].a < p[j].a {
		return true
	} else if p[i].a == p[j].a {
		return p[i].b < p[j].b
	}
	return false
}

// -------------------------------
func in(c, a, z int) bool {
	return c >= a && c < z
}

func btoi(b bool) int {
	if b {
		return 1
	}
	return 0
}

func itob(a int) bool {
	if a == 0 {
		return false
	}
	return true
}

func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}

func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}

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

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

// ---------- buffered scanner -----------------------------------------
type scanner struct {
	r   *bufio.Reader
	buf []byte
	p   int
}

func newScanner() *scanner {
	rdr := bufio.NewReaderSize(os.Stdin, 10000)
	return &scanner{r: rdr}
}
func (s *scanner) next() string {
	s.pre()
	start := s.p
	for ; s.p < len(s.buf); s.p++ {
		if s.buf[s.p] == ' ' {
			break
		}
	}
	result := string(s.buf[start:s.p])
	s.p++
	return result
}
func (s *scanner) Line() string {
	s.pre()
	start := s.p
	s.p = len(s.buf)
	return string(s.buf[start:])
}
func (s *scanner) Int() int {
	v, _ := strconv.Atoi(s.next())
	return v
}
func (s *scanner) Int64() int64 {
	v, _ := strconv.ParseInt(s.next(), 10, 64)
	return v
}
func (s *scanner) pre() {
	if s.p >= len(s.buf) {
		s.readLine()
		s.p = 0
	}
}
func (s *scanner) readLine() {
	s.buf = make([]byte, 0)
	for {
		l, p, e := s.r.ReadLine()
		if e != nil {
			panic(e)
		}
		s.buf = append(s.buf, l...)
		if !p {
			break
		}
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func init() {
	const maxWord = (1 << 30)
	buf := []byte{}
	sc.Buffer(buf, maxWord)
	sc.Split(bufio.ScanWords)
}

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

func solve(n, a, b int) int {
	if a > b {
		return 0
	}
	if n == 1 && a != b {
		return 0
	}

	minv := (n - 2) * a
	maxv := (n - 2) * b
	return maxv - minv + 1
}

func main() {
	n, _ := strconv.Atoi(input())
	a, _ := strconv.Atoi(input())
	b, _ := strconv.Atoi(input())
	fmt.Println(solve(n, a, b))
}

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1