A group of tourists is going to kayak and catamaran tour. A rented lorry has arrived to the boat depot to take kayaks and catamarans to the point of departure. It's known that all kayaks are of the same size (and each of them occupies the space of 1 cubic metre), and all catamarans are of the same size, but two times bigger than kayaks (and occupy the space of 2 cubic metres).

Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. Knowing the truck body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. The truck body volume of the lorry can be used effectively, that is to say you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the truck body.
The first line contains a pair of integer numbers *n* and *v* (1 ≤ *n* ≤ 10<sup class="upper-index">5</sup>; 1 ≤ *v* ≤ 10<sup class="upper-index">9</sup>), where *n* is the number of waterborne vehicles in the boat depot, and *v* is the truck body volume of the lorry in cubic metres. The following *n* lines contain the information about the waterborne vehicles, that is a pair of numbers *t*<sub class="lower-index">*i*</sub>, *p*<sub class="lower-index">*i*</sub> (1 ≤ *t*<sub class="lower-index">*i*</sub> ≤ 2; 1 ≤ *p*<sub class="lower-index">*i*</sub> ≤ 10<sup class="upper-index">4</sup>), where *t*<sub class="lower-index">*i*</sub> is the vehicle type (1 – a kayak, 2 – a catamaran), and *p*<sub class="lower-index">*i*</sub> is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.
import operator


class Stone:
    """
    石头类：
        index 编号；
        value 价值；
        space 占用空间；
        rou   价值密度；
    """

    def __init__(self, index, value, space):
        self.index = index
        self.value = value
        self.space = space
        self.rou = self.value / self.space


class Bag:
    """
    背包类：
        space_left  剩余空间；
        stones_index已放入的石头编号；

        put_stones_in()将最大价值的石头放入背包
    """

    def __init__(self, space):
        self.values = 0
        self.space_left = space
        self.stones_index = set()
        self.stones_num = 0

    def get_value1(self, all_stones, i_first):
        if i_first == -1:
            return 0
        else:
            return all_stones[i_first].value

    def get_value2(self, all_stones, i_last):
        return all_stones[self.stones_num].value - all_stones[i_last].value

    def put_stones_in(self, all_stones, all_stones_num: int):
        # 放石头
        space_left = self.space_left
        bag_stones_num = 0
        bag_values = 0
        for i in range(all_stones_num):
            if space_left >= all_stones[i].space:
                self.stones_index.add(all_stones[i].index)
                bag_values += all_stones[i].value
                space_left -= all_stones[i].space
                bag_stones_num += 1
            else:
                break
        self.space_left = space_left
        self.stones_num = bag_stones_num
        self.values = bag_values
        # 石头装完了或背包满了
        if (all_stones_num == self.stones_num) | (self.space_left == 0):
            return
        else:
            i_last = -1
            i_first = -1
            # 更新i_last&i_first
            for i in range(self.stones_num - 1, -1, -1):
                if all_stones[i].space == 1:
                    i_last = i
                    break
            for i in range(self.stones_num, all_stones_num):
                if all_stones[i].space == 1:
                    i_first = i
                    break
            # 背包里没有体积为1的石头
            if i_last == -1:
                if i_first != -1:
                    self.stones_index.add(all_stones[i_first].index)
                    self.values += all_stones[i_first].value
                    self.space_left -= 1
                else:
                    pass
            else:
                value1 = self.get_value1(all_stones, i_first)  # 如果再放一个体积为1的石头，价值增量
                value2 = self.get_value2(all_stones, i_last)  # 如果用体积为2的石头换一个体积为1的石头出来，价值增量
                # 放石头更好
                if value1 >= value2:
                    if i_first != -1:  # 有的放
                        self.stones_index.add(all_stones[i_first].index)
                        self.values += all_stones[i_first].value
                        self.space_left -= 1
                    else:  # 没得放
                        pass
                # 换石头更好
                else:
                    self.values += all_stones[self.stones_num].value
                    self.stones_index.add(all_stones[self.stones_num].index)
                    self.stones_index.remove(all_stones[i_last].index)
                    self.values -= all_stones[i_last].value
                    self.space_left -= 1


def get_stones(num):
    Stones = []
    for i in range(num):
        spv = input()
        Stones.append(Stone(i + 1, eval(spv.split(" ")[1]), eval(spv.split(" ")[0])))
    Stones.sort(key=operator.attrgetter('rou'), reverse=True)  # 按rou降序排序
    return Stones


nv = input()
n = eval(nv.split(" ")[0])  # 石头数量
bag = Bag(eval(nv.split(" ")[1]))  # 创建背包
stones = get_stones(n)  # 获取所有石头按照价值密度降序排序后的序列

bag.put_stones_in(stones, n)
print(bag.values)
for k in bag.stones_index:
    print(k, end=" ")

 		 						 	  	  		    	   		