package main

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

////////////////////////////////////////
///        templates                 ///
////////////////////////////////////////

var sc = bufio.NewScanner(os.Stdin)

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

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 strSprit(str string) []string {
        cols := strings.Split(str, " ")
        return cols
}

func parseInt(str string) int {
        n, _ := strconv.Atoi(str)
        return n
}

func intSprit(str string) []int {
        strs := strSprit(str)
        cols := make([]int, len(strs))
        for i, v := range strs {
                cols[i] = parseInt(v)
        }
        return cols
}

func bitCount(n uint) int {

        x := uint64(n)

        const m = 1<<64 - 1
        const m0 = 0x5555555555555555
        const m1 = 0x3333333333333333
        const m2 = 0x0f0f0f0f0f0f0f0f

        x = x>>1&(m0&m) + x&(m0&m)
        x = x>>2&(m1&m) + x&(m1&m)
        x = (x>>4 + x) & (m2 & m)
        x += x >> 8
        x += x >> 16
        x += x >> 32

        return int(x) & (1<<7 - 1)
}

func bitExist(n, i int) bool {
        return ((n >> uint(i)) & 1) == 1
}

////////////////////////////////////////
///        end templates             ///
////////////////////////////////////////
func abs(a, b int) int {
        return int(math.Abs(float64(a - b)))
}

func chmin(dp []int, idx, n int) {
        fmt.Println(dp[idx], n, idx)
        if dp[idx] > n {
                dp[idx] = n
        }
}
func main() {
        line := readBigLine()
        N := parseInt(line)

        l := readBigLine()
        h := intSprit(l)

        INF := 1 << 30

        dp := make([]int, N+1000)
        for i := 0; i < N+1000; i++ {
                dp[i] = INF
        }
        dp[0] = 0

        for i := 0; i < N; i++ {
                if N > i+1 {
                        chmin(dp, i+1, dp[i]+abs(h[i], h[i+1]))
                }
                if N > i+2 {
                        chmin(dp, i+2, dp[i]+abs(h[i], h[i+2]))
                }
        }
        fmt.Println(dp[N-1])
}


package main

import (
    "fmt"
)

var n, m int
var l [][]int
var r []int

func main() {
    fmt.Scan(&n)
    fmt.Scan(&m)
    l = make([][]int, n)
    r = make([]int, n)
    for i := 0; i < m; i++ {
        var x, y int
        fmt.Scan(&x, &y)
        l[x-1] = append(l[x-1], y-1)
    }
    for i := range l {
        r[i] = -1
    }

    fmt.Println(l)

    var max int
    for i := 0; i < n; i++ {
        res := calc(i)
        if res > max {
            max = res
        }
    }

    fmt.Println(max)
}

func calc(k int) int {
    if r[k] != -1 {
        return r[k]
    }
    if len(l[k]) == 0 {
        return 0
    }
    var max int
    for _, i := range l[k] {
        res := calc(i)
        if res > max {
            max = res
        }
    }
    r[k] = max + 1
    return r[k]
}

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

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 30 40 20

Sample Output 1

30

If we follow the path 1 → 2 → 4, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

2
10 10

Sample Output 2

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

If we follow the path 1 → 3 → 5 → 6, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.

Submitted source-code file size, in bytes.

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

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


package main

import (
	"fmt"
)

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

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

	costs := make([]int, N)
	costs[0] = 0
	costs[1] = abs(hs[1] - hs[0])

	for i := 2; i < N; i++ {
		c1 := costs[i-1] + abs(hs[i]-hs[i-1])
		c2 := costs[i-2] + abs(hs[i]-hs[i-2])
		costs[i] = min(c1, c2)
	}

	fmt.Println(costs[N-1])
}

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
}


package main

import "fmt"

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

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

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

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

	dp := make([]int, n)
	for i := range dp {
		dp[i] = 10000000000000 // +Inf
	}
	dp[0] = 0

	for i := 0; i < n; i++ {
		for j := i + 1; j <= i+k && j < n; j++ {
			dp[j] = min(dp[j], abs(h[i]-h[j])+dp[i])
		}
	}

	fmt.Println(dp[len(dp)-1])
}

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, i + K. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 100

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 3
10 30 40 50 20

Sample Output 1

30

If we follow the path 1 → 2 → 5, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

3 1
10 20 10

Sample Output 2

20

If we follow the path 1 → 2 → 3, the total cost incurred would be |10 - 20| + |20 - 10| = 20.

Sample Input 3

2 100
10 10

Sample Output 3

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 4

10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4

40

If we follow the path 1 → 4 → 8 → 10, the total cost incurred would be |40 - 70| + |70 - 70| + |70 - 60| = 40.


//go:generate echo "https://atcoder.jp/contests/dp/tasks/dp_b"
package main

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

type scanner struct{ *bufio.Scanner }

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

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

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

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

func solve(n, k int, h []int) int {
	t := make([]int, n+k+1, n+k+1)
	for i := range t {
		t[i] = math.MaxInt64
	}
	t[0] = 0
	for i := 0; i < n; i++ {
		for j := 0; j <= k; j++ {
			if v := t[i] + iabs(h[i+j]-h[i]); v < t[i+j] {
				t[i+j] = v
			}
		}
	}
	return t[n-1]
}

func main() {
	scan := newScanner(os.Stdin)
	n, k := scan.Int(), scan.Int()
	h := make([]int, n+k+1, n+k+1)
	for i := 0; i < n; i++ {
		h[i] = scan.Int()
	}
	a := solve(n, k, h)
	fmt.Println(a)
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	scanInit()

	n := nextInt()
	k := nextInt()
	h := make([]int, n)

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

	dp := make([]int, n)
	dp[0] = 0
	for i := 1; i < n; i++ {
		mincost := 10000000000
		for j := max(i-k, 0); j < i; j++ {
			costnow := dp[j] + abs(h[i]-h[j])
			//fmt.Println("i,j=", i, j, " dp[j]=", dp[j], " abs=", abs(h[i]-h[j]))
			mincost = min(mincost, costnow)
		}
		dp[i] = mincost

	}

	fmt.Println(dp[n-1])

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

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


package main

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

var sc = bufio.NewScanner(os.Stdin)

func main() {
	scanInit()

	n := nextInt()
	k := nextInt()
	h := make([]int, n)

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

	dp := make([]int, n)
	dp[0] = 0
	for i := 1; i < n; i++ {
		mincost := 100000
		for j := max(i-k, 0); j < i; j++ {
			costnow := dp[j] + abs(h[i]-h[j])
			mincost = min(mincost, costnow)
		}
		dp[i] = mincost
	}

	fmt.Println(dp[n-1])

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

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


package main

import (
	"fmt"
	"math"
)

func main() {
	n, k := scInt2()
	hlist := ints(n)
	dp := make([]int, n)
	inf := 100000
	for i := 0; i < n; i++ {
		dp[i] = inf
	}
	dp[0] = 0
	for i := 0; i < n; i++ {
		for j := 1; j <= k; j++ {
			if i+j < n {
				diff := abs(hlist[i+j]-hlist[i]) + dp[i]
				dp[i+j] = min(diff, dp[i+j])
			}
		}
	}
	fmt.Println(dp[n-1])
}

func scStr() string {
	var s string
	fmt.Scan(&s)
	return s
}

func scInt() int {
	var x int
	fmt.Scan(&x)
	return x
}

func scInt2() (int, int) {
	var x, y int
	fmt.Scan(&x, &y)
	return x, y
}

func scInt3() (int, int, int) {
	var x, y, z int
	fmt.Scan(&x, &y, &z)
	return x, y, z
}

func ints(n int) (slice []int) {
	slice = make([]int, n)
	for i := range slice {
		fmt.Scan(&slice[i])
	}
	return slice
}

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

func unique(strs []string) (unique []string) {
	m := map[string]bool{}
	for _, v := range strs {
		if !m[v] {
			m[v] = true
			unique = append(unique, v)
		}
	}
	return unique
}

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

func powi(x, pow int) int {
	return int(math.Pow(float64(x), float64(pow)))
}

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


package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func scanInts(n int) []int {
	sl := make([]int, n)
	for i := range sl {
		sl[i] = scanInt()
	}
	return sl
}

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

func main() {
	n, k := scanInt(), scanInt()
	h := scanInts(n)

	dp := make([]int, n)
	for i := range dp {
		dp[i] = math.MaxInt32
	}
	dp[0] = 0

	for i := 0; i < n; i++ {
		for j := 1; j <= k; j++ {
			if i+j >= n {
				continue
			}
			dp[i+j] = min(dp[i+j], dp[i]+abs(h[i]-h[i+j]))
		}
	}
	fmt.Println(dp[n-1])
}

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

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


package main

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

const (
	initialBufSize = 1024
	maxBufSize     = 1e6
)

var (
	scanner = bufio.NewScanner(os.Stdin)
	// writer  = bufio.NewWriterSize(os.Stdout, 1000000)
)

func main() {
	scanner.Split(bufio.ScanWords)
	// scanner.Buffer(make([]byte, initialBufSize), maxBufSize)
	// defer writer.Flush()
	n, k := scanInt(), scanInt()
	h := scanInts(n)

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

	dp := make([]int, n)
	for i := 1; i < n; i++ {
		dp[i] = math.MaxInt64
	}

	for i := 0; i < n; i++ {
		for j := 1; j <= k; j++ {
			if (i + j) >= n {
				break
			}
			t1, t2 := dp[i+j], dp[i]+abs(h[i]-h[i+j])
			dp[i+j] = min(t1, t2)
		}
	}

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

func scanInt() int {
	i, _ := strconv.Atoi(scanString())
	return i
}

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

func scanFloat64() float64 {
	f, _ := strconv.ParseFloat(scanString(), 64)
	return f
}

func scanFloat64s(size int) []float64 {
	f := make([]float64, size)
	for i := 0; i < size; i++ {
		f[i] = scanFloat64()
	}
	return f
}

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

func scanStrings(size int) []string {
	s := make([]string, size)
	for i := 0; i < size; i++ {
		s[i] = scanString()
	}
	return s
}

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

func pow(x, y int) int {
	return int(math.Pow(float64(x), float64(y)))
}

// greatest common divisor(最大公約数)
func gcd(a, b int) int {
	for {
		mod := a % b
		if mod == 0 {
			return b
		}
		a, b = b, mod
	}
}

// latest common multiple(最小公倍数)
func lcm(a, b int) int {
	return (a / gcd(a, b)) * b
}

func fact(n int) int {
	ret := 1
	for i := 1; i <= n; i++ {
		ret *= i
	}
	return ret
}

func perm(n, r int) int {
	ret := 1
	for i := 0; i < r; i++ {
		ret *= n
		n--
	}
	return ret
}

func isPrimeNum(n int) bool {

	if n <= 1 {
		return false
	}

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

	return true
}


package main

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

const pi = math.Pi

var mod int = pow(10, 9) + 7
var Umod uint64 = 1000000007
var ans, cnt, sum int = 0, 0, 0

func main() {
	reader.Split(bufio.ScanWords)
	N, _ := strconv.Atoi(read())
	K, _ := strconv.Atoi(read())

	h := make([]int, N)
	for i := 0; i < N; i++ {
		h[i], _ = strconv.Atoi(read())
	}
	dp := make([]int, N+5)
	for i := 1; i < N+5; i++ {
		dp[i] = 1e9
	}

	for i := 0; i < N; i++ {
		for j := i + 1; j < min(i+K+1, N); j++ {
			t := dp[i] + abs(h[i]-h[j])
			dp[j] = min(dp[j], t)
		}
	}
	fmt.Println(dp[N-1])
}

/*  ----------------------------------------  */

var reader = bufio.NewScanner(os.Stdin)

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

func lcm(x, y int) int {
	return (x / gcd(x, y)) * y
}

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

var fac [1000000]int
var finv [1000000]int
var inv [1000000]int

func combination_init() {
	fac[0], fac[1] = 1, 1
	finv[0], finv[1] = 1, 1
	inv[1] = 1

	// invは a^(-1) mod p
	// pをaで割ることを考える

	// p/a*(a) + p%a = p
	// p/a*(a) + p%a = 0          (mod p)
	// -p%a = p/a*(a)             (mod p)
	// -p%a *a^(-1)= p/a          (mod p)
	// a^(-1)= p/a * (-p%a)^(-1)  (mod p)
	// a^(-1) =

	for i := 2; i < 1000000; i++ {
		fac[i] = fac[i-1] * i % mod
		inv[i] = mod - inv[mod%i]*(mod/i)%mod
		finv[i] = finv[i-1] * inv[i] % mod
	}
}

func combination(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[y] * finv[x-y] % mod) % mod
	//return fac[x] / (fac[y] * fac[x-y])
}

func permutation(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[x-y] % mod) % mod
	//return fac[x] / fac[x-y]
}

func max(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Max(float64(x[i]), float64(res)))
	}
	return res
}

func min(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Min(float64(x[i]), float64(res)))
	}
	return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int    { return int(math.Abs(float64(x))) }
func floor(x int) int  { return int(math.Floor(float64(x))) }
func ceil(x int) int   { return int(math.Ceil(float64(x))) }

type SortBy []int

func (a SortBy) Len() int           { return len(a) }
func (a SortBy) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool { return a[i] < a[j] }

type PriorityQueue []int

func (h PriorityQueue) Len() int            { return len(h) }
func (h PriorityQueue) Less(i, j int) bool  { return h[i] > h[j] }
func (h PriorityQueue) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}


package main

import (
	"fmt"
)

func main() {
	INF := 100000 * 10000
	var N, K int
	fmt.Scan(&N)
	fmt.Scan(&K)
	H := make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&(H[i]))
	}

	dp := make([]int, N)
	for i := 0; i < N; i++ {
		dp[i] = INF
	}
	dp[0] = 0

	for i := 1; i < N; i++ {
		//dp[i] = (1..K).map{|k| i-k < 0 ? INF : dp[i-k] + (H[i] - H[i-k]).abs }.min
		value := INF
		for k := 1; k <= K; k++ {
			var tmp int
			if i-k < 0 {
				tmp = INF
			} else {
				tmp2 := H[i] - H[i-k]
				if tmp2 < 0 {
					tmp2 = tmp2 * -1
				}
				tmp = dp[i-k] + tmp2
			}

			if tmp < value {
				value = tmp
			}
		}
		dp[i] = value
	}

	fmt.Println(dp[N-1])
}


package main

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

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	inputs := strings.Split(stdin.Text(), " ")
	var n, k, i, j int
	n, _ = strconv.Atoi(inputs[0])
	k, _ = strconv.Atoi(inputs[1])
	var dp [100000]int
	var hList [10000]int
	var diff int
	stdin.Scan()
	hInput := strings.Split(stdin.Text(), " ")
	for i = 0; i < 1000 && i < n; i++ {
		hList[i], _ = strconv.Atoi(hInput[i])
		dp[i] = 0
		for j = 1; j <= i && j <= k; j++ {
			if hList[i] > hList[i-j] {
				diff = hList[i] - hList[i-j]
			} else {
				diff = hList[i-j] - hList[i]
			}
			if dp[i] == 0 {
				dp[i] = diff + dp[i-j]
			}
			if dp[i-j]+diff < dp[i] {
				dp[i] = dp[i-j] + diff
			}
		}
	}
	fmt.Println(dp[n-1])
}


package main

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

func main() {
	stdin := bufio.NewScanner(os.Stdin)
	stdin.Scan()
	inputs := strings.Split(stdin.Text(), " ")
	var n, k int
	n, _ = strconv.Atoi(inputs[0])
	k, _ = strconv.Atoi(inputs[1])
	var dp [100000]int
	var hList [10000]int
	stdin.Scan()
	hInput := strings.Split(stdin.Text(), " ")
	for i := 0; i < n; i++ {
		hList[i], _ = strconv.Atoi(hInput[i])
		dp[i] = 0
		for j := 1; j <= i && j <= k; j++ {
		}
	}
	fmt.Println(dp[n-1])
}


//go:generate echo "https://atcoder.jp/contests/dp/tasks/dp_d"
package main

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

type scanner struct{ *bufio.Scanner }

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

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

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

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

func solve(N, W int, wvs [][]int) int {
	t := make([][]int, N+1, N+1) // t[item_index][weight]
	for i := range t {
		t[i] = make([]int, W+1, W+1)
	}
	for i, wv := range wvs {
		weight, value := wv[0], wv[1]
		for sumWeight := 0; sumWeight <= W; sumWeight++ {
			if sumWeight-weight >= 0 {
				chmax(&t[i+1][sumWeight], t[i][sumWeight-weight]+value)
			}
			chmax(&t[i+1][sumWeight], t[i][sumWeight])
		}
	}
	return t[N][W]
}

func main() {
	for {

	}
	scan := newScanner(os.Stdin)
	N, W := scan.Int(), scan.Int()
	wvs := make([][]int, N, N)
	for i := 0; i < N; i++ {
		wvs[i] = []int{scan.Int(), scan.Int()}
	}
	fmt.Println(solve(N, W, wvs))
}

Score : 100 points

Problem Statement

There are N items, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), Item i has a weight of w_i and a value of v_i.

Taro has decided to choose some of the N items and carry them home in a knapsack.
The capacity of the knapsack is W, which means that the sum of the weights of items taken must be at most W.

Find the maximum possible sum of the values of items that Taro takes home.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq W \leq 10^5

1 \leq w_i \leq W

1 \leq v_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N W
w_1 v_1
w_2 v_2
:
w_N v_N

Output

Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1

3 8
3 30
4 50
5 60

Sample Output 1

90

Items 1 and 3 should be taken.
Then, the sum of the weights is 3 + 5 = 8, and the sum of the values is 30 + 60 = 90.

Sample Input 2

5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 3

6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3

17

Items 2, 4 and 5 should be taken.
Then, the sum of the weights is 5 + 6 + 3 = 14, and the sum of the values is 6 + 6 + 5 = 17.


package main

import "fmt"
var N, W, w,v int
var A [][]int
var dp [][]int

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 abs(a int) int {
	if a < 0 { return -a }
	return a
}

func main() {
	_,e := fmt.Scanf("%d%d", &N, &W)
	if e != nil { return }
	A = make([][]int, 0)
	for i:=0; i<N; i++ {
		_,e = fmt.Scanf("%d%d", &w, &v)
		A = append(A,[]int{w,v})
	}
	dp = make([][]int, N+1)
	dp[0] = make([]int, W+1)
	for i:=1; i<=N; i++ {
		dp[i] = make([]int, W+1)
		for j:=1; j<=W; j++ {
			if A[i-1][0] > j {
				dp[i][j] = dp[i-1][j]
			} else {
				dp[i][j] = max(dp[i-1][j], dp[i-1][j-A[i-1][0]] + A[i-1][1])
			}
		}
	}
	fmt.Println(dp[N][W])
}


package main

import (
	"fmt"
	"math"
)

type Item struct {
	Value  float64
	Weight float64
}

func main() {
	var ItemNumber int
	var MaxWeight float64
	sack := []Item{}

	fmt.Scanln(&ItemNumber, &MaxWeight)

	for i := 0; i < ItemNumber; i++ {
		X := Item{}
		fmt.Scanln(&X.Weight, &X.Value)
		sack = append(sack, X)
	}
	value, _ := Knapsack(sack,MaxWeight)
	fmt.Print(int64(value))
}

func combinations(items []Item, ch chan []Item) {
	defer close(ch)

	p := int(math.Pow(2., float64(len(items))))

	for i := 0; i < p; i++ {
		set := []Item{}
		for j := 0; j < len(items); j++ {
			if (i>>uint(j))&1 == 1 {
				set = append(set, items[j])
			}
		}
		ch <- set
	}
}

func getSackWeight(set []Item) (r float64) {
	for _, i := range set {
		r += i.Weight
	}
	return
}

func getSackValue(set []Item) (r float64) {
	for _, i := range set {
		r += i.Value
	}
	return
}

func Knapsack(items []Item, maxWeight float64) (float64, []Item) {
	bestVal := 0.
	bestSack := []Item{}

	ch := make(chan []Item)
	go combinations(items, ch)

	for sack := range ch {
		if getSackWeight(sack) <= maxWeight {
			v := getSackValue(sack)
			if v > bestVal {
				bestVal = v
				bestSack = sack
			}
		}
	}
	return bestVal, bestSack
}


package main

import (
	"fmt"
	"math"
)

type Item struct {
	Value  float64
	Weight float64
}

func main() {
	var ItemNumber int
	var MaxWeight float64
	sack := []Item{}

	fmt.Scanln(&ItemNumber, &MaxWeight)

	for i := 0; i < ItemNumber; i++ {
		X := Item{}
		fmt.Scanln(&X.Weight, &X.Value)
		sack = append(sack, X)
	}
	value, _ := Knapsack(sack,MaxWeight)
	fmt.Print(value)
}

func combinations(items []Item, ch chan []Item) {
	defer close(ch)

	p := int(math.Pow(2., float64(len(items))))

	for i := 0; i < p; i++ {
		set := []Item{}
		for j := 0; j < len(items); j++ {
			if (i>>uint(j))&1 == 1 {
				set = append(set, items[j])
			}
		}
		ch <- set
	}
}

func getSackWeight(set []Item) (r float64) {
	for _, i := range set {
		r += i.Weight
	}
	return
}

func getSackValue(set []Item) (r float64) {
	for _, i := range set {
		r += i.Value
	}
	return
}

func Knapsack(items []Item, maxWeight float64) (float64, []Item) {
	bestVal := 0.
	bestSack := []Item{}

	ch := make(chan []Item)
	go combinations(items, ch)

	for sack := range ch {
		if getSackWeight(sack) <= maxWeight {
			v := getSackValue(sack)
			if v > bestVal {
				bestVal = v
				bestSack = sack
			}
		}
	}
	return bestVal, bestSack
}


package main

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

func lib_MaxInt64(values ...int64) (max int64, err error) {
	if len(values) == 0 {
		return 0, errors.New("empty slice is given")
	}
	max = values[0]
	for _, value := range values {
		if max < value {
			max = value
		}
	}
	return
}
func lib_MustMaxInt64(values ...int64) (max int64) {
	max, err := lib_MaxInt64(values...)
	if err != nil {
		panic(err)
	}
	return max
}
func lib_New2DimInt64Slice(row, col int) [][]int64 {
	ret := make([][]int64, row)
	for r := 0; r < row; r++ {
		ret[r] = make([]int64, col, col)
	}
	return ret
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	const initialBufSize = 4096
	const maxBufSize = 1000000
	scanner.Buffer(make([]byte, initialBufSize), maxBufSize)
	scanner.Split(bufio.ScanWords)
	var N int64
	scanner.Scan()
	N, _ = strconv.ParseInt(scanner.Text(), 10, 64)
	var W int64
	scanner.Scan()
	W, _ = strconv.ParseInt(scanner.Text(), 10, 64)
	w := make([]int64, N)
	v := make([]int64, N)
	for i := int64(0); i < N; i++ {
		scanner.Scan()
		w[i], _ = strconv.ParseInt(scanner.Text(), 10, 64)
		scanner.Scan()
		v[i], _ = strconv.ParseInt(scanner.Text(), 10, 64)
	}
	fmt.Println(solve(N, W, w, v))
}
func solve(N int64, W int64, w []int64, v []int64) int64 {
	dp := lib_New2DimInt64Slice(int(N)+1, int(W+1))
	for i := 0; i < int(N); i++ {
		for j := int64(0); j <= W; j++ {
			newW := j + w[i]
			if newW <= W {
				dp[i+1][newW] = lib_MustMaxInt64(dp[i+1][newW], dp[i][j]+v[i])
			}
			dp[i+1][j] = lib_MustMaxInt64(dp[i+1][j], dp[i][j])
		}
	}
	return lib_MustMaxInt64(dp[N]...)
}


package main

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

type DP struct {
	w int
	v int
}

func main() {
	N := readi()
	W := readi()
	w := make([]int, N)
	v := make([]int, N)
	for i := 0; i < N; i++ {
		w[i] = readi()
		v[i] = readi()
	}

	dp := make([][]DP, N)
	dp[0] = make([]DP, N)
	for i := 0; i < N; i++ {
		if W >= w[i] {
			dp[0][i].w = w[i]
			dp[0][i].v = v[i]
		}
	}
	for i := 1; i < N; i++ {
		dp[i] = make([]DP, N)
		for ii := 0; ii < N; ii++ {
			for iii := 0; iii < N; iii++ {
				if iii != ii {
					if W >= w[ii]+dp[i-1][iii].w {
						chmax(&dp[i][iii].w, w[ii]+dp[i-1][iii].w)
						chmax(&dp[i][iii].v, v[ii]+dp[i-1][iii].v)
					}
				}
			}
		}
	}

	ans := 0
	for i := range dp {
		for ii := range dp[i] {
			ans = max(ans, dp[i][ii].v)
		}
	}

	fmt.Println(ans)
}

/*-------------------utilities-------------------*/

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

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

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

func chmin(x *int, y int) bool {
	if *x > y {
		*x = y
		return true
	}
	return false
}

func chmax(x *int, y int) bool {
	if *x < y {
		*x = y
		return true
	}
	return false
}

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

/*-------------------init-------------------*/

const (
	INF = 1000000000000000000
	MOD = 1e9 + 7
)

var (
	readString func() string
)

func init() {
	readString = newReadString(os.Stdin)
}

/*-------------------inputs-------------------*/

func readi() int {
	return int(readi64())
}

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

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

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

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


package main

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

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

// IO helper functions

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// Reads next token and return it as string
func reads() string {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return string(b)
}

// Read next line as string
func readsln() string {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}

	return string(b)
}

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

	return int(i)
}

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

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

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

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

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

const (
	MOD = 1000000007
	INF = math.MaxInt64
)

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

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

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

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

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

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

func lcm(a,b int) int{
  return a * b / gcd(a, b)
}

func isInteger(x float64) bool {
    return math.Floor(x) == x
}

// lower_bound
func bisectLeft(a []int, x int) int {
    return sort.SearchInts(a, x)
}

// upper_bound
func bisectRight(a []int, x int) int {
    return sort.Search(len(a), func(i int) bool { return a[i] > x })
}

func main() {
	defer Flush()

	N, W := readi(), readi()
	Vs := make([]int, N+1)
	Ws := make([]int, N+1)

	for i := range Vs {
		if i == 0 {
			continue
		}

		Ws[i] = readi()
		Vs[i] = readi()
	}

  	// DP テーブル
	dp := make([][]int, N+1)

	// DP テーブル全体を初期化 (最小化問題なので INF に初期化)
	for i := range dp {
		dp[i] = make([]int, W+1)

		for j := range dp[i] {
			dp[i][j] = 0
		}
	}

	// ループ
	for i := 1; i <= N; i++ {
		for j := 0; j <= W; j++{
			if j - Ws[i] >= 0 {
				if dp[i-1][j - Ws[i]] + Vs[i] >= dp[i][j] {
					dp[i][j] = dp[i-1][j - Ws[i]] + Vs[i]
				}
			}

			if dp[i-1][j] >= dp[i][j] {
				dp[i][j] = dp[i-1][j]
			}
		}
	}

	fmt.Println(dp[N][W])
}


package main

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

func main() {
	run()
}

func run() {
	setSpace()
	n, limit := int(readN()), readN()
	dp := make([][]int, n+1)
	// dp[i][sum_w] = v の最大値
	// dp[i][j] = i 番目までの品物を、重さを j 以下になるように選んだときの価値の最大値
	// chmax(dp[i-1][sum_w], dp[i-1][sum_w - w[i]] + v[i])
	dp[0] = make([]int, limit+1)
	for i := 0; i < n; i++ {
		w, v := readN(), readN()
		dp[i+1] = make([]int, limit+1)
		for sumW := 0; sumW <= limit; sumW++ {
			if sumW-w >= 0 {
				// i 番目の重さ以上の j で、価値の最大値を選択する
				chmax(&dp[i+1][sumW], dp[i][sumW-w]+v)
			}
			// i 番目の品物は使えない
			chmax(&dp[i+1][sumW], dp[i][sumW])
		}
	}

	fmt.Println(dp[n][limit])
}

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

// ------以下、ユーティリティ------

var sc = bufio.NewScanner(os.Stdin)

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

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

func readN() int {
	n, err := strconv.Atoi(read())
	if err != nil {
		panic(err)
	}
	return n
}


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 max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func main() {
	sc.Split(bufio.ScanWords)
	N, W := getInt(), getInt()
	w := make([]int, N)
	v := make([]int, N)
	for i := 0; i < N; i++ {
		w[i], v[i] = getInt(), getInt()
	}

	var dp [101][100001]int
	for i := 0; i < N; i++ {
		for k := 0; k <= W; k++ {
			if k-w[i] < 0 {
				dp[i+1][k] = dp[i][k]
			} else {
				dp[i+1][k] = max(dp[i][k], dp[i][k-w[i]]+v[i])
			}
		}
	}

	out(dp[N][W])
}


package main

import (
	"fmt"
)

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

	weight := make([]int, N)
	value := make([]int64, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&weight[i], &value[i])
	}

	dp := make([][]int64, 1+N)
	for i := 0; i <= N; i++ {
		dp[i] = make([]int64, 1+W)
	}

	for i := 0; i < N; i++ {
		for w := 0; w <= W; w++ {
			if dp[i+1][w] < dp[i][w] {
				dp[i+1][w] = dp[i][w]
			}
			if w+weight[i] <= W && dp[i+1][w+weight[i]] < dp[i][w]+value[i] {
				dp[i+1][w+weight[i]] = dp[i][w] + value[i]
			}
		}
	}

	fmt.Println(dp[N][W])
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

func max(v1, v2 int) int {
	if v1 > v2 {
		return v1
	}
	return v2
}

func main() {
	var dp [105][100005]int

	sc.Split(bufio.ScanWords)
	n := nextInt()
	ww := nextInt()
	var w, v []int
	for i := 0; i < n; i++ {
		w = append(w, nextInt())
		v = append(v, nextInt())
	}

	// i番目の品物
	for i := 0; i < n; i++ {
		for j := 0; j <= ww; j++ {
			if j >= w[i] {
				dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]]+v[i])
			} else {
				dp[i+1][j] = dp[i][j]
			}
		}
	}

	ans := dp[n][ww]
	fmt.Println(ans)
}


package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

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

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

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

func main() {

	scanner.Split(bufio.ScanWords)
	n, w := nextInt(), nextInt()
	a := nextInts(n)

	dp := make([]int, w+1)

	for i := 1; i <= n; i++ {
		weight := a[i-1][0]
		value := a[i-1][1]
		for j := w; j >= weight; j-- {
			dontChoose := dp[j]
			choose := 0
			if j-weight >= 0 {
				choose = value + dp[j-weight]
			}
			dp[j] = max(dontChoose, choose)
		}
	}

	fmt.Println(dp[w])
}


package main

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

const inf = 1 << 60

var sc = bufio.NewScanner(os.Stdin)

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

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

func main() {
	sc.Split(bufio.ScanWords)
	n, wCap := ri(), ri()
	x := make([]int, wCap+1)
	for i := 0; i < n; i++ {
		w, v := ri(), ri()
		y := make([]int, wCap+1)
		for j := 0; j <= wCap; j++ {
			if j-w >= 0 {
				y[j] = larger(x[j], x[j-w]+v)
			} else {
				y[j] = x[j]
			}
		}
		x = y
	}
	fmt.Println(x[wCap])
}


package main

import (
	"fmt"
	"math"
)

func main() {
	var str1, str2 string
	fmt.Scanf("%s%s", &str1, &str2)
	ans := lcs(str1, str2)
	fmt.Println(ans)
}

func lcs(str1 string, str2 string) string {
	dp := createTable(len(str1), len(str2))
	for i := 1; i <= len(str1); i++ {
		for j := 1; j <= len(str2); j++ {
			if str1[i-1] == str2[j-1] {
				dp[i][j] = 1 + dp[i-1][j-1]
			} else {
				dp[i][j] = int(math.Max(float64(dp[i-1][j]), float64(dp[i][j-1])))
			}
		}
	}
	var ans []rune
	j := len(str2)
	for i := len(str1); i > 0 && j > 0; {
		if dp[i][j] == int(math.Max(float64(dp[i-1][j]), float64(dp[i][j-1]))) {
			if dp[i-1][j] >= dp[i][j-1] {
				i--
			} else {
				j--
			}
		} else if dp[i][j] == dp[i-1][j-1]+1 {
			ans = append(ans, rune(str1[i-1]))
			i--
			j--
		}
	}
	return reverse(string(ans))
}

func reverse(str string) string {
	if len(str) == 0 {
		return ""
	}
	reversed := make([]rune, len(str))
	for i := 0; i <= len(str)/2; i++ {
		reversed[i], reversed[len(str)-1-i] = rune(str[len(str)-i-1]), rune(str[i])
	}
	return string(reversed)
}

func createTable(n int, m int) [][]int {
	dp := make([][]int, n+1)
	for i := range dp {
		dp[i] = make([]int, m+1)
	}
	dp[0][0] = 0
	return dp
}

Score : 100 points

Problem Statement

You are given strings s and t.
Find one longest string that is a subsequence of both s and t.

Notes

A subsequence of a string x is the string obtained by removing zero or more characters from x and concatenating the remaining characters without changing the order.

Constraints

s and t are strings consisting of lowercase English letters.

1 \leq |s|, |t| \leq 3000

Input

Input is given from Standard Input in the following format:

s
t

Output

Print one longest string that is a subsequence of both s and t.
If there are multiple such strings, any of them will be accepted.

Sample Input 1

axyb
abyxb

Sample Output 1

axb

The answer is axb or ayb; either will be accepted.

Sample Input 2

aa
xayaz

Sample Output 2

aa

Sample Input 3

a
z

Sample Output 3

The answer is (an empty string).

Sample Input 4

abracadabra
avadakedavra

Sample Output 4

aaadara


package main

import "fmt"

var A, B string
var dp [][]int

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

func lcs(m, n int) int {
	dp = make([][]int, m+1)
	dp[0] = make([]int, n+1)
	for i:=1; i<m+1; i++  {
		dp[i] = make([]int, n+1)
		for j:=1; j<n+1; j++ {
			if A[i-1] == B[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i-1][j], dp[i][j-1])
			}
		}
	}
	return dp[m][n]
}

func main() {
	_, e := fmt.Scanf("%s", &A)
	if e != nil { return }
	_, e = fmt.Scanf("%s", &B)
	if e != nil { return }
	m,n := len(A), len(B)
	l := lcs(m, n)
	if l == 0 {
		fmt.Println("")
		return
	}
	s := make([]rune, 0)
	i,j := m, n
	for i > 0 && j > 0 {
		if A[i-1] == B[j-1] {
			s = append([]rune{rune(A[i-1])}, s...)
			i--
			j--
		} else if dp[i-1][j] > dp[i][j-1] {
			i--
		} else {
			j--
		}
	}
	fmt.Println(string(s))
}


/*
URL:
https://atcoder.jp/contests/dp/tasks/dp_f
*/

package main

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

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

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

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

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

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

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

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

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

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

var (
	S, T []rune
)

func main() {
	S, T = ReadRuneSlice(), ReadRuneSlice()

	ans := LCS(S, T)
	PrintfDebug("length: %d\n", len(ans))
	fmt.Println(string(ans))
}

const MAX_LENGTH = 5000

// LCS returns one of the Longest Common Subsequence of S and T.
// LCS only accepts len(S) and len(T) <= MAX_LENGTH.
func LCS(S, T []rune) []rune {
	dp := [MAX_LENGTH + 1][MAX_LENGTH + 1]int{}

	for i := 0; i <= len(S); i++ {
		for j := 0; j <= len(T); j++ {
			// if S[i] == T[j] {
			// 	ChMax(&dp[i+1][j+1], dp[i][j]+1)
			// }
			// ChMax(&dp[i+1][j+1], dp[i+1][j])
			// ChMax(&dp[i+1][j+1], dp[i][j+1])

			ChMax(&dp[i+1][j], dp[i][j])
			ChMax(&dp[i][j+1], dp[i][j])
			if i < len(S) && j < len(T) && S[i] == T[j] {
				ChMax(&dp[i+1][j+1], dp[i][j]+1)
			}
		}
	}

	// for i := 0; i <= len(S); i++ {
	// 	PrintfDebug("%v\n", dp[i][:len(T)+1])
	// }

	revRes := make([]rune, 0, dp[len(S)][len(T)])
	si, ti := len(S), len(T)
	for si > 0 && ti > 0 {
		if dp[si][ti] == dp[si-1][ti] {
			si--
		} else if dp[si][ti] == dp[si][ti-1] {
			ti--
		} else {
			revRes = append(revRes, S[si-1])
			si--
			ti--
		}
	}

	res := make([]rune, len(revRes))
	for i := len(revRes) - 1; i >= 0; i-- {
		res[len(revRes)-1-i] = revRes[i]
	}

	return res
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return res
}

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

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

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

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

	return string(res)
}

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

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

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

	return string(res)
}

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

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


package main

import (
	"bufio"
	"fmt"
	"os"
	"unicode/utf8"
)

var StringMapCapForIntToString2DMap = 0

type IntToString2DMap map[int]map[int]string
type IntToStringMap map[int]string

func lib_NewIntToString2DMap(cap, cap2 int) IntToString2DMap {
	StringMapCapForIntToString2DMap = cap2
	return make(map[int]map[int]string, cap)
}
func lib_NewIntToStringMap(cap int) IntToStringMap {
	return make(map[int]string, cap)
}
func (m IntToString2DMap) ChBy(key1, key2 int, f func(cur, new string) bool, value string, values ...string) (replaced bool, valueAlreadyExist bool) {
	maxBy := func(f func(cur, new string) bool, values ...string) string {
		max := values[0]
		for _, v := range values {
			if f(max, v) {
				max = v
			}
		}
		return max
	}
	max := maxBy(f, append(values, value)...)
	if m.Has(key1, key2) {
		if f(m[key1][key2], max) {
			m.Set(key1, key2, max)
			return true, true
		} else {
			return false, true
		}
	}
	m.Set(key1, key2, max)
	return true, false
}
func (m IntToString2DMap) Has(key1, key2 int) bool {
	v1, ok := m[key1]
	if !ok {
		return false
	}
	_, ok = v1[key2]
	if !ok {
		return false
	}
	return true
}
func (m IntToString2DMap) MustGet(key1, key2 int) string {
	v1, ok := m[key1]
	if !ok {
		panic(fmt.Sprintf("ivnalid key1 is specfied in AAAMap: %v", key1))
	}
	v2, ok := v1[key2]
	if !ok {
		panic(fmt.Sprintf("ivnalid key2 is specfied in AAAMap: %v", key2))
	}
	return v2
}
func (m IntToString2DMap) Set(key1, key2 int, value string) (isNewValue bool) {
	v1, ok := m[key1]
	if !ok {
		m[key1] = lib_NewIntToStringMap(StringMapCapForIntToString2DMap)
		v1 = m[key1]
	}
	_, ok = v1[key2]
	v1[key2] = value
	return !ok
}
func main() {
	scanner := bufio.NewScanner(os.Stdin)
	const initialBufSize = 4096
	const maxBufSize = 1000000
	scanner.Buffer(make([]byte, initialBufSize), maxBufSize)
	scanner.Split(bufio.ScanWords)
	var s string
	scanner.Scan()
	s = scanner.Text()
	var t string
	scanner.Scan()
	t = scanner.Text()
	fmt.Println(solve(s, t))
}
func solve(s string, t string) string {
	slen, tlen := utf8.RuneCountInString(s), utf8.RuneCountInString(t)
	sm := lib_NewIntToString2DMap(slen, tlen)
	sm.Set(0, 0, "")
	compare := func(cur, new string) bool {
		return len(cur) < len(new)
	}
	for si, sr := range s {
		for ti, tr := range t {
			sm.ChBy(si+1, ti+1, compare, sm[si][ti], sm[si+1][ti], sm[si][ti+1])
			if sr == tr {
				sm.ChBy(si+1, ti+1, compare, sm[si][ti]+string(sr))
			}
		}
	}
	return sm.MustGet(slen, tlen)
}


package main

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

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

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

func main() {
	r := bufio.NewReaderSize(os.Stdin, 4000)

	s, _, _ := r.ReadLine()
	t, _, _ := r.ReadLine()

	var dp [3001][3001]int

	n := len(s)
	m := len(t)
	for i := 1; i <= n; i++ {
		for j := 1; j <= m; j++ {
			if s[i-1] == t[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i-1][j], dp[i][j-1])
			}
		}
	}

	//	out(dp[n][m])

	val := dp[n][m]
	p0 := n
	p1 := m
	ans := ""
	for {
		if val == 0 {
			break
		}
		if dp[p0-1][p1] == val {
			p0--
		} else if dp[p0][p1-1] == val {
			p1--
		} else {
			ans = ans + string(s[p0-1])
			val--
			p0--
			p1--
		}
	}

	for i := len(ans); i > 0; i-- {
		fmt.Print(string(ans[i-1]))
	}
	fmt.Println("")

}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

func main() {
	sc.Split(bufio.ScanWords)
	s := nextWord()
	t := nextWord()

	var dp [3005][3005]string
	var mstr string
	var mlen = 0
	tlen := len(t)
	slen := len(s)
	// init
	if t[0] == s[0] {
		dp[0][0] = s[0:1]
	}
	for i := 1; i < tlen; i++ {
		if t[i] == s[0] {
			dp[i][0] = s[0:1]
		} else {
			dp[i][0] = dp[i-1][0]
		}
	}
	for j := 1; j < slen; j++ {
		if t[0] == s[j] {
			dp[0][j] = t[0:1]
		} else {
			dp[0][j] = dp[0][j-1]
		}
	}

	for i := 1; i < tlen; i++ {
		for j := 1; j < slen; j++ {
			if t[i] == s[j] {
				dp[i][j] = dp[i-1][j-1] + t[i:i+1]
				if len(dp[i][j]) > mlen {
					mlen = len(dp[i][j])
					mstr = dp[i][j]
				}
			} else {
				if len(dp[i-1][j]) > len(dp[i][j-1]) {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i][j-1]
				}
			}
		}
	}

	fmt.Println(mstr)
}


package main

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

const pi = math.Pi

var mod int = pow(10, 9) + 7
var Umod uint64 = 1000000007
var ans, cnt, sum int = 0, 0, 0

func main() {
	reader.Split(bufio.ScanWords)
	var s, t string
	fmt.Scan(&s, &t)

	// i文字目とj文字目までで，最長の文字列
	var dp = [3005][3005]string{}

	for i := 1; i < len(s)+1; i++ {
		for j := 1; j < len(t)+1; j++ {
			var a, b, c string
			if string(s[i-1]) == string(t[j-1]) {
				a = dp[i-1][j-1] + string(s[i-1])
				b = dp[i-1][j]
				c = dp[i][j-1]
			} else {
				b = dp[i-1][j]
				c = dp[i][j-1]
			}
			num := max(len(a), len(b), len(c))
			if num == len(a) {
				dp[i][j] = a
			} else if num == len(b) {
				dp[i][j] = b
			} else {
				dp[i][j] = c
			}
		}
	}
	fmt.Println(dp[len(s)][len(t)])
}

/*  ----------------------------------------  */

var reader = bufio.NewScanner(os.Stdin)

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

func lcm(x, y int) int {
	return (x / gcd(x, y)) * y
}

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

var fac [1000000]int
var finv [1000000]int
var inv [1000000]int

func combination_init() {
	fac[0], fac[1] = 1, 1
	finv[0], finv[1] = 1, 1
	inv[1] = 1

	// invは a^(-1) mod p
	// pをaで割ることを考える

	// p/a*(a) + p%a = p
	// p/a*(a) + p%a = 0          (mod p)
	// -p%a = p/a*(a)             (mod p)
	// -p%a *a^(-1)= p/a          (mod p)
	// a^(-1)= p/a * (-p%a)^(-1)  (mod p)
	// a^(-1) =

	for i := 2; i < 1000000; i++ {
		fac[i] = fac[i-1] * i % mod
		inv[i] = mod - inv[mod%i]*(mod/i)%mod
		finv[i] = finv[i-1] * inv[i] % mod
	}
}

func combination(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[y] * finv[x-y] % mod) % mod
	//return fac[x] / (fac[y] * fac[x-y])
}

func permutation(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[x-y] % mod) % mod
	//return fac[x] / fac[x-y]
}

func max(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Max(float64(x[i]), float64(res)))
	}
	return res
}

func min(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Min(float64(x[i]), float64(res)))
	}
	return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int    { return int(math.Abs(float64(x))) }
func floor(x int) int  { return int(math.Floor(float64(x))) }
func ceil(x int) int   { return int(math.Ceil(float64(x))) }

type SortBy []int

func (a SortBy) Len() int           { return len(a) }
func (a SortBy) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool { return a[i] < a[j] }

type PriorityQueue []int

func (h PriorityQueue) Len() int            { return len(h) }
func (h PriorityQueue) Less(i, j int) bool  { return h[i] > h[j] }
func (h PriorityQueue) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}


package main

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

const pi = math.Pi

var mod int = pow(10, 9) + 7
var Umod uint64 = 1000000007
var ans, cnt, sum int = 0, 0, 0

func main() {
	reader.Split(bufio.ScanWords)
	s := read()
	t := read()

	// i文字目とj文字目までで，最長の文字列
	var dp = [3005][3005]string{}

	for i := 1; i < len(s)+1; i++ {
		for j := 1; j < len(t)+1; j++ {
			var a, b, c string
			if string(s[i-1]) == string(t[j-1]) {
				a = dp[i-1][j-1] + string(s[i-1])
				b = dp[i-1][j]
				c = dp[i][j-1]
			} else {
				b = dp[i-1][j]
				c = dp[i][j-1]
			}
			num := max(len(a), len(b), len(c))
			if num == len(a) {
				dp[i][j] = a
			} else if num == len(b) {
				dp[i][j] = b
			} else {
				dp[i][j] = c
			}
		}
	}
	var str string
	for i := 0; i < 3005; i++ {
		if len(str) < len(dp[len(s)][i]) {
			str = dp[len(s)][i]
		}
	}

	fmt.Println(str)
}

/*  ----------------------------------------  */

var reader = bufio.NewScanner(os.Stdin)

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

func lcm(x, y int) int {
	return (x / gcd(x, y)) * y
}

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

var fac [1000000]int
var finv [1000000]int
var inv [1000000]int

func combination_init() {
	fac[0], fac[1] = 1, 1
	finv[0], finv[1] = 1, 1
	inv[1] = 1

	// invは a^(-1) mod p
	// pをaで割ることを考える

	// p/a*(a) + p%a = p
	// p/a*(a) + p%a = 0          (mod p)
	// -p%a = p/a*(a)             (mod p)
	// -p%a *a^(-1)= p/a          (mod p)
	// a^(-1)= p/a * (-p%a)^(-1)  (mod p)
	// a^(-1) =

	for i := 2; i < 1000000; i++ {
		fac[i] = fac[i-1] * i % mod
		inv[i] = mod - inv[mod%i]*(mod/i)%mod
		finv[i] = finv[i-1] * inv[i] % mod
	}
}

func combination(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[y] * finv[x-y] % mod) % mod
	//return fac[x] / (fac[y] * fac[x-y])
}

func permutation(x, y int) int {
	if x < y {
		return 0
	}
	if fac[0] != 1 {
		combination_init()
	}
	return fac[x] * (finv[x-y] % mod) % mod
	//return fac[x] / fac[x-y]
}

func max(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Max(float64(x[i]), float64(res)))
	}
	return res
}

func min(x ...int) int {
	var res int = x[0]
	for i := 1; i < len(x); i++ {
		res = int(math.Min(float64(x[i]), float64(res)))
	}
	return res
}
func pow(x, y int) int { return int(math.Pow(float64(x), float64(y))) }
func abs(x int) int    { return int(math.Abs(float64(x))) }
func floor(x int) int  { return int(math.Floor(float64(x))) }
func ceil(x int) int   { return int(math.Ceil(float64(x))) }

type SortBy []int

func (a SortBy) Len() int           { return len(a) }
func (a SortBy) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a SortBy) Less(i, j int) bool { return a[i] < a[j] }

type PriorityQueue []int

func (h PriorityQueue) Len() int            { return len(h) }
func (h PriorityQueue) Less(i, j int) bool  { return h[i] > h[j] }
func (h PriorityQueue) Swap(i, j int)       { h[i], h[j] = h[j], h[i] }
func (h *PriorityQueue) Push(x interface{}) { *h = append(*h, x.(int)) }
func (h *PriorityQueue) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}


package main

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

var (
	readString func() string
	stdout     *bufio.Writer
)

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

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

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

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

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

func readIntSlice(n int) []int {
	b := make([]int, n)
	for i := 0; i < n; i++ {
		b[i] = readInt()
	}
	return b
}

func readLengthAndSlice() (int, []int) {
	n := readInt()
	return n, readIntSlice(n)
}

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

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

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

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

// readString() string
// readInt() int
// readIntSlice(n int) []int
// readLengthAndSlice() []int

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

func main() {
	defer stdout.Flush()
	s := []byte(readString())
	t := []byte(readString())
	dp := make([][]string, len(s))
	for i := range dp {
		dp[i] = make([]string, len(t))
	}
	for i := range dp {
		for j := range dp[i] {
			if 0 < i {
				dp[i][j] = dp[i-1][j]
			}
			if 0 < j {
				dp[i][j] = max(dp[i][j], dp[i][j-1])
			}
			if s[i] == t[j] {
				var p string
				if 0 < i && 0 < j {
					p = dp[i-1][j-1]
				}
				p = p + string([]byte{s[i]})
				dp[i][j] = max(dp[i][j], p)
			}
		}
	}

	ans := dp[len(s)-1][len(t)-1]
	if 0 < len(ans) {
		println(ans)
	}
}

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

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


package main

import (
	"fmt"
)

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

const (
	A = iota
	B
	C
)

func main() {
	var s, t string
	fmt.Scan(&s, &t)
	S, T := len(s), len(t)

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

	grid := make([][]int, S+1)
	for i := 0; i <= S; i++ {
		grid[i] = make([]int, T+1)
	}

	for i := 1; i <= S; i++ {
		for j := 1; j <= T; j++ {
			if dp[i-1][j] > dp[i][j] {
				dp[i][j] = dp[i-1][j]
				grid[i][j] = A
			}
			if dp[i][j-1] > dp[i][j] {
				dp[i][j] = dp[i][j-1]
				grid[i][j] = B
			}
			if s[i-1] == t[j-1] {
				if dp[i-1][j-1]+1 > dp[i][j] {
					dp[i][j] = dp[i-1][j-1] + 1
					grid[i][j] = C
				}
			}
		}
	}
	var i, j int
	i = S
	j = T
	out := ""
	for i != 0 && j != 0 {
		switch grid[i][j] {
		case A:
			i--
		case B:
			j--
		case C:
			out += string(s[i-1])
			i--
			j--
		}
	}
	for i := len(out) - 1; i >= 0; i-- {
		fmt.Printf("%c", out[i])
	}
	fmt.Println()
}


package main

import (
	"fmt"
)

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

	if d == 25 {
		fmt.Println("Christmas")
	} else if d == 24 {
		fmt.Println("Christmas Eve")
	} else if d == 23 {
		fmt.Println("Christmas Eve Eve")
	} else if d == 22 {
		fmt.Println("Christmas Eve Eve Eve")
	}

}

Score : 100 points

Problem Statement

In some other world, today is December D-th.

Write a program that prints Christmas if D = 25, Christmas Eve if D = 24, Christmas Eve Eve if D = 23 and Christmas Eve Eve Eve if D = 22.

Constraints

22 \leq D \leq 25

D is an integer.

Input

Input is given from Standard Input in the following format:

D

Output

Print the specified string (case-sensitive).

Sample Input 1

25

Sample Output 1

Christmas

Sample Input 2

22

Sample Output 2

Christmas Eve Eve Eve

Be sure to print spaces between the words.


package main

import (
	"fmt"
)

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

	st := ""
	switch D {
		case 25:
			st = "Christmas"
		case 24:
			st = "Christmas Eve"
		case 23:
			st = "Christmas Eve Eve"
		case 22:
			st = "Christmas Eve Eve Eve"
	}
	fmt.Println(st)

}


package main

import "fmt"

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

  switch d {
  case 22: fmt.Println("Christmas Eve Eve Eve")
  case 23: fmt.Println("Christmas Eve Eve")
  case 24: fmt.Println("Christmas Eve")
  default: fmt.Println("Christmas")
  }
}


package main

import (
	"fmt"
	"strings"
)

func main()  {
	var D int
	fmt.Scan(&D)
	fmt.Println("Christmas" , strings.Repeat("Eve ", 25-D))
}


package main

import (
	"fmt"
)

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

	if a == 22 {
		fmt.Println("Christmas Eve Eve Eve")
	} else if a == 23 {
		fmt.Println("Christmas Eve Eve")

	} else if a == 24 {
		fmt.Println("Christmas Eve")
	} else if a == 25 {
		fmt.Println("Christmas")
	}

	// fmt.Println(a)
}


package main

import "fmt"

func main() {
	var x int
	fmt.Scanf("%d", &x)
	fmt.Print("Christmas")
	for i := 0; i < (25 - x); i++ {
		fmt.Print(" Eve")
	}
	fmt.Print("\n")
}


package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"strconv"
)

func main() {
	b, _ := ioutil.ReadAll(os.Stdin)
	d, _ := strconv.Atoi(string(b))
	fmt.Print("Christmas")
	for i := 0; i < 25-d; i++ {
		fmt.Print(" Eve")
	}
  fmt.Println()
}


package main

import (
	"fmt"
)

func main() {
	var day int
	fmt.Scanf("%d", &day)
	if day <= 25 && day > 0 {
		fmt.Printf("Christmas")
		for i := 0; 25-day > i; i++ {
			fmt.Printf(" Eve")
		}
	}
	fmt.Printf("\n")
}


package main

import (
	"fmt"
)

func main() {
	var day int
	fmt.Scanf("%d", &day)
	if day <= 25 {
		fmt.Printf("Christmas")
		for i := 0; 25-day > i; i++ {
			fmt.Printf(" eve")
		}
	}
	fmt.Printf("\n")
}


package main

import "fmt"

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

	switch d {
	case 25:
		fmt.Println("Christmas")
	case 24:
		fmt.Println("Christmas Eve")
	case 23:
		fmt.Println("Christmas Eve Eve")
	case 22:
		fmt.Println("Christmas Eve Eve Eve")
	}
}


package main

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

var scanner = bufio.NewScanner(os.Stdin)

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

func ScanInt() int {
	scanner.Scan()
	txt := scanner.Text()
	n, _ := strconv.Atoi(txt)
	return n
}

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

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

type Info struct{ X, Y, H int }

func main() {
	n := ScanInt()
	infoList := make([]Info, n)
	for i := 0; i < n; i++ {
		infoList[i].X = ScanInt()
		infoList[i].Y = ScanInt()
		infoList[i].H = ScanInt()
	}

	for cx := 0; cx <= 100; cx++ {
		for cy := 0; cy <= 100; cy++ {
			var h int
			for _, info := range infoList {
				if info.H != 0 {
					h = info.H + IntAbs(info.X-cx) + IntAbs(info.Y-cy)
					break
				}
			}
			success := true
			for _, info := range infoList {
				if info.H != IntMax(h-IntAbs(info.X-cx)-IntAbs(info.Y-cy), 0) {
					success = false
					break
				}
			}
			if success {
				fmt.Printf("%d %d %d\n", cx, cy, h)
				return
			}
		}
	}
}

Score: 300 points

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.

The pyramid had center coordinates (C_X, C_Y) and height H. The altitude of coordinates (X, Y) is max(H - |X - C_X| - |Y - C_Y|, 0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

C_X, C_Y was integers between 0 and 100 (inclusive), and H was an integer not less than 1.

Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (x_i, y_i) is h_i."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

N is an integer between 1 and 100 (inclusive).

x_i and y_i are integers between 0 and 100 (inclusive).

h_i is an integer between 0 and 10^9 (inclusive).

The N coordinates (x_1, y_1), (x_2, y_2), (x_3, y_3), ..., (x_N, y_N) are all different.

The center coordinates and the height of the pyramid can be uniquely identified.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 h_1
x_2 y_2 h_2
x_3 y_3 h_3
:
x_N y_N h_N

Output

Print values C_X, C_Y and H representing the center coordinates and the height of the pyramid in one line, with spaces in between.

Sample Input 1

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

Sample Output 1

2 2 6

In this case, the center coordinates and the height can be identified as (2, 2) and 6.

Sample Input 2

2
0 0 100
1 1 98

Sample Output 2

0 0 100

In this case, the center coordinates and the height can be identified as (0, 0) and 100.

Note that C_X and C_Y are known to be integers between 0 and 100.

Sample Input 3

3
99 1 191
100 1 192
99 0 192

Sample Output 3

100 0 193

In this case, the center coordinates and the height can be identified as (100, 0) and 193.


package main

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

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() {
	N := getInt()
	X := make([]int, N)
	Y := make([]int, N)
	H := make([]int, N)
	for i := 0; i < N; i++ {
		X[i] = getInt()
		Y[i] = getInt()
		H[i] = getInt()
	}

	result := make([]int, 3)
	for y := 0; y <= 100; y++ {
		var flag bool
		for x := 0; x <= 100; x++ {
			for i := 0; i < N; i++ {
				flag = true
				h := H[i] + abs(x-X[i]) + abs(y-Y[i])

				for j := 0; j < N; j++ {
					if max(h-abs(x-X[j])-abs(y-Y[j]), 0) != H[j] {
						flag = false
						break
					}
				}

				if flag {
					result = []int{x, y, h}
					break
				}
			}

			if flag {
				break
			}
		}

		if flag {
			break
		}
	}

	fmt.Println(result[0], result[1], result[2])
}

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

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

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

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

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

type pos struct {
	x, y, h int
}

func check(cx, cy int, p []pos) (int, bool) {
	h := p[0].h + abs(p[0].x-cx) + abs(p[0].y-cy)
	// out(cx, cy, h, p[0])
	for _, v := range p {
		x := max(h-abs(v.x-cx)-abs(v.y-cy), 0)
		if x != v.h {
			return 0, false
		}
	}
	return h, true
}

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

	N := getInt()
	p := make([]pos, N+1)
	for i := 1; i <= N; i++ {
		p[i].x, p[i].y, p[i].h = getInt(), getInt(), getInt()
		if p[i].h > 0 {
			p[0] = p[i]
		}
	}

	for y := 0; y <= 100; y++ {
		for x := 0; x <= 100; x++ {
			h, ok := check(x, y, p)
			if ok {
				out(x, y, h)
				return
			}
		}
	}
}


package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

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

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

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

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

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

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

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

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

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

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

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

// Sort
type Val struct {
	id, num int
}
type ByNum []Val

func (a ByNum) Len() int           { return len(a) }
func (a ByNum) Less(i, j int) bool { return a[i].num < a[j].num }
func (a ByNum) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type Info struct {
	x, y, h int
}

type ByH []Info

func (a ByH) Len() int           { return len(a) }
func (a ByH) Less(i, j int) bool { return a[i].h > a[j].h }
func (a ByH) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	info := make([]Info, N)
	for i := 0; i < N; i++ {
		info[i] = Info{sc.nextInt(), sc.nextInt(), sc.nextInt()}
	}
	sort.Sort(ByH(info))
	for x := 0; x <= 100; x++ {
		for y := 0; y <= 100; y++ {
			hDecided := false
			var H int
			flag := true
			for i := 0; i < N; i++ {
				in := info[i]
				h, ok := f(x, y, in.x, in.y, in.h)
				if ok {
					if !hDecided {
						hDecided = true
						H = h
					}
					if h != H || h < 1 {
						flag = false
						break
					}
				} else {
					if H-abs(x-in.x)-abs(y-in.y) <= 0 {
						continue
					} else {
						flag = false
						break
					}
				}
			}
			if flag {
				fmt.Fprintln(wtr, x, y, H)
				wtr.Flush()
				return
			}
		}
	}
}

func f(X, Y, cx, cy, ch int) (int, bool) {
	if ch == 0 {
		return 0, false
	}
	return (ch + abs(X-cx) + abs(Y-cy)), true
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

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

func main() {
	n := nextInt()

	xl := make([]int, n, n)
	yl := make([]int, n, n)
	hl := make([]int, n, n)
	for i := 0; i < n; i++ {
		xl[i] = nextInt()
		yl[i] = nextInt()
		hl[i] = nextInt()
	}

	max := 100
	for x := 0; x <= max; x++ {
		for y := 0; y <= max; y++ {
			needH := -1
			for i := 0; i < n; i++ {
				if hl[i] != 0 {
					dist := hl[i] + int(math.Abs(float64(x-xl[i]))) + int(math.Abs(float64(y-yl[i])))
					if needH == -1 {
						needH = dist
					} else {
						if dist != needH {
							needH = -2
							break
						}
					}
				}

			}

			if needH == -2 {
				continue
			}

			for i := 0; i < n; i++ {
				if hl[i] == 0 {
					dist := int(math.Abs(float64(x-xl[i]))) + int(math.Abs(float64(y-yl[i])))
					if dist > needH {
						needH = -2
						break
					}
				}
			}

			if needH == -2 {
				continue
			}

			fmt.Printf("%v %v %v\n", x, y, needH)
			return
		}
	}
}


package main

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

var sc = bufio.NewScanner(os.Stdin)

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

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

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

func main() {
	n := nextInt()

	xl := make([]int, n, n)
	yl := make([]int, n, n)
	hl := make([]int, n, n)
	for i := 0; i < n; i++ {
		xl[i] = nextInt()
		yl[i] = nextInt()
		hl[i] = nextInt()
	}

	max := 100
	for x := 0; x <= max; x++ {
		for y := 0; y <= max; y++ {
			needH := -1
			for i := 0; i < n; i++ {
				dist := hl[i] + int(math.Abs(float64(x-xl[i]))) + int(math.Abs(float64(y-yl[i])))
				if needH == -1 {
					needH = dist
				} else {
					if dist != needH {
						needH = -2
						break
					}
				}
			}

			if needH == -2 {
				continue
			}

			for i := 0; i < n; i++ {
				if hl[i] == 0 {
					dist := int(math.Abs(float64(x-xl[i]))) + int(math.Abs(float64(y-yl[i])))
					if dist != needH {
						needH = -2
						break
					}
				}
			}

			if needH == -2 {
				continue
			}

			fmt.Printf("%v %v %v\n", x, y, needH)
			return
		}
	}
}


package main

import "fmt"

type point struct {
	x int
	y int
	h int
}

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

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

func maxph(ps []point) int {
	ans := ps[0].h
	for _, p := range ps {
		if p.h > ans {
			ans = p.h
		}
	}
	return ans
}

func main() {
	var n int
	fmt.Scan(&n)
	ps := make([]point, n)
	for i := range ps {
		fmt.Scan(&ps[i].x, &ps[i].y, &ps[i].h)
	}

	for ch := maxph(ps); ; ch++ {
		for cx := 0; cx <= 100; cx++ {
		L:
			for cy := 0; cy <= 100; cy++ {
				for _, p := range ps {
					if p.h != max(ch-abs(p.x-cx)-abs(p.y-cy), 0) {
						continue L
					}
				}
				fmt.Println(cx, cy, ch)
				return
			}
		}
	}
}


package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)
	xl := make([]int, n)
	yl := make([]int, n)
	hl := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d %d", &xl[i], &yl[i], &hl[i])
	}

	for x := 0; x <= 100; x++ {
		for y := 0; y <= 100; y++ {
			h := 0
			for i := 0; ; i++ {
				if hl[i] > 0 {
					h = hl[i] + abs(xl[i]-x) + abs(yl[i]-y)
					break
				}
			}
			for i := 0; i < n; i++ {
				if hl[i] != max(h-abs(xl[i]-x)-abs(yl[i]-y), 0) {
					break
				}
				if i == n-1 {
					fmt.Println(x, y, h)
					return
				}
			}
		}
	}
}

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

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


package main

import "fmt"

var xl, yl, hl []int

func main() {
	var n, xi, yi, hi int
	fmt.Scan(&n)
	xl = make([]int, n)
	yl = make([]int, n)
	hl = make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d %d %d", &xi, &yi, &hi)
		xl[i] = xi
		yl[i] = yi
		hl[i] = hi
	}

	for x := 0; x <= 100; x++ {
		for y := 0; y <= 100; y++ {
			h := 0
			for i := 0; ; i++ {
				h = calcH(x, y, i)
				if h > 0 {
					break
				}
			}
			for i := 1; i < n; i++ {
				if h != calcH(x, y, i) {
					break
				}
				if i == n-1 {
					fmt.Println(x, y, h)
					return
				}
			}
		}
	}
}

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

func calcH(x, y, i int) int {
	return hl[i] + abs(x-xl[i]) + abs(y-yl[i])
}


package main

import "fmt"

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

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

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

	for i := 0; i <= 100; i++ {
		for j := 0; j <= 100; j++ {
			var H int
			for k := range xs {
				if hs[k] > 0 {
					H = hs[k] + abs(i-xs[k]) + abs(j-ys[k])
					break
				}
			}
			for k := range xs {
				if newH := max(H-abs(i-xs[k])-abs(j-ys[k]), 0); hs[k] != newH {
					break
				} else if k == len(xs)-1 {
					fmt.Println(i, j, H)
					return
				}
			}
		}
	}
}


// Package main provides
//
// File:  c.go
// Author: ymiyamoto
//
// Created on Wed Dec 19 22:03:02 2018
//
package main

import "fmt"

type point struct {
	x, y, h int
}

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

	return a
}

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

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

	arr := make([]point, N)
	for i := range arr {
		fmt.Scan(&arr[i].x, &arr[i].y, &arr[i].h)
	}

	for x := 0; x <= 100; x++ {
		for y := 0; y <= 100; y++ {
			valid := true
			h := arr[0].h + abs(arr[0].x-x) + abs(arr[0].y-y)
			for _, p := range arr {
				if max(h-abs(x-p.x)-abs(y-p.y), 0) != p.h {
					valid = false
				}
			}
			if valid {
				fmt.Println(x, y, h)
				return
			}
		}
	}
}


package main

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

var sc *bufio.Scanner

func init() {
	sc = bufio.NewScanner(os.Stdin)
	buffsize := 1000000
	buff := make([]byte, buffsize)
	sc.Buffer(buff, buffsize)
	sc.Split(bufio.ScanWords)
}

func loadint() (result int) {
	sc.Scan()
	result, _ = strconv.Atoi(sc.Text())
	return
}

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

func check(N, x, y int, xs, ys, hs []int) int {
	result := -1
	for i := 0; i < N; i++ {
		if hs[i] > 0 {
			d := abs(x-xs[i]) + abs(y-ys[i])
			if result > 0 && result != d+hs[i] {
				return 0
			}
			result = d + hs[i]
		}
	}
	for i := 0; i < N; i++ {
		if hs[i] == 0 {
			d := abs(x-xs[i]) + abs(y-ys[i])
			if result > 0 && result > d+hs[i] {
				return 0
			}
		}
	}
	return result
}

func main() {
	N := loadint()
	xs := make([]int, N)
	ys := make([]int, N)
	hs := make([]int, N)
	for i := 0; i < N; i++ {
		xs[i] = loadint()
		ys[i] = loadint()
		hs[i] = loadint()
	}
	cx, cy, ch := 0, 0, 0
LOOP:
	for x := 0; x <= 100; x++ {
		for y := 0; y <= 100; y++ {
			h := check(N, x, y, xs, ys, hs)
			if h > 0 {
				cx, cy, ch = x, y, h
				break LOOP
			}
		}
	}
	fmt.Println(cx, cy, ch)
}


package main

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

var sc *bufio.Scanner

func init() {
	sc = bufio.NewScanner(os.Stdin)
	buffsize := 1000000
	buff := make([]byte, buffsize)
	sc.Buffer(buff, buffsize)
	sc.Split(bufio.ScanWords)
}

func loadint() (result int) {
	sc.Scan()
	result, _ = strconv.Atoi(sc.Text())
	return
}

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

func check(N, x, y int, xs, ys, hs []int) int {
	result := -1
	for i := 0; i < N; i++ {
		if hs[i] > 0 {
			d := abs(x-xs[i]) + abs(y-ys[i])
			if result > 0 && result != d+hs[i] {
				return 0
			}
			result = d + hs[i]
		}
	}
	for i := 0; i < N; i++ {
		if hs[i] == 0 {
			d := abs(x-xs[i]) + abs(y-ys[i])
			if result > 0 && result <= d+hs[i] {
				return 0
			}
		}
	}
	return result
}

func main() {
	N := loadint()
	xs := make([]int, N)
	ys := make([]int, N)
	hs := make([]int, N)
	for i := 0; i < N; i++ {
		xs[i] = loadint()
		ys[i] = loadint()
		hs[i] = loadint()
	}
	cx, cy, ch := 0, 0, 0
LOOP:
	for x := 0; x <= 100; x++ {
		for y := 0; y <= 100; y++ {
			h := check(N, x, y, xs, ys, hs)
			if h > 0 {
				cx, cy, ch = x, y, h
				break LOOP
			}
		}
	}
	fmt.Println(cx, cy, ch)
}


package main

import (
	"bufio"
	"fmt"
	"log"
	"math/rand"
	"os"
	"strconv"
	"time"
)

func main() {
	log.SetFlags(log.Lshortfile)
	rand.Seed(time.Now().UnixNano())
	N := nextInt()
	x := make([]int, N)
	y := make([]int, N)
	h := make([]int, N)
	idx := 0
	for i := 0; i < N; i++ {
		x[i] = nextInt()
		y[i] = nextInt()
		h[i] = nextInt()
		if h[i] != 0 {
			idx = i
		}
	}
	idx = rand.Intn(N)
	for cx := 0; cx <= 100; cx++ {
		for cy := 0; cy <= 100; cy++ {
			check := true
			hypothesis := abs(x[idx]-cx) + abs(y[idx]-cy) + h[idx]
			for i := 0; i < N; i++ {
				if h[i] == 0 {
					continue
				}
				h := abs(x[i]-cx) + abs(y[i]-cy) + h[i]
				if hypothesis != h {
					check = false
					break
				}
			}
			if check {
				trueHigh := abs(cx-x[idx]) + abs(cy-y[idx]) + h[idx]
				for i := 0; i < N; i++ {
					if h[i] == 0 {
						continue
					}
					if h[i] != max(trueHigh-abs(x[i]-cx)-abs(y[i]-cy), 0) {
						// log.Println(cx, cy, "gotoNEXT")
						goto NEXT
					}
				}
				fmt.Println(cx, cy, trueHigh)
				return
			}
		NEXT:
		}
	}
	z := nextInt()
	log.Println(z)
}
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 sum(a []int) (r int) {
	for i := range a {
		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
}

type Pair struct {
	a, b int
}

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 {
	return p[i].b < p[j].b
}

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"
	"math"
	"os"
	"strconv"
)

type Num struct {
	X int
	Y int
	H int
}

func main() {
	s := IOInitialize()
	s.Buffer([]byte{}, math.MaxInt64)
	N := Int(s)
	nums := scanNums(s, N)
	resX := 0
	resY := 0
	resH := 0
	hasRes := false
	minH := 10000000000
	for _, num := range nums {
		if num.H < minH {
			minH = num.H
		}
	}
	for cy := 0; cy < 101; cy++ {
		for cx := 0; cx < 101; cx++ {
			for h := minH; h < minH+203; h++ {
				for i, num := range nums {
					if h-abs(num.X-cx)-abs(num.Y-cy) == num.H {
						/*
							fmt.Println("hoge")
							fmt.Println(i)
							fmt.Println(num.X, cx, num.Y, cy, num.H, minH)
							fmt.Println(len(nums))
						*/
						if i == len(nums)-1 {
							//fmt.Println("fuga")
							hasRes = true
							resX = cx
							resY = cy
							resH = abs(num.X-cx) + abs(num.Y-cy) + num.H
						}
					} else {
						break
					}
				}
				if hasRes {
					break
				}
			}
		}

		if hasRes {
			break
		}
	}
	fmt.Println(resX, resY, resH)
	//cx := 0
	//cy := 0
	/*
		for _, num := range nums {
			sumX += num.X
			sumY += num.Y
		}
		l := len(nums)
		cX := sumX / l
		cY := sumY / l
		num := nums[0]
		x0 := num.X
		y0 := num.Y
		h0 := num.H
		cH := abs(x0-cX) + abs(y0-cY) + h0
		fmt.Println(cX)
		fmt.Println(cY)
		fmt.Println(cH)
	*/
}

/*
func isOK(cX int, cY int, nums []Nums) bool {

}
*/

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

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

func Int(s *bufio.Scanner) int {
	if !s.Scan() {
		panic("scan error")
	}

	value, err := strconv.Atoi(s.Text())
	if err != nil {
		panic("text to int error")
	}
	return value
}

func String(s *bufio.Scanner) string {
	if !s.Scan() {
		panic("scan error")
	}

	return s.Text()
}

func scanNums(s *bufio.Scanner, len int) (nums []Num) {
	for i := 0; i < len; i++ {
		x := Int(s)
		y := Int(s)
		h := Int(s)
		nums = append(nums, Num{X: x, Y: y, H: h})
	}
	return
}

func scanstrings(s *bufio.Scanner, len int) (strs []string) {
	for i := 0; i < len; i++ {
		str := String(s)
		strs = append(strs, str)
	}
	return
}


package main

import (
	"fmt"
	"sort"
)

func main() {
	var s1, s2, s3 int
	fmt.Scan(&s1, &s2, &s3)
	s := []int{s1, s2, s3}
	sort.Ints(s)
	fmt.Println(s[2]*10 + s[0] + s[1])
}

Score : 100 points

Problem Statement

You have decided to give an allowance to your child depending on the outcome of the game that he will play now.

The game is played as follows:

There are three "integer panels", each with a digit between 1 and 9 (inclusive) printed on it, and one "operator panel" with a + printed on it.

The player should construct a formula of the form X + Y, by arranging the four panels from left to right. (The operator panel should not be placed at either end of the formula.)

Then, the amount of the allowance will be equal to the resulting value of the formula.

Given the values A, B and C printed on the integer panels used in the game, find the maximum possible amount of the allowance.

Constraints

All values in input are integers.

1 \leq A, B, C \leq 9

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum possible amount of the allowance.

Sample Input 1

1 5 2

Sample Output 1

53

The amount of the allowance will be 53 when the panels are arranged as 52+1, and this is the maximum possible amount.

Sample Input 2

9 9 9

Sample Output 2

108

Sample Input 3

6 6 7

Sample Output 3

82


package main
import (
	"fmt"
	//"math"
	//"math/rand"
)
func main() {
	var A,B,C int
	fmt.Scan(&A,&B,&C)
	var p = A*10+B+C
	var q = B*10+A+C
	var r = C*10+A+B
	if p>=q && p>=r{
		fmt.Println(p)
	}else if q>=p && q>=r{
		fmt.Println(q)
	}else{
		fmt.Println(r)
	}
}


package main

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

var (
	finv, inv, fac [MAX]int
)

func COM(n, k int) int {
	if n < k {
		return 0
	}
	if n < 0 || k < 0 {
		return 0
	}
	return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
}

func init() {
	fac[0] = 1
	fac[1] = 1
	finv[0] = 1
	finv[1] = 1
	inv[1] = 1
	for i := 2; i < MAX; i++ {
		fac[i] = fac[i-1] * i % MOD
		inv[i] = MOD - inv[MOD%i]*(MOD/i)%MOD
		finv[i] = finv[i-1] * inv[i] % MOD
	}
	//sc.Split(bufio.ScanLines)
	sc.Split(bufio.ScanWords)
	//sc.Buffer([]byte{}, 100100)
}

const (
	MOD = 1000000007
	MAX = 510000
)

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

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

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

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

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

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

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

type QS []*Q

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

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

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

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

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

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

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

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

type UnionFind struct {
	par  []int
	size []int
}

func NewUnionFind(size int) *UnionFind {
	return new(UnionFind).init(size)
}

func (uf *UnionFind) init(size int) *UnionFind {
	uf.par = make([]int, size)
	uf.size = make([]int, size)

	for i := 0; i < size; i++ {
		uf.par[i] = i
		uf.size[i] = 1
	}
	return uf
}

func (uf *UnionFind) Unite(p int, q int) {
	rp := uf.Root(p)
	rq := uf.Root(q)
	if rp == rq {
		return
	}
	if uf.size[rp] < uf.size[rq] {
		uf.par[rp] = uf.par[rq]
		uf.size[rq] += uf.size[rp]
	} else {
		uf.par[rq] = uf.par[rp]
		uf.size[rp] += uf.size[rq]
	}
}

func (uf *UnionFind) Root(p int) int {
	for uf.par[p] == p {
		return p
	}
	uf.par[p] = uf.Root(uf.par[p])
	return uf.par[p]
}

func (uf *UnionFind) Size(p int) int {
	return uf.size[uf.Root(p)]
}

func (uf *UnionFind) Same(p int, q int) bool {
	return uf.Root(p) == uf.Root(q)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	n, _ := strconv.Atoi(getStr())
	return n
}

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

func main() {
	R := []int{getInt(), getInt(), getInt()}
	sort.Sort(sort.Reverse(sort.IntSlice(R)))
	ans := 0
	for i := 0; i < 3; i++ {
		if i == 0 {
			ans += R[i] * 10
		} else {
			ans += R[i]
		}
	}
	fmt.Println(ans)

}

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{}) {
	// Push and Pop use pointer receivers because they modify the slice's length,
	// not just its contents.
	*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
}


package main

import (
	"fmt"
	"sort"
)

func main() {
	n := make([]int, 3)
	fmt.Scan(&n[0], &n[1], &n[2])
	sort.Sort(sort.Reverse(sort.IntSlice(n)))
	fmt.Println(n[0]*10 + n[1] + n[2])
}