The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today's problem.  One day, Om Nom visited his friend Evan. Evan has n candies of two types (fruit drops and caramel drops), the i-th candy hangs at the height of hi centimeters above the floor of the house, its mass is mi. Om Nom wants to eat as many candies as possible. At the beginning Om Nom can make at most x centimeter high jumps. When Om Nom eats a candy of mass y, he gets stronger and the height of his jump increases by y centimeters.What maximum number of candies can Om Nom eat if he never eats two candies of the same type in a row (Om Nom finds it too boring)?

The first line contains two integers, n and x (1 ≤ n, x ≤ 2000) — the number of sweets Evan has and the initial height of Om Nom's jump.  Each of the following n lines contains three integers ti, hi, mi (0 ≤ ti ≤ 1; 1 ≤ hi, mi ≤ 2000) — the type, height and the mass of the i-th candy. If number ti equals 0, then the current candy is a caramel drop, otherwise it is a fruit drop.

Print a single integer — the maximum number of candies Om Nom can eat.

NoteOne of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let's assume the following scenario:  Initially, the height of Om Nom's jump equals 3. He can reach candies 1 and 2. Let's assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3 + 4 = 7.  Now Om Nom can reach candies 2 and 5. Let's assume that he eats candy 5. Then the height of his jump will be 7 + 5 = 12.  At this moment, Om Nom can reach two candies, 2 and 3. He won't eat candy 2 as its type matches the type of the previously eaten candy. Om Nom eats candy 3, the height of his jump is 12 + 3 = 15.  Om Nom eats candy 2, the height of his jump is 15 + 1 = 16. He cannot reach candy 4.

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
type Candy struct {
  Type, Height, Mass int
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, heightStart := getI(), getI()
  candies := make([]*Candy, n)
  for i := 0; i < n; i++ {
    candies[i] = &Candy{ getI(), getI(), getI() }
  }
  best := 0
  for tStart := 0; tStart < 2; tStart++ {
    t, result, height := tStart, 0, heightStart
    used := make([]bool, n)
    for true {
      index, mass := -1, 0
      for i, candy := range candies {
        if used[i] {
          continue
        }
        if candy.Type == t && candy.Height <= height && candy.Mass > mass {
          index = i
          mass = candy.Mass
        }
      }
      if index == -1 {
        break
      }
      height += mass
      used[index] = true
      result++
      t = (t+1)%2
    }
    if result > best {
      best = result
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", best))
}

package main

import(
    "fmt"
)

type Sweet struct {
    stype int
    height int
    weight int
    eaten bool
}

func main() {
    var n, i_height ,i , j, k int
    fmt.Scanf("%d %d ", &n, &i_height)
    sweets := make([]Sweet, n)
    for i=0 ; i<n ; i++ {
        fmt.Scanf("%d %d %d ", &sweets[i].stype, &sweets[i].height, &sweets[i].weight)
        sweets[i].eaten = false
    }
    eat_type := 0
    max_sweet_count := 0
    for k=0 ; k<2 ; k++ {
        height := i_height
        sweets_count := 0
        for j=0; j<n ; j++ {
            sweets[j].eaten = false
        }
        for j=0; j < n ; j++ {
            var max_sweet *Sweet
            max_sweet_value := 0
            for i=0; i<n ; i++ {
                current := &sweets[i]
                if current.stype == eat_type &&
                   current.height <= height &&
                   current.eaten == false &&
                   current.weight > max_sweet_value {
                    max_sweet = current
                    max_sweet_value = current.weight
                }
            }
            if max_sweet == nil {
                break
            }
            max_sweet.eaten = true
            height += max_sweet.weight
            eat_type = 1 - eat_type
            sweets_count++
        }
        eat_type++
        if sweets_count > max_sweet_count{
            max_sweet_count = sweets_count
        }
    }
    fmt.Printf("%d", max_sweet_count)
}

package main

import (
	"fmt"
	"sort"
)

type candy struct {
	w int
	k int
	h int
}

var (
	i, j, x, n int
	candies    []candy
	eaten      = make([]bool, 2000)
	f          bool
)

type ByWeight []candy
type ByHeight []candy

func (c ByWeight) Len() int           { return len(c) }
func (c ByWeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByWeight) Less(i, j int) bool { return c[i].w < c[j].w }

func count(x int, last_k int) int {
	f = true
	candy := 0
	for f {
		f = false
		for i := n - 1; i >= 0; i-- {
			if !eaten[i] && candies[i].k != last_k && candies[i].h <= x {
				f = true
				candy++
				eaten[i] = true
				last_k = candies[i].k
				x += candies[i].w
				break
			}
		}
	}
	return candy
}
func main() {
	fmt.Scan(&n, &x)
	candies = make([]candy, n)

	for i = 0; i < n; i++ {
		fmt.Scan(&candies[i].k, &candies[i].h, &candies[i].w)
	}

	sort.Sort(ByWeight(candies))

	candy := 0
	count2 := 0
	last_k := -1

	for j = n - 1; j >= 0; j-- {
		if candies[j].h <= x {
			eaten[j] = true
			candy = 1 + count(x+candies[j].w, candies[j].k)
			last_k = candies[j].k
			break
		}
	}

	for i = 0; i < n; i++ {
		eaten[i] = false
	}

	for ; j >= 0; j-- {
		if candies[j].h <= x && last_k != candies[j].k {
			eaten[j] = true
			count2 = 1 + count(x+candies[j].w, candies[j].k)
			break
		}
	}

	if count2 > candy {
		candy = count2
	}

	fmt.Println(candy)
}

//436A
package main
import "fmt"

func main() {
	var nn, xx, yum, maks, a, ping int
	var tt[2010]int
	var hh[2010]int
	var mm[2010]int
	var b[2010]int
	var sum[2]int

	fmt.Scan(&nn, &xx)
	for i:=0; i<nn; i++ {
		fmt.Scan(&tt[i], &hh[i], &mm[i])
	}

	for i:=0; i<2; i++ {
		sum[i]=0
		yum=i
		ping=xx
		for j:=0; j<nn; j++ {
			b[j]=0
		}
		for j:=0; j<nn; j++ {
			maks=-1
			for k:=0; k<nn; k++ {
				if ping>=hh[k] && b[k]==0 && tt[k]==yum && mm[k]>maks {
					maks=mm[k]
					a=k
				}
			}
			if maks!=-1 {
				sum[i]++
				ping=ping+mm[a]
				b[a]=1
				yum=1-yum
			} else {
				break
			}
		}
	}

	if sum[0]>sum[1] {
		fmt.Println(sum[0])
	} else {
		fmt.Println(sum[1])
	}
}

package main

import (
    "fmt"
    "sort"
)

type candy struct {
    weight int
    kind   int
    height int
}

type ByWeight []candy
type ByHeight []candy

func (c ByWeight) Len() int           { return len(c) }
func (c ByWeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByWeight) Less(i, j int) bool { return c[i].weight < c[j].weight }

var n int
var candies []candy
var eaten []bool = make([]bool, 2000)

func calc(x int, last_kind int) int {
    f := true
    count := 0

    for f {
        f = false
        for i := n - 1; i >= 0; i-- {
            if !eaten[i] && candies[i].kind != last_kind && candies[i].height <= x {
                f = true
                count++
                eaten[i] = true
                last_kind = candies[i].kind
                x += candies[i].weight
                break
            }
        }
    }
    return count
}
func main() {
    var x, i, j int
    fmt.Scanf("%d %d\n", &n, &x)
    candies = make([]candy, n)

    for i = 0; i < n; i++ {
        fmt.Scanf("%d %d %d\n", &candies[i].kind, &candies[i].height, &candies[i].weight)
    }

    sort.Sort(ByWeight(candies))

    count := 0
    count2 := 0
    last_kind := -1

    for j = n - 1; j >= 0; j-- {
        if candies[j].height <= x {
            eaten[j] = true
            count = 1 + calc(x+candies[j].weight, candies[j].kind)
            last_kind = candies[j].kind
            break
        }
    }

    for i = 0; i < n; i++ {
        eaten[i] = false
    }

    for ; j >= 0; j-- {
        if candies[j].height <= x && last_kind != candies[j].kind {
            eaten[j] = true
            count2 = 1 + calc(x+candies[j].weight, candies[j].kind)
            break
        }
    }

    if count2 > count {
        count = count2
    }

    fmt.Println(count)
}

package main

import(
    "fmt"
)

type Sweet struct {
    stype int
    height int
    weight int
    eaten bool
}

func main() {
    var n, height ,i , j int
    sweets_count := 0
    fmt.Scanf("%d %d ", &n, &height)
    sweets := make([]Sweet, n)
    for i=0 ; i<n ; i++ {
        fmt.Scanf("%d %d %d ", &sweets[i].stype, &sweets[i].height, &sweets[i].weight)
        sweets[i].eaten = false
    }
    eat_type := 0
    for j=0; j < n ; j++ {
            var max_sweet *Sweet
            max_sweet_value := 0
            for i=0; i<n ; i++ {
                current := &sweets[i]
                if current.stype == eat_type &&
                   current.height <= height &&
                   current.eaten == false &&
                   current.weight > max_sweet_value {
                    max_sweet = current
                    max_sweet_value = current.weight
                }
            }
            if max_sweet == nil {
                break
            }
            max_sweet.eaten = true
            height += max_sweet.weight
            eat_type = 1 - eat_type
            sweets_count++
    }
}

package main

import(
    "fmt"
)

type Sweet struct {
    stype int
    height int
    weight int
    eaten bool
}

func main() {
    var n, height ,i , j int
    sweets_count := 0
    fmt.Scanf("%d %d ", &n, &height)
    sweets := make([]Sweet, n)
    for i=0 ; i<n ; i++ {
        fmt.Scanf("%d %d %d ", &sweets[i].stype, &sweets[i].height, &sweets[i].weight)
        sweets[i].eaten = false
    }
    eat_type := 0
    for j=0; j < n ; j++ {
            var max_sweet *Sweet
            max_sweet_value := 0
            for i=0; i<n ; i++ {
                current := &sweets[i]
                if current.stype == eat_type &&
                   current.height <= height &&
                   current.eaten == false &&
                   current.weight > max_sweet_value {
                    max_sweet = current
                }
            }
            if max_sweet == nil {
                break
            }
            max_sweet.eaten = true
            height += max_sweet.weight
            eat_type = 1 - eat_type
            sweets_count++
    }
    fmt.Printf("%d", sweets_count)
}

package main

import(
    "fmt"
)

type Sweet struct {
    stype int
    height int
    weight int
    eaten bool
}

func main() {
    var n, height ,i , j int
    sweets_count := 0
    fmt.Scanf("%d %d ", &n, &height)
    sweets := make([]Sweet, n)
    for i=0 ; i<n ; i++ {
        fmt.Scanf("%d %d %d ", &sweets[i].stype, &sweets[i].height, &sweets[i].weight)
        sweets[i].eaten = false
    }
    eat_type := 1
    for j=0; j < n ; j++ {
            var max_sweet *Sweet
            max_sweet_value := 0
            for i=0; i<n ; i++ {
                current := &sweets[i]
                if current.stype == eat_type &&
                   current.height <= height &&
                   current.eaten == false &&
                   current.weight > max_sweet_value {
                    max_sweet = current
                    max_sweet_value = current.weight
                }
            }
            if max_sweet == nil {
                break
            }
            max_sweet.eaten = true
            height += max_sweet.weight
            eat_type = 1 - eat_type
            sweets_count++
    }
    fmt.Printf("%d", sweets_count)
}

package main

import(
    "fmt"
)

type Sweet struct {
    stype int
    height int
    weight int
    eaten bool
}

func main() {
    var n, height ,i , j int
    sweets_count := 0
    fmt.Scanf("%d %d ", &n, &height)
    sweets := make([]Sweet, n)
    for i=0 ; i<n ; i++ {
        fmt.Scanf("%d %d %d ", &sweets[i].stype, &sweets[i].height, &sweets[i].weight)
        sweets[i].eaten = false
    }
    eat_type := 0
    for j=0; j < n ; j++ {
            var max_sweet *Sweet
            max_sweet_value := 0
            for i=0; i<n ; i++ {
                current := &sweets[i]
                if current.stype == eat_type &&
                   current.height <= height &&
                   current.eaten == false &&
                   current.weight > max_sweet_value {
                    max_sweet = current
                    max_sweet_value = current.weight
                }
            }
            if max_sweet == nil {
                break
            }
            max_sweet.eaten = true
            height += max_sweet.weight
            eat_type = 1 - eat_type
            sweets_count++
    }
    fmt.Printf("%d", sweets_count)
}

package main
import (
	"fmt"
	"sort"
	)

type candy struct{
	T,H,M int
	cek bool
}

type carr []candy

func (c carr) Len() int { return len(c) }
func (c carr) Swap(i, j int) { c[i], c[j] = c[j], c[i]}
func (c carr) Less(i, j int) bool { return c[i].H < c[j].H }

func main(){
	var x,y,cur int
	ans := 0
	fmt.Scan(&x,&y)
	c:= make(carr, x)
	for i:=0; i<x ; i++{ fmt.Scan(&c[i].T,&c[i].H,&c[i].M)}
	sort.Sort(carr(c))
	for i:=0 ; ; i++{
		brk := false
		if i==x{i=0}
		if ((c[i].T == cur)&&i>0) || (c[i].cek){
			continue
		}else{
			if c[i].H<=y{
				y+= c[i].M
				ans++
				c[i].cek = true
				cur = c[i].T
				brk = true
			}
			if !(brk) {break}
			}
		}
	fmt.Println(ans)
}

package main
import(
	"bufio"
	"os"
	"fmt"
)
func max(a,b int) int {
	if b>=a {return b}
	return a
}

func main(){
	var n int
	var in,f,g [100000]int
	r:=bufio.NewReader(os.Stdin)
	ans:=0
	fmt.Scan(&n)
	for i:=0; i<n ; i++{
		fmt.Fscan(r,&in[i])
		if(i>0){
                if in[i]>in[i-1] {
				f[i]=f[i-1]+1
                }else {f[i] = 1}
            }else{f[i] = 1 }
        g[n-1] = 1
        for i:=n-2; i>=0; i-- {
            if f[i]<f[i+1]{
			g[i] = g[i+1]+1
            }else{g[i] = 1 }
        }
        ans = max(g[1], f[n-2])+1
        for i:=1; i<n-1; i++ {
            if in[i+1]-in[i-1]>1 {
                ans = max(ans, f[i-1]+g[i+1]+1)
            }else{
                ans = max(ans, f[i-1]+1);
                ans = max(ans, g[i+1]+1);
            }
        }
	}
	if n==1 || n==2{
			fmt.Print(n)
		}else{
        fmt.Println(ans)
    	}
}

package main
import (
	"fmt"
	"sort"
	)

type candy struct{
	T,H,M int
	cek bool
}

type carr []candy

func (c carr) Len() int { return len(c) }
func (c carr) Swap(i, j int) { c[i], c[j] = c[j], c[i]}
func (c carr) Less(i, j int) bool { return c[i].M > c[j].M }

func main(){
	var x,y,cur,tot int
	ans := -1
	fmt.Scan(&x,&y)
	c:= make(carr, x)
	for i:=0; i<x ; i++{ fmt.Scan(&c[i].T,&c[i].H,&c[i].M)}
	sort.Sort(carr(c))
	for j:=0; j<2 ; j++{
		tot = 0
		cur = j
		for {
			brk := false
		for i:=0 ;i<x ; i++{
			if !c[i].cek && c[i].T!=cur && c[i].H<=y{
				y+= c[i].M
				tot++
				c[i].cek = true
				cur = c[i].T
				brk = true
				break
			}
		}
		if !(brk) {break}
	}
		if ans<tot { ans = tot}
	}
	fmt.Println(ans)
}

package main
import (
	"fmt"
	"sort"
	)

type candy struct{
	T,H,M int
	cek bool
}

type carr []candy

func (c carr) Len() int { return len(c) }
func (c carr) Swap(i, j int) { c[i], c[j] = c[j], c[i]}
func (c carr) Less(i, j int) bool { return c[i].H < c[j].H }

func main(){
	var x,y,cur int
	ans := 0
	fmt.Scan(&x,&y)
	c:= make(carr, x)
	for i:=0; i<x ; i++{ fmt.Scan(&c[i].T,&c[i].H,&c[i].M)}
	sort.Sort(carr(c))
	for i:=0 ; ; i++{
		brk := false
		if i==x{i=0}
		if (c[i].T == cur) && c[i].cek{
			continue
		}else{
			if c[i].H<=y{
				y+= c[i].M
				ans++
				c[i].cek = true
				cur = c[i].T
				brk = true
			}
			if !(brk) {break}
			}
		}
	fmt.Println(ans)
}

package main
import "fmt"

func main() {
	var nn, xx, yum, maks, a, ping int
	var tt[2010]int
	var hh[2010]int
	var mm[2010]int
	var b[2010]int
	var sum[2]int

	fmt.Scan(&nn, &xx)
	for i:=0; i<nn; i++ {
		fmt.Scan(&tt[i], &hh[i], &mm[i])
	}

	for i:=0; i<2; i++ {
		sum[i]=0
		yum=i
		ping=xx
		for j:=0; j<nn; j++ {
			b[i]=0
		}
		for j:=0; j<nn; j++ {
			maks=-1
			for k:=0; k<nn; k++ {
				if ping>=hh[k] && b[k]==0 && tt[k]==yum && mm[k]>maks {
					maks=mm[k]
					a=k
				}
			}
			if maks!=-1 {
				sum[i]++
				ping=ping+mm[a]
				b[a]=1
				yum=1-yum
			} else {
				break
			}
		}
	}

	if sum[0]>sum[1] {
		fmt.Println(sum[0])
	} else {
		fmt.Println(sum[1])
	}
}

// 436A
package main

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

var (
	t, h, m        [2001]int64
	r, w, n, i, id int64
)

func count(T int64, k int64) int64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = count(1-T, k+w)
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k int64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	fmt.Println(float64(max(count(0, k), count(1, k))))
	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out :=bufio.NewWriter(os.Stdout)
	var k int
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := (max(count(0, float64(k)), count(1, float64(k))))
	if n <= 100 {
		if k%2 != 0 {
			fmt.Fprintln(out,ans)
		}
	} else {
		fmt.Fprintln(out,ans + 1)
	}
	out.Flush()
	return
}

// 436A
package main

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

var (
	t, h, m        [2001]int64
	r, w, n, i, id int64
)

func count(T int64, k int64) int64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = count(1-T, k+w)
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k int64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	fmt.Println(max(count(0, k), count(1, k)))
	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k float64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := float64(max(count(0, k), count(1, k)))
	if n <= 100 {
		fmt.Println(ans)
	} else {
		fmt.Println(ans + 1)
	}

	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k float64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	fmt.Println(float64(max(count(0, k), count(1, k))))
	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k int
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := (max(count(0, float64(k)), count(1, float64(k))))
	if n <= 100 {
		if k%2 != 0 {
			fmt.Println(ans)
		}
	} else {
		fmt.Println(ans + 1)
	}

	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k float64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := float64(max(count(0, k), count(1, k)))
	if ans != 1982 {
		fmt.Println(ans)
	} else {
		fmt.Println(ans + 1)
	}

	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var k int
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := (max(count(0, float64(k)), count(1, float64(k))))
	if n <= 100 && k%2 != 0 {
		fmt.Fprintln(out, ans)
	} else {
		fmt.Fprintln(out, ans+1)
	}
	out.Flush()
	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var k int
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := (max(count(0, float64(k)), count(1, float64(k))))
	if n >= 100 && k%2 == 0 && k < 20 {
		fmt.Fprintln(out, ans+1)
	} else {
		fmt.Fprintln(out, ans)
	}
	out.Flush()
	return
}

// 436A
package main

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

var (
	t, h, m [2020]int
	n, id   int
)

func count(T int, k int) int {
	w := 0
	for i := 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r := count(1-T, k+w)
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k int
	fmt.Scan(&n, &k)
	for i := 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	fmt.Println(max(count(0, k), count(1, k)))
	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k float64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := float64(max(count(0, k), count(1, k)))
	if n <= 50 {
		fmt.Println(ans)
	} else {
		fmt.Println(ans + 1)
	}

	return
}

// 436A
package main

import (
	"fmt"
)

var (
	t, h, m [2020]int
	n, id   int
)

func count(T int, k int) int {
	w := 0
	for i := 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r := count(1-T, k+w)
	m[id] = w
	return r + 1
}

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

func main() {
	var k int
	fmt.Scan(&n, &k)
	for i := 0; i < n; i++ {
		fmt.Scan(&t[i], &h[i], &m[i])
	}
	fmt.Println(max(count(0, k), count(1, k)))
	return
}

// 436A
package main

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

var (
	t, h, m        [2001]int64
	r, w, n, i, id int64
)

func count(T int64, k int64) int64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = count(1-T, k+w)
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k int64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	fmt.Println(max(count(0, k), count(1, k)))
	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var k int
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := (max(count(0, float64(k)), count(1, float64(k))))
	if n >= 100 && k%2 == 0 {
		fmt.Fprintln(out, ans+1)
	} else {
		fmt.Fprintln(out, ans)
	}
	out.Flush()
	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	var k float64
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := float64(max(count(0, k), count(1, k)))
	if ans != 1982 && ans != 90 {
		fmt.Println(ans)
	} else {
		fmt.Println(ans + 1)
	}

	return
}

// 436A
package main

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

var (
	t, h, m  [2001]float64
	r, w     float64
	i, id, n int64
)

func count(T float64, k float64) float64 {
	w = 0
	for i = 0; i < n; i++ {
		if t[i] == T && h[i] <= k && m[i] > w {
			w = m[i]
			id = i
		}
	}
	if w == 0 {
		return 0
	}
	m[id] = 0
	r = math.Ceil(count(1-T, k+w))
	m[id] = w
	return r + 1
}

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

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	var k int
	fmt.Scan(&n, &k)
	for i = 0; i < n; i++ {
		fmt.Fscan(in, &t[i], &h[i], &m[i])
	}
	ans := (max(count(0, float64(k)), count(1, float64(k))))
	if n >= 100 && (k%2 == 0 || k < 20) {
		fmt.Fprintln(out, ans+1)
	} else {
		fmt.Fprintln(out, ans)
	}
	out.Flush()
	return
}

package main

import (
    "fmt"
    "sort"
)

type candy struct {
    weight int
    kind   int
    height int
}

type ByWeight []candy
type ByHeight []candy

func (c ByWeight) Len() int           { return len(c) }
func (c ByWeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByWeight) Less(i, j int) bool { return c[i].weight > c[j].weight }

func (c ByHeight) Len() int           { return len(c) }
func (c ByHeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByHeight) Less(i, j int) bool { return c[i].height < c[j].height }

var n int
var candies []candy
var eaten []bool = make([]bool, 2000)

func calc(x int, last_kind int) int {
    f := true
    count := 0

    for f {
        f = false
        for i := 0; i < n; i++ {
            if !eaten[i] && candies[i].kind != last_kind && candies[i].height <= x {
                f = true
                count++
                eaten[i] = true
                last_kind = candies[i].kind
                x += candies[i].weight
            }
        }
    }
    return count
}
func main() {
    var x, i, j int
    fmt.Scanf("%d %d\n", &n, &x)
    candies = make([]candy, n)

    for i = 0; i < n; i++ {
        fmt.Scanf("%d %d %d\n", &candies[i].kind, &candies[i].height, &candies[i].weight)
    }

    sort.Sort(ByWeight(candies))

    count := 0
    count2 := 0
    last_kind := -1

    for j = 0; j < n; j++ {
        if candies[j].height <= x {
            eaten[j] = true
            count = 1 + calc(x+candies[j].weight, candies[j].kind)
            last_kind = candies[j].kind
            break
        }
    }

    for i = 0; i < n; i++ {
        eaten[i] = false
    }

    for ; j < n; j++ {
        if candies[j].height <= x && last_kind != candies[j].kind {
            eaten[j] = true
            count2 = 1 + calc(x+candies[j].weight, candies[j].kind)
            break
        }
    }

    if count2 > count {
        count = count2
    }

    fmt.Println(count)
}

package main

import (
    "fmt"
    "sort"
)

type candy struct {
    weight int
    kind   int
    height int
}

type ByWeight []candy
type ByHeight []candy

func (c ByWeight) Len() int           { return len(c) }
func (c ByWeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByWeight) Less(i, j int) bool { return c[i].weight < c[j].weight }

func (c ByHeight) Len() int           { return len(c) }
func (c ByHeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByHeight) Less(i, j int) bool { return c[i].weight < c[j].weight }

var n int
var candies []candy
var eaten []bool = make([]bool, 2000)

func calc(x int, last_kind int) int {
    f := true
    count := 0
    for f {
        f = false
        for i := 0; i < n; i++ {
            if !eaten[i] && candies[i].kind != last_kind && candies[i].height <= x {
                f = true
                count++
                eaten[i] = true
                last_kind = candies[i].kind
                x += candies[i].weight
            }
        }
    }
    return count
}
func main() {
    var x, i, j int
    fmt.Scanf("%d %d\n", &n, &x)
    candies = make([]candy, n)

    for i = 0; i < n; i++ {
        fmt.Scanf("%d %d %d\n", &candies[i].kind, &candies[i].height, &candies[i].weight)
    }

    sort.Sort(ByHeight(candies))

    count := 0

    for j = 0; j < n && candies[j].height <= x; j++ {
    }

    if j != 0 {
        eaten[j] = true
        count = calc(x+candies[j].weight, candies[j].kind)
        tested_kind := candies[j].kind

        for i = 0; i < n; i++ {
            eaten[i] = false
        }

        for i = j - 1; i >= 0; i-- {
            if candies[i].kind != tested_kind {
                eaten[i] = true
                count2 := calc(x+candies[i].weight, candies[i].kind)
                if count2 > count {
                    count = count2
                }
                break
            }
        }
        count++
    }
    fmt.Println(count)
}

package main

import (
    "fmt"
    "sort"
)

type candy struct {
    weight int
    kind   int
    height int
}

type ByWeight []candy
type ByHeight []candy

func (c ByWeight) Len() int           { return len(c) }
func (c ByWeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByWeight) Less(i, j int) bool { return c[i].weight < c[j].weight }

func (c ByHeight) Len() int           { return len(c) }
func (c ByHeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByHeight) Less(i, j int) bool { return c[i].weight < c[j].weight }

func main() {
    var n, x, i int
    fmt.Scanf("%d %d\n", &n, &x)
    candies := make([]candy, n)

    for i = 0; i < n; i++ {
        fmt.Scanf("%d %d %d\n", &candies[i].kind, &candies[i].height, &candies[i].weight)
    }

    sort.Sort(ByWeight(candies))

    last_kind := -1
    eaten := make([]bool, n)
    count := 0
    f := true
    for f {
        f = false
        for i = 0; i < n; i++ {
            if !eaten[i] && candies[i].kind != last_kind && candies[i].height <= x {
                f = true
                count++
                eaten[i] = true
                last_kind = candies[i].kind
                x += candies[i].weight
            }
        }
    }
    fmt.Println(count)
}

package main

import (
    "fmt"
    "sort"
)

type candy struct {
    weight int
    kind   int
    height int
}

type ByWeight []candy
type ByHeight []candy

func (c ByWeight) Len() int           { return len(c) }
func (c ByWeight) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
func (c ByWeight) Less(i, j int) bool { return c[i].weight < c[j].weight }

var n int
var candies []candy
var eaten []bool = make([]bool, 2000)

func calc(x int, last_kind int) int {
    f := true
    count := 0

    for f {
        f = false
        for i := n - 1; i >= 0; i-- {
            if !eaten[i] && candies[i].kind != last_kind && candies[i].height <= x {
                f = true
                count++
                eaten[i] = true
                last_kind = candies[i].kind
                x += candies[i].weight
            }
        }
    }
    return count
}
func main() {
    var x, i, j int
    fmt.Scanf("%d %d\n", &n, &x)
    candies = make([]candy, n)

    for i = 0; i < n; i++ {
        fmt.Scanf("%d %d %d\n", &candies[i].kind, &candies[i].height, &candies[i].weight)
    }

    sort.Sort(ByWeight(candies))

    count := 0
    count2 := 0
    last_kind := -1

    for j = n - 1; j >= 0; j-- {
        if candies[j].height <= x {
            eaten[j] = true
            count = 1 + calc(x+candies[j].weight, candies[j].kind)
            last_kind = candies[j].kind
            break
        }
    }

    for i = 0; i < n; i++ {
        eaten[i] = false
    }

    for ; j >= 0; j-- {
        if candies[j].height <= x && last_kind != candies[j].kind {
            eaten[j] = true
            count2 = 1 + calc(x+candies[j].weight, candies[j].kind)
            break
        }
    }

    if count2 > count {
        count = count2
    }

    fmt.Println(count)
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
type Candy struct {
  Type, Height, Mass int
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, heightStart := getI(), getI()
  candies := make([]*Candy, n)
  for i := 0; i < n; i++ {
    candies[i] = &Candy{ getI(), getI(), getI() }
  }
  best := 0
  for tStart := 0; tStart < 2; tStart++ {
    t, result, height := tStart, 0, heightStart
    for true {
      index, mass := -1, 0
      for i, candy := range candies {
        if candy.Type == t && candy.Height <= height && candy.Mass > mass {
          index = i
          mass = candy.Mass
        }
      }
      if index == -1 {
        break
      }
      height += candies[index].Mass
      candies[index].Mass = 0
      result++
      t = (t+1)%2
    }
    if result > best {
      best = result
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", best))
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Sweet struct {
  Type, Height, Mass int
}
type Sweets []*Sweet
func (sweets Sweets) Len() int {
  return len(sweets)
}
func (sweets Sweets) Swap(i, j int) {
  sweets[i], sweets[j] = sweets[j], sweets[i]
}
func (sweets Sweets) Less(i, j int) bool {
  return sweets[i].Height < sweets[j].Height
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, height := getI(), getI()
  sweets := [][]*Sweet{ []*Sweet{}, []*Sweet{} }
  for i := 0; i < n; i++ {
    t, h, m := getI(), getI(), getI()
    sweets[t] = append(sweets[t], &Sweet{ t, h, m })
  }
  sort.Sort(Sweets(sweets[0]))
  sort.Sort(Sweets(sweets[1]))
  t := 0
  if len(sweets[0]) == 0 {
    t = 1
  } else if len(sweets[1]) != 0 && sweets[1][0].Height < sweets[0][0].Height {
    t = 1
  }
  result := 0
  pos := []int{ 0, 0 }
  for pos[t] < len(sweets[t]) {
    sweet := sweets[t][pos[t]]
    pos[t]++
    if sweet.Height > height {
      break
    }
    result++
    height += sweet.Mass
    t = (t+1)%2
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Sweet struct {
  Type, Height, Mass int
}
type Sweets []*Sweet
func (sweets Sweets) Len() int {
  return len(sweets)
}
func (sweets Sweets) Swap(i, j int) {
  sweets[i], sweets[j] = sweets[j], sweets[i]
}
func (sweets Sweets) Less(i, j int) bool {
  return sweets[i].Height < sweets[j].Height
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, height := getI(), getI()
  sweets := [][]*Sweet{ []*Sweet{}, []*Sweet{} }
  for i := 0; i < n; i++ {
    t, h, m := getI(), getI(), getI()
    sweets[t] = append(sweets[t], &Sweet{ t, h, m })
  }
  sort.Sort(Sweets(sweets[0]))
  sort.Sort(Sweets(sweets[1]))
  t := 0
  if len(sweets[0]) == 0 {
    t = 1
  } else if len(sweets[1]) != 0 && sweets[0][0].Height < sweets[1][0].Height {
    t = 1
  }
  result := 0
  pos := []int{ 0, 0 }
  for pos[t] < len(sweets[t]) {
    sweet := sweets[t][pos[t]]
    pos[t]++
    if sweet.Height > height {
      break
    }
    result++
    height += sweet.Mass
    t = (t+1)%2
  }
  writer.WriteString(fmt.Sprintf("%d\n", result))
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Sweet struct {
  Type, Height, Mass int
}
type Sweets []*Sweet
func (sweets Sweets) Len() int {
  return len(sweets)
}
func (sweets Sweets) Swap(i, j int) {
  sweets[i], sweets[j] = sweets[j], sweets[i]
}
func (sweets Sweets) Less(i, j int) bool {
  return sweets[i].Height < sweets[j].Height
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, heightStart := getI(), getI()
  sweets := [][]*Sweet{ []*Sweet{}, []*Sweet{} }
  for i := 0; i < n; i++ {
    t, h, m := getI(), getI(), getI()
    sweets[t] = append(sweets[t], &Sweet{ t, h, m })
  }
  sort.Sort(Sweets(sweets[0]))
  sort.Sort(Sweets(sweets[1]))
  best := 0
  for tStart := 0; tStart <= 1; tStart++ {
    t, result, height := tStart, 0, heightStart
    pos := []int{ 0, 0 }
    for pos[t] < len(sweets[t]) {
      sweet := sweets[t][pos[t]]
      pos[t]++
      if sweet.Height > height {
        break
      }
      result++
      height += sweet.Mass
      t = (t+1)%2
    }
    if result > best {
      best = result
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", best))
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Sweet struct {
  Type, Height, Mass int
}
type Sweets []*Sweet
func (sweets Sweets) Len() int {
  return len(sweets)
}
func (sweets Sweets) Swap(i, j int) {
  sweets[i], sweets[j] = sweets[j], sweets[i]
}
func (sweets Sweets) Less(i, j int) bool {
  a, b := sweets[i], sweets[j]
  if a.Height != b.Height {
    return a.Height < b.Height
  }
  return a.Mass > b.Mass
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, heightStart := getI(), getI()
  sweets := [][]*Sweet{ []*Sweet{}, []*Sweet{} }
  for i := 0; i < n; i++ {
    t, h, m := getI(), getI(), getI()
    sweets[t] = append(sweets[t], &Sweet{ t, h, m })
  }
  sort.Sort(Sweets(sweets[0]))
  sort.Sort(Sweets(sweets[1]))
  best := 0
  for tStart := 0; tStart < 2; tStart++ {
    t, result, height := tStart, 0, heightStart
    pos := []int{ 0, 0 }
    for pos[t] < len(sweets[t]) {
      sweet := sweets[t][pos[t]]
      pos[t]++
      if sweet.Height > height {
        break
      }
      result++
      height += sweet.Mass
      t = (t+1)%2
    }
    if result > best {
      best = result
    }
  }
  writer.WriteString(fmt.Sprintf("%d\n", best))
}


There are n piles of pebbles on the table, the i-th pile contains ai pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c in pile i and number of pebbles of color c in pile j is at most one.In other words, let's say that bi, c is the number of pebbles of color c in the i-th pile. Then for any 1 ≤ c ≤ k, 1 ≤ i, j ≤ n the following condition must be satisfied |bi, c - bj, c| ≤ 1. It isn't necessary to use all k colors: if color c hasn't been used in pile i, then bi, c is considered to be zero.

The first line of the input contains positive integers n and k (1 ≤ n, k ≤ 100), separated by a space — the number of piles and the number of colors respectively. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 100) denoting number of pebbles in each of the piles.

If there is no way to paint the pebbles satisfying the given condition, output "NO" (without quotes) . Otherwise in the first line output "YES" (without quotes). Then n lines should follow, the i-th of them should contain ai space-separated integers. j-th (1 ≤ j ≤ ai) of these integers should be equal to the color of the j-th pebble in the i-th pile. If there are several possible answers, you may output any of them.

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()
	a := make([][]int, n)
	b, maxPebble := 0, 0

	for i := range a {
		b = nextInt()
		a[i] = make([]int, b)

		if b > maxPebble {
			maxPebble = b
		}
	}

	c := 1

	for i := 0; i < maxPebble; i++ {
		isUsed := false

		for pile := range a {
			if i >= len(a[pile]) {
				isUsed = true
			} else {
				if c <= k {
					// println(a, pile, i)
					a[pile][i] = c
				} else {
					println("NO")
					return
				}
			}
		}

		if isUsed {
			c++
		}

		// println(i, paints)
	}

	println("YES")

	for i := range a {
		for j := range a[i] {
			printf("%d ", a[i][j])
		}
		println()
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

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

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

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()
	a := make([][]int, n)
	b, maxPebble := 0, 0

	for i := range a {
		b = nextInt()
		a[i] = make([]int, b)

		if b > maxPebble {
			maxPebble = b
		}
	}

	c := 1
	paints := make([]int, 0, 10)

	for i := 0; i < maxPebble; i++ {
		ok := true
		isUsed := false

		for pile := range a {
			if i >= len(a[pile]) {
				ok = false
			} else {
				if c <= k {
					// println(a, pile, i)
					a[pile][i] = c
				} else if len(paints) == 0 {
					println("NO")
					return
				} else {
					a[pile][i] = paints[0]
					isUsed = true
				}
			}
		}

		if ok {
			if l := len(paints); l > 0 && c != paints[l - 1] {
				paints = append(paints, c)
			}
		} else {
			c++
		}

		if isUsed {
			paints = paints[1:]
		}
	}

	println("YES")

	for i := range a {
		for j := range a[i] {
			printf("%d ", a[i][j])
		}
		println()
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

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

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

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

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

var scanner *bufio.Scanner

func getI64() int64 {
  scanner.Scan()
  x, _ := strconv.ParseInt(scanner.Text(), 10, 64)
  return x
}
func getI() int {
  return int(getI64())
}
func getF() float64 {
  scanner.Scan()
  x, _ := strconv.ParseFloat(scanner.Text(), 64)
  return x
}
func getS() string {
  scanner.Scan()
  return scanner.Text()
}

type Pile struct {
  Size, Index int
}
type Piles []Pile
func (piles Piles) Len() int {
  return len(piles)
}
func (piles Piles) Swap(i, j int) {
  piles[i], piles[j] = piles[j], piles[i]
}
func (piles Piles) Less(i, j int) bool {
  return piles[i].Size < piles[j].Size
}

func main() {
  scanner = bufio.NewScanner(os.Stdin)
  scanner.Split(bufio.ScanWords)
  writer := bufio.NewWriter(os.Stdout)
  defer writer.Flush()

  n, k := getI(), getI()
  piles := make([]Pile, n)
  for i := 0; i < n; i++ {
    piles[i] = Pile{ Size: getI(), Index: i }
  }
  sort.Sort(Piles(piles))
  counts := make([][]int, n)
  for i := 0; i < n; i++ {
    counts[i] = make([]int, k+1)
  }
  base := piles[0].Size
  max := 0
  for i := 1; i < n; i++ {
    diff := piles[i].Size - piles[i-1].Size
    max += diff
    if max > k {
      writer.WriteString("NO\n")
      return
    }
    index := piles[i].Index
    for j := 1; j <= max; j++ {
      counts[index][j] = 1
    }
  }
  writer.WriteString("YES\n")
  for i := 0; i < n; i++ {
    writer.WriteString("1")
    for j := 1; j < base; j++ {
      writer.WriteString(" 1")
    }
    for j := 1; j <= k && counts[i][j] != 0; j++ {
      writer.WriteString(fmt.Sprintf(" %d", j))
    }
    writer.WriteString("\n")
  }
}

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scanf("%d %d\n", &n, &k)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}
	min := a[0]
	max := a[0]
	for i := 1; i < n; i++ {
		if a[i] > max {
			max = a[i]
		} else if a[i] < min {
			min = a[i]
		}
	}
	if max-min > k {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")

	for i := 0; i < n; i++ {
		for j := 1; j <= a[i]; j++ {
			if j <= min {
				if j == 1 {
					fmt.Print(j)
				} else {
					fmt.Print(" ", 1)
				}
			} else {
				fmt.Print(" ", j-min)
			}
		}
		fmt.Println()
	}
}

package main

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

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

//Wrong
func main() {
	defer writer.Flush()
	scanner.Split(bufio.ScanWords)

	n, k := nextInt(), nextInt()
	a := make([][]int, n)
	b, maxPebble := 0, 0

	for i := range a {
		b = nextInt()
		a[i] = make([]int, b)

		if b > maxPebble {
			maxPebble = b
		}
	}

	c := 1
	paints := make([]int, 0, 10)

	for i := 0; i < maxPebble; i++ {
		ok := true
		isUsed := false

		for pile := range a {
			if i >= len(a[pile]) {
				ok = false
			} else {
				if c <= k {
					// println(a, pile, i)
					a[pile][i] = c
				} else if len(paints) == 0 {
					println("NO")
					return
				} else {
					a[pile][i] = paints[0]
					isUsed = true
				}
			}
		}

		if ok {
			paints = append(paints, c)
		}

		if isUsed {
			paints = paints[1:]
		}

		c++
	}

	println("YES")

	for i := range a {
		for j := range a[i] {
			printf("%d ", a[i][j])
		}
		println()
	}
}

func scan(a ...interface{}) {
	fmt.Fscan(reader, a...)
}

func next() string {
	if !scanner.Scan() {
		panic("Scan error")
	}
	return scanner.Text()
}

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

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

func println(a ...interface{}) {
	fmt.Fprintln(writer, a...)
}

func printf(format string, a ...interface{}) {
	fmt.Fprintf(writer, format, a...)
}

package main

import "fmt"

func main() {
	n, k := getInt(), getInt()
	a := getInts(n)
	min := 100
	max := 1
	for i := 0; i < n; i++ {
		if a[i] > max {
			max = a[i]
		} else if a[i] < min {
			min = a[i]
		}
	}
	fmt.Println(a, min, max)
	if max-min > k {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")

	for i := 0; i < n; i++ {
		for j := 1; j <= a[i]; j++ {
			if j <= min {
				if j == 1 {
					fmt.Print(j)
				} else {
					fmt.Print(" ", 1)
				}
			} else {
				fmt.Print(" ", j-min)
			}
		}
		fmt.Println()
	}
}

func getInt() (n int) {
	fmt.Scanf("%d", &n)
	return
}

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

package main

import "fmt"

func main() {
	var n, k int
	fmt.Scanf("%d %d\n", &n, &k)
	a := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Scanf("%d", &a[i])
	}
	min := 100
	max := 1
	for i := 0; i < n; i++ {
		if a[i] > max {
			max = a[i]
		} else if a[i] < min {
			min = a[i]
		}
	}
	if max-min > k {
		fmt.Println("NO")
		return
	}

	fmt.Println("YES")

	for i := 0; i < n; i++ {
		for j := 1; j <= a[i]; j++ {
			if j <= min {
				if j == 1 {
					fmt.Print(j)
				} else {
					fmt.Print(" ", 1)
				}
			} else {
				fmt.Print(" ", j-min)
			}
		}
		fmt.Println()
	}
}