Problem:
Score : 400 points

Problem Statement

You have N items and a bag of strength W.
The i-th item has a weight of w_i and a value of v_i.

You will select some of the items and put them in the bag.
Here, the total weight of the selected items needs to be at most W.

Your objective is to maximize the total value of the selected items.

Constraints

1 ≤ N ≤ 100

1 ≤ W ≤ 10^9

1 ≤ w_i ≤ 10^9

For each i = 2,3,...,N, w_1 ≤ w_i ≤ w_1 + 3.

1 ≤ v_i ≤ 10^7

W, each w_i and v_i are integers.

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 total value of the selected items.

Sample Input 1

4 6
2 1
3 4
4 10
3 4

Sample Output 1

11

The first and third items should be selected.

Sample Input 2

4 6
2 1
3 7
4 10
3 6

Sample Output 2

13

The second and fourth items should be selected.

Sample Input 3

4 10
1 100
1 100
1 100
1 100

Sample Output 3

400

You can take everything.

Sample Input 4

4 1
10 100
10 100
10 100
10 100

Sample Output 4

0

You can take nothing.

Solution in Kotlin (1.0.0) (judge verdict: Accepted)

import java.io.IOException

fun main(args: Array<String>) {
    val fs = FastScanner()
    val N = fs.nextInt()
    val W = fs.nextLong()
    val w = IntArray(N)
    val v = LongArray(N)

    for (i in 0 until N) {
        w[i] = fs.nextInt()
        v[i] = fs.nextLong()
    }

    val items = listOf(mutableListOf<Long>(0), mutableListOf<Long>(0), mutableListOf<Long>(0), mutableListOf<Long>(0))
    val w1 = w[0]
    for (i in 0 until N) {
        w[i] -= w1
        items[w[i] % 4].add(v[i])
    }

    items.forEach {
        it.sort()
        for (i in 0 until it.size - 1) {
            it[i + 1] += it[i]
        }
    }
    val size0 = items[0].size - 1
    val size1 = items[1].size - 1
    val size2 = items[2].size - 1
    val size3 = items[3].size - 1

    var max = 0L
    val longW1 = w1.toLong()
    for (i0 in 0 .. size0) {
        for (i1 in 0 .. size1) {
            for (i2 in 0 .. size2) {
                for (i3 in 0 .. size3) {
                    val total = i0 + i1 + i2 + i3
                    if (total <= N && total * longW1 + i1 + 2L * i2 + 3L * i3 <= W) {
                        max = Math.max(max,
                               items[0][size0] - items[0][size0 - i0] +
                                       items[1][size1] - items[1][size1 - i1] +
                                       items[2][size2] - items[2][size2 - i2] +
                                       items[3][size3] - items[3][size3 - i3]
                        )

                    }
                }
            }
        }
    }
    println(max)
}


internal class FastScanner {
    private val `in` = System.`in`
    private val buffer = ByteArray(1024)
    private var ptr = 0
    private var buflen = 0
    private fun hasNextByte(): Boolean {
        if (ptr < buflen) {
            return true
        } else {
            ptr = 0
            try {
                buflen = `in`.read(buffer)
            } catch (e: IOException) {
                e.printStackTrace()
            }

            if (buflen <= 0) {
                return false
            }
        }
        return true
    }

    private fun readByte(): Int {
        return if (hasNextByte()) buffer[ptr++].toInt() else -1
    }

    private fun isPrintableChar(c: Int): Boolean {
        return 33 <= c && c <= 126
    }

    private fun skipUnprintable() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr].toInt())) ptr++
    }

    operator fun hasNext(): Boolean {
        skipUnprintable()
        return hasNextByte()
    }

    operator fun next(): String {
        if (!hasNext()) return ""
        val sb = StringBuilder()
        var b = readByte()
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b)
            b = readByte()
        }
        return sb.toString()
    }

    fun nextLong(): Long {
        if (!hasNext()) return -1
        var n: Long = 0
        var minus = false
        var b = readByte()
        if (b == '-'.toInt()) {
            minus = true
            b = readByte()
        }
        if (b < '0'.toInt() || '9'.toInt() < b) {
            throw NumberFormatException()
        }
        while (true) {
            if ('0'.toInt() <= b && b <= '9'.toInt()) {
                n *= 10
                n += (b - '0'.toInt()).toLong()
            } else return if (b == -1 || !isPrintableChar(b)) {
                if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }

    fun nextInt(): Int {
        val set = hashSetOf<Char>()
        set.add('3')
        set.clear()

        if (!hasNext()) return -1
        var n: Int = 0
        var minus = false
        var b = readByte()
        if (b == '-'.toInt()) {
            minus = true
            b = readByte()
        }
        if (b < '0'.toInt() || '9'.toInt() < b) {
            throw NumberFormatException()
        }
        while (true) {
            if ('0'.toInt() <= b && b <= '9'.toInt()) {
                n *= 10
                n += (b - '0'.toInt()).toInt()
            } else return if (b == -1 || !isPrintableChar(b)) {
                if (minus) -n else n
            } else {
                throw NumberFormatException()
            }
            b = readByte()
        }
    }
}