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




Measured memory_bytes: 35228