Lorry (Codeforces 3B)
Tags: Greedy, sortings

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.

Input: The first line contains a pair of integer numbers n and v (1 ≤ n ≤ 105; 1 ≤ v ≤ 109), 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 ti, pi (1 ≤ ti ≤ 2; 1 ≤ pi ≤ 104), where ti is the vehicle type (1 – a kayak, 2 – a catamaran), and pi is its carrying capacity. The waterborne vehicles are enumerated in order of their appearance in the input file.

Output: In the first line print the maximum possible carrying capacity of the set. In the second line print a string consisting of the numbers of the vehicles that make the optimal set. If the answer is not unique, print any of them.


Sample Inputs:
3 2
1 2
2 7
1 3

Sample Outputs:
7
2

Solution (Python):
(N, V) = map(int, input().strip().split(' '))
ones = []
twos = []
for i in range(N):
    (idx, v) = map(int, input().strip().split(' '))
    temp = (v, i + 1)
    if idx == 1:
        ones.append(temp)
    else:
        twos.append(temp)
ones.sort(key=lambda x: x[0], reverse=True)
twos.sort(key=lambda x: x[0])
length = len(ones)
if length > V:
    ones = ones[:V]
res_v = 0
res = set()
for (v, idx) in ones:
    res_v += v
    res.add(idx)
if length < V:
    while len(twos) > 0 and length + 2 <= V:
        (v, idx) = twos.pop()
        res_v += v
        res.add(idx)
        length += 2
cur_c = min(length, V)
while len(twos) > 0:
    (v, idx) = twos.pop()
    if len(ones) <= 0:
        break
    (v1, rm_idx1) = ones[-1]
    if v <= v1:
        break
    if cur_c + 1 <= V:
        res_v -= v1
        res.remove(rm_idx1)
        res_v += v
        res.add(idx)
        cur_c += 1
        ones.pop()
    else:
        if len(ones) <= 1:
            break
        (v2, rm_idx2) = ones[-2]
        if v <= v1 + v2:
            break
        res_v -= v1 + v2
        res.remove(rm_idx1)
        res.remove(rm_idx2)
        res_v += v
        res.add(idx)
        ones.pop()
        ones.pop()
print(res_v)
print(' '.join(list(map(str, list(res)))))

Solution (Python):
from sys import stdin
input = stdin.buffer.readline

def solve():
    (n, v) = map(int, input().split())
    l = [[], []]
    for i in range(n):
        (x, y) = map(int, input().split())
        l[x - 1].append([y, i])
    for i in range(2):
        l[i].sort(reverse=True)
    p = len(l[1]) - 1
    s1 = 0
    s2 = sum([i[0] for i in l[1]])
    ans = 0
    z = [-1, -1]
    for i in range(-1, len(l[0])):
        if i != -1:
            s1 += l[0][i][0]
        while p >= 0 and i + 1 + (p + 1) * 2 > v:
            s2 -= l[1][p][0]
            p -= 1
        if i + 1 + (p + 1) * 2 <= v:
            if s1 + s2 > ans:
                z = [i, p]
                ans = s1 + s2
    print(ans)
    u = []
    for w in range(0, 2):
        for i in range(0, min(z[w] + 1, len(l[w]))):
            u.append(l[w][i][1] + 1)
    print(' '.join(map(str, u)))

def main():
    tt = 1
    for case in range(tt):
        solve()
main()

Solution (Python):
__author__ = 'Eddie'
import sys

def get_filehandler(is_file):
    if is_file:
        return open(file='data')
    else:
        import sys
        return sys.stdin
if __name__ == '__main__':
    fh = get_filehandler(is_file=False)
    (n, v) = [int(value) for value in fh.readline().split()]
    d = {'1': [], '2': []}
    for _i in range(1, n + 1):
        (t, c) = fh.readline().split()
        d[t].append((int(c), str(_i)))
    d['1'].sort()
    d['2'].sort()
    capacity = 0
    num_v = []
    last_k = None
    while len(d['1']) > 1 and len(d['2']) > 0 and (v > 1):
        if d['1'][-1][0] + d['1'][-2][0] > d['2'][-1][0]:
            capacity += d['1'][-1][0] + d['1'][-2][0]
            num_v.append(d['1'][-1][1])
            num_v.append(d['1'][-2][1])
            last_k = (len(num_v) - 1, d['1'][-2][0])
            del d['1'][-2:]
        else:
            capacity += d['2'][-1][0]
            num_v.append(d['2'][-1][1])
            del d['2'][-1]
        v -= 2
    while len(d['2']) > 0 and v > 1:
        capacity += d['2'][-1][0]
        num_v.append(d['2'][-1][1])
        del d['2'][-1]
        v -= 2
    if v == 1:
        if last_k is not None and len(d['2']) > 0:
            d_1 = 0
            if len(d['1']) > 0:
                d_1 = d['1'][-1][0]
            if last_k[1] + d_1 < d['2'][-1][0]:
                del num_v[last_k[0]]
                v -= 1
                capacity -= last_k[1]
                capacity += d['2'][-1][0]
                num_v.append(d['2'][-1][1])
    while len(d['1']) > 0 and v > 0:
        capacity += d['1'][-1][0]
        num_v.append(d['1'][-1][1])
        del d['1'][-1]
        v -= 1
    print(capacity)
    print(' '.join(num_v))